/** * 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 closeScope_shouldRemoveChildScope_whenChildScopeIsClosed() throws Exception { //GIVEN Toothpick.openScopes("foo", "bar"); //WHEN Toothpick.closeScope("bar"); //THEN assertThat(((ScopeNode) Toothpick.openScope("foo")).getChildrenScopes().isEmpty(), is(true)); }
@Override public void doRun() { Toothpick.openScope(scopeName).toString(); setIsSuccessful(true); } }
@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)); }
@Before public void setUp() throws Exception { Toothpick.openScope(ROOT_SCOPE); scopeNames.clear(); }
@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 public void getScope_shouldNotReturnNull_whenNoScopeByThisKeyWasCreated() throws Exception { //GIVEN //WHEN Scope scope = Toothpick.openScope(this); //THEN assertThat(scope, notNullValue()); }
@Test public void createScope_shouldMarkThisScopeAsOpen() throws Exception { //GIVEN //WHEN ScopeImpl scope = (ScopeImpl) Toothpick.openScope("foo"); //THEN assertThat(scope.isOpen, is(true)); }
@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 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))); }
@Override public void doRun() { Toothpick.openScope(scopeName).getInstance(clazz); setIsSuccessful(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 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)); }
@Test public void testBindScopeAnnotation_shouldReturnTrue_whenRootScopeAskedForSingleton() { //GIVEN Scope parentScope = Toothpick.openScope("root"); //WHEN boolean isBoundToSingleton = parentScope.isBoundToScopeAnnotation(Singleton.class); //THEN assertThat(isBoundToSingleton, is(true)); }
@Test(expected = IllegalArgumentException.class) public void testBindScopeAnnotation_shouldFail_whenSingleton() { //GIVEN Scope scope = Toothpick.openScope("root"); //WHEN scope.bindScopeAnnotation(Singleton.class); //THEN fail("Should throw an exception"); }
@Test(expected = IllegalArgumentException.class) public void testBindScopeAnnotation_shouldFail_whenAnnotationIsNotAScope() { //GIVEN Scope scope = Toothpick.openScope("root"); //WHEN scope.bindScopeAnnotation(NotAScope.class); //THEN fail("Should throw an exception"); }
@Test(expected = IllegalStateException.class) public void testGetParentScope_shouldFail_whenNoParentBoundToScopeAnnotation() { //GIVEN Scope scope = Toothpick.openScope("root"); //WHEN scope.getParentScope(CustomScope.class); //THEN fail("Should throw an exception"); }
@Test(expected = IllegalArgumentException.class) public void testGetParentScope_shouldFail_WhenAnnotationIsNotAScope() { //GIVEN Scope scope = Toothpick.openScope("root"); //WHEN scope.getParentScope(NotAScope.class); //THEN fail("Should throw an exception"); }
@Before public void setUp() throws Exception { Toothpick.setConfiguration(Configuration.forProduction().disableReflection()); Toothpick.openScope(ROOT_SCOPE); scopeNames.clear(); }