@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(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(expected = IllegalStateException.class) public void testLazyAfterClosingScope() throws Exception { //GIVEN String scopeName = ""; FooWithLazy fooWithLazy = new FooWithLazy(); //WHEN Toothpick.inject(fooWithLazy, Toothpick.openScope(scopeName)); Toothpick.closeScope(scopeName); System.gc(); //THEN fooWithLazy.bar.get(); // should crash } }
@Test public void closeScope_shouldMarkThisScopeAsClosed() throws Exception { //GIVEN ScopeImpl scope = (ScopeImpl) Toothpick.openScope("foo"); //WHEN Toothpick.closeScope("foo"); //THEN assertThat(scope.isOpen, is(false)); }
/** * Opens a scope without any parent. * If a scope by this {@code name} already exists, it is returned. * Otherwise a new scope is created. */ public static Scope openScope(Object name) { return openScope(name, true); }
@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 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(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 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 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))); }
@Override public void evaluate() throws Throwable { try { base.evaluate(); } finally { Toothpick.reset(); } } }
@Override protected void onCreate(Bundle savedInstanceState) { scope = Toothpick.openScopes(getApplication(), PRESENTER_SCOPE, this); scope.installModules(new SmoothieActivityModule(this)); super.onCreate(savedInstanceState); Toothpick.inject(this, scope); setContentView(R.layout.simple_activity); ButterKnife.bind(this); title.setText("MVP"); subTitle.setText(contextNamer.getInstanceCount()); button.setVisibility(View.GONE); }
/** * Clears all scopes. Useful for testing and not getting any leak... */ public static void reset() { for (Object name : Collections.list(MAP_KEY_TO_SCOPE.keys())) { closeScope(name); } ConfigurationHolder.configuration.onScopeForestReset(); ScopeImpl.resetUnBoundProviders(); }
@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."); }
public void inject(Object objectUnderTest) { Toothpick.inject(objectUnderTest, scope); }
@Override public void doRun() { Toothpick.openScopes(parentScopeName, childScopeName); setIsSuccessful(true); } }
@Before public void setUpForDevelopmentConfiguration() throws Exception { Toothpick.setConfiguration(Configuration.forDevelopment()); } }
/** * Removes all nodes of {@code scope} using DFS. We don't lock here. * * @param scope the parent scope of which all children will recursively be removed * from the map. We don't do anything else to the children nodes are they will be * garbage collected soon. We just cut a whole sub-graph in the references graph of the JVM * normally. */ private static void removeScopeAndChildrenFromMap(ScopeNode scope) { MAP_KEY_TO_SCOPE.remove(scope.getName()); scope.close(); for (ScopeNode childScope : scope.childrenScopes.values()) { removeScopeAndChildrenFromMap(childScope); } }