@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 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 = 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"); }
@Test public void testGetParentScope_shouldReturnParentScope_whenParentBoundToScopeAnnotation() { //GIVEN Scope parentScope = Toothpick.openScope("root"); parentScope.bindScopeAnnotation(CustomScope.class); Scope childScope = Toothpick.openScopes("root", "child"); //WHEN Scope scope = childScope.getParentScope(CustomScope.class); //THEN assertThat(scope, is(parentScope)); }
@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 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); }