@Before public void setUpForDevelopmentConfiguration() throws Exception { Toothpick.setConfiguration(Configuration.forDevelopment()); } }
@Before public void setUp() throws Exception { Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); Toothpick.openScope(ROOT_SCOPE); scopeNames.clear(); }
@Test public void createScope_shouldReturnAnScopeWithAParent_whenThisScopeByThisKeyWasCreatedWithAParent() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forProduction()); ScopeNode scopeParent = (ScopeNode) Toothpick.openScope("foo"); //WHEN ScopeNode scope = (ScopeNode) Toothpick.openScope("bar"); scopeParent.addChild(scope); //THEN assertThat(scope, notNullValue()); assertThat(scope.getParentScope(), sameInstance(scopeParent)); }
@AfterClass public static void staticTearDown() throws Exception { Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); }
@Before public void setUp() throws Exception { Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); }
@Test public void destroyScope_shouldClearThisScope_WhenThisScopesWasCreated() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forProduction()); Scope scope = Toothpick.openScope("foo"); //WHEN Toothpick.closeScope("foo"); Scope scopeAfterReset = Toothpick.openScope("foo"); //THEN assertThat(scopeAfterReset, not(sameInstance(scope))); }
@Test public void reset_shouldClear_WhenSomeScopesWereCreated() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forProduction()); Scope scope0 = Toothpick.openScope("foo"); Scope scope1 = Toothpick.openScope("bar"); //WHEN Toothpick.reset(); Scope scope0AfterReset = Toothpick.openScope("foo"); Scope scope1AfterReset = Toothpick.openScope("bar"); //THEN assertThat(scope0AfterReset, not(sameInstance(scope0))); assertThat(scope1AfterReset, not(sameInstance(scope1))); }
@Test public void getOrCreateScope_shouldReturnANewScopeScope_WhenOneWasNotCreatedWithSameKey() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forProduction()); ScopeNode scopeParent = (ScopeNode) Toothpick.openScope("bar"); //WHEN ScopeNode scope = (ScopeNode) Toothpick.openScope("foo"); scopeParent.addChild(scope); ScopeNode scope2 = (ScopeNode) Toothpick.openScope("foo"); //THEN assertThat(scope, notNullValue()); assertThat(scope2, sameInstance(scope)); assertThat(scope.getParentScope(), sameInstance(scopeParent)); }
@BeforeClass public static void setUp() throws Exception { ToothpickBaseTest.setUp(); Toothpick.setConfiguration(Configuration.forDevelopment().disableReflection()); }
@Test public void testGetMemberInjector_shouldReturnNull_whenThereAreNoRegistries() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); MemberInjectorRegistryLocator.setRootRegistry(null); //WHEN MemberInjector<Foo> memberInjector = MemberInjectorRegistryLocator.getMemberInjector(Foo.class); //THEN assertThat(memberInjector, nullValue()); }
@Test(expected = MultipleRootException.class) public void opening2rootScope_shouldThrowAnException_whenConfigurationPreventsMultipleRootScopes() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forDevelopment().preventMultipleRootScopes()); Toothpick.openScope("foo"); //WHEN Toothpick.openScope("bar"); //THEN }
@Test public void binding_shouldCreateAnnotatedClassInScopeBoundToScopeAnnotationViaFactory_whenParentScopeIsBoundToScopeAnnotation() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scopeParent = Toothpick.openScope(CustomScope.class); Scope scope1 = Toothpick.openScopes(CustomScope.class, "child"); //WHEN FooCustomScope instanceInParent = scopeParent.getInstance(FooCustomScope.class); FooCustomScope instanceInChild = scope1.getInstance(FooCustomScope.class); //THEN assertThat(instanceInParent, sameInstance(instanceInChild)); } }
@Test(expected = MultipleRootException.class) public void openingAClosedChildScope_shouldThrowAnException_whenConfigurationPreventsMultipleRootScopes() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forDevelopment().preventMultipleRootScopes()); Toothpick.openScopes("foo", "bar"); Toothpick.closeScope("bar"); //WHEN Toothpick.openScope("bar"); //THEN }
@Test public void binding_shouldCreateAnnotatedClassInScopeBoundToScopeAnnotationViaProvider_whenParentScopeIsBoundToScopeAnnotation() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scopeParent = Toothpick.openScope(CustomScope.class); Scope scope1 = Toothpick.openScopes(CustomScope.class, "child"); //WHEN FooProviderAnnotatedProvidesSingleton instanceInParent = scopeParent.getInstance(FooProviderAnnotatedProvidesSingleton.class); FooProviderAnnotatedProvidesSingleton instanceInChild = scope1.getInstance(FooProviderAnnotatedProvidesSingleton.class); //THEN assertThat(instanceInParent, sameInstance(instanceInChild)); }
@Test public void binding_shouldCreateAnnotatedClassInRootScope_whenInjectingSingletonAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scopeParent = Toothpick.openScope("root"); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN FooProviderAnnotatedSingletonImpl instanceInParent = scopeParent.getInstance(FooProviderAnnotatedSingletonImpl.class); FooProviderAnnotatedSingletonImpl instanceInChild = scope1.getInstance(FooProviderAnnotatedSingletonImpl.class); //THEN assertThat(instanceInParent, sameInstance(instanceInChild)); }
@Test public void singleton_shouldBeShared_whenBoundingIsSimpleOnSingletonAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope root = Toothpick.openScopes("root"); root.installModules(new Module() { { bind(FooSingleton.class); } }); //WHEN FooSingleton instance = root.getInstance(FooSingleton.class); FooSingleton instance2 = root.getInstance(FooSingleton.class); //THEN assertThat(instance, sameInstance(instance2)); }
@Test(expected = IllegalStateException.class) public void binding_shouldCrashForScopeAnnotatedClass_whenBindingIsSimple() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN scope1.installModules(new Module() { { bind(FooSingleton.class); } }); //THEN fail("This test should throw a IllegalBindingException."); }
@BeforeClass public static void setUp() throws Exception { MemberInjectorRegistryLocator.setRootRegistry(new toothpick.test.MemberInjectorRegistry()); FactoryRegistryLocator.setRootRegistry(new toothpick.test.FactoryRegistry()); Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); }
@Test(expected = IllegalStateException.class) public void binding_shouldCrashForScopeAnnotatedClass_whenBindingToAProvider() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN scope1.installModules(new Module() { { bind(IFoo.class).toProvider(FooProviderAnnotatedSingletonImpl.class); } }); //THEN fail("This test should throw a IllegalBindingException."); }
@Test(expected = IllegalStateException.class) public void binding_shouldCrashForScopeAnnotatedClass_whenBindingToAClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN scope1.installModules(new Module() { { bind(IFooSingleton.class).to(FooSingleton.class); } }); //THEN fail("This test should throw a IllegalBindingException."); }