public void testExceptionHandling() { final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized( new ConstructorInjection.ConstructorInjector( TargetInvocationExceptionTester.class, ThrowingComponent.class, null)); final TargetInvocationExceptionTester tester = (TargetInvocationExceptionTester) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); try { tester.throwsCheckedException(); fail("ClassNotFoundException expected"); } catch (final ClassNotFoundException e) { assertEquals("junit", e.getMessage()); } try { tester.throwsRuntimeException(); fail("RuntimeException expected"); } catch (final RuntimeException e) { assertEquals("junit", e.getMessage()); } try { tester.throwsError(); fail("Error expected"); } catch (final Error e) { assertEquals("junit", e.getMessage()); } }
/** * Test usage from multiple threads. * * @throws InterruptedException if interrupted */ public final void testInstancesUsedFromMultipleThreads() throws InterruptedException { final Set<Touchable> set = Collections.synchronizedSet(new HashSet<Touchable>()); final List<Touchable> list = Collections.synchronizedList(new ArrayList<Touchable>()); final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized( new ConstructorInjection.ConstructorInjector( Touchable.class, SimpleTouchable.class, null)); final Touchable touchable = (Touchable) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); final Thread[] threads = { new Thread(new Runner(touchable, list, set), "junit-1"), new Thread(new Runner(touchable, list, set), "junit-2"), new Thread(new Runner(touchable, list, set), "junit-3"), }; for (int i = threads.length; i-- > 0; ) { threads[i].start(); } Thread.sleep(300); for (int i = threads.length; i-- > 0; ) { synchronized (threads[i]) { threads[i].notifyAll(); } } Thread.sleep(300); for (int i = threads.length; i-- > 0; ) { threads[i].interrupt(); } Thread.sleep(300); assertEquals(6, list.size()); assertEquals(3, set.size()); }
/** Test ComponentAdapter using simple keys. */ public final void testSimpleKeys() { final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized<ArrayList>( new ConstructorInjection.ConstructorInjector("List", ArrayList.class, null)); final List hello = (List) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); assertNotNull(hello); }
public void testThreadLocalInstancesEqual() throws Exception { final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized( new ConstructorInjection.ConstructorInjector( Touchable.class, SimpleTouchable.class, null)); final Touchable touchable = (Touchable) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); assertEquals(touchable, touchable); }