Commits

Dmytro Kovalchuk committed 7fa7c51 Draft

Now child nodes cannot be removed by using "remove" method if childIterator

Comments (0)

Files changed (2)

registry/src/main/java/net/anatolich/registry/AbstractNode.java

     }
 
     public Iterator<AbstractNode> childIterator() {
-        return children.iterator();
+        return Collections.unmodifiableList(children).iterator();
     }
 
     public final int childrenCount() {

registry/src/test/java/net/anatolich/registry/AbstractNodeTest.java

  * @author Anatolich <anatolich@anatolich.net>
  */
 public class AbstractNodeTest extends EasyMockSupport {
+
     protected AbstractNode groupNode;
     protected AbstractNode node1 = new AbstractNode("node1", null, 0) {
     };
         AbstractNode result = instance.getParent();
         assertEquals(expResult, result);
     }
-    
+
     @Test
-    public void testHasParent(){
+    public void testHasParent() {
         final AbstractNode parentNode = createNiceMock(AbstractNode.class);
         AbstractNode instance = new AbstractNodeImpl("instance", null);
-        
-        boolean result = instance.hasParent();        
+
+        boolean result = instance.hasParent();
         assertFalse("Parent node it not set so hasParent must be false", result);
-        
+
         instance.setParent(parentNode);
         result = instance.hasParent();
         assertTrue("Parent node it set so hasParent must be true", result);
         assertTrue("node100a, node100a", comparator.compare(node100a, node100a) == 0);
 
     }
-    
-    @Test(expected=IllegalArgumentException.class)
-    public void testInvalidNodeName(){
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidNodeName() {
         String invalidName = "invalid/node/name"; //must not contain slashes
-        
+
         AbstractNode node = new AbstractNodeImpl(invalidName);
     }
 
     @Before
     public void setUp() {
-        groupNode = new AbstractNode("Name", new Attributes() {}, 0) {};
+        groupNode = new AbstractNode("Name", new Attributes() {
+        }, 0) {
+        };
     }
 
     @Test
     public void testAddingChild() {
         NodeChangeListener changeListener = createMock(NodeChangeListener.class);
-        
+
         changeListener.nodeAdded(new NodeChangeEvent(node1, groupNode));
         changeListener.nodeAdded(new NodeChangeEvent(node2, groupNode));
-        
+
         groupNode.addNodeChangeListener(changeListener);
         replay(changeListener);
-        
+
         groupNode.addChild(node1);
         assertEquals(1, groupNode.childrenCount());
-        
+
         groupNode.addChild(node2);
         assertEquals(2, groupNode.childrenCount());
-        
+
         verify(changeListener);
     }
-    
+
     @Test
-    public void testRemoveChild(){
+    public void testRemoveChild() {
         NodeChangeListener changeListener = createMock(NodeChangeListener.class);
-        
+
         changeListener.nodeAdded(new NodeChangeEvent(node1, groupNode));
         changeListener.nodeAdded(new NodeChangeEvent(node2, groupNode));
-        
+
         changeListener.nodeRemoved(new NodeChangeEvent(node2, groupNode));
         changeListener.nodeRemoved(new NodeChangeEvent(node1, groupNode));
-        
+
         replay(changeListener);
-        
+
         //Initialization
         groupNode.addNodeChangeListener(changeListener);
         groupNode.addChild(node1);
         groupNode.addChild(node2);
-        
+
         groupNode.removeChild(node2);
         assertEquals("Should be one child node after removing one child", 1, groupNode.childrenCount());
-        
+
         groupNode.removeChild(node2);
         assertEquals("Children count should not change after removing non-existing node", 1, groupNode.childrenCount());
-        
+
         groupNode.removeChild(node1);
         assertEquals("Children count should not change after removing non-existing node", 0, groupNode.childrenCount());
-                
-        verify(changeListener);        
+
+        verify(changeListener);
     }
 
     @Test
     public void testChildSorting() {
-        AbstractNode node100 = new AbstractNode("Node100", null, 100) {};        
-        AbstractNode node200 = new AbstractNode("Node200", null, 200) {};
-        AbstractNode node300 = new AbstractNode("Node300", null, 300) {};
-        
+        AbstractNode node100 = new AbstractNode("Node100", null, 100) {
+        };
+        AbstractNode node200 = new AbstractNode("Node200", null, 200) {
+        };
+        AbstractNode node300 = new AbstractNode("Node300", null, 300) {
+        };
+
         groupNode.addChild(node300);
         groupNode.addChild(node100);
         groupNode.addChild(node200);
-        
+
         Iterator<AbstractNode> childIterator = groupNode.childIterator();
-        
+
         assertEquals(node100, childIterator.next());
         assertEquals(node200, childIterator.next());
         assertEquals(node300, childIterator.next());
     }
-    
+
     @Test
-    public void testNodeOverrideByName(){
+    public void testNodeOverrideByName() {
         String nodeName = "item";
-        AbstractNode earlierNode = new AbstractNode(nodeName, null, 0) {};
-        AbstractNode latterNode = new AbstractNode(nodeName, null, 10) {};
-        
+        AbstractNode earlierNode = new AbstractNode(nodeName, null, 0) {
+        };
+        AbstractNode latterNode = new AbstractNode(nodeName, null, 10) {
+        };
+
         groupNode.addChild(earlierNode);
         groupNode.addChild(latterNode);
-        
-        assertEquals("Nodes with the same name must override each other",1, groupNode.childrenCount());
+
+        assertEquals("Nodes with the same name must override each other", 1, groupNode.childrenCount());
         assertEquals("Latter node must be available by named query", latterNode, groupNode.getChild(nodeName));
     }
 
         groupNode.addChild(childNode);
         assertEquals(groupNode, childNode.getParent());
     }
-    
+
     @Test
-    public void testParentChanging(){
-        AbstractNode parent1 = new AbstractNode("parent_1", null, 0) {};
-        AbstractNode parent2 = new AbstractNode("parent_2", null, 0) {};
-        
+    public void testParentChanging() {
+        AbstractNode parent1 = new AbstractNode("parent_1", null, 0) {
+        };
+        AbstractNode parent2 = new AbstractNode("parent_2", null, 0) {
+        };
+
         AbstractNode child = new AbstractNode.LeafNode<>("child", null, 0);
-        
+
         parent1.addChild(child);
         parent2.addChild(child);
+
+        assertNull("Old parent node must not contain child node", parent1.getChild("child"));
+
+    }
+
+    @Test
+    public void testThatRemoveMethodDoesNotChangeChildList() {
+        AbstractNode parent = new AbstractNode("parent", null, 0) {
+        };
+        AbstractNode child1 = new AbstractNode.LeafNode<>("child1", null, 0);
+        AbstractNode child2 = new AbstractNode.LeafNode<>("child2", null, 0);
+
+        parent.addChild(child1);
+        parent.addChild(child2);
+
+        Iterator childIterator = parent.childIterator();
+
+        childIterator.next();
+        try {
+            childIterator.remove();
+            fail("Exception must be thrown by iterator remove method");
+        } catch (UnsupportedOperationException ex) {
+            assertEquals("Children must not be affected by calling iterator remove method", 2, parent.childrenCount());
+        }
         
-        assertNull("Old parent node must not contain child node", parent1.getChild("child"));
-        
+
     }
 
     /**
         public AbstractNodeImpl(String name, Attributes attributes, int position) {
             super(name, attributes, position);
         }
-        
     }
 }