/** * Tests the isSuccessful() method of the result object if at least one child initializer has * thrown an exception. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeResultsIsSuccessfulFalse() throws ConcurrentException { final ChildBackgroundInitializer child = new ChildBackgroundInitializer(); child.ex = new Exception(); initializer.addInitializer(CHILD_INIT, child); initializer.start(); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = initializer.get(); assertFalse("Wrong success flag", res.isSuccessful()); }
/** * Tests whether a child initializer has been executed. Optionally the expected executor service * can be checked, too. * * @param child the child initializer * @param expExec the expected executor service (null if the executor should not be checked) * @throws ConcurrentException if an error occurs */ private void checkChild(final BackgroundInitializer<?> child, final ExecutorService expExec) throws ConcurrentException { final ChildBackgroundInitializer cinit = (ChildBackgroundInitializer) child; final Integer result = cinit.get(); assertEquals("Wrong result", 1, result.intValue()); assertEquals("Wrong number of executions", 1, cinit.initializeCalls); if (expExec != null) { assertEquals("Wrong executor service", expExec, cinit.currentExecutor); } }
/** * Tests the behavior of the initializer if one of the child initializers throws a checked * exception. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeEx() throws ConcurrentException { final ChildBackgroundInitializer child = new ChildBackgroundInitializer(); child.ex = new Exception(); initializer.addInitializer(CHILD_INIT, child); initializer.start(); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = initializer.get(); assertTrue("No exception flag", res.isException(CHILD_INIT)); assertNull("Got a results object", res.getResultObject(CHILD_INIT)); final ConcurrentException cex = res.getException(CHILD_INIT); assertEquals("Wrong cause", child.ex, cex.getCause()); }
/** * Tests the behavior of the initializer if one of the child initializers throws a runtime * exception. */ @Test public void testInitializeRuntimeEx() { final ChildBackgroundInitializer child = new ChildBackgroundInitializer(); child.ex = new RuntimeException(); initializer.addInitializer(CHILD_INIT, child); initializer.start(); try { initializer.get(); fail("Runtime exception not thrown!"); } catch (final Exception ex) { assertEquals("Wrong exception", child.ex, ex); } }
/** * Tests the behavior of initialize() if a child initializer has a specific executor service. Then * this service should not be overridden. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeChildWithExecutor() throws ConcurrentException, InterruptedException { final String initExec = "childInitializerWithExecutor"; final ExecutorService exec = Executors.newSingleThreadExecutor(); try { final ChildBackgroundInitializer c1 = new ChildBackgroundInitializer(); final ChildBackgroundInitializer c2 = new ChildBackgroundInitializer(); c2.setExternalExecutor(exec); initializer.addInitializer(CHILD_INIT, c1); initializer.addInitializer(initExec, c2); initializer.start(); initializer.get(); checkChild(c1, initializer.getActiveExecutor()); checkChild(c2, exec); } finally { exec.shutdown(); exec.awaitTermination(1, TimeUnit.SECONDS); } }