/** * Opens multiple scopes in a row. * Opened scopes will be children of each other in left to right order (e.g. {@code * openScopes(a,b)} opens scopes {@code a} and {@code b} * and {@code b} is a child of {@code a}. * * @param names of the scopes to open hierarchically. * @return the last opened scope, leaf node of the created subtree of scopes. */ public static Scope openScopes(Object... names) { if (names == null) { throw new IllegalArgumentException("null scope names are not allowed."); } if (names.length == 0) { throw new IllegalArgumentException("Minimally, one scope name is required."); } ScopeNode lastScope = null; ScopeNode previousScope = (ScopeNode) openScope(names[0], true); for (int i = 1; i < names.length; i++) { lastScope = (ScopeNode) openScope(names[i], false); lastScope = previousScope.addChild(lastScope); previousScope = lastScope; } return previousScope; }
@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(expected = IllegalStateException.class) public void testAddChild_shouldFail_whenChildAlreadyHasParent() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); ScopeNode parentScope2 = new ScopeImpl("foo"); ScopeNode childScope = new ScopeImpl("child"); parentScope.addChild(childScope); //WHEN parentScope2.addChild(childScope); //THEN fail("Should throw an exception"); }
@Test public void testGetParentScopeNames_shouldReturnParentNames_whenThereAreParents() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); ScopeNode childScope = new ScopeImpl("child"); parentScope.addChild(childScope); //WHEN final List<Object> parentScopesNames = childScope.getParentScopesNames(); //THEN assertThat(parentScopesNames.size(), is(1)); assertThat(parentScopesNames.iterator().next(), is(parentScope.getName())); }
@Test(expected = IllegalArgumentException.class) public void testAddChild_shouldFail_whenChildIsNull() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); //WHEN parentScope.addChild(null); //THEN fail("Should throw an exception"); }
@Test public void testGetParentScopeNames_shouldReturnParentNamesInOrder_whenThereAreParents() { //GIVEN ScopeNode parentScope = new ScopeImpl("root"); ScopeNode childScope = new ScopeImpl("child"); ScopeNode grandChildScope = new ScopeImpl("grandChild"); parentScope.addChild(childScope); childScope.addChild(grandChildScope); //WHEN final List<Object> grandParentScopesNames = grandChildScope.getParentScopesNames(); //THEN assertThat(grandParentScopesNames.size(), is(2)); final Iterator<Object> iterator = grandParentScopesNames.iterator(); assertThat(iterator.next(), is(childScope.getName())); assertThat(iterator.next(), is(parentScope.getName())); }
@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 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 public void getOrCreateScope_shouldReturnANewScopeScope_WhenOneWasNotCreatedWithSameKey() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forProduction()); ScopeNode scopeParent = (ScopeNode) Toothpick.openScope("bar"); //WHEN ScopeNode scope = (ScopeNode) Toothpick.openScope("foo"); scopeParent.addChild(scope); ScopeNode scope2 = (ScopeNode) Toothpick.openScope("foo"); //THEN assertThat(scope, notNullValue()); assertThat(scope2, sameInstance(scope)); assertThat(scope.getParentScope(), sameInstance(scopeParent)); }
@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"); }
/** * Opens multiple scopes in a row. * Opened scopes will be children of each other in left to right order (e.g. {@code openScopes(a,b)} opens scopes {@code a} and {@code b} * and {@code b} is a child of {@code a}. * * @param names of the scopes to open hierarchically. * @return the last opened scope, leaf node of the created subtree of scopes. */ public static Scope openScopes(Object... names) { if (names == null) { throw new IllegalArgumentException("null scopes can't be open."); } ScopeNode previousScope; ScopeNode lastScope = null; for (Object name : names) { previousScope = lastScope; lastScope = (ScopeNode) openScope(name); if (previousScope != null) { //if there was already such a node, we add a new child //but there might already be such a child, in that case //we use it. lastScope = previousScope.addChild(lastScope); } } return lastScope; }
/** * Opens multiple scopes in a row. * Opened scopes will be children of each other in left to right order (e.g. {@code * openScopes(a,b)} opens scopes {@code a} and {@code b} * and {@code b} is a child of {@code a}. * * @param names of the scopes to open hierarchically. * @return the last opened scope, leaf node of the created subtree of scopes. */ public static Scope openScopes(Object... names) { if (names == null) { throw new IllegalArgumentException("null scope names are not allowed."); } if (names.length == 0) { throw new IllegalArgumentException("Minimally, one scope name is required."); } ScopeNode lastScope = null; ScopeNode previousScope = (ScopeNode) openScope(names[0], true); for (int i = 1; i < names.length; i++) { lastScope = (ScopeNode) openScope(names[i], false); lastScope = previousScope.addChild(lastScope); previousScope = lastScope; } return previousScope; }