/** * Tests the background processing if there are no child initializers. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeNoChildren() throws ConcurrentException { assertTrue("Wrong result of start()", initializer.start()); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = initializer.get(); assertTrue("Got child initializers", res.initializerNames().isEmpty()); assertTrue("Executor not shutdown", initializer.getActiveExecutor().isShutdown()); }
/** * Tests the isSuccessful() method of the result object if no child initializer has thrown an * exception. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeResultsIsSuccessfulTrue() throws ConcurrentException { final ChildBackgroundInitializer child = new ChildBackgroundInitializer(); initializer.addInitializer(CHILD_INIT, child); initializer.start(); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = initializer.get(); assertTrue("Wrong success flag", res.isSuccessful()); }
/** * Tests that the set with the names of the initializers cannot be modified. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test(expected = UnsupportedOperationException.class) public void testResultInitializerNamesModify() throws ConcurrentException { checkInitialize(); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = initializer.get(); final Iterator<String> it = res.initializerNames().iterator(); it.next(); it.remove(); }
/** * 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()); }
/** * Helper method for testing the initialize() method. This method can operate with both an * external and a temporary executor service. * * @return the result object produced by the initializer * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ private MultiBackgroundInitializer.MultiBackgroundInitializerResults checkInitialize() throws ConcurrentException { final int count = 5; for (int i = 0; i < count; i++) { initializer.addInitializer(CHILD_INIT + i, new ChildBackgroundInitializer()); } initializer.start(); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = initializer.get(); assertEquals("Wrong number of child initializers", count, res.initializerNames().size()); for (int i = 0; i < count; i++) { final String key = CHILD_INIT + i; assertTrue("Name not found: " + key, res.initializerNames().contains(key)); assertEquals("Wrong result object", Integer.valueOf(1), res.getResultObject(key)); assertFalse("Exception flag", res.isException(key)); assertNull("Got an exception", res.getException(key)); checkChild(res.getInitializer(key), initializer.getActiveExecutor()); } return res; }
/** * Tests whether MultiBackgroundInitializers can be combined in a nested way. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeNested() throws ConcurrentException { final String nameMulti = "multiChildInitializer"; initializer.addInitializer(CHILD_INIT, new ChildBackgroundInitializer()); final MultiBackgroundInitializer mi2 = new MultiBackgroundInitializer(); final int count = 3; for (int i = 0; i < count; i++) { mi2.addInitializer(CHILD_INIT + i, new ChildBackgroundInitializer()); } initializer.addInitializer(nameMulti, mi2); initializer.start(); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = initializer.get(); final ExecutorService exec = initializer.getActiveExecutor(); checkChild(res.getInitializer(CHILD_INIT), exec); final MultiBackgroundInitializer.MultiBackgroundInitializerResults res2 = (MultiBackgroundInitializer.MultiBackgroundInitializerResults) res.getResultObject(nameMulti); assertEquals("Wrong number of initializers", count, res2.initializerNames().size()); for (int i = 0; i < count; i++) { checkChild(res2.getInitializer(CHILD_INIT + i), exec); } assertTrue("Executor not shutdown", exec.isShutdown()); }
/** * Tries to query the exception flag of an unknown child initializer from the results object. This * should cause an exception. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test(expected = NoSuchElementException.class) public void testResultIsExceptionUnknown() throws ConcurrentException { final MultiBackgroundInitializer.MultiBackgroundInitializerResults res = checkInitialize(); res.isException("unknown"); }