Author: wavey
Date: 2008-04-15 20:20:08 +0000 (Tue, 15 Apr 2008)
New Revision: 19362

Modified:
   trunk/freenet/test/freenet/support/FieldsTest.java
Log:
added unit tests for Fields (bools/hashcode/commaList)

Modified: trunk/freenet/test/freenet/support/FieldsTest.java
===================================================================
--- trunk/freenet/test/freenet/support/FieldsTest.java  2008-04-15 20:20:01 UTC 
(rev 19361)
+++ trunk/freenet/test/freenet/support/FieldsTest.java  2008-04-15 20:20:08 UTC 
(rev 19362)
@@ -135,4 +135,89 @@
                        // expect this
                }
        }
+       
+       public void testStringToBool() {
+               assertTrue(Fields.stringToBool("true"));
+               assertTrue(Fields.stringToBool("TRUE"));
+               assertFalse(Fields.stringToBool("false"));
+               assertFalse(Fields.stringToBool("FALSE"));
+               
+               try {
+                       Fields.stringToBool("Free Tibet");
+                       fail();
+               }
+               catch(NumberFormatException e) {
+                       // expect this
+               }
+               
+               try {
+                       Fields.stringToBool(null);
+                       fail();
+               }
+               catch(NumberFormatException e) {
+                       // expect this
+               }
+       }
+       
+       public void testStringToBoolWithDefault() {
+               assertTrue(Fields.stringToBool("true", false));
+               assertFalse(Fields.stringToBool("false", true));
+               assertTrue(Fields.stringToBool("TruE", false));
+               assertFalse(Fields.stringToBool("faLSE", true));
+               assertTrue(Fields.stringToBool("trueXXX", true));
+               assertFalse(Fields.stringToBool("XXXFalse", false));
+               assertTrue(Fields.stringToBool(null, true));
+       }
+       
+       public void testBoolToString() {
+               assertEquals(Fields.boolToString(true), "true");
+               assertEquals(Fields.boolToString(false), "false");
+       }
+       
+       public void testCommaListFromString() {
+               String[] expected = new String[] {"one", "two", "three", 
"four"};
+               String[] actual = Fields.commaList("one,two,     three    ,  
four");
+               
+               for(int i = 0; i < expected.length; i++) {
+                       assertEquals(expected[i], actual[i]);
+               }
+               
+               // null
+               assertNull(Fields.commaList((String)null));
+               
+               // no items
+               expected = new String[] {};
+               actual = Fields.commaList("");
+               
+               assertTrue(expected.length == actual.length);
+       }
+       
+       public void testStringArrayToCommaList() {
+               
+               String[] input = new String[] { "one", "two", "three", "four" };
+               
+               String expected = "one,two,three,four";
+               String actual = Fields.commaList(input);
+               
+               assertEquals(expected, actual);
+               
+               // empty
+               input = new String[] {};
+               
+               expected = "";
+               actual = Fields.commaList(input);
+               
+               assertEquals(expected, actual);
+       }
+       
+       public void testHashcodeForByteArray() {
+               byte[] input = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
+               
+               assertEquals(67372036, Fields.hashCode(input));
+               
+               // empty
+               input = new byte[] {};
+               
+               assertEquals(0, Fields.hashCode(input));
+       }
 }


Reply via email to