/** * 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); } }
/** * Adds a new {@code BackgroundInitializer} to this object. When this * {@code MultiBackgroundInitializer} is started, the given initializer will * be processed. This method must not be called after {@link #start()} has * been invoked. * * @param name the name of the initializer (must not be <b>null</b>) * @param init the {@code BackgroundInitializer} to add (must not be * <b>null</b>) * @throws IllegalArgumentException if a required parameter is missing * @throws IllegalStateException if {@code start()} has already been called */ public void addInitializer(final String name, final BackgroundInitializer<?> init) { Validate.isTrue(name != null, "Name of child initializer must not be null!"); Validate.isTrue(init != null, "Child initializer must not be null!"); synchronized (this) { if (isStarted()) { throw new IllegalStateException( "addInitializer() must not be called after start()!"); } childInitializers.put(name, init); } }
/** * 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); } }
/** * 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 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()); }
final ExecutorService exec = getActiveExecutor(); for (final BackgroundInitializer<?> bi : inits.values()) { if (bi.getExternalExecutor() == null) {
/** * 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); }
@Before public void setUp() throws Exception { initializer = new MultiBackgroundInitializer(); }
/** * 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 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()); }
/** * 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 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()); }
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
final ExecutorService exec = getActiveExecutor(); for (final BackgroundInitializer<?> bi : inits.values()) { if (bi.getExternalExecutor() == null) {
/** * Adds a new {@code BackgroundInitializer} to this object. When this * {@code MultiBackgroundInitializer} is started, the given initializer will * be processed. This method must not be called after {@link #start()} has * been invoked. * * @param name the name of the initializer (must not be <b>null</b>) * @param init the {@code BackgroundInitializer} to add (must not be * <b>null</b>) * @throws IllegalArgumentException if a required parameter is missing * @throws IllegalStateException if {@code start()} has already been called */ public void addInitializer(final String name, final BackgroundInitializer<?> init) { Validate.isTrue(name != null, "Name of child initializer must not be null!"); Validate.isTrue(init != null, "Child initializer must not be null!"); synchronized (this) { if (isStarted()) { throw new IllegalStateException( "addInitializer() must not be called after start()!"); } childInitializers.put(name, init); } }
/** * 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); } }
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()); assertFalse("Exception flag", res.isException(key)); assertNull("Got an exception", res.getException(key)); checkChild(res.getInitializer(key), initializer.getActiveExecutor());
final ExecutorService exec = getActiveExecutor(); for (final BackgroundInitializer<?> bi : inits.values()) { if (bi.getExternalExecutor() == null) {
/** * Adds a new {@code BackgroundInitializer} to this object. When this * {@code MultiBackgroundInitializer} is started, the given initializer will * be processed. This method must not be called after {@link #start()} has * been invoked. * * @param name the name of the initializer (must not be <b>null</b>) * @param init the {@code BackgroundInitializer} to add (must not be * <b>null</b>) * @throws IllegalArgumentException if a required parameter is missing * @throws IllegalStateException if {@code start()} has already been called */ public void addInitializer(final String name, final BackgroundInitializer<?> init) { Validate.isTrue(name != null, "Name of child initializer must not be null!"); Validate.isTrue(init != null, "Child initializer must not be null!"); synchronized (this) { if (isStarted()) { throw new IllegalStateException( "addInitializer() must not be called after start()!"); } childInitializers.put(name, init); } }
/** * 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()); }