View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Test string utils.
30   *
31   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
32   * @version $Id: $Id
33   * @since 3.4.0
34   */
35  public class StringUtilsTest {
36  
37      /**
38       * <p>testIsEmpty.</p>
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       * <p>testIsNotEmpty.</p>
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       * <p>testIsBlank.</p>
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       * <p>testIsNotBlank.</p>
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       * <p>testCapitalizeFirstLetter.</p>
96       */
97      @Test
98      public void testCapitalizeFirstLetter() {
99          assertEquals("Id", StringUtils.capitalizeFirstLetter("id"));
100         assertEquals("Id", StringUtils.capitalizeFirstLetter("Id"));
101     }
102 
103     /**
104      * <p>testCapitalizeFirstLetterTurkish.</p>
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      * <p>testLowerCaseFirstLetter.</p>
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      * <p>testLowerCaseFirstLetterTurkish.</p>
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      * <p>testRemoveAndHump.</p>
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      * <p>testRemoveAndHumpTurkish.</p>
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      * <p>testQuote_EscapeEmbeddedSingleQuotes.</p>
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      * <p>testQuote_EscapeEmbeddedSingleQuotesWithPattern.</p>
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      * <p>testQuote_EscapeEmbeddedDoubleQuotesAndSpaces.</p>
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      * <p>testQuote_DontQuoteIfUnneeded.</p>
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      * <p>testQuote_WrapWithSingleQuotes.</p>
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      * <p>testQuote_PreserveExistingQuotes.</p>
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      * <p>testQuote_WrapExistingQuotesWhenForceIsTrue.</p>
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      * <p>testQuote_ShortVersion_SingleQuotesPreserved.</p>
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      * <p>testSplit.</p>
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      * <p>testRemoveDuplicateWhitespace.</p>
299      *
300      * @throws java.lang.Exception if any.
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      * <p>testUnifyLineSeparators.</p>
316      *
317      * @throws java.lang.Exception if any.
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 }