public <T> T getInstance(Class<T> clazz, String name) { return scope.getInstance(clazz, name); }
@Test public void testNestedClassInjection() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); //WHEN FooNested fooNested = scope.getInstance(FooNested.class); FooNested.InnerClass1 innerClass1 = scope.getInstance(FooNested.InnerClass1.class); FooNested.InnerClass1.InnerClass2 innerClass2 = scope.getInstance(FooNested.InnerClass1.InnerClass2.class); //THEN assertThat(fooNested.bar, notNullValue()); assertThat(fooNested.bar, isA(Bar.class)); assertThat(innerClass1.bar, notNullValue()); assertThat(innerClass1.bar, isA(Bar.class)); assertThat(innerClass2.bar, notNullValue()); assertThat(innerClass2.bar, isA(Bar.class)); }
@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 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)); }
@Override public void doRun() { Toothpick.openScope(scopeName).getInstance(clazz); setIsSuccessful(true); } }
@Test public void testInjection_shouldFail_whenFieldsAreMasked() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); //WHEN FooChildMaskingMember fooChildMaskingMember = scope.getInstance(FooChildMaskingMember.class); String parentBarToString = fooChildMaskingMember.toString(); //THEN assertThat(parentBarToString, notNullValue()); assertThat(fooChildMaskingMember.bar, not(sameInstance(((FooParentMaskingMember) fooChildMaskingMember).bar))); }
@Test public void simpleBinding_shouldCreateInjectedInstances_whenNotSingleton() { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new Module() { { bind(Foo.class); } }); //WHEN Foo foo = scope.getInstance(Foo.class); Foo foo2 = scope.getInstance(Foo.class); //THEN assertThat(foo, notNullValue()); assertThat(foo, not(sameInstance(foo2))); assertThat(foo.bar, notNullValue()); }
@Test public void testNamedInjection() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new SimpleModule()); //WHEN Foo namedInstance = scope.getInstance(Foo.class, "bar"); //THEN assertThat(namedInstance, is(namedFooInstance)); }
@Test public void providerClassBinding_shouldCreateInstancesViaProviderInstances_whenInstancesInScopeViaCode() { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new Module() { { bind(Foo.class).toProvider(FooProviderReusingInstance.class).instancesInScope(); } }); //WHEN Foo foo = scope.getInstance(Foo.class); Foo foo2 = scope.getInstance(Foo.class); //THEN assertThat(foo, notNullValue()); assertThat(foo2, not(sameInstance(foo))); }
@Test(expected = NoFactoryFoundException.class) public void testCycleDetection_whenGetInstanceFails_shouldCloseCycle() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); //WHEN try { scope.getInstance(IFoo.class); } catch (NoFactoryFoundException nfe) { nfe.printStackTrace(); } scope.getInstance(IFoo.class); //THEN fail("Should throw NoFactoryFoundException as IFoo does not have any implementation bound." + "But It should not throw CyclicDependencyException as it was removed from the stack."); } }
@Test public void providerClassBinding_shouldCreateProviderSingleton_whenSingletonViaCode() { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new Module() { { bind(Foo.class).toProvider(FooProviderReusingInstance.class).singletonInScope(); } }); //WHEN Foo foo = scope.getInstance(Foo.class); Foo foo2 = scope.getInstance(Foo.class); //THEN assertThat(foo, notNullValue()); assertThat(foo2, sameInstance(foo)); }
@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 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 public void providerClassBinding_shouldProvideSingletons_whenProvidesSingletonViaCode() { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new Module() { { bind(Foo.class).toProvider(FooProvider.class).providesSingletonInScope(); } }); //WHEN Foo foo = scope.getInstance(Foo.class); Foo foo2 = scope.getInstance(Foo.class); //THEN assertThat(foo, notNullValue()); assertThat(foo2, sameInstance(foo)); }
@Test(expected = CyclicDependencyException.class) public void testSimpleCycleDetection() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); //WHEN scope.getInstance(CyclicFoo.class); //THEN fail("Should throw an exception as a cycle is detected"); }
@Test public void providerClassBinding_shouldCreateNonInjectedSingleton_whenProviderClassIsAnnotatedProvidesSingleton() { //GIVEN Scope scope = new ScopeImpl(""); scope.bindScopeAnnotation(CustomScope.class); scope.installModules(new Module() { { bind(IFoo.class).toProvider(FooProviderAnnotatedProvidesSingleton.class); } }); //WHEN IFoo foo = scope.getInstance(IFoo.class); IFoo foo2 = scope.getInstance(IFoo.class); //THEN assertThat(foo, notNullValue()); assertThat(foo2, sameInstance(foo)); }
@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 providerInstanceBinding_shouldProvideSingletons_whenProvidesSingletonViaCode() { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new Module() { { bind(Foo.class).toProviderInstance(new FooProvider()).providesSingletonInScope(); } }); //WHEN Foo foo = scope.getInstance(Foo.class); Foo foo2 = scope.getInstance(Foo.class); //THEN assertThat(foo, notNullValue()); assertThat(foo2, sameInstance(foo)); }
@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 public void singleton_shouldBeShared_whenBoundExplicitlyToSingletonAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope root = Toothpick.openScopes("root"); root.installModules(new Module() { { bind(IFooSingleton.class).to(FooSingleton.class); } }); //WHEN IFooSingleton instance = root.getInstance(IFooSingleton.class); IFooSingleton instance2 = root.getInstance(IFooSingleton.class); //THEN assertThat(instance, sameInstance(instance2)); }