Unit Testing Apache Commons Daemon in Java

804 Views Asked by At

Iv researched around and cant seem to find any decent resource to help me JUnit test an apache commons daemon written in Java. I would like to be able to test that when a daemon starts it starts without fail and when it shuts down its shuts down without fail.

Here is some example code of the daemon starting and stopping after a period:

Update

public class MyDaemon implements Daemon
{
    private Logger myLogger = LogManager.getLogger(FileLoggerImpl.class);
    private List<FileLogger> loggers;
    private List<Thread> threads;

    public void init(DaemonContext arg0) throws DaemonInitException, Exception 
    {
        myLogger.info("Starting Logger");
        loggers = new ArrayList<FileLogger>();
        threads = new ArrayList<Thread>();
        myLogger.info("Finished starting Logger");
    }

    public void start() throws Exception 
    {

        if(threads.size()>0 || loggers.size()>0)
            stop();

        for(int i = 0; i < 1; i++)
        {
            FileLogger logger = new FileLoggerImpl(Integer.toString(i));
            Thread thread = new Thread(logger);
            loggers.add(logger);
            threads.add(thread);
            thread.start();
        }

    }

    public void stop() throws Exception
    {
        myLogger.info("Cleaning up threads...");
        for(int i = 0; i < 1; i++)
        {
            FileLogger logger = loggers.get(i);
            Thread thread = threads.get(i);
            logger.isExecuting(false);
            thread.join();
        }
        myLogger.info("Stopping thread");
    }
    public void destroy() 
    {
        myLogger.info("Destroying resources...");
        loggers = null;
        threads = null;
        myLogger.info("Destroyed resources.");
    }
    public static void main(String argsv[]) 
            throws Exception
        {
            MyDaemon myDaemon = new MyDaemon();

            myDaemon.init(null);
    myDaemon.start();
            Thread.sleep(30000);
            myDaemon.stop();

        }

}

The Logger Class reads in a text file and writes random lines from this text file to a log file *

    public void run() 
    {
        String fileLocation = "/textFileLocation";
        while(isExecuting) 
        {
            try {
                logger.info(getRandomLineOpt(fileLocation));
            } catch (IOException e) {
                logger.warn("File :" + fileLocation +" not found!");
                e.printStackTrace();
            }
            pause(DELAY_SECONDS);   
        }
    }


 public String getRandomLine(String fileLoc) throws IOException
    {
        BufferedReader reader = new BufferedReader(new FileReader(fileLoc));
        //Commons IO
        ArrayList<String> lines = new ArrayList<String>();

        String line =null;
        while( (line = reader.readLine())!= null ) 
            lines.add(line);
        return lines.get(new Random().nextInt(lines.size()));
    }

Any help much appreciated.

1

There are 1 best solutions below

4
On

Since your example code is missing some important facts we can only guess what you are trying to do.

  • I assume that MyDaemon is a subclass of Thread. If I'm correct, then you shouldn't use stop for the shutdown (read http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html for more information about this).
  • Next point: to do some Unit-testing you need a "unit" to test, i.e. you need some methods which should be tested where you can specify the expected output for certain input parameters.
  • Last point: Since your code ends with the stop call, you cannot determine if the Daemon has been stopped by the stop call or by the shutdown of the whole Virtual machine after the end of the main method has been reached.