1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.util;
39
40 import java.util.Arrays;
41 import java.util.List;
42 import junit.framework.TestCase;
43
44 /***
45 * Tests for StringUtil
46 *
47 * @version $Revision: 1.6 $
48 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49 */
50 public class StringUtilTest extends TestCase {
51
52 /***
53 * Create a new test.
54 *
55 * @param name The name of the test.
56 */
57 public StringUtilTest( final String name ) {
58 super( name );
59 }
60
61
62 /***
63 * Test joining ints together
64 */
65 public void testJoinInt() {
66 assertEquals( "1,2,3", StringUtil.join( new int[]{1, 2, 3}, "," ) );
67 }
68
69
70 /*** */
71 public void testJoinInt_EmptyArray() {
72 assertEquals( "", StringUtil.join( new int[]{}, "," ) );
73 }
74
75 /*** */
76 public void testJoinInt_OneElement() {
77 assertEquals( "1", StringUtil.join( new int[]{1}, "," ) );
78 }
79
80 /*** */
81 public void testJoinInt_NullArray() {
82 try {
83 assertEquals( "1", StringUtil.join( (int[])null, "," ) );
84 }
85 catch( final NullPointerException e ) {
86
87 }
88 }
89
90 /*** */
91 public void testJoinInt_NullSeparator() {
92 try {
93 assertEquals( "1", StringUtil.join( new int[]{2, 3}, null ) );
94 }
95 catch( final NullPointerException e ) {
96
97 }
98 }
99
100 /*** */
101 public void testJoinCollection() {
102 final List list = Arrays.asList( new String[]{"1", "2", "3"} );
103 assertEquals( "1,2,3", StringUtil.join( list, "," ) );
104 }
105
106
107 /***
108 * Test splitAtFirst()
109 */
110 public void testSplitAtFirst() {
111 final String data[][] = {
112 {"foo=bar", "=", "foo", "bar"},
113 {"foobar", "=", "foobar", ""},
114 {"foo=", "=", "foo", ""},
115 {"=bar", "=", "", "bar"}
116 };
117
118 String result[];
119
120 int i;
121 for( i = 0; i < data.length; i++ ) {
122 result = StringUtil.splitAtFirst( data[i][0], data[i][1] );
123 assertEquals( "first", data[i][2], result[0] );
124 assertEquals( "second", data[i][3], result[1] );
125 }
126 }
127
128
129 /***
130 * Test split()
131 */
132 public void testSplit() {
133 final Object data[][] = {
134 {"foo=bar", "=", new String[]{"foo", "bar"}},
135 {"foo!=bar", "!=", new String[]{"foo", "bar"}},
136 {"foo,bar,cat,dog", ",", new String[]{"foo", "bar", "cat", "dog"}},
137 {",foo,,cat,dog", ",", new String[]{"", "foo", "", "cat", "dog"}},
138 {"", "=", new String[0]},
139 {"foo", "=", new String[]{"foo"}},
140 {"==", "==", new String[]{"", ""}},
141 };
142
143 String result[];
144 String expectedResult[];
145 String line;
146 String separator;
147
148 int i;
149 int j;
150 for( i = 0; i < data.length; i++ ) {
151 line = ( String )data[i][0];
152 separator = ( String )data[i][1];
153 expectedResult = ( String[] )data[i][2];
154
155 result = StringUtil.split( line, separator );
156
157 assertEquals( "Number of strings for input[" + line + "]",
158 expectedResult.length,
159 result.length );
160
161 for( j = 0; j < result.length; j++ ) {
162 assertEquals( "element[" + j + "] for input[" + line + "]",
163 expectedResult[j],
164 result[j] );
165 }
166 }
167 }
168
169
170 /***
171 * Test split() with a null line
172 */
173 public void testSplit_NullLine() {
174 try {
175 StringUtil.split( null, "," );
176 fail( "Expected exception" );
177 }
178 catch( final DetailedNullPointerException e ) {
179 assertEquals("line", e.getArgumentName());
180 }
181 }
182
183
184 /***
185 * Test split() with a null separator
186 */
187 public void testSplit_NullSeparator() {
188 try {
189 StringUtil.split( "foo", null );
190 fail( "Expected exception" );
191 }
192 catch( final DetailedNullPointerException e ) {
193 assertEquals( "separator", e.getArgumentName() );
194
195 }
196 }
197
198
199 /***
200 * Test split() with an empty separator
201 */
202 public void testSplit_EmptySeparator() {
203 try {
204 StringUtil.split( "foo", "" );
205 fail( "Expected exception" );
206 }
207 catch( final IllegalArgumentException e ) {
208
209 }
210 }
211
212
213 /***
214 * Test replace() with a variety of inputs
215 */
216 public void testReplace() {
217 final String[][] data = {
218 {"", "", "", ""},
219 {"foo", "bar", "b", "foo"},
220 {"foo bar", "foo", "rhino", "rhino bar"},
221 {"foo bar", "foo", "foo", "foo bar"},
222 {"afoob", "foo", "bar", "abarb"},
223 {"afoobfooc", "foo", "", "abc"},
224 };
225
226 for( int i = 0; i < data.length; i++ ) {
227 final String source = data[i][0];
228 final String oldText = data[i][1];
229 final String newText = data[i][2];
230 final String expectedResult = data[i][3];
231
232 final String description = "StringUtil.replace(\"" + source + "\", \""
233 + oldText + "\", \"" + newText + "\")";
234 assertEquals( description, expectedResult, StringUtil.replace( source, oldText, newText ) );
235 }
236 }
237 }
238