scolebourne    2003/07/29 17:08:38

  Modified:    lang/src/java/org/apache/commons/lang CharSetUtils.java
               lang/src/test/org/apache/commons/lang CharSetUtilsTest.java
                        StringEscapeUtilsTest.java
  Log:
  Javadoc fixes and improved tests
  bug 21952, from Phil Steitz
  
  Revision  Changes    Path
  1.15      +5 -4      
jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java
  
  Index: CharSetUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- CharSetUtils.java 19 Jul 2003 20:22:36 -0000      1.14
  +++ CharSetUtils.java 30 Jul 2003 00:08:38 -0000      1.15
  @@ -334,10 +334,11 @@
        *
        * @param str  String to replace characters in, may be null
        * @param repl  String to find that will be replaced, must not be null
  -     * @param with  String to put into the target String, must not be null
  +     * @param with  String to put into the target String, must not be null or empty 
("")
        * @return translated String, <code>null</code> if null string input
  -     * @throws NullPointerException if <code>target</code>, <code>with</code>
  -     *  or <code>repl</code> is <code>null</code>
  +     * @throws NullPointerException if <code>with</code> or <code>repl</code> 
  +     *  is <code>null</code>
  +     * @throws ArrayIndexOutOfBoundsException if <code>with</code> is empty ("")
        */
       public static String translate(String str, String repl, String with) {
           if (str == null) {
  
  
  
  1.9       +55 -1     
jakarta-commons/lang/src/test/org/apache/commons/lang/CharSetUtilsTest.java
  
  Index: CharSetUtilsTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/CharSetUtilsTest.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CharSetUtilsTest.java     19 Jul 2003 20:22:36 -0000      1.8
  +++ CharSetUtilsTest.java     30 Jul 2003 00:08:38 -0000      1.9
  @@ -94,19 +94,27 @@
   
       public void testSqueeze() {
           assertEquals(null, CharSetUtils.squeeze(null, (String[]) null));
  +        assertEquals(null, CharSetUtils.squeeze(null, (String) null));
           assertEquals(null, CharSetUtils.squeeze(null, new String[] { "el" }));
           assertEquals("helo", CharSetUtils.squeeze("hello", new String[] { "el" }));
  +        assertEquals("hello", CharSetUtils.squeeze("hello", ""));
           assertEquals("", CharSetUtils.squeeze("", new String[] { "el" }));
           assertEquals("hello", CharSetUtils.squeeze("hello", new String[] { "e" }));
           assertEquals("fofof", CharSetUtils.squeeze("fooffooff", new String[] { "of" 
}));
           assertEquals("fof", CharSetUtils.squeeze("fooooff", new String[] { "fo" }));
           try {
               CharSetUtils.squeeze("hello", (String[]) null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {}
  +        try {
  +            CharSetUtils.squeeze("hello", new String[] { "", null });
  +            fail("Expecting NullPointerException");
           } catch (NullPointerException ex) {}
       }
   
       public void testCount() {
           assertEquals(0, CharSetUtils.count(null, (String[]) null));
  +        assertEquals(0, CharSetUtils.count(null, (String) null));
           assertEquals(0, CharSetUtils.count(null, new String[] { "el" }));
           assertEquals(3, CharSetUtils.count("hello", new String[] { "el" }));
           assertEquals(0, CharSetUtils.count("", new String[] { "el" }));
  @@ -114,37 +122,83 @@
           assertEquals(2, CharSetUtils.count("hello", new String[] { "e-i" }));
           assertEquals(5, CharSetUtils.count("hello", new String[] { "a-z" }));
           assertEquals(0, CharSetUtils.count("hello", new String[] { "" }));
  +        assertEquals(0, CharSetUtils.count("hello", ""));
           try {
               CharSetUtils.count("hello", (String[]) null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {}
  +        try {
  +            CharSetUtils.count("hello", new String[] { "", null });
  +            fail("Expecting NullPointerException");
           } catch (NullPointerException ex) {}
       }
   
       public void testKeep() {
           assertEquals(null, CharSetUtils.keep(null, (String[]) null));
  +        assertEquals(null, CharSetUtils.keep(null, (String) null));
           assertEquals(null, CharSetUtils.keep(null, new String[] { "el" }));
           assertEquals("ell", CharSetUtils.keep("hello", new String[] { "el" }));
           assertEquals("hello", CharSetUtils.keep("hello", new String[] { "elho" }));
           assertEquals("", CharSetUtils.keep("hello", new String[] { "" }));
  +        assertEquals("", CharSetUtils.keep("hello", ""));
           assertEquals("hello", CharSetUtils.keep("hello", new String[] { "a-z" }));
           assertEquals("----", CharSetUtils.keep("----", new String[] { "-" }));
           assertEquals("ll", CharSetUtils.keep("hello", new String[] { "l" }));
           try {
               CharSetUtils.keep("hello", (String[]) null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {}
  +        try {
  +            CharSetUtils.keep("hello", new String[] { "", null});
  +            fail("Expecting NullPointerException");
           } catch (NullPointerException ex) {}
       }
   
       public void testDelete() {
           assertEquals(null, CharSetUtils.delete(null, (String[]) null));
  +        assertEquals(null, CharSetUtils.delete(null,(String) null));
           assertEquals(null, CharSetUtils.delete(null, new String[] { "el" }));
           assertEquals("ho", CharSetUtils.delete("hello", new String[] { "el" }));
           assertEquals("", CharSetUtils.delete("hello", new String[] { "elho" }));
           assertEquals("hello", CharSetUtils.delete("hello", new String[] { "" }));
  +        assertEquals("hello", CharSetUtils.delete("hello", ""));
           assertEquals("", CharSetUtils.delete("hello", new String[] { "a-z" }));
           assertEquals("", CharSetUtils.delete("----", new String[] { "-" }));
           assertEquals("heo", CharSetUtils.delete("hello", new String[] { "l" }));
           try {
               CharSetUtils.delete("hello", (String[]) null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {}
  +        try {
  +            CharSetUtils.delete("hello",  new String[] { "-", null });
  +            fail("Expecting NullPointerException");
           } catch (NullPointerException ex) {}
       }
       
  +    public void testTranslate() {
  +        assertEquals(null, CharSetUtils.translate(null, null, null));
  +        assertEquals("", CharSetUtils.translate("","a", "b"));
  +        assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jy"));
  +        assertEquals("jellj", CharSetUtils.translate("hello", "ho", "j"));
  +        assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jyx"));
  +        assertEquals("\rhello\r", CharSetUtils.translate("\nhello\n", "\n", "\r"));
  +        assertEquals("hello", CharSetUtils.translate("hello", "", "x"));
  +        assertEquals("hello", CharSetUtils.translate("hello", "", ""));
  +        try {
  +            CharSetUtils.translate("hello", null, null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {}
  +        try {
  +            CharSetUtils.translate("hello", "h", null);
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {}
  +        try {
  +            CharSetUtils.translate("hello", null, "a");
  +            fail("Expecting NullPointerException");
  +        } catch (NullPointerException ex) {}
  +        try {
  +            CharSetUtils.translate("hello", "h", "");
  +            fail("Expecting ArrayIndexOutOfBoundsException");
  +        } catch (ArrayIndexOutOfBoundsException ex) {}
  +    }         
   }
  
  
  
  1.10      +17 -4     
jakarta-commons/lang/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
  
  Index: StringEscapeUtilsTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- StringEscapeUtilsTest.java        19 Jul 2003 20:22:36 -0000      1.9
  +++ StringEscapeUtilsTest.java        30 Jul 2003 00:08:38 -0000      1.10
  @@ -153,11 +153,18 @@
               fail();
           } catch (IllegalArgumentException ex) {
           }
  +        try {
  +            String str = StringEscapeUtils.unescapeJava("\\u02-3");
  +            fail();
  +        } catch (RuntimeException ex) {
  +        }
           
           assertUnescapeJava("", "");
           assertUnescapeJava("test", "test");
           assertUnescapeJava("\ntest\b", "\\ntest\\b");
           assertUnescapeJava("\u123425foo\ntest\b", "\\u123425foo\\ntest\\b");
  +        assertUnescapeJava("'\foo\teste\r", "\\'\\foo\\teste\\r");
  +        assertUnescapeJava("\\", "\\");
           //foo
           assertUnescapeJava("lowercase unicode", "\uABCDx", "\\uabcdx");
           assertUnescapeJava("uppercase unicode", "\uABCDx", "\\uABCDx");
  @@ -174,9 +181,9 @@
   
           assertEquals("unescape(String) failed" +
                   (message == null ? "" : (": " + message)) +
  -                ": expected '" + StringUtils.escape(expected) +
  +                ": expected '" + StringEscapeUtils.escapeJava(expected) +
                   // we escape this so we can see it in the error message
  -                "' actual '" + StringUtils.escape(actual) + "'",
  +                "' actual '" + StringEscapeUtils.escapeJava(actual) + "'",
                   expected, actual);
   
           StringPrintWriter writer = new StringPrintWriter();
  @@ -213,6 +220,7 @@
           {"no escaping", "plain text", "plain text"},
           {"no escaping", "plain text", "plain text"},
           {"empty string", "", ""},
  +        {"null", null, null},
           {"ampersand", "bread &amp; butter", "bread & butter"},
           {"quotes", "&quot;bread&quot; &amp; butter", "\"bread\" & butter"},
           {"final character only", "greater than &gt;", "greater than >"},
  @@ -241,7 +249,7 @@
               assertEquals(htmlEscapes[i][0], htmlEscapes[i][2], 
StringEscapeUtils.unescapeHtml(htmlEscapes[i][1]));
               // todo: add test for (and implement) Writer-based version
           }
  -        // \u00E7 is a cedilla (�)
  +        // \u00E7 is a cedilla (c with wiggle under)
           // note that the test string must be 7-bit-clean (unicode escaped) or else 
it will compile incorrectly
           // on some locales
           assertEquals("funny chars pass through OK", "Fran\u00E7ais", 
StringEscapeUtils.unescapeHtml("Fran\u00E7ais"));
  @@ -272,6 +280,9 @@
   
           assertEquals("ain't", StringEscapeUtils.unescapeXml("ain&apos;t"));
           assertEquals("ain&apos;t", StringEscapeUtils.escapeXml("ain't"));
  +        assertEquals("", StringEscapeUtils.escapeXml(""));
  +        assertEquals(null, StringEscapeUtils.escapeXml(null));
  +        assertEquals(null, StringEscapeUtils.unescapeXml(null));
       }
   
       // SQL
  @@ -281,6 +292,8 @@
       public void testEscapeSql() throws Exception
       {
           assertEquals("don''t stop", StringEscapeUtils.escapeSql("don't stop"));
  +        assertEquals("", StringEscapeUtils.escapeSql(""));
  +        assertEquals(null, StringEscapeUtils.escapeSql(null));
       }
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to