Commits

Dmytro Kovalchuk committed 34b758c Draft

Code readability improvements

  • Participants
  • Parent commits 28ce5ab

Comments (0)

Files changed (9)

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

+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.registry;
+
+/**
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+public class EntryNotFoundException extends RuntimeException {
+
+    private final Path path;
+    private final String message;
+
+    public EntryNotFoundException(Path path) {
+        this.path = path;
+        this.message = createMessage();
+    }
+
+    public EntryNotFoundException(Path path, Throwable cause) {
+        super(cause);
+        this.path = path;
+        this.message = createMessage();
+
+    }
+
+    @Override
+    public String getMessage() {
+        return message;
+
+    }
+
+    public Path getPath() {
+        return path;
+    }
+
+    private String createMessage() {
+        return String.format("No registry entry exist for path %1$s", path.toString());
+    }
+}

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

-/*
- * Copyright 2012 Anatolich <anatolich@anatolich.net>.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.anatolich.registry;
-
-/**
- *
- * @author Anatolich <anatolich@anatolich.net>
- */
-public final class NodeIsNotPartOfRegistryException extends RuntimeException {
-
-    private final AbstractNode node;
-    private final String message;
-
-    public NodeIsNotPartOfRegistryException(AbstractNode node) {
-        this.node = node;
-        this.message = createMessage();
-    }
-
-    public NodeIsNotPartOfRegistryException(AbstractNode node, Throwable cause) {
-        super(cause);
-        this.node = node;
-        this.message = createMessage();
-    }
-
-    @Override
-    public String getMessage() {
-        return message;
-
-    }
-
-    private String createMessage() {
-        return String.format("Node %1$s is not registered in registry", node);
-    }
-}

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

-/*
- * Copyright 2012 Anatolich <anatolich@anatolich.net>.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.anatolich.registry;
-
-/**
- *
- * @author Anatolich <anatolich@anatolich.net>
- */
-public class NotExistingEntryException extends RuntimeException {
-
-    private final Path path;
-    private final String message;
-
-    public NotExistingEntryException(Path path) {
-        this.path = path;
-        this.message = createMessage();
-    }
-
-    public NotExistingEntryException(Path path, Throwable cause) {
-        super(cause);
-        this.path = path;
-        this.message = createMessage();
-
-    }
-
-    @Override
-    public String getMessage() {
-        return message;
-
-    }
-
-    public Path getPath() {
-        return path;
-    }
-
-    private String createMessage() {
-        return String.format("No registry entry exist for path %1$s", path.toString());
-    }
-}

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

+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.registry;
+
+/**
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+public final class NotInRegistryException extends RuntimeException {
+
+    private final AbstractNode node;
+    private final String message;
+
+    public NotInRegistryException(AbstractNode node) {
+        this.node = node;
+        this.message = createMessage();
+    }
+
+    public NotInRegistryException(AbstractNode node, Throwable cause) {
+        super(cause);
+        this.node = node;
+        this.message = createMessage();
+    }
+
+    @Override
+    public String getMessage() {
+        return message;
+
+    }
+
+    private String createMessage() {
+        return String.format("Node %1$s is not registered in registry", node);
+    }
+}

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

         return elementList.isEmpty();
     }
 
-    protected Iterator<String> pathElementsIterator() {
+    Iterator<String> pathElementsIterator() {
         return Collections.unmodifiableList(elementList).iterator();
     }
 

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

      * 
      * @param path
      * @return 
-     * @throws NotExistingEntryException when registry entry for path not exist.
+     * @throws EntryNotFoundException when registry entry for path not exist.
      */
     public AbstractNode getNodeByPath(Path path){
         Iterator<String> pathElementsIterator = path.pathElementsIterator();
             String nodeName = pathElementsIterator.next();
             currentNode = currentNode.getChild(nodeName);
             if (currentNode ==  null){
-                throw new NotExistingEntryException(path);
+                throw new EntryNotFoundException(path);
             }
         }
         return currentNode;
      * 
      * @param node
      * @return 
-     * @throws NodeIsNotPartOfRegistryException when node is not registered in registry.
+     * @throws NotInRegistryException when node is not registered in registry.
      */
     public Path getNodePath(AbstractNode node) {
         List<String> pathElements = new ArrayList<>();
         }
         
         if(!currentNode.equals(rootNode)){ // Check if node is detached 
-            throw new NodeIsNotPartOfRegistryException(node);
+            throw new NotInRegistryException(node);
         }
         
         Collections.reverse(pathElements);

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

 
     @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());

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

