@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 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 simpleBinding_shouldCreateInjectedInstances_whenCreateInstancesInScopeViaCode() { //GIVEN Scope scope = new ScopeImpl("root"); scope.installModules(new Module() { { bind(Foo.class).instancesInScope(); } }); //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()); assertThat(foo.bar, isA(Bar.class)); }
@Override public void doRun() { Scope scope = Toothpick.openScope(rootScopeName); scope.installModules(new Module() { { Class clazz = classCreator.allClasses[random.nextInt(classCreator.allClasses.length)]; bind(clazz).toInstance(new Foo()); } }); setIsSuccessful(true); } }
@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 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 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 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 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 testGetInstance_shouldReturnNamedBinding_whenAskingNamedBinding() throws Exception { //GIVEN ModuleWithNamedBindings moduleWithNamedBindings = new ModuleWithNamedBindings(); toothPickRule.getScope().installModules(moduleWithNamedBindings); //WHEN entryPoint = toothPickRule.getInstance(EntryPoint.class, "Foo"); //THEN assertThat(entryPoint, notNullValue()); assertThat(entryPoint, sameInstance(moduleWithNamedBindings.instance)); }
@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 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(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 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 = IllegalArgumentException.class) public void testInstallModule_shouldFail_ifModuleUsesClassAsNameButNotAnAnnotation() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); //WHEN scope.installModules(new Module() { { bind(Foo.class).withName(NotQualifierAnnotationFooName.class).toInstance(namedFooInstance); } }); //THEN fail("Should have thrown an exception"); }
@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 public void singleton_shouldBeShared_whenBoundingIsSimpleOnScopeAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope childScope = Toothpick.openScopes("root", "child"); childScope.bindScopeAnnotation(CustomScope.class); childScope.installModules(new Module() { { bind(FooProviderAnnotatedProvidesSingleton.class); } }); //WHEN FooProviderAnnotatedProvidesSingleton instance = childScope.getInstance(FooProviderAnnotatedProvidesSingleton.class); FooProviderAnnotatedProvidesSingleton instance2 = childScope.getInstance(FooProviderAnnotatedProvidesSingleton.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)); }
@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 public void testCycleDetection_whenSameClass_and_differentName_shouldNotCrash() throws Exception { //GIVEN final CyclicNamedFoo instance1 = new CyclicNamedFoo(); Scope scope = new ScopeImpl(""); scope.installModules(new Module() { { bind(CyclicNamedFoo.class).withName("foo").toInstance(instance1); } }); //WHEN CyclicNamedFoo instance2 = scope.getInstance(CyclicNamedFoo.class); //THEN // Should not crashed assertThat(instance2, notNullValue()); assertThat(instance2.cyclicFoo, sameInstance(instance1)); }