@Override public void doRun() { Toothpick.openScopes(parentScopeName, childScopeName); setIsSuccessful(true); } }
@Test(expected = IllegalArgumentException.class) public void openScopes_shouldFail_whenScopeNamesAreNull() throws Exception { //GIVEN //WHEN Toothpick.openScopes((Object[]) null); //THEN }
@Test public void singleton_shouldBeSharedBySubscopes() throws Exception { //GIVEN Scope scope1 = Toothpick.openScopes("root", "scope1"); Scope scope2 = Toothpick.openScopes("root", "scope2"); //WHEN FooSingleton instance = scope1.getInstance(FooSingleton.class); FooSingleton instance2 = scope2.getInstance(FooSingleton.class); //THEN assertThat(instance, sameInstance(instance2)); }
@Test public void closeScope_shouldRemoveChildScope_whenChildScopeIsClosed() throws Exception { //GIVEN Toothpick.openScopes("foo", "bar"); //WHEN Toothpick.closeScope("bar"); //THEN assertThat(((ScopeNode) Toothpick.openScope("foo")).getChildrenScopes().isEmpty(), is(true)); }
@Test public void testBindScopeAnnotation_shouldReturnFalse_whenNonRootScopeAskedForSingleton() { //GIVEN Scope childScope = Toothpick.openScopes("root", "child"); //WHEN boolean isBoundToSingleton = childScope.isBoundToScopeAnnotation(Singleton.class); //THEN assertThat(isBoundToSingleton, is(false)); }
@Test public void singletonDiscoveredDynamically_shouldGoInRootScope() throws Exception { //GIVEN Scope scopeParent = Toothpick.openScope("root"); Scope scope = Toothpick.openScopes("root", "child"); //WHEN FooSingleton instance = scope.getInstance(FooSingleton.class); FooSingleton instance2 = scopeParent.getInstance(FooSingleton.class); //THEN assertThat(instance, sameInstance(instance2)); assertThat(instance, notNullValue()); }
@Test public void testGetParentScope_shouldReturnRootScope_whenAskedForSingleton() { //GIVEN Scope parentScope = Toothpick.openScope("root"); Scope childScope = Toothpick.openScopes("root", "child"); //WHEN Scope scope = childScope.getParentScope(Singleton.class); //THEN assertThat(scope, is(parentScope)); }
@Test public void testGetParentScope_shouldReturnItself_whenBoundToScopeAnnotation() { //GIVEN Scope childScope = Toothpick.openScopes("root", "child"); childScope.bindScopeAnnotation(CustomScope.class); //WHEN Scope scope = childScope.getParentScope(CustomScope.class); //THEN assertThat(scope, is(childScope)); }
@Test public void testGetRootScope_shouldReturnRootScope_whenHasParent() { //GIVEN Scope parentScope = Toothpick.openScope("root"); Scope childScope = Toothpick.openScopes("root", "child"); //WHEN Scope scope = childScope.getRootScope(); //THEN assertThat(scope, is(parentScope)); }
@Override public void doRun() { //pick a random node in the tree, starting from root //add a new child node to this node Scope scopeName = NodeUtil.findRandomNode(rootScopeName, ACCEPTANCE_THRESHOLD); if (scopeName == null) { setIsSuccessful(true); return; } Object name = scopeName.getName(); Toothpick.openScopes(name, new Object()); setIsSuccessful(true); } }
@Test public void testGetParentScope_shouldReturnParentScope_whenParentBoundToScopeAnnotation() { //GIVEN Scope parentScope = Toothpick.openScope("root"); parentScope.bindScopeAnnotation(CustomScope.class); Scope childScope = Toothpick.openScopes("root", "child"); //WHEN Scope scope = childScope.getParentScope(CustomScope.class); //THEN assertThat(scope, is(parentScope)); }
@Test public void testInjection_shouldWork_whenInheritingBinding() throws Exception { //GIVEN Scope scope = Toothpick.openScope("root"); scope.installModules(new Module() { { bind(Foo.class).to(Foo.class); } }); Scope childScope = Toothpick.openScopes("root", "child"); Foo foo = new Foo(); //WHEN Toothpick.inject(foo, childScope); //THEN assertThat(foo.bar, notNullValue()); assertThat(foo.bar, isA(Bar.class)); }
@Test public void childInjector_shouldReturnInstancesInParentScopeUsingChildScope_whenChildOverridesBinding() throws Exception { //GIVEN Scope scopeParent = Toothpick.openScope("root"); Scope scope = Toothpick.openScopes("root", "child"); scope.installModules(new Module() { { bind(Bar.class).to(BarChild.class); } }); //WHEN scopeParent.getInstance(Foo.class); // Create Foo internal provider in parent scope dynamically Foo instance = scope.getInstance(Foo.class); //THEN assertThat(instance.bar, instanceOf(BarChild.class)); }
@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_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."); }
@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."); }
@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."); }