+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.registry;
+
+import net.anatolich.registry.RegistryTest.MenuItemNode;
+import net.anatolich.registry.RegistryTest.MenuNode;
+
+/**
+ * Contains fixture for registry holding simple menu hierarchy.
+ * <pre>
+ * /
+ * -- menus
+ *   -- file
+ *     -- save
+ *     -- open
+ *   -- edit
+ *     -- cut
+ *     -- copy
+ * </pre>
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+final class DefaultRegistryFixture {
+
+    private Registry registry;
+    private AbstractNode menusNode;
+    private AbstractNode fileMenuNode;
+    private AbstractNode fileOpenMenuItemNode;
+    private AbstractNode fileSaveMenuItemNode;
+    private AbstractNode editMenuNode;
+    private AbstractNode editCutMenuItemNode;
+    private AbstractNode editCopyMenuItemNode;
+
+    public DefaultRegistryFixture() {
+        registry = new Registry();
+        
+        fileSaveMenuItemNode = new MenuItemNode("save", 100);
+        fileOpenMenuItemNode = new MenuItemNode("open", 200);
+        fileMenuNode = new MenuNode("file", 100);
+        
+        fileMenuNode.addChild(fileOpenMenuItemNode);
+        fileMenuNode.addChild(fileSaveMenuItemNode);
+        
+        editCutMenuItemNode = new MenuItemNode("cut", 100);
+        editCopyMenuItemNode = new MenuItemNode("copy", 200);
+        editMenuNode = new MenuNode("edit", 200);
+        
+        editMenuNode.addChild(editCutMenuItemNode);
+        editMenuNode.addChild(editCopyMenuItemNode);
+        menusNode = new MenuNode("menus", 0);
+        
+        menusNode.addChild(fileMenuNode);
+        menusNode.addChild(editMenuNode);
+        
+        registry.rootNode().addChild(menusNode);
+    }
+
+    /**
+     * @return the editMenuNode
+     */
+    public AbstractNode getEditMenuNode() {
+        return editMenuNode;
+    }
+
+    /**
+     * @return the registry
+     */
+    public Registry getRegistry() {
+        return registry;
+    }
+
+    /**
+     * @return the editCopyMenuItemNode
+     */
+    public AbstractNode getEditCopyMenuItemNode() {
+        return editCopyMenuItemNode;
+    }
+
+    /**
+     * @return the fileSaveMenuItemNode
+     */
+    public AbstractNode getFileSaveMenuItemNode() {
+        return fileSaveMenuItemNode;
+    }
+
+    /**
+     * @return the fileMenuNode
+     */
+    public AbstractNode getFileMenuNode() {
+        return fileMenuNode;
+    }
+
+    /**
+     * @return the fileOpenMenuItemNode
+     */
+    public AbstractNode getFileOpenMenuItemNode() {
+        return fileOpenMenuItemNode;
+    }
+
+    /**
+     * @return the menusNode
+     */
+    public AbstractNode getMenusNode() {
+        return menusNode;
+    }
+
+    /**
+     * @return the editCutMenuItemNode
+     */
+    public AbstractNode getEditCutMenuItemNode() {
+        return editCutMenuItemNode;
+    }
+}

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

  */
 package net.anatolich.registry;
 
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
 import org.junit.Before;
 import org.junit.Test;
-import static org.junit.Assert.*;
-import static org.easymock.EasyMock.*;
 
 /**
  *
  */
 public class RegistryTest {
     
-    private Registry registry;
-    private AbstractNode menusNode;
-    private AbstractNode fileMenuNode;
-    private AbstractNode fileSaveMenuItemNode;
-    private AbstractNode fileOpenMenuItemNode;
-    private AbstractNode editMenuNode;
-    private AbstractNode editCutMenuItemNode;
-    private AbstractNode editCopyMenuItemNode;
-    
+    private DefaultRegistryFixture defaultRegistryFixture;
+
     @Before
     public void setUp() {
-        registry = new Registry();
+        defaultRegistryFixture = new DefaultRegistryFixture();
+    }
 
-        fileSaveMenuItemNode = new MenuItemNode("save", 100);
-        fileOpenMenuItemNode = new MenuItemNode("open", 200);
-        
-        fileMenuNode = new MenuNode("file", 100);
-        fileMenuNode.addChild(fileOpenMenuItemNode);
-        fileMenuNode.addChild(fileSaveMenuItemNode);
-        
-        editCutMenuItemNode = new MenuItemNode("cut", 100);
-        editCopyMenuItemNode = new MenuItemNode("copy", 200);
-        
-        editMenuNode = new MenuNode("edit", 200);
-        editMenuNode.addChild(editCutMenuItemNode);
-        editMenuNode.addChild(editCopyMenuItemNode);
-        
-        menusNode = new MenuNode("menus", 0);
-        menusNode.addChild(fileMenuNode);
-        menusNode.addChild(editMenuNode);        
-        
-        registry.rootNode().addChild(menusNode);
-    }
-    
     /**
      * Test of getNodeByPath method, of class Registry.
      */
     @Test
     public void testGetNodeByPath() {
         Path path = Path.fromString("/menus/file/save");
-        AbstractNode expResult = fileSaveMenuItemNode;
-        AbstractNode result = registry.getNodeByPath(path);
+        AbstractNode expResult = defaultRegistryFixture.getFileSaveMenuItemNode();
+        AbstractNode result = defaultRegistryFixture.getRegistry().getNodeByPath(path);
         assertEquals(expResult, result);
     }
