/** * 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(expected = IllegalArgumentException.class) public void testRemoveChild_shouldFail_whenChildIsNull() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); //WHEN parentScope.removeChild(null); //THEN fail("Should throw an exception"); }
@Test public void testRemoveChild() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); ScopeNode childScope = new ScopeImpl("child"); parentScope.addChild(childScope); //WHEN parentScope.removeChild(childScope); //WHEN Collection<ScopeNode> childrenScopes = parentScope.getChildrenScopes(); //THEN assertThat(childrenScopes.isEmpty(), is(true)); }
@Test(expected = IllegalStateException.class) public void testRemoveChild_shouldFail_whenChildHasNoParent() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); ScopeNode childScope = new ScopeImpl("child"); //WHEN parentScope.removeChild(childScope); //THEN fail("Should throw an exception"); }
@Test(expected = IllegalStateException.class) public void testRemoveChild_shouldFail_whenChildHasDifferentParent() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); ScopeNode parentScope2 = new ScopeImpl("foo"); ScopeNode childScope = new ScopeImpl("child"); parentScope.addChild(childScope); //WHEN parentScope2.removeChild(childScope); //THEN fail("Should throw an exception"); }
/** * 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); } removeScopeAndChildrenFromMap(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); } }