1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Arrays;
20 import java.util.Locale;
21
22 import org.junit.jupiter.api.Test;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28
29
30
31
32
33
34
35 public class StringUtilsTest {
36
37
38
39
40 @Test
41 public void testIsEmpty() {
42 assertEquals(true, StringUtils.isEmpty(null));
43 assertEquals(true, StringUtils.isEmpty(""));
44 assertEquals(false, StringUtils.isEmpty(" "));
45 assertEquals(false, StringUtils.isEmpty("foo"));
46 assertEquals(false, StringUtils.isEmpty(" foo "));
47 }
48
49
50
51
52 @Test
53 public void testIsNotEmpty() {
54 assertEquals(false, StringUtils.isNotEmpty(null));
55 assertEquals(false, StringUtils.isNotEmpty(""));
56 assertEquals(true, StringUtils.isNotEmpty(" "));
57 assertEquals(true, StringUtils.isNotEmpty("foo"));
58 assertEquals(true, StringUtils.isNotEmpty(" foo "));
59 }
60
61 @org.junit.jupiter.api.Test
62 public void testIsNotEmptyNegatesIsEmpty() {
63 assertEquals(!StringUtils.isEmpty(null), StringUtils.isNotEmpty(null));
64 assertEquals(!StringUtils.isEmpty(""), StringUtils.isNotEmpty(""));
65 assertEquals(!StringUtils.isEmpty(" "), StringUtils.isNotEmpty(" "));
66 assertEquals(!StringUtils.isEmpty("foo"), StringUtils.isNotEmpty("foo"));
67 assertEquals(!StringUtils.isEmpty(" foo "), StringUtils.isNotEmpty(" foo "));
68 }
69
70
71
72
73 @Test
74 public void testIsBlank() {
75 assertEquals(true, StringUtils.isBlank(null));
76 assertEquals(true, StringUtils.isBlank(""));
77 assertEquals(true, StringUtils.isBlank(" \t\r\n"));
78 assertEquals(false, StringUtils.isBlank("foo"));
79 assertEquals(false, StringUtils.isBlank(" foo "));
80 }
81
82
83
84
85 @Test
86 public void testIsNotBlank() {
87 assertEquals(false, StringUtils.isNotBlank(null));
88 assertEquals(false, StringUtils.isNotBlank(""));
89 assertEquals(false, StringUtils.isNotBlank(" \t\r\n"));
90 assertEquals(true, StringUtils.isNotBlank("foo"));
91 assertEquals(true, StringUtils.isNotBlank(" foo "));
92 }
93
94
95
96
97 @Test
98 public void testCapitalizeFirstLetter() {
99 assertEquals("Id", StringUtils.capitalizeFirstLetter("id"));
100 assertEquals("Id", StringUtils.capitalizeFirstLetter("Id"));
101 }
102
103
104
105
106 @Test
107 public void testCapitalizeFirstLetterTurkish() {
108 Locale l = Locale.getDefault();
109 Locale.setDefault(new Locale("tr"));
110 assertEquals("Id", StringUtils.capitalizeFirstLetter("id"));
111 assertEquals("Id", StringUtils.capitalizeFirstLetter("Id"));
112 Locale.setDefault(l);
113 }
114
115
116
117
118 @org.junit.jupiter.api.Test
119 public void testLowerCaseFirstLetter() {
120 assertEquals("id", StringUtils.lowercaseFirstLetter("id"));
121 assertEquals("id", StringUtils.lowercaseFirstLetter("Id"));
122 }
123
124
125
126
127 @org.junit.jupiter.api.Test
128 public void testLowerCaseFirstLetterTurkish() {
129 Locale l = Locale.getDefault();
130 Locale.setDefault(new Locale("tr"));
131 assertEquals("id", StringUtils.lowercaseFirstLetter("id"));
132 assertEquals("id", StringUtils.lowercaseFirstLetter("Id"));
133 Locale.setDefault(l);
134 }
135
136
137
138
139 @org.junit.jupiter.api.Test
140 public void testRemoveAndHump() {
141 assertEquals("Id", StringUtils.removeAndHump("id", "-"));
142 assertEquals("SomeId", StringUtils.removeAndHump("some-id", "-"));
143 }
144
145
146
147
148 @Test
149 public void testRemoveAndHumpTurkish() {
150 Locale l = Locale.getDefault();
151 Locale.setDefault(new Locale("tr"));
152 assertEquals("Id", StringUtils.removeAndHump("id", "-"));
153 assertEquals("SomeId", StringUtils.removeAndHump("some-id", "-"));
154 Locale.setDefault(l);
155 }
156
157
158
159
160 @Test
161 public void testQuote_EscapeEmbeddedSingleQuotes() {
162 String src = "This \'is a\' test";
163 String check = "\'This \\\'is a\\\' test\'";
164
165 char[] escaped = {'\'', '\"'};
166 String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
167
168 assertEquals(check, result);
169 }
170
171
172
173
174 @org.junit.jupiter.api.Test
175 public void testQuote_EscapeEmbeddedSingleQuotesWithPattern() {
176 String src = "This \'is a\' test";
177 String check = "\'This pre'postis apre'post test\'";
178
179 char[] escaped = {'\'', '\"'};
180 String result = StringUtils.quoteAndEscape(src, '\'', escaped, new char[] {' '}, "pre%spost", false);
181
182 assertEquals(check, result);
183 }
184
185
186
187
188 @Test
189 public void testQuote_EscapeEmbeddedDoubleQuotesAndSpaces() {
190 String src = "This \"is a\" test";
191 String check = "\'This\\ \\\"is\\ a\\\"\\ test\'";
192
193 char[] escaped = {'\'', '\"', ' '};
194 String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
195
196 assertEquals(check, result);
197 }
198
199
200
201
202 @org.junit.jupiter.api.Test
203 public void testQuote_DontQuoteIfUnneeded() {
204 String src = "ThisIsATest";
205
206 char[] escaped = {'\'', '\"'};
207 String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
208
209 assertEquals(src, result);
210 }
211
212
213
214
215 @Test
216 public void testQuote_WrapWithSingleQuotes() {
217 String src = "This is a test";
218 String check = "\'This is a test\'";
219
220 char[] escaped = {'\'', '\"'};
221 String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
222
223 assertEquals(check, result);
224 }
225
226
227
228
229 @org.junit.jupiter.api.Test
230 public void testQuote_PreserveExistingQuotes() {
231 String src = "\'This is a test\'";
232
233 char[] escaped = {'\'', '\"'};
234 String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
235
236 assertEquals(src, result);
237 }
238
239
240
241
242 @org.junit.jupiter.api.Test
243 public void testQuote_WrapExistingQuotesWhenForceIsTrue() {
244 String src = "\'This is a test\'";
245 String check = "\'\\\'This is a test\\\'\'";
246
247 char[] escaped = {'\'', '\"'};
248 String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', true);
249
250 assertEquals(check, result);
251 }
252
253
254
255
256 @org.junit.jupiter.api.Test
257 public void testQuote_ShortVersion_SingleQuotesPreserved() {
258 String src = "\'This is a test\'";
259
260 String result = StringUtils.quoteAndEscape(src, '\'');
261
262 assertEquals(src, result);
263 }
264
265
266
267
268 @org.junit.jupiter.api.Test
269 public void testSplit() {
270 String[] tokens;
271
272 tokens = StringUtils.split("", ", ");
273 assertNotNull(tokens);
274 assertEquals(Arrays.asList(new String[0]), Arrays.asList(tokens));
275
276 tokens = StringUtils.split(", ,,, ,", ", ");
277 assertNotNull(tokens);
278 assertEquals(Arrays.asList(new String[0]), Arrays.asList(tokens));
279
280 tokens = StringUtils.split("this", ", ");
281 assertNotNull(tokens);
282 assertEquals(Arrays.asList(new String[] {"this"}), Arrays.asList(tokens));
283
284 tokens = StringUtils.split("this is a test", ", ");
285 assertNotNull(tokens);
286 assertEquals(Arrays.asList(new String[] {"this", "is", "a", "test"}), Arrays.asList(tokens));
287
288 tokens = StringUtils.split(" this is a test ", ", ");
289 assertNotNull(tokens);
290 assertEquals(Arrays.asList(new String[] {"this", "is", "a", "test"}), Arrays.asList(tokens));
291
292 tokens = StringUtils.split("this is a test, really", ", ");
293 assertNotNull(tokens);
294 assertEquals(Arrays.asList(new String[] {"this", "is", "a", "test", "really"}), Arrays.asList(tokens));
295 }
296
297
298
299
300
301
302 @org.junit.jupiter.api.Test
303 public void testRemoveDuplicateWhitespace() throws Exception {
304 String s = "this is test ";
305 assertEquals("this is test ", StringUtils.removeDuplicateWhitespace(s));
306 s = "this \r\n is \n \r test ";
307 assertEquals("this is test ", StringUtils.removeDuplicateWhitespace(s));
308 s = " this \r\n is \n \r test";
309 assertEquals(" this is test", StringUtils.removeDuplicateWhitespace(s));
310 s = "this \r\n is \n \r test \n ";
311 assertEquals("this is test ", StringUtils.removeDuplicateWhitespace(s));
312 }
313
314
315
316
317
318
319 @Test
320 public void testUnifyLineSeparators() throws Exception {
321 String s = "this\r\nis\na\r\ntest";
322
323 try {
324 StringUtils.unifyLineSeparators(s, "abs");
325 assertTrue(false, "Exception NOT catched");
326 } catch (IllegalArgumentException e) {
327 assertTrue(true, "Exception catched");
328 }
329
330 assertEquals("this\nis\na\ntest", StringUtils.unifyLineSeparators(s, "\n"));
331 assertEquals("this\r\nis\r\na\r\ntest", StringUtils.unifyLineSeparators(s, "\r\n"));
332 }
333 }