commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1225917 - /commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationNode.java
Date Fri, 30 Dec 2011 20:42:09 GMT
Author: oheger
Date: Fri Dec 30 20:42:09 2011
New Revision: 1225917

URL: http://svn.apache.org/viewvc?rev=1225917&view=rev
Log:
Converted tests to JUnit 4, fixed warnings.

Modified:
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationNode.java

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationNode.java?rev=1225917&r1=1225916&r2=1225917&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationNode.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationNode.java
Fri Dec 30 20:42:09 2011
@@ -16,18 +16,29 @@
  */
 package org.apache.commons.configuration.tree;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
 import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
 
-import junit.framework.TestCase;
+import org.junit.Before;
+import org.junit.Test;
 
 /**
  * Test class for DefaultConfigurationNode.
  *
- * @author Oliver Heger
+ * @author <a
+ * href="http://commons.apache.org/configuration/team-list.html">Commons
+ * Configuration team</a>
+ * @version $Id$
  */
-public class TestDefaultConfigurationNode extends TestCase
+public class TestDefaultConfigurationNode
 {
     /** Constant array for the field names. */
     private static final String[] FIELD_NAMES =
@@ -44,9 +55,9 @@ public class TestDefaultConfigurationNod
     /** The node to be tested. */
     DefaultConfigurationNode node;
 
-    protected void setUp() throws Exception
+    @Before
+    public void setUp() throws Exception
     {
-        super.setUp();
         node = new DefaultConfigurationNode();
         node.setName("table");
         node.setReference("TestReference");
@@ -79,6 +90,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests a newly created, uninitialized node.
      */
+    @Test
     public void testNewNode()
     {
         node = new DefaultConfigurationNode();
@@ -96,20 +108,22 @@ public class TestDefaultConfigurationNod
                 .isEmpty());
         assertNull("Node has a parent", node.getParentNode());
         assertFalse("Node is defined", node.isDefined());
-        try
-        {
-            node.getAttribute(0);
-            fail("Could access non existing attribute!");
-        }
-        catch (IndexOutOfBoundsException iex)
-        {
-            // ok
-        }
+    }
+
+    /**
+     * Tries to access an attribute using an invalid index.
+     */
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void testGetAttributeNonExisting()
+    {
+        node = new DefaultConfigurationNode();
+        node.getAttribute(0);
     }
 
     /**
      * Tests accessing a node's reference.
      */
+    @Test
     public void testGetReference()
     {
         assertEquals("Reference was not stored", "TestReference", node
@@ -119,12 +133,13 @@ public class TestDefaultConfigurationNod
     /**
      * Tests accessing the node's children.
      */
+    @Test
     public void testGetChildren()
     {
         assertEquals("Number of children incorrect", FIELD_NAMES.length + 1,
                 node.getChildrenCount());
-        List children = node.getChildren();
-        Iterator it = children.iterator();
+        List<ConfigurationNode> children = node.getChildren();
+        Iterator<ConfigurationNode> it = children.iterator();
         DefaultConfigurationNode child = (DefaultConfigurationNode) it.next();
         assertEquals("Wrong node", "name", child.getName());
         checkFieldNodes(it);
@@ -133,9 +148,10 @@ public class TestDefaultConfigurationNod
     /**
      * Tests accessing the node's children by name.
      */
+    @Test
     public void testGetChildrenByName()
     {
-        List children = node.getChildren("field");
+        List<ConfigurationNode> children = node.getChildren("field");
         assertEquals("Incorrect number of child nodes", FIELD_NAMES.length,
                 children.size());
         assertEquals("Incorrect result of getChildrenCount()",
@@ -150,6 +166,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests adding a new child node.
      */
+    @Test
     public void testAddChild()
     {
         int cnt = node.getChildrenCount();
@@ -157,7 +174,7 @@ public class TestDefaultConfigurationNod
                 "xyz");
         node.addChild(ndNew);
         assertEquals("New node was not added", cnt + 1, node.getChildrenCount());
-        List children = node.getChildren();
+        List<ConfigurationNode> children = node.getChildren();
         assertEquals("Incorrect number of children", node.getChildrenCount(),
                 children.size());
         assertSame("Node was not added to end", ndNew, children.get(cnt));
@@ -168,34 +185,27 @@ public class TestDefaultConfigurationNod
     }
 
     /**
-     * Tests adding invalid child nodes.
+     * Tries to add a null child node.
      */
-    public void testAddUndefinedChild()
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddChildNull()
     {
-        try
-        {
-            node.addChild(null);
-            fail("null node could be added!");
-        }
-        catch (IllegalArgumentException iex)
-        {
-            // ok
-        }
+        node.addChild(null);
+    }
 
-        try
-        {
-            node.addChild(new DefaultConfigurationNode());
-            fail("Node without name could be added!");
-        }
-        catch (IllegalArgumentException iex)
-        {
-            // ok
-        }
+    /**
+     * Tries to add a node without a name.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddUndefinedChild()
+    {
+        node.addChild(new DefaultConfigurationNode());
     }
 
     /**
      * Tests removing a child node.
      */
+    @Test
     public void testRemoveChild()
     {
         DefaultConfigurationNode child = (DefaultConfigurationNode) node
@@ -203,9 +213,9 @@ public class TestDefaultConfigurationNod
         int cnt = node.getChildrenCount();
         node.removeChild(child);
         assertEquals("Child was not removed", cnt - 1, node.getChildrenCount());
-        for (Iterator it = node.getChildren().iterator(); it.hasNext();)
+        for (ConfigurationNode nd : node.getChildren())
         {
-            assertNotSame("Found removed node", child, it.next());
+            assertNotSame("Found removed node", child, nd);
         }
         assertNull("Parent reference was not removed", child.getParentNode());
     }
@@ -213,6 +223,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests removing a child node that does not belong to this node.
      */
+    @Test
     public void testRemoveNonExistingChild()
     {
         int cnt = node.getChildrenCount();
@@ -227,6 +238,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests removing children by their name.
      */
+    @Test
     public void testRemoveChildByName()
     {
         int cnt = node.getChildrenCount();
@@ -240,6 +252,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests removing all children at once.
      */
+    @Test
     public void testRemoveChildren()
     {
         node.removeChildren();
@@ -250,6 +263,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests accessing a child by its index.
      */
+    @Test
     public void testGetChild()
     {
         ConfigurationNode child = node.getChild(2);
@@ -259,28 +273,22 @@ public class TestDefaultConfigurationNod
     /**
      * Tests accessing child nodes with invalid indices.
      */
+    @Test(expected = IndexOutOfBoundsException.class)
     public void testGetChildInvalidIndex()
     {
-        try
-        {
-            node.getChild(4724);
-            fail("Could access invalid index!");
-        }
-        catch (IndexOutOfBoundsException iex)
-        {
-            // ok
-        }
+        node.getChild(4724);
     }
 
     /**
      * Tests accessing the node's attributes.
      */
+    @Test
     public void testGetAttributes()
     {
         assertEquals("Number of attributes incorrect", 1, node
                 .getAttributeCount());
-        List attributes = node.getAttributes();
-        Iterator it = attributes.iterator();
+        List<ConfigurationNode> attributes = node.getAttributes();
+        Iterator<ConfigurationNode> it = attributes.iterator();
         DefaultConfigurationNode attr = (DefaultConfigurationNode) it.next();
         assertEquals("Wrong node", "type", attr.getName());
         assertFalse("More attributes", it.hasNext());
@@ -289,6 +297,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests accessing the node's attributes by name.
      */
+    @Test
     public void testGetAttributesByName()
     {
         assertEquals("Incorrect number of attributes", 1, node
@@ -297,7 +306,7 @@ public class TestDefaultConfigurationNod
                 .getChildren().get(1);
         assertEquals("Incorrect number of attributes", 2, field
                 .getAttributeCount("attribute"));
-        List attrs = field.getAttributes("attribute");
+        List<ConfigurationNode> attrs = field.getAttributes("attribute");
         assertEquals("Wrong value", "primarykey",
                 ((DefaultConfigurationNode) attrs.get(0)).getValue());
         assertEquals("Wrong value", "unique", ((DefaultConfigurationNode) attrs
@@ -307,6 +316,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests adding a new attribute node.
      */
+    @Test
     public void testAddAttribute()
     {
         int cnt = node.getAttributeCount();
@@ -315,7 +325,7 @@ public class TestDefaultConfigurationNod
         node.addAttribute(ndNew);
         assertEquals("New node was not added", cnt + 1, node
                 .getAttributeCount());
-        List attrs = node.getAttributes();
+        List<ConfigurationNode> attrs = node.getAttributes();
         assertEquals("Incorrect number of attributes",
                 node.getAttributeCount(), attrs.size());
         assertSame("Node was not added to end", ndNew, attrs.get(cnt));
@@ -328,6 +338,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests removing an attribute node.
      */
+    @Test
     public void testRemoveAttribute()
     {
         DefaultConfigurationNode attr = (DefaultConfigurationNode) node
@@ -336,9 +347,9 @@ public class TestDefaultConfigurationNod
         node.removeAttribute(attr);
         assertEquals("Attribute was not removed", cnt - 1, node
                 .getAttributeCount());
-        for (Iterator it = node.getAttributes().iterator(); it.hasNext();)
+        for (ConfigurationNode nd : node.getAttributes())
         {
-            assertNotSame("Found removed node", attr, it.next());
+            assertNotSame("Found removed node", attr, nd);
         }
         assertNull("Parent reference was not removed", attr.getParentNode());
     }
@@ -346,6 +357,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests removing attributes by their names.
      */
+    @Test
     public void testRemoveAttributeByName()
     {
         ConfigurationNode field = node.getChild(1);
@@ -362,6 +374,7 @@ public class TestDefaultConfigurationNod
     /**
      * Tests removing all attributes.
      */
+    @Test
     public void testRemoveAttributes()
     {
         node.removeAttributes();
@@ -372,23 +385,17 @@ public class TestDefaultConfigurationNod
     /**
      * Tests changing a node's attribute state.
      */
+    @Test(expected = IllegalStateException.class)
     public void testChangeAttributeState()
     {
         ConfigurationNode attr = node.getAttribute(0);
-        try
-        {
-            attr.setAttribute(false);
-            fail("Could change node's attribute state!");
-        }
-        catch (IllegalStateException iex)
-        {
-            // ok
-        }
+        attr.setAttribute(false);
     }
 
     /**
      * Tests the visit() method using a simple visitor.
      */
+    @Test
     public void testVisit()
     {
         CountNodeVisitor visitor = new CountNodeVisitor();
@@ -402,6 +409,7 @@ public class TestDefaultConfigurationNod
      * Tests the visit() method with a visitor that terminates the visit
      * process.
      */
+    @Test
     public void testVisitWithTerminate()
     {
         CountNodeVisitor visitor = new CountNodeVisitor(10);
@@ -416,22 +424,16 @@ public class TestDefaultConfigurationNod
      * Tests the visit() method when null is passed in. This should throw an
      * exception.
      */
+    @Test(expected = IllegalArgumentException.class)
     public void testVisitWithNullVisitor()
     {
-        try
-        {
-            node.visit(null);
-            fail("Could pass in null visitor!");
-        }
-        catch (IllegalArgumentException iex)
-        {
-            // ok
-        }
+        node.visit(null);
     }
 
     /**
      * Tests cloning a node.
      */
+    @Test
     public void testClone()
     {
         node.setValue("TestValue");
@@ -448,14 +450,14 @@ public class TestDefaultConfigurationNod
      *
      * @param itFields the iterator with the child nodes
      */
-    private void checkFieldNodes(Iterator itFields)
+    private void checkFieldNodes(Iterator<ConfigurationNode> itFields)
     {
         for (int i = 0; i < FIELD_NAMES.length; i++)
         {
             DefaultConfigurationNode child = (DefaultConfigurationNode) itFields
                     .next();
             assertEquals("Wrong node", "field", child.getName());
-            List nameNodes = child.getChildren("name");
+            List<ConfigurationNode> nameNodes = child.getChildren("name");
             assertEquals("Wrong number of name nodes", 1, nameNodes.size());
             DefaultConfigurationNode nameNode = (DefaultConfigurationNode) nameNodes
                     .get(0);



Mime
View raw message