-    
-    @Test(expected=NotExistingEntryException.class)
+
+    @Test(expected = EntryNotFoundException.class)
     public void testGetNodeByPathFail() {
         Path path = Path.fromString("/menus/file/new");
-        AbstractNode expResult = fileSaveMenuItemNode;
-        AbstractNode result = registry.getNodeByPath(path);
-        
+        defaultRegistryFixture.getRegistry().getNodeByPath(path); // This operation must be failed
+
         fail("This code must not be reached due to exception thrown");
     }
-    
+
     @Test
     public void testGetRootNodeByPath() {
         Path path = Path.fromString("/");
-        AbstractNode expResult = registry.rootNode();
-        AbstractNode result = registry.getNodeByPath(path);
+        AbstractNode expResult = defaultRegistryFixture.getRegistry().rootNode();
+        AbstractNode result = defaultRegistryFixture.getRegistry().getNodeByPath(path);
         assertEquals(expResult, result);
     }
-    
+
     @Test
-    public void testGetNodePath(){
+    public void testGetNodePath() {
         Path expResut = Path.fromString("/menus/file/save");
-        Path result = registry.getNodePath(fileSaveMenuItemNode);
-        
+        Path result = defaultRegistryFixture.getRegistry().getNodePath(defaultRegistryFixture.getFileSaveMenuItemNode());
+
         assertEquals(expResut, result);
     }
-    
-    @Test(expected=NodeIsNotPartOfRegistryException.class)
-    public void testGetNodePathForDetachedNode(){
+
+    @Test(expected = NotInRegistryException.class)
+    public void testGetNodePathForDetachedNode() {
         AbstractNode detachedNode = new MenuItemNode("new", 0);
-        
-        registry.getNodePath(detachedNode);
+
+        defaultRegistryFixture.getRegistry().getNodePath(detachedNode);
     }
-    
+
     @Test
-    public void testNotificationFiringOnAddingNode(){
+    public void testNotificationFiringOnAddingNode() {
         NodeEventSupport eventSupport = createMock(NodeEventSupport.class);
         Registry reg = new Registry(eventSupport);
-        
+
         AbstractNode addedNode = new MenuItemNode("new", 0);
-        
-        eventSupport.fireNodeAddedEvent(menusNode);
+
+        eventSupport.fireNodeAddedEvent(defaultRegistryFixture.getMenusNode());
         eventSupport.fireNodeAddedEvent(addedNode);
         expectLastCall().anyTimes();
         replay(eventSupport);
-        
-        reg.rootNode().addChild(menusNode);
-        fileMenuNode.addChild(addedNode);
-        
-        verify(eventSupport);
-    }
-    
-    @Test
-    public void testNotificationFiringOnRemovingNode(){
-        NodeEventSupport eventSupport = createMock(NodeEventSupport.class);
-        Registry reg = new Registry(eventSupport);
-        
-        AbstractNode addedNode = new MenuItemNode("new", 0);
-        
-        eventSupport.fireNodeAddedEvent(menusNode);
-        eventSupport.fireNodeAddedEvent(addedNode);
-        eventSupport.fireNodeRemovedEvent(addedNode);
-        expectLastCall().anyTimes();
-        replay(eventSupport);
-        
-        reg.rootNode().addChild(menusNode);
-        fileMenuNode.addChild(addedNode);
-        
-        fileMenuNode.removeChild(addedNode);
-        
+
+        reg.rootNode().addChild(defaultRegistryFixture.getMenusNode());
+        defaultRegistryFixture.getFileMenuNode().addChild(addedNode);
+
         verify(eventSupport);
     }
 
-    private static class MenuNode extends AbstractNode<Attributes>{
+    @Test
+    public void testNotificationFiringOnRemovingNode() {
+        NodeEventSupport eventSupport = createMock(NodeEventSupport.class);
+        Registry reg = new Registry(eventSupport);
+
+        AbstractNode addedNode = new MenuItemNode("new", 0);
+
+        eventSupport.fireNodeAddedEvent(anyObject(AbstractNode.class));
+        expectLastCall().anyTimes();
+
+        eventSupport.fireNodeRemovedEvent(addedNode);
+
+        replay(eventSupport);
+
+        reg.rootNode().addChild(defaultRegistryFixture.getMenusNode());
+        defaultRegistryFixture.getFileMenuNode().addChild(addedNode);
+
+        defaultRegistryFixture.getFileMenuNode().removeChild(addedNode);
+
+        verify(eventSupport);
+    }
+
+    static class MenuNode extends AbstractNode<Attributes> {
 
         public MenuNode(String name, int position) {
-            super(name, new Attributes() {}, position);
+            super(name, new Attributes() {
+            }, position);
         }
+    }
 
-    }
-    
-    private static class MenuItemNode extends AbstractItemNode<Attributes> {
+    static class MenuItemNode extends AbstractItemNode<Attributes> {
 
         public MenuItemNode(String name, int position) {
-            super(name, new Attributes() {}, position);
+            super(name, new Attributes() {
+            }, position);
         }
-        
     }
-    
+
 }