3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertNull;
5 import static org.testng.AssertJUnit.assertTrue;
7 import java.util.Arrays;
9 import org.testng.annotations.Test;
11 public class StringUtilsTest
14 @Test(groups ={ "Functional" })
15 public void testInsertCharAt()
17 char[] c1 = "ABC".toCharArray();
18 char[] expected = new char[]
19 { 'A', 'B', 'C', 'w', 'w' };
20 assertTrue(Arrays.equals(expected,
21 StringUtils.insertCharAt(c1, 3, 2, 'w')));
23 { 'A', 'B', 'C', 'w', 'w' };
24 assertTrue(Arrays.equals(expected,
25 StringUtils.insertCharAt(c1, 4, 2, 'w')));
26 assertTrue(Arrays.equals(expected,
27 StringUtils.insertCharAt(c1, 5, 2, 'w')));
28 assertTrue(Arrays.equals(expected,
29 StringUtils.insertCharAt(c1, 6, 2, 'w')));
30 assertTrue(Arrays.equals(expected,
31 StringUtils.insertCharAt(c1, 7, 2, 'w')));
34 @Test(groups ={ "Functional" })
35 public void testDeleteChars()
37 char[] c1 = "ABC".toCharArray();
39 // delete second position
40 assertTrue(Arrays.equals(new char[]
41 { 'A', 'C' }, StringUtils.deleteChars(c1, 1, 2)));
43 // delete positions 1 and 2
44 assertTrue(Arrays.equals(new char[]
45 { 'C' }, StringUtils.deleteChars(c1, 0, 2)));
47 // delete positions 1-3
48 assertTrue(Arrays.equals(new char[]
49 {}, StringUtils.deleteChars(c1, 0, 3)));
52 assertTrue(Arrays.equals(new char[]
53 { 'A', 'B' }, StringUtils.deleteChars(c1, 2, 3)));
55 // out of range deletion is ignore
56 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
59 @Test(groups ={ "Functional" })
60 public void testGetLastToken()
62 assertNull(StringUtils.getLastToken(null, null));
63 assertNull(StringUtils.getLastToken(null, "/"));
64 assertEquals("a", StringUtils.getLastToken("a", null));
66 assertEquals("abc", StringUtils.getLastToken("abc", "/"));
67 assertEquals("c", StringUtils.getLastToken("abc", "b"));
68 assertEquals("file1.dat", StringUtils.getLastToken(
69 "file://localhost:8080/data/examples/file1.dat", "/"));
72 @Test(groups ={ "Functional" })
73 public void testSeparatorListToArray()
75 String[] result = StringUtils.separatorListToArray(
76 "foo=',',min='foo',max='1,2,3',fa=','", ",");
77 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
78 Arrays.toString(result));
80 * Comma nested in '' is not treated as delimiter; tokens are not trimmed
82 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
83 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
86 * String delimited by | containing a quoted | (should not be treated as
89 assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
90 .separatorListToArray("abc='|'d|ef|g", "|")));
93 @Test(groups ={ "Functional" })
94 public void testArrayToSeparatorList()
96 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
97 assertEquals("*", StringUtils.arrayToSeparatorList(new String[]
99 assertEquals("a*bc*cde", StringUtils.arrayToSeparatorList(new String[]
100 { "a", "bc", "cde" }, "*"));
101 assertEquals("a*cde", StringUtils.arrayToSeparatorList(new String[]
102 { "a", null, "cde" }, "*"));
103 assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[]
104 { "a", "", "cde" }, "*"));
105 // delimiter within token is not (yet) escaped
106 assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
107 { "a", "b*c", "cde" }, "*"));