public EventTestRunnerAdaptor(ManagerBuilder builder) { Validate.notNull(builder, "ManagerBuilder must be specified"); this.manager = builder.create(); this.manager.start(); }
@Test public void shouldBeAbleToUseProtectedFieldsAndMethods() throws Exception { Class<?> observerClass = SecurityActions.loadClass( this.getClass().getPackage().getName() + ".test.PrivateObserver"); Manager manager = ManagerBuilder.from().extension(observerClass).create(); manager.fire("Test"); Field wasCalled = observerClass.getField("wasCalled"); wasCalled.setAccessible(true); Assert.assertTrue((Boolean) wasCalled.get(null)); } }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnBindWithNoFoundScopedContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); manager.bind(ManagerTestScoped.class, Object.class, new Object()); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnBindWithNonActiveScopedContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class).create(); manager.bind(ManagerTestScoped.class, Object.class, new Object()); }
@Test(expected = IOException.class) public void shouldBeAbleToDetectExceptionEventLoopAndThrowOriginalException() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .extensions(ExtensionObservingExceptionLoop.class).create(); manager.fire(new IOException("should cause exception")); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnBindWithNonActiveScopedContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class).create(); manager.bind(ManagerTestScoped.class, Object.class, new Object()); }
@Test(expected = IOException.class) public void shouldBeAbleToDetectExceptionEventLoopAndThrowOriginalException() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .extensions(ExtensionObservingExceptionLoop.class).create(); manager.fire(new IOException("should cause exception")); }
@Test public void shouldExecuteProducersOnSameEventBeforeConsumers() throws Exception { Manager manager = ManagerBuilder.from() .extensions(ConsumerOne.class, ProducerOne.class, ProducerTwo.class).create(); manager.fire("test"); Assert.assertEquals("ProducerOne", callOrder.get(0)); Assert.assertEquals("ProducerTwo", callOrder.get(1)); Assert.assertEquals("ConsumerOne", callOrder.get(2)); }
@Test public void shouldExecuteProducersOnSameEventBeforeConsumers() throws Exception { Manager manager = ManagerBuilder.from() .extensions(ConsumerOne.class, ProducerOne.class, ProducerTwo.class).create(); manager.fire("test"); Assert.assertEquals("ProducerOne", callOrder.get(0)); Assert.assertEquals("ProducerTwo", callOrder.get(1)); Assert.assertEquals("ConsumerOne", callOrder.get(2)); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionIfTryingToSetAUnScopedInstance() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); InstanceProducer<Object> instance = InstanceImpl.of(Object.class, null, manager); instance.set(new Object()); Assert.fail("Should have thrown " + IllegalStateException.class); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionIfTryingToSetAUnScopedInstance() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); InstanceProducer<Object> instance = InstanceImpl.of(Object.class, null, manager); instance.set(new Object()); Assert.fail("Should have thrown " + IllegalStateException.class); }
@Test public void shouldResolveToNullExtensionIfNotFound() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); Assert.assertNull(manager.getExtension(ManagerTestContextImpl.class)); }
@Test public void shouldResolveToNullIfNoActiveContexts() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); Assert.assertNull(manager.resolve(Object.class)); }
@Test public void shouldResolveToNullIfNoActiveContexts() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); Assert.assertNull(manager.resolve(Object.class)); }
@Test public void shouldResolveToNullContextIfNotFound() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); Assert.assertNull(manager.getContext(ManagerTestContext.class)); }
@Test public void shouldResolveToNullContextIfNotFound() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from().create(); Assert.assertNull(manager.getContext(ManagerTestContext.class)); }
@Test public void shouldBeAbleToFireEventToAExtension() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .extension(ExtensionWithObservers.class).create(); manager.fire(new Object()); Assert.assertTrue(manager.getExtension(ExtensionWithObservers.class).methodOneWasCalled); }
@Test public void shouldBeAbleToFireEventToAExtension() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .extension(ExtensionWithObservers.class).create(); manager.fire(new Object()); Assert.assertTrue(manager.getExtension(ExtensionWithObservers.class).methodOneWasCalled); }
@Test public void shouldBeAbleToFireExceptionEventOnFailingObserver() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .extensions(ExtensionWithExceptionObserver.class, ExtensionObservingException.class).create(); manager.fire("should cause exception"); Assert.assertTrue(manager.getExtension(ExtensionObservingException.class).methodOneWasCalled); }
@Test public void shouldBeAbleToFireExceptionEventOnFailingObserver() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .extensions(ExtensionWithExceptionObserver.class, ExtensionObservingException.class).create(); manager.fire("should cause exception"); Assert.assertTrue(manager.getExtension(ExtensionObservingException.class).methodOneWasCalled); }