@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 = IllegalArgumentException.class) public void shouldThrowExceptionOnBindWithNonActiveScopedContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class).create(); manager.bind(ManagerTestScoped.class, Object.class, new Object()); }
@Before public final void create() throws Exception { contexts = new ArrayList<Class<? extends Context>>(); ManagerBuilder builder = ManagerBuilder.from(); addContexts(contexts); for (Class<? extends Context> context : contexts) { builder.context(context); } // Add ApplicationContext, it's internal to Manager, but needs to be registered as a context so EventRecorder will pick up on it contexts.add(0, ApplicationContextImpl.class); builder.extension(EventRegisterObserver.class); List<Class<?>> extensions = new ArrayList<Class<?>>(); addExtensions(extensions); for (Class<?> extension : extensions) { builder.extension(extension); } manager = (ManagerImpl) builder.create(); beforeStartManager(manager); manager.start(); executeInApplicationScope(new Callable<Void>() { @Override public Void call() throws Exception { manager.resolve(Injector.class).inject(AbstractManagerTestBase.this); return null; } }); startContexts(manager); }
@Before public final void create() throws Exception { contexts = new ArrayList<Class<? extends Context>>(); ManagerBuilder builder = ManagerBuilder.from(); addContexts(contexts); for (Class<? extends Context> context : contexts) { builder.context(context); } // Add ApplicationContext, it's internal to Manager, but needs to be registered as a context so EventRecorder will pick up on it contexts.add(0, ApplicationContextImpl.class); builder.extension(EventRegisterObserver.class); List<Class<?>> extensions = new ArrayList<Class<?>>(); addExtensions(extensions); for (Class<?> extension : extensions) { builder.extension(extension); } manager = (ManagerImpl) builder.create(); beforeStartManager(manager); manager.start(); executeInApplicationScope(new Callable<Void>() { @Override public Void call() throws Exception { manager.resolve(Injector.class).inject(AbstractManagerTestBase.this); return null; } }); startContexts(manager); }
@Test public void shouldFireEventOnSet() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class) .extension(TestObserver.class).create(); ManagerTestContext context = manager.getContext(ManagerTestContext.class); try { context.activate(); InstanceProducer<Object> instance = InstanceImpl.of(Object.class, ManagerTestScoped.class, manager); instance.set(new Object()); Assert.assertTrue(manager.getExtension(TestObserver.class).wasCalled); } finally { context.deactivate(); context.destroy(); } }
@Test public void shouldFireEventOnSet() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class) .extension(TestObserver.class).create(); ManagerTestContext context = manager.getContext(ManagerTestContext.class); try { context.activate(); InstanceProducer<Object> instance = InstanceImpl.of(Object.class, ManagerTestScoped.class, manager); instance.set(new Object()); Assert.assertTrue(manager.getExtension(TestObserver.class).wasCalled); } finally { context.deactivate(); context.destroy(); } }
@Test public void shouldBeAbleToLookupInContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class).create(); Object testObject = new Object(); ManagerTestContext context = manager.getContext(ManagerTestContext.class); try { context.activate(); context.getObjectStore().add(Object.class, testObject); Instance<Object> instance = InstanceImpl.of(Object.class, ManagerTestScoped.class, manager); Assert.assertEquals( "Verify expected object was returned", testObject, instance.get()); } finally { context.deactivate(); context.destroy(); } }
@Test public void shouldBindToTheScopedContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class) .context(ManagerTest2ContextImpl.class).create(); ManagerTestContext suiteContext = manager.getContext(ManagerTestContext.class); ManagerTest2Context classContext = manager.getContext(ManagerTest2Context.class); try { suiteContext.activate(); classContext.activate("A"); Object testObject = new Object(); manager.bind(ManagerTestScoped.class, Object.class, testObject); Assert.assertEquals( "Verify value was bound to the correct context", testObject, suiteContext.getObjectStore().get(Object.class)); Assert.assertNull( "Verify value was not bound to any other context", classContext.getObjectStore().get(Object.class)); } finally { classContext.deactivate(); classContext.destroy("A"); suiteContext.deactivate(); suiteContext.destroy(); } }
@Test public void shouldBeAbleToLookupInContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class).create(); Object testObject = new Object(); ManagerTestContext context = manager.getContext(ManagerTestContext.class); try { context.activate(); context.getObjectStore().add(Object.class, testObject); Instance<Object> instance = InstanceImpl.of(Object.class, ManagerTestScoped.class, manager); Assert.assertEquals( "Verify expected object was returned", testObject, instance.get()); } finally { context.deactivate(); context.destroy(); } }
@Test public void shouldBeAbleToRegisterContextAndExtensions() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class) .extension(TestExtension.class).create(); ManagerTestContext context = manager.getContext(ManagerTestContext.class); try { context.activate(); // bind something to the context so our Instance<Object> is resolved context.getObjectStore().add(Object.class, new Object()); manager.fire("some string"); Assert.assertTrue(manager.getExtension(TestExtension.class).wasCalled); } finally { context.deactivate(); context.destroy(); } }
@Test public void shouldBindToTheScopedContext() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class) .context(ManagerTest2ContextImpl.class).create(); ManagerTestContext suiteContext = manager.getContext(ManagerTestContext.class); ManagerTest2Context classContext = manager.getContext(ManagerTest2Context.class); try { suiteContext.activate(); classContext.activate("A"); Object testObject = new Object(); manager.bind(ManagerTestScoped.class, Object.class, testObject); Assert.assertEquals( "Verify value was bound to the correct context", testObject, suiteContext.getObjectStore().get(Object.class)); Assert.assertNull( "Verify value was not bound to any other context", classContext.getObjectStore().get(Object.class)); } finally { classContext.deactivate(); classContext.destroy("A"); suiteContext.deactivate(); suiteContext.destroy(); } }
@Test public void shouldBeAbleToRegisterContextAndExtensions() throws Exception { ManagerImpl manager = (ManagerImpl) ManagerBuilder.from() .context(ManagerTestContextImpl.class) .extension(TestExtension.class).create(); ManagerTestContext context = manager.getContext(ManagerTestContext.class); try { context.activate(); // bind something to the context so our Instance<Object> is resolved context.getObjectStore().add(Object.class, new Object()); manager.fire("some string"); Assert.assertTrue(manager.getExtension(TestExtension.class).wasCalled); } finally { context.deactivate(); context.destroy(); } }