/**
  * 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());
 }
 /**
  * Tries to add another child initializer after the start() method has been called. This should
  * not be allowed.
  *
  * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
  */
 @Test
 public void testAddInitializerAfterStart() throws ConcurrentException {
   initializer.start();
   try {
     initializer.addInitializer(CHILD_INIT, new ChildBackgroundInitializer());
     fail("Could add initializer after start()!");
   } catch (final IllegalStateException istex) {
     initializer.get();
   }
 }
 /**
  * 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 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();
 }
 /**
  * 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 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);
   }
 }
 /**
  * Tests background processing if an external executor service is provided.
  *
  * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
  */
 @Test
 public void testInitializeExternalExec() throws ConcurrentException, InterruptedException {
   final ExecutorService exec = Executors.newCachedThreadPool();
   try {
     initializer = new MultiBackgroundInitializer(exec);
     checkInitialize();
     assertEquals("Wrong executor", exec, initializer.getActiveExecutor());
     assertFalse("Executor was shutdown", exec.isShutdown());
   } finally {
     exec.shutdown();
     exec.awaitTermination(1, TimeUnit.SECONDS);
   }
 }
 /**
  * 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());
 }
 /** Tests addInitializer() if a null initializer is passed in. This should cause an exception. */
 @Test(expected = IllegalArgumentException.class)
 public void testAddInitializerNullInit() {
   initializer.addInitializer(CHILD_INIT, null);
 }
 /** Tests addInitializer() if a null name is passed in. This should cause an exception. */
 @Test(expected = IllegalArgumentException.class)
 public void testAddInitializerNullName() {
   initializer.addInitializer(null, new ChildBackgroundInitializer());
 }
 /**
  * Tests background processing if a temporary executor is used.
  *
  * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
  */
 @Test
 public void testInitializeTempExec() throws ConcurrentException {
   checkInitialize();
   assertTrue("Executor not shutdown", initializer.getActiveExecutor().isShutdown());
 }