/** * Performs no runtime checks. This configuration * is faster than {@link #forDevelopment()}. * It can be used in production. * * @return a production configuration. */ public static Configuration forProduction() { return new Configuration(); }
@Override public <T> T getInstance(Class<T> clazz, String name) { crashIfClosed(); ConfigurationHolder.configuration.checkCyclesStart(clazz, name); T t; try { t = lookupProvider(clazz, name).get(this); } finally { ConfigurationHolder.configuration.checkCyclesEnd(clazz, name); } return t; }
/** * Opens a scope without any parent. * If a scope by this {@code name} already exists, it is returned. * Otherwise a new scope is created. * * @param name the name of the scope to open. * @param shouldCheckMultipleRootScopes whether or not to check that the return scope * is not introducing a second tree in TP forest of scopes. */ private static Scope openScope(Object name, boolean shouldCheckMultipleRootScopes) { if (name == null) { throw new IllegalArgumentException("null scope names are not allowed."); } Scope scope = MAP_KEY_TO_SCOPE.get(name); if (scope != null) { return scope; } scope = new ScopeImpl(name); Scope previous = MAP_KEY_TO_SCOPE.putIfAbsent(name, scope); if (previous != null) { //if there was already a scope by this name, we return it scope = previous; } else if (shouldCheckMultipleRootScopes) { ConfigurationHolder.configuration.checkMultipleRootScopes(scope); } return scope; }
@AfterClass public static void staticTearDown() throws Exception { Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); }
@BeforeClass public static void setUp() throws Exception { ToothpickBaseTest.setUp(); Toothpick.setConfiguration(Configuration.forDevelopment().disableReflection()); }
@Test public void createScope_shouldReturnAnScopeWithAParent_whenThisScopeByThisKeyWasCreatedWithAParent() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forProduction()); ScopeNode scopeParent = (ScopeNode) Toothpick.openScope("foo"); //WHEN ScopeNode scope = (ScopeNode) Toothpick.openScope("bar"); scopeParent.addChild(scope); //THEN assertThat(scope, notNullValue()); assertThat(scope.getParentScope(), sameInstance(scopeParent)); }
/** * 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(); }
@Before public void setUpForDevelopmentConfiguration() throws Exception { Toothpick.setConfiguration(Configuration.forDevelopment()); } }
@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 }
throw new IllegalStateException("null binding are not allowed. Should not happen unless getBindingSet is overridden."); ConfigurationHolder.configuration.checkIllegalBinding(binding, this);
public static <T> Factory<T> getFactory(Class<T> clazz) { return ConfigurationHolder.configuration.getFactory(clazz); }
@Before public void setUp() throws Exception { Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); }
@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))); }
/** * Detach a scope from its parent, this will trigger the garbage collection of this scope and it's * sub-scopes * if they are not referenced outside of Toothpick. * * @param name the name of the scope to close. */ public static void closeScope(Object name) { //we remove the scope first, so that other threads don't see it, and see the next snapshot of the tree ScopeNode scope = (ScopeNode) MAP_KEY_TO_SCOPE.remove(name); if (scope != null) { ScopeNode parentScope = scope.getParentScope(); if (parentScope != null) { parentScope.removeChild(scope); } else { ConfigurationHolder.configuration.onScopeForestReset(); } removeScopeAndChildrenFromMap(scope); } }
@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(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 }
throw new IllegalStateException("null binding are not allowed. Should not happen unless getBindingSet is overridden."); ConfigurationHolder.configuration.checkIllegalBinding(binding, this);
@Before public void setUp() throws Exception { Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); Toothpick.openScope(ROOT_SCOPE); scopeNames.clear(); }
@Override public <T> T getInstance(Class<T> clazz, String name) { crashIfClosed(); ConfigurationHolder.configuration.checkCyclesStart(clazz, name); T t; try { t = lookupProvider(clazz, name).get(this); } finally { ConfigurationHolder.configuration.checkCyclesEnd(clazz, name); } return t; }
@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))); }