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