public static Scope findRandomNode(Object rooScopeName, int acceptanceThreshold) { Scope root = Toothpick.openScope(rooScopeName); Scope result = null; Stack<Scope> scopeStack = new Stack<Scope>(); scopeStack.push(root); while (result == null && !scopeStack.isEmpty()) { Scope scope = scopeStack.pop(); if (RANDOM.nextInt(RANDOM_INTERVAL_LENGTH) < acceptanceThreshold && scope != root) { result = scope; } else { for (Scope childScope : ((ScopeNode) scope).getChildrenScopes()) { scopeStack.push(childScope); } } } return result; }
public static Scope findRandomNode(Object rooScopeName, int acceptanceThreshold) { ScopeNode root = (ScopeNode) Toothpick.openScope(rooScopeName); ScopeNode result = null; Stack<ScopeNode> scopeStack = new Stack<>(); scopeStack.push(root); while (result == null && !scopeStack.isEmpty()) { ScopeNode scope = scopeStack.pop(); if (RANDOM.nextInt(RANDOM_INTERVAL_LENGTH) < acceptanceThreshold && scope != root) { result = scope; } else { for (ScopeNode childScope : scope.getChildrenScopes()) { scopeStack.push(childScope); } } } return result; } }
@Test public void testGetChildrenScopes_shouldReturnEmptyList_whenHasNoChildren() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); //WHEN boolean hasNoChildren = parentScope.getChildrenScopes().isEmpty(); //THEN assertThat(hasNoChildren, is(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)); }
@Test public void testGetChildrenScopes_shouldReturnChildren_whenHasChildren() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); ScopeNode childScope = new ScopeImpl("child"); parentScope.addChild(childScope); //WHEN Collection<ScopeNode> childrenScopes = parentScope.getChildrenScopes(); //THEN assertThat(childrenScopes.isEmpty(), is(false)); assertThat(childrenScopes.size(), is(1)); assertThat(childrenScopes.iterator().next(), is(childScope)); }
@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)); }