javajunitexecutorserviceantlr4invokeandwait

Antlr 4 JDialog created by tree.inspect(parser) is automatically closed by JUnit


In Antlr 4, such code works in a general main function.

public static void main(String[] args) {
   .....
   SicstusPrologParser parser = new SicstusPrologParser(tokens);
   ParserRuleContext tree =(ParserRuleContext)parser.program();
   tree.inspect(parser);
}

The last statement pops up a model JDialog which shows the parser tree structure. But I copied the code into a junit test case as below:

 @Test
public void testParserClause() { //clause
    .....
   SicstusPrologParser parser = new SicstusPrologParser(tokens);
   ParserRuleContext tree =(ParserRuleContext)parser.program();
   tree.inspect(parser);
 }

The JDialog created by "tree.inpect(parser)" has just been closed by junt before I clicked the "OK" button. I dived into the "inspect" function, its main logic flow is as the following:

   .....
 Callable<JDialog> callable = new Callable<JDialog>() {
    JDialog result;

    @Override
        public JDialog call() throws Exception {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                                         //fill the tree nodes and show the dialog.
                    result = showInDialog(viewer);
                }
            });

            return result;
        }
    };

    ExecutorService executor = Executors.newSingleThreadExecutor();

    try {
        return executor.submit(callable);
    }
    finally {
        executor.shutdown();
    }

Why is the model JDialog closed before I do sth with it? I used the return value of "inspect", but it still did work.

       Future<JDialog> fu = tree.inpect(parser);
       fu.get();

any help?


Solution

  • A utility method is provided in case you need to wait for the window to close before proceeding:

    Future<JDialog> future = tree.inspect(parser);
    Utils.waitForClose(future.get());