@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)); }
public MultipleRootException(Scope scope) { super(String.format("Scope %s is a new root in TP scope forest. Only one root is allowed in this configuration.", scope.getName())); } }
public void setScopeName(Object scopeName) { if (scope != null) { throw new IllegalStateException("scope is already initialized, use a constructor without a scope name for the rule."); } scope = Toothpick.openScope(scopeName); scope.installTestModules(testModule); }
Class<? extends Annotation> annotationType = annotation.annotationType(); if (annotationType.isAnnotationPresent(javax.inject.Scope.class)) { if (!scope.isBoundToScopeAnnotation(annotationType)) { throw new IllegalBindingException(format("Class %s cannot be bound." + " It has a scope annotation: %s that is not supported by current scope: %s", clazz.getName(), annotationType.getName(), scope.getName()));
@Test public void testNamedProviderInjection_shouldNotBeConfusedWithUnNamedInjection() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new SimpleModule()); //WHEN Provider<Foo> provider = scope.getProvider(Foo.class, "bar"); Provider<Foo> provider2 = scope.getProvider(Foo.class, "bar"); Provider<Foo> provider3 = scope.getProvider(Foo.class, FooName.class.getName()); Provider<Foo> provider4 = scope.getProvider(Foo.class); //THEN assertThat(provider.get(), is(namedFooInstance)); assertThat(provider2.get(), is(namedFooInstance)); assertThat(provider3.get(), is(namedFooInstance)); assertThat(provider4.get(), notNullValue()); assertThat(provider, not(sameInstance(provider4))); }
public <T> T getInstance(Class<T> clazz, String name) { return scope.getInstance(clazz, name); }
@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_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)); }
@Override public void doRun() { //pick a random node in the tree, starting from root //add a new child node to this node Scope scope = NodeUtil.findRandomNode(rootScopeName, ACCEPTANCE_THRESHOLD); if (scope == null) { setIsSuccessful(true); return; } //remove any node except root if (scope.getParentScope() == null) { setIsSuccessful(true); return; } Object scopeName = scope.getName(); Toothpick.closeScope(scopeName); setIsSuccessful(true); } }
@Test public void testBindScopeAnnotation_shouldReturnTrue_whenRootScopeAskedForSingleton() { //GIVEN Scope parentScope = Toothpick.openScope("root"); //WHEN boolean isBoundToSingleton = parentScope.isBoundToScopeAnnotation(Singleton.class); //THEN assertThat(isBoundToSingleton, is(true)); }
@Override public synchronized void checkMultipleRootScopes(Scope scope) { if (rootScope == null && scope != null) { rootScope = scope; return; } if (scope == rootScope) { return; } if (scope.getParentScope() != null) { return; } throw new MultipleRootException(scope); }
@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 testBindScopeAnnotation_shouldReturnTrue_whenBound() { //GIVEN Scope parentScope = Toothpick.openScope("root"); parentScope.bindScopeAnnotation(CustomScope.class); //WHEN boolean isBoundToScopeAnnotation = parentScope.isBoundToScopeAnnotation(CustomScope.class); //THEN assertThat(isBoundToScopeAnnotation, is(true)); }
@Override public T get() { //the first test avoids accessing a volatile when not needed if (isLazy && instance != null) { return instance; } //ensure both sync for DSL //and sync around provider //so that dev's providers don't deal with concurrency synchronized (this) { if (isLazy) { //DCL if (instance == null) { instance = getScope().getInstance(clazz, name); scope.clear(); } return instance; } return getScope().getInstance(clazz, name); } }
@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 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)); }
@Override public void doRun() { //pick a random node in the tree, starting from root //add a new child node to this node Scope scope = NodeUtil.findRandomNode(scopeNames, ACCEPTANCE_THRESHOLD); if (scope == null) { setIsSuccessful(true); return; } //remove any node except root if (scope.getParentScope() == null) { setIsSuccessful(true); return; } Object scopeName = scope.getName(); Toothpick.closeScope(scopeName); setIsSuccessful(true); } }
Class<? extends Annotation> annotationType = annotation.annotationType(); if (annotationType.isAnnotationPresent(javax.inject.Scope.class)) { if (!scope.isBoundToScopeAnnotation(annotationType)) { throw new IllegalBindingException(format("Class %s cannot be bound." + " It has a scope annotation: %s that is not supported by current scope: %s", clazz.getName(), annotationType.getName(), scope.getName()));
@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 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)); }