/** * 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(); }
@Override public void doRun() { Toothpick.closeScope(childScopeName); setIsSuccessful(true); } }
@Test public void closeScope_shouldNotFail_WhenThisScopesWasNotCreated() throws Exception { //GIVEN //WHEN Toothpick.closeScope("foo"); //THEN }
@Test public void closeScope_shouldMarkThisScopeAsClosed() throws Exception { //GIVEN ScopeImpl scope = (ScopeImpl) Toothpick.openScope("foo"); //WHEN Toothpick.closeScope("foo"); //THEN assertThat(scope.isOpen, is(false)); }
@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 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(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 } }
@Override protected void onDestroy() { Toothpick.closeScope(this); super.onDestroy(); }
@Override protected void onDestroy() { Toothpick.closeScope(this); super.onDestroy(); } }
@Override protected void onDestroy() { Toothpick.closeScope(this); super.onDestroy(); } }
@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 }
@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); } }
@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); } }
@Override protected void onDestroy() { Toothpick.closeScope(this); subscription.unsubscribe(); if (isFinishing()) { //when we leave the presenter flow, //we close its scope rxPresenter.stop(); Toothpick.closeScope(PRESENTER_SCOPE); } super.onDestroy(); }
@Override public void onBackPressed() { //when we leave the presenter flow, //we close its scope Toothpick.closeScope(PRESENTER_SCOPE); super.onBackPressed(); }
@Override protected void onDestroy() { subscriptions.unsubscribe(); super.onDestroy(); if (isFinishing()) { closeScope(DealDetailsScopeSingleton.class); } closeScope(this); }
/** * 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(); }