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.Collections;
21  import java.util.Locale;
22  
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.*;
26  
27  /**
28   * Test string utils.
29   *
30   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
31   * @since 3.4.0
32   */
33  class StringUtilsTest {
34  
35      @SuppressWarnings("ConstantValue")
36      @Test
37      void isEmpty() {
38          assertTrue(StringUtils.isEmpty(null));
39          assertTrue(StringUtils.isEmpty(""));
40          assertFalse(StringUtils.isEmpty(" "));
41          assertFalse(StringUtils.isEmpty("foo"));
42          assertFalse(StringUtils.isEmpty("  foo  "));
43      }
44  
45      @SuppressWarnings("ConstantValue")
46      @Test
47      void isNotEmpty() {
48          assertFalse(StringUtils.isNotEmpty(null));
49          assertFalse(StringUtils.isNotEmpty(""));
50          assertTrue(StringUtils.isNotEmpty(" "));
51          assertTrue(StringUtils.isNotEmpty("foo"));
52          assertTrue(StringUtils.isNotEmpty("  foo  "));
53      }
54  
55      @Test
56      void isNotEmptyNegatesIsEmpty() {
57          //noinspection ConstantValue
58          assertEquals(!StringUtils.isEmpty(null), StringUtils.isNotEmpty(null));
59          assertEquals(!StringUtils.isEmpty(""), StringUtils.isNotEmpty(""));
60          assertEquals(!StringUtils.isEmpty(" "), StringUtils.isNotEmpty(" "));
61          assertEquals(!StringUtils.isEmpty("foo"), StringUtils.isNotEmpty("foo"));
62          assertEquals(!StringUtils.isEmpty("  foo  "), StringUtils.isNotEmpty("  foo  "));
63      }
64  
65      @Test
66      void isBlank() {
67          assertTrue(StringUtils.isBlank(null));
68          assertTrue(StringUtils.isBlank(""));
69          assertTrue(StringUtils.isBlank(" \t\r\n"));
70          assertFalse(StringUtils.isBlank("foo"));
71          assertFalse(StringUtils.isBlank("  foo  "));
72      }
73  
74      @Test
75      void isNotBlank() {
76          assertFalse(StringUtils.isNotBlank(null));
77          assertFalse(StringUtils.isNotBlank(""));
78          assertFalse(StringUtils.isNotBlank(" \t\r\n"));
79          assertTrue(StringUtils.isNotBlank("foo"));
80          assertTrue(StringUtils.isNotBlank("  foo  "));
81      }
82  
83      @Test
84      void capitalizeFirstLetter() {
85          assertEquals("Id", StringUtils.capitalizeFirstLetter("id"));
86          assertEquals("Id", StringUtils.capitalizeFirstLetter("Id"));
87      }
88  
89      @Test
90      void capitalizeFirstLetterTurkish() {
91          Locale l = Locale.getDefault();
92          Locale.setDefault(new Locale("tr"));
93          assertEquals("Id", StringUtils.capitalizeFirstLetter("id"));
94          assertEquals("Id", StringUtils.capitalizeFirstLetter("Id"));
95          Locale.setDefault(l);
96      }
97  
98      @Test
99      void lowerCaseFirstLetter() {
100         assertEquals("id", StringUtils.lowercaseFirstLetter("id"));
101         assertEquals("id", StringUtils.lowercaseFirstLetter("Id"));
102     }
103 
104     @Test
105     void lowerCaseFirstLetterTurkish() {
106         Locale l = Locale.getDefault();
107         Locale.setDefault(new Locale("tr"));
108         assertEquals("id", StringUtils.lowercaseFirstLetter("id"));
109         assertEquals("id", StringUtils.lowercaseFirstLetter("Id"));
110         Locale.setDefault(l);
111     }
112 
113     @Test
114     void removeAndHump() {
115         assertEquals("Id", StringUtils.removeAndHump("id", "-"));
116         assertEquals("SomeId", StringUtils.removeAndHump("some-id", "-"));
117     }
118 
119     @Test
120     void removeAndHumpTurkish() {
121         Locale l = Locale.getDefault();
122         Locale.setDefault(new Locale("tr"));
123         assertEquals("Id", StringUtils.removeAndHump("id", "-"));
124         assertEquals("SomeId", StringUtils.removeAndHump("some-id", "-"));
125         Locale.setDefault(l);
126     }
127 
128     @Test
129     void quoteEscapeEmbeddedSingleQuotes() {
130         String src = "This 'is a' test";
131         String check = "'This \\'is a\\' test'";
132 
133         char[] escaped = {'\'', '\"'};
134         String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
135 
136         assertEquals(check, result);
137     }
138 
139     @Test
140     void quoteEscapeEmbeddedSingleQuotesWithPattern() {
141         String src = "This 'is a' test";
142         String check = "'This pre'postis apre'post test'";
143 
144         char[] escaped = {'\'', '\"'};
145         String result = StringUtils.quoteAndEscape(src, '\'', escaped, new char[] {' '}, "pre%spost", false);
146 
147         assertEquals(check, result);
148     }
149 
150     @Test
151     void quoteEscapeEmbeddedDoubleQuotesAndSpaces() {
152         String src = "This \"is a\" test";
153         String check = "'This\\ \\\"is\\ a\\\"\\ test'";
154 
155         char[] escaped = {'\'', '\"', ' '};
156         String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
157 
158         assertEquals(check, result);
159     }
160 
161     @Test
162     void quoteDontQuoteIfUnneeded() {
163         String src = "ThisIsATest";
164 
165         char[] escaped = {'\'', '\"'};
166         String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
167 
168         assertEquals(src, result);
169     }
170 
171     @Test
172     void quoteWrapWithSingleQuotes() {
173         String src = "This is a test";
174         String check = "'This is a test'";
175 
176         char[] escaped = {'\'', '\"'};
177         String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
178 
179         assertEquals(check, result);
180     }
181 
182     @Test
183     void quotePreserveExistingQuotes() {
184         String src = "'This is a test'";
185 
186         char[] escaped = {'\'', '\"'};
187         String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', false);
188 
189         assertEquals(src, result);
190     }
191 
192     @Test
193     void quoteWrapExistingQuotesWhenForceIsTrue() {
194         String src = "'This is a test'";
195         String check = "'\\'This is a test\\''";
196 
197         char[] escaped = {'\'', '\"'};
198         String result = StringUtils.quoteAndEscape(src, '\'', escaped, '\\', true);
199 
200         assertEquals(check, result);
201     }
202 
203     @Test
204     void quoteShortVersionSingleQuotesPreserved() {
205         String src = "'This is a test'";
206 
207         String result = StringUtils.quoteAndEscape(src, '\'');
208 
209         assertEquals(src, result);
210     }
211 
212     @Test
213     void split() {
214         String[] tokens;
215 
216         tokens = StringUtils.split("", ", ");
217         assertNotNull(tokens);
218         assertEquals(Collections.emptyList(), Arrays.asList(tokens));
219 
220         tokens = StringUtils.split(", ,,,   ,", ", ");
221         assertNotNull(tokens);
222         assertEquals(Collections.emptyList(), Arrays.asList(tokens));
223 
224         tokens = StringUtils.split("this", ", ");
225         assertNotNull(tokens);
226         assertEquals(Collections.singletonList("this"), Arrays.asList(tokens));
227 
228         tokens = StringUtils.split("this is a test", ", ");
229         assertNotNull(tokens);
230         assertEquals(Arrays.asList("this", "is", "a", "test"), Arrays.asList(tokens));
231 
232         tokens = StringUtils.split("   this   is   a   test  ", ", ");
233         assertNotNull(tokens);
234         assertEquals(Arrays.asList("this", "is", "a", "test"), Arrays.asList(tokens));
235 
236         tokens = StringUtils.split("this is a test, really", ", ");
237         assertNotNull(tokens);
238         assertEquals(Arrays.asList("this", "is", "a", "test", "really"), Arrays.asList(tokens));
239     }
240 
241     @Test
242     void removeDuplicateWhitespace() {
243         String s = "this     is     test   ";
244         assertEquals("this is test ", StringUtils.removeDuplicateWhitespace(s));
245         s = "this  \r\n   is \n  \r  test   ";
246         assertEquals("this is test ", StringUtils.removeDuplicateWhitespace(s));
247         s = "     this  \r\n   is \n  \r  test";
248         assertEquals(" this is test", StringUtils.removeDuplicateWhitespace(s));
249         s = "this  \r\n   is \n  \r  test   \n ";
250         assertEquals("this is test ", StringUtils.removeDuplicateWhitespace(s));
251     }
252 
253     @Test
254     void unifyLineSeparators() {
255         String s = "this\r\nis\na\r\ntest";
256 
257         assertThrows(IllegalArgumentException.class, () -> StringUtils.unifyLineSeparators(s, "abs"));
258 
259         assertEquals("this\nis\na\ntest", StringUtils.unifyLineSeparators(s, "\n"));
260         assertEquals("this\r\nis\r\na\r\ntest", StringUtils.unifyLineSeparators(s, "\r\n"));
261     }
262 }