简体   繁体   English

Java单元测试Apache Commons守护程序

[英]Unit Testing Apache Commons Daemon in Java

Iv researched around and cant seem to find any decent resource to help me JUnit test an apache commons daemon written in Java. iv进行了研究,似乎找不到任何合适的资源来帮助我JUnit测试用Java编写的apache commons守护程序。 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. 任何帮助,不胜感激。

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. 我假设MyDaemon是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). 如果我是正确的,那么您不应该使用stop进行关机(有关此信息,请参阅http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html )。
  • Next point: to do some Unit-testing you need a "unit" to test, ie 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. 最后一点:由于您的代码以stop调用结束,因此您无法确定在到达main方法结束之后是通过stop调用还是通过关闭整个虚拟机来停止守护程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM