ScopeNode implements Scope { ScopeNode addChild(ScopeNode child) { if (child == null) { throw new IllegalArgumentException("Child must be non null."); } final ScopeNode parentScope = child.getParentScope(); if (parentScope == this) { return child; } if (parentScope != null) { throw new IllegalStateException( format("Scope %s already has a parent: %s which is not %s", child, parentScope, this)); } ScopeNode scope = childrenScopes.putIfAbsent(child.getName(), child); if (scope != null) { return scope; } child.parentScopes.add(this); child.parentScopes.addAll(parentScopes); return child; } ScopeNode(Object name); @Override Object getName(); @Override boolean equals(Object o); @Override int hashCode(); @Override ScopeNode getParentScope(); @SuppressWarnings({"unused", "used by generated code"}) @Override ScopeNode getParentScope(Class<A> scopeAnnotationClass); @SuppressWarnings({"unused", "used by generated code"}) @Override ScopeNode getRootScope(); @Override Scope supportScopeAnnotation(Class<? extends Annotation> scopeAnnotationClass); @Override boolean isScopeAnnotationSupported(Class<? extends Annotation> scopeAnnotationClass);  }
@Test(expected = IllegalArgumentException.class) public void testAddChild_shouldFail_whenChildIsNull() { ScopeNode parentScope = new ScopeImpl("root"); parentScope.addChild(null); fail("Should throw an exception"); }
@Test public void testAddChild_shouldReturnChild() { ScopeNode parentScope = new ScopeImpl("root"); ScopeNode childScope = new ScopeImpl("child"); parentScope.addChild(childScope); ScopeNode child = parentScope.addChild(childScope); assertThat(child, is(childScope)); }
@Test(expected = IllegalStateException.class) public void testAddChild_shouldFail_whenChildAlreadyHasParent() { ScopeNode parentScope = new ScopeImpl("root"); ScopeNode parentScope2 = new ScopeImpl("foo"); ScopeNode childScope = new ScopeImpl("child"); parentScope.addChild(childScope); parentScope2.addChild(childScope); fail("Should throw an exception"); }