commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1225013 - /commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
Date Tue, 27 Dec 2011 20:59:36 GMT
Author: oheger
Date: Tue Dec 27 20:59:35 2011
New Revision: 1225013

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

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

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java?rev=1225013&r1=1225012&r2=1225013&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
Tue Dec 27 20:59:35 2011
@@ -17,27 +17,34 @@
 
 package org.apache.commons.configuration;
 
-import java.lang.reflect.Method;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.annotation.ElementType;
 import java.math.BigDecimal;
 import java.util.Iterator;
 import java.util.List;
 
-import junit.framework.TestCase;
-
-import org.apache.commons.lang.SystemUtils;
+import org.junit.Test;
 
 /**
  * Test class for PropertyConverter.
  *
  * @author Emmanuel Bourg
- * @version $Revision$, $Date$
+ * @version $Id$
  */
-public class TestPropertyConverter extends TestCase
+public class TestPropertyConverter
 {
+    /** Constant for an enumeration class used by some tests. */
+    private static final Class<ElementType> ENUM_CLASS = ElementType.class;
+
+    @Test
     public void testSplit()
     {
         String s = "abc, xyz , 123";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 3, list.size());
         assertEquals("1st token for '" + s + "'", "abc", list.get(0));
@@ -45,10 +52,11 @@ public class TestPropertyConverter exten
         assertEquals("3rd token for '" + s + "'", "123", list.get(2));
     }
 
+    @Test
     public void testSplitNoTrim()
     {
         String s = "abc, xyz , 123";
-        List list = PropertyConverter.split(s, ',', false);
+        List<String> list = PropertyConverter.split(s, ',', false);
 
         assertEquals("size", 3, list.size());
         assertEquals("1st token for '" + s + "'", "abc", list.get(0));
@@ -56,20 +64,22 @@ public class TestPropertyConverter exten
         assertEquals("3rd token for '" + s + "'", " 123", list.get(2));
     }
 
+    @Test
     public void testSplitWithEscapedSeparator()
     {
         String s = "abc\\,xyz, 123";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 2, list.size());
         assertEquals("1st token for '" + s + "'", "abc,xyz", list.get(0));
         assertEquals("2nd token for '" + s + "'", "123", list.get(1));
     }
 
+    @Test
     public void testSplitEmptyValues()
     {
         String s = ",,";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 3, list.size());
         assertEquals("1st token for '" + s + "'", "", list.get(0));
@@ -77,19 +87,21 @@ public class TestPropertyConverter exten
         assertEquals("3rd token for '" + s + "'", "", list.get(2));
     }
 
+    @Test
     public void testSplitWithEndingSlash()
     {
         String s = "abc, xyz\\";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 2, list.size());
         assertEquals("1st token for '" + s + "'", "abc", list.get(0));
         assertEquals("2nd token for '" + s + "'", "xyz\\", list.get(1));
     }
 
+    @Test
     public void testSplitNull()
     {
-        List list = PropertyConverter.split(null, ',');
+        List<String> list = PropertyConverter.split(null, ',');
         assertNotNull(list);
         assertTrue(list.isEmpty());
     }
@@ -97,9 +109,10 @@ public class TestPropertyConverter exten
     /**
      * Tests whether an escape character can be itself escaped.
      */
+    @Test
     public void testSplitEscapeEscapeChar()
     {
-        List list = PropertyConverter.split("C:\\Temp\\\\,xyz", ',');
+        List<String> list = PropertyConverter.split("C:\\Temp\\\\,xyz", ',');
         assertEquals("Wrong list size", 2, list.size());
         assertEquals("Wrong element 1", "C:\\Temp\\", list.get(0));
         assertEquals("Wrong element 2", "xyz", list.get(1));
@@ -108,6 +121,7 @@ public class TestPropertyConverter exten
     /**
      * Tests whether delimiters are correctly escaped.
      */
+    @Test
     public void testEscapeDelimiters()
     {
         assertEquals("Wrong escaped delimiters",
@@ -118,6 +132,7 @@ public class TestPropertyConverter exten
     /**
      * Tests whether only the list delimiter can be escaped.
      */
+    @Test
     public void testEscapeListDelimiter()
     {
         assertEquals("Wrong escaped list delimiter", "C:\\Temp\\\\,D:\\Data\\",
@@ -125,11 +140,12 @@ public class TestPropertyConverter exten
                         ','));
     }
 
+    @Test
     public void testToIterator()
     {
         int[] array = new int[]{1, 2, 3};
 
-        Iterator it = PropertyConverter.toIterator(array, ',');
+        Iterator<?> it = PropertyConverter.toIterator(array, ',');
 
         assertEquals("1st element", new Integer(1), it.next());
         assertEquals("2nd element", new Integer(2), it.next());
@@ -139,6 +155,7 @@ public class TestPropertyConverter exten
     /**
      * Tests the interpolation features.
      */
+    @Test
     public void testInterpolateString()
     {
         PropertiesConfiguration config = new PropertiesConfiguration();
@@ -152,6 +169,7 @@ public class TestPropertyConverter exten
     /**
      * Tests interpolation of an object. Here nothing should be substituted.
      */
+    @Test
     public void testInterpolateObject()
     {
         assertEquals("Object was not correctly interpolated", new Integer(42),
@@ -162,6 +180,7 @@ public class TestPropertyConverter exten
      * Tests complex interpolation where the variables' values contain in turn
      * other variables.
      */
+    @Test
     public void testInterpolateRecursive()
     {
         PropertiesConfiguration config = new PropertiesConfiguration();
@@ -178,27 +197,21 @@ public class TestPropertyConverter exten
      * Tests an interpolation that leads to a cycle. This should throw an
      * exception.
      */
+    @Test(expected = IllegalStateException.class)
     public void testCyclicInterpolation()
     {
         PropertiesConfiguration config = new PropertiesConfiguration();
         config.addProperty("animal", "${animal_attr} ${species}");
         config.addProperty("animal_attr", "quick brown");
         config.addProperty("species", "${animal}");
-        try
-        {
-            PropertyConverter.interpolate("This is a ${animal}", config);
-            fail("Cyclic interpolation was not detected!");
-        }
-        catch (IllegalStateException iex)
-        {
-            // ok
-        }
+        PropertyConverter.interpolate("This is a ${animal}", config);
     }
 
     /**
      * Tests interpolation if a variable is unknown. Then the variable won't be
      * substituted.
      */
+    @Test
     public void testInterpolationUnknownVariable()
     {
         PropertiesConfiguration config = new PropertiesConfiguration();
@@ -212,6 +225,7 @@ public class TestPropertyConverter exten
      * Tests conversion to numbers when the passed in objects are already
      * numbers.
      */
+    @Test
     public void testToNumberDirect()
     {
         Integer i = new Integer(42);
@@ -224,6 +238,7 @@ public class TestPropertyConverter exten
      * Tests conversion to numbers when the passed in objects have a compatible
      * string representation.
      */
+    @Test
     public void testToNumberFromString()
     {
         assertEquals("Incorrect Integer value", new Integer(42), PropertyConverter.toNumber("42",
Integer.class));
@@ -234,6 +249,7 @@ public class TestPropertyConverter exten
      * Tests conversion to numbers when the passed in objects are strings with
      * prefixes for special radices.
      */
+    @Test
     public void testToNumberFromHexString()
     {
         Number n = PropertyConverter.toNumber("0x10", Integer.class);
@@ -244,23 +260,17 @@ public class TestPropertyConverter exten
      * Tests conversion to numbers when an invalid Hex value is passed in.
      * This should cause an exception.
      */
+    @Test(expected = ConversionException.class)
     public void testToNumberFromInvalidHexString()
     {
-        try
-        {
-            PropertyConverter.toNumber("0xNotAHexValue", Integer.class);
-            fail("Could convert invalid hex value!");
-        }
-        catch (ConversionException cex)
-        {
-            // ok
-        }
+        PropertyConverter.toNumber("0xNotAHexValue", Integer.class);
     }
 
     /**
      * Tests conversion to numbers when the passed in objects are strings with
      * prefixes for special radices.
      */
+    @Test
     public void testToNumberFromBinaryString()
     {
         Number n = PropertyConverter.toNumber("0b1111", Integer.class);
@@ -271,135 +281,61 @@ public class TestPropertyConverter exten
      * Tests conversion to numbers when an invalid binary value is passed in.
      * This should cause an exception.
      */
+    @Test(expected = ConversionException.class)
     public void testToNumberFromInvalidBinaryString()
     {
-        try
-        {
-            PropertyConverter.toNumber("0bNotABinValue", Integer.class);
-            fail("Could convert invalid binary value!");
-        }
-        catch (ConversionException cex)
-        {
-            // ok
-        }
+        PropertyConverter.toNumber("0bNotABinValue", Integer.class);
     }
 
     /**
      * Tests conversion to numbers when the passed in objects have no numeric
      * String representation. This should cause an exception.
      */
+    @Test(expected = ConversionException.class)
     public void testToNumberFromInvalidString()
     {
-        try
-        {
-            PropertyConverter.toNumber("Not a number", Byte.class);
-            fail("Could convert invalid String!");
-        }
-        catch (ConversionException cex)
-        {
-            // ok
-        }
+        PropertyConverter.toNumber("Not a number", Byte.class);
     }
 
     /**
      * Tests conversion to numbers when the passed in target class is invalid.
      * This should cause an exception.
      */
+    @Test(expected = ConversionException.class)
     public void testToNumberWithInvalidClass()
     {
-        try
-        {
-            PropertyConverter.toNumber("42", Object.class);
-            fail("Could convert to invalid target class!");
-        }
-        catch (ConversionException cex)
-        {
-            //ok
-        }
-    }
-
-    // enumeration type used for the tests, Java 5 only
-    private Class enumClass;
-    private Object enumObject;
-    {
-        if (SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            try
-            {
-                enumClass = Class.forName("java.lang.annotation.ElementType");
-
-                Method valueOfMethod = enumClass.getMethod("valueOf", new Class[] { String.class
});
-                enumObject = valueOfMethod.invoke(null, new Object[] { "METHOD" });
-            }
-            catch (Exception e)
-            {
-            }
-        }
+        PropertyConverter.toNumber("42", Object.class);
     }
 
+    @Test
     public void testToEnumFromEnum()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        assertEquals(enumObject, PropertyConverter.toEnum(enumObject, enumClass));
+        assertEquals(ElementType.METHOD, PropertyConverter.toEnum(ElementType.METHOD, ENUM_CLASS));
     }
 
+    @Test
     public void testToEnumFromString()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        assertEquals(enumObject, PropertyConverter.toEnum("METHOD", enumClass));
+        assertEquals(ElementType.METHOD, PropertyConverter.toEnum("METHOD", ENUM_CLASS));
     }
 
+    @Test(expected = ConversionException.class)
     public void testToEnumFromInvalidString()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        try
-        {
-            assertEquals(enumObject, PropertyConverter.toEnum("FOO", enumClass));
-            fail("Could convert invalid String!");
-        }
-        catch (ConversionException e)
-        {
-            // expected
-        }
+        PropertyConverter.toEnum("FOO", ENUM_CLASS);
     }
 
+    @Test
     public void testToEnumFromNumber()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        assertEquals(enumObject, PropertyConverter.toEnum(new Integer(2), enumClass));
+        assertEquals(ElementType.METHOD, PropertyConverter.toEnum(
+                Integer.valueOf(ElementType.METHOD.ordinal()),
+                ENUM_CLASS));
     }
 
+    @Test(expected = ConversionException.class)
     public void testToEnumFromInvalidNumber()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        try
-        {
-            assertEquals(enumObject, PropertyConverter.toEnum(new Integer(-1), enumClass));
-            fail("Could convert invalid number!");
-        }
-        catch (ConversionException e)
-        {
-            // expected
-        }
+        PropertyConverter.toEnum(Integer.valueOf(-1), ENUM_CLASS);
     }
 }



Mime
View raw message