View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.util;
5   
6   import static org.junit.Assert.assertEquals;
7   
8   import org.junit.Test;
9   public class StringUtilTest {
10  
11      @Test
12      public void testReplaceWithOneChar() {
13          assertEquals("faa", StringUtil.replaceString("foo", 'o', "a"));
14      }
15  
16      @Test
17      public void testReplaceWithMultipleChars() {
18          assertEquals("faaaa", StringUtil.replaceString("foo", 'o', "aa"));
19      }
20  
21      @Test
22      public void testReplaceStringWithString() {
23          assertEquals("foo]]>bar", StringUtil.replaceString("foo]]>bar", "]]>", "]]>"));
24      }
25  
26      @Test
27      public void testReplaceStringWithString2() {
28          assertEquals("replaceString didn't work with a >", "foobar", StringUtil.replaceString("foobar", "]]>", "]]>"));
29      }
30  
31      @Test
32      public void testReplaceWithNull() {
33          assertEquals("replaceString didn't work with a char", "f", StringUtil.replaceString("foo", 'o', null));
34      }
35  
36      /**
37       * Usually you would set the system property "net.sourceforge.pmd.supportUTF8" to either "no" or "yes", to
38       * switch UTF8 support.
39       *
40       * e.g.
41       * <code>System.setProperty("net.sourceforge.pmd.supportUTF8","yes");</code>
42       */
43      @Test
44      public void testUTF8NotSupported() {
45      	StringBuilder sb = new StringBuilder();
46          String test = "é";
47          StringUtil.appendXmlEscaped(sb, test, false);
48          assertEquals("&#xe9;", sb.toString());
49      }
50  
51      @Test
52      public void testUTF8Supported() {
53      	StringBuilder sb = new StringBuilder();
54          String test = "é";
55          StringUtil.appendXmlEscaped(sb, test, true);
56          assertEquals("é", sb.toString());
57      }
58  
59      public static junit.framework.Test suite() {
60          return new junit.framework.JUnit4TestAdapter(StringUtilTest.class);
61      }
62  }
63