View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link StringUtils} strip methods.
27   */
28  class StringUtilsStripTest extends AbstractLangTest {
29      private static final String FOO = "foo";
30  
31      @Test
32      void testStripAccents() {
33          final String cue = "\u00C7\u00FA\u00EA";
34          assertEquals("Cue", StringUtils.stripAccents(cue), "Failed to strip accents from " + cue);
35  
36          final String lots = "\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D1\u00D2\u00D3"
37                  + "\u00D4\u00D5\u00D6\u00D9\u00DA\u00DB\u00DC\u00DD";
38          assertEquals("AAAAAACEEEEIIIINOOOOOUUUUY", StringUtils.stripAccents(lots), "Failed to strip accents from " + lots);
39  
40          assertNull(StringUtils.stripAccents(null), "Failed null safety");
41          assertEquals("", StringUtils.stripAccents(""), "Failed empty String");
42          assertEquals("control", StringUtils.stripAccents("control"), "Failed to handle non-accented text");
43          assertEquals("eclair", StringUtils.stripAccents("\u00E9clair"), "Failed to handle easy example");
44          assertEquals("ALOSZZCND aloszzcnd",
45                  StringUtils.stripAccents("\u0104\u0141\u00D3\u015A\u017B\u0179\u0106\u0143\u0110 \u0105\u0142\u00F3\u015B\u017C\u017A\u0107\u0144\u0111"));
46          assertEquals("The cafe\u2019s pinata gave me deja vu.", StringUtils.stripAccents("The caf\u00e9\u2019s pi\u00f1ata gave me d\u00e9j\u00e0 vu."),
47                  "Failed to handle accented text");
48          assertEquals("fluid quest", StringUtils.stripAccents("\ufb02uid que\ufb06"), "Failed to handle ligatures");
49          assertEquals("a b c 1 2 3", StringUtils.stripAccents("\u1d43 \u1d47 \u1d9c \u00b9 \u00b2 \u00b3"), "Failed to handle superscript text");
50          assertEquals("math italic",
51                  StringUtils.stripAccents("\uD835\uDC5A\uD835\uDC4E\uD835\uDC61\u210E \uD835\uDC56\uD835\uDC61\uD835\uDC4E\uD835\uDC59\uD835\uDC56\uD835\uDC50"),
52                  "Failed to handle UTF32 example");
53          assertEquals("\uD83D\uDF01 \uD83D\uDF02 \uD83D\uDF03 \uD83D\uDF04", StringUtils.stripAccents("\uD83D\uDF01 \uD83D\uDF02 \uD83D\uDF03 \uD83D\uDF04"),
54                  "Failed to handle non-accented text");
55      }
56  
57      @Test
58      void testStripAccentsBengali() {
59          // Bengali vowel sign O, both precomposed and decomposed.
60          assertEquals("\u09CB", StringUtils.stripAccents("\u09CB"));
61          assertEquals("\u09CB", StringUtils.stripAccents("\u09C7\u09BE"));
62      }
63  
64      @Test
65      void testStripAccentsIWithBar() {
66          assertEquals("I i I i I", StringUtils.stripAccents("\u0197 \u0268 \u1D7B \u1DA4 \u1DA7"));
67      }
68  
69      @Test
70      void testStripAccentsJapanese() {
71          // Katakana GA retains its dakuten in precomposed, decomposed, and halfwidth forms.
72          assertEquals("\u30AC", StringUtils.stripAccents("\u30AC"));
73          assertEquals("\u30AC", StringUtils.stripAccents("\u30AB\u3099"));
74          assertEquals("\u30AC", StringUtils.stripAccents("\uFF76\uFF9E"));
75      }
76  
77      @Test
78      void testStripAccentsKorean() {
79          // LANG-1655
80          final String input = "\uC78A\uC9C0\uB9C8 \uB10C \uD750\uB9B0 \uC5B4\uB460\uC0AC\uC774 \uC67C\uC190\uC73C\uB85C \uADF8\uB9B0 \uBCC4 \uD558\uB098";
81          assertEquals(input, StringUtils.stripAccents(input), "Failed to handle Korean text");
82      }
83  
84      @Test
85      void testStripAccentsKoreanJamo() {
86          // Compose modern Jamo with and without a trailing consonant.
87          assertEquals("\uAC00", StringUtils.stripAccents("\u1100\u1161"));
88          assertEquals("\uAC01", StringUtils.stripAccents("\u1100\u1161\u11A8"));
89          // Compatibility Jamo are still folded before composition.
90          assertEquals("\uAC00", StringUtils.stripAccents("\u3131\u314F"));
91      }
92  
93      @Test
94      void testStripAccentsKoreanWithLatinAccents() {
95          final String expected = "\uAC00 \uAC01 cafe deja vu";
96          assertEquals(expected, StringUtils.stripAccents("\uAC00 \uAC01 caf\u00E9 d\u00E9j\u00E0 vu"));
97          assertEquals(expected, StringUtils.stripAccents("\u1100\u1161 \u1100\u1161\u11A8 cafe\u0301 de\u0301ja\u0300 vu"));
98      }
99  
100     /**
101      * Decomposes ligatures and digraphs per the KD column in the <a href = "https://www.unicode.org/charts/normalization/">Unicode Normalization Chart.</a>
102      */
103     @Test
104     void testStripAccentsSymbolMath() {
105         // Noop
106         final String lt = "<";
107         assertEquals(lt, StringUtils.stripAccents(lt));
108         // https://www.unicode.org/charts/normalization/chart_Symbol-Math.html
109         assertEquals(lt, StringUtils.stripAccents("\uFE64"));
110         assertEquals(lt, StringUtils.stripAccents("\uFF1C"));
111         assertEquals(lt, StringUtils.stripAccents("\u226E"));
112         // Noop
113         final String gt = ">";
114         assertEquals(gt, StringUtils.stripAccents(gt));
115         // https://www.unicode.org/charts/normalization/chart_Symbol-Math.html
116         assertEquals(gt, StringUtils.stripAccents("\uFE65"));
117         assertEquals(gt, StringUtils.stripAccents("\uFE65"));
118         assertEquals(gt, StringUtils.stripAccents("\u226F"));
119         //
120         assertEquals("</script>", StringUtils.stripAccents("\uFF1C\uFF0Fscript\uFF1E"));
121     }
122 
123     @Test
124     void testStripAccentsTWithStroke() {
125         assertEquals("T t", StringUtils.stripAccents("\u0166 \u0167"));
126     }
127 
128     /**
129      * Tests Unicode vulgar fractions.
130      */
131     @Test
132     void testStripAccentsUnicodeVulgarFractions() {
133         // 1/4, note "⁄", not "/".
134         assertEquals("1⁄4", StringUtils.stripAccents("\u00BC"));
135         // 1/2, note "⁄", not "/".
136         assertEquals("1⁄2", StringUtils.stripAccents("\u00BD"));
137         // 3/4, note "⁄", not "/".
138         assertEquals("3⁄4", StringUtils.stripAccents("\u00BE"));
139     }
140 
141     @Test
142     void testStripAccentsUWithBar() {
143         assertEquals("U u U u", StringUtils.stripAccents("\u0244 \u0289 \u1D7E \u1DB6"));
144     }
145 
146     @Test
147     void testStripAll() {
148         // test stripAll method, merely an array version of the above strip
149         final String[] empty = {};
150         final String[] fooSpace = { "  " + FOO + "  ", "  " + FOO, FOO + "  " };
151         final String[] fooDots = { ".." + FOO + "..", ".." + FOO, FOO + ".." };
152         final String[] foo = { FOO, FOO, FOO };
153 
154         assertNull(StringUtils.stripAll((String[]) null));
155         // Additional varargs tests
156         assertArrayEquals(empty, StringUtils.stripAll()); // empty array
157         assertArrayEquals(new String[] { null }, StringUtils.stripAll((String) null)); // == new String[]{null}
158 
159         assertArrayEquals(empty, StringUtils.stripAll(empty));
160         assertArrayEquals(foo, StringUtils.stripAll(fooSpace));
161 
162         assertNull(StringUtils.stripAll(null, null));
163         assertArrayEquals(foo, StringUtils.stripAll(fooSpace, null));
164         assertArrayEquals(foo, StringUtils.stripAll(fooDots, "."));
165     }
166 
167     @Test
168     void testStripEndStringString() {
169         // null stripEnd
170         assertNull(StringUtils.stripEnd(null, null));
171         assertEquals("", StringUtils.stripEnd("", null));
172         assertEquals("", StringUtils.stripEnd("        ", null));
173         assertEquals("  abc", StringUtils.stripEnd("  abc  ", null));
174         assertEquals(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE,
175                 StringUtils.stripEnd(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null));
176 
177         // "" stripEnd
178         assertNull(StringUtils.stripEnd(null, ""));
179         assertEquals("", StringUtils.stripEnd("", ""));
180         assertEquals("        ", StringUtils.stripEnd("        ", ""));
181         assertEquals("  abc  ", StringUtils.stripEnd("  abc  ", ""));
182         assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(StringUtilsTest.WHITESPACE, ""));
183 
184         // " " stripEnd
185         assertNull(StringUtils.stripEnd(null, " "));
186         assertEquals("", StringUtils.stripEnd("", " "));
187         assertEquals("", StringUtils.stripEnd("        ", " "));
188         assertEquals("  abc", StringUtils.stripEnd("  abc  ", " "));
189 
190         // "ab" stripEnd
191         assertNull(StringUtils.stripEnd(null, "ab"));
192         assertEquals("", StringUtils.stripEnd("", "ab"));
193         assertEquals("        ", StringUtils.stripEnd("        ", "ab"));
194         assertEquals("  abc  ", StringUtils.stripEnd("  abc  ", "ab"));
195         assertEquals("abc", StringUtils.stripEnd("abcabab", "ab"));
196         assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(StringUtilsTest.WHITESPACE, ""));
197     }
198 
199     @Test
200     void testStripStartStringString() {
201         // null stripStart
202         assertNull(StringUtils.stripStart(null, null));
203         assertEquals("", StringUtils.stripStart("", null));
204         assertEquals("", StringUtils.stripStart("        ", null));
205         assertEquals("abc  ", StringUtils.stripStart("  abc  ", null));
206         assertEquals(StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE,
207                 StringUtils.stripStart(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null));
208 
209         // "" stripStart
210         assertNull(StringUtils.stripStart(null, ""));
211         assertEquals("", StringUtils.stripStart("", ""));
212         assertEquals("        ", StringUtils.stripStart("        ", ""));
213         assertEquals("  abc  ", StringUtils.stripStart("  abc  ", ""));
214         assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripStart(StringUtilsTest.WHITESPACE, ""));
215 
216         // " " stripStart
217         assertNull(StringUtils.stripStart(null, " "));
218         assertEquals("", StringUtils.stripStart("", " "));
219         assertEquals("", StringUtils.stripStart("        ", " "));
220         assertEquals("abc  ", StringUtils.stripStart("  abc  ", " "));
221 
222         // "ab" stripStart
223         assertNull(StringUtils.stripStart(null, "ab"));
224         assertEquals("", StringUtils.stripStart("", "ab"));
225         assertEquals("        ", StringUtils.stripStart("        ", "ab"));
226         assertEquals("  abc  ", StringUtils.stripStart("  abc  ", "ab"));
227         assertEquals("cabab", StringUtils.stripStart("abcabab", "ab"));
228         assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripStart(StringUtilsTest.WHITESPACE, ""));
229     }
230 
231     @Test
232     void testStripString() {
233         assertNull(StringUtils.strip(null));
234         assertEquals("", StringUtils.strip(""));
235         assertEquals("", StringUtils.strip("        "));
236         assertEquals("abc", StringUtils.strip("  abc  "));
237         assertEquals(StringUtilsTest.NON_WHITESPACE,
238                 StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
239     }
240 
241     @Test
242     void testStripStringString() {
243         // null strip
244         assertNull(StringUtils.strip(null, null));
245         assertEquals("", StringUtils.strip("", null));
246         assertEquals("", StringUtils.strip("        ", null));
247         assertEquals("abc", StringUtils.strip("  abc  ", null));
248         assertEquals(StringUtilsTest.NON_WHITESPACE,
249                 StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null));
250 
251         // "" strip
252         assertNull(StringUtils.strip(null, ""));
253         assertEquals("", StringUtils.strip("", ""));
254         assertEquals("        ", StringUtils.strip("        ", ""));
255         assertEquals("  abc  ", StringUtils.strip("  abc  ", ""));
256         assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(StringUtilsTest.WHITESPACE, ""));
257 
258         // " " strip
259         assertNull(StringUtils.strip(null, " "));
260         assertEquals("", StringUtils.strip("", " "));
261         assertEquals("", StringUtils.strip("        ", " "));
262         assertEquals("abc", StringUtils.strip("  abc  ", " "));
263 
264         // "ab" strip
265         assertNull(StringUtils.strip(null, "ab"));
266         assertEquals("", StringUtils.strip("", "ab"));
267         assertEquals("        ", StringUtils.strip("        ", "ab"));
268         assertEquals("  abc  ", StringUtils.strip("  abc  ", "ab"));
269         assertEquals("c", StringUtils.strip("abcabab", "ab"));
270         assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(StringUtilsTest.WHITESPACE, ""));
271     }
272 
273     @Test
274     void testStripSupplementaryCodePoints() {
275         // U+1D51E and U+1D51F share the high surrogate \uD835; U+1D11E shares the low surrogate \uDD1E with U+1D51E.
276         final String mathA = "\uD835\uDD1E"; // U+1D51E
277         final String mathB = "\uD835\uDD1F"; // U+1D51F
278         final String clef = "\uD834\uDD1E";  // U+1D11E
279 
280         // A supplementary strip character must not match a code point that only shares one surrogate half.
281         assertEquals(mathA + "abc", StringUtils.stripStart(mathA + "abc", mathB));
282         assertEquals("abc" + clef, StringUtils.stripEnd("abc" + clef, mathA));
283         assertEquals(mathA + "abc" + mathA, StringUtils.strip(mathA + "abc" + mathA, mathB));
284 
285         // Genuine membership still strips the whole supplementary code point.
286         assertEquals("abc", StringUtils.stripStart(mathA + mathA + "abc", mathA));
287         assertEquals("abc", StringUtils.stripEnd("abc" + mathA + mathA, mathA));
288         assertEquals("abc", StringUtils.strip(mathA + "abc" + mathA, mathA));
289     }
290 
291     @Test
292     void testStripToEmptyString() {
293         assertEquals("", StringUtils.stripToEmpty(null));
294         assertEquals("", StringUtils.stripToEmpty(""));
295         assertEquals("", StringUtils.stripToEmpty("        "));
296         assertEquals("", StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE));
297         assertEquals("ab c", StringUtils.stripToEmpty("  ab c  "));
298         assertEquals(StringUtilsTest.NON_WHITESPACE,
299                 StringUtils.stripToEmpty(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
300     }
301 
302     @Test
303     void testStripToNullString() {
304         assertNull(StringUtils.stripToNull(null));
305         assertNull(StringUtils.stripToNull(""));
306         assertNull(StringUtils.stripToNull("        "));
307         assertNull(StringUtils.stripToNull(StringUtilsTest.WHITESPACE));
308         assertEquals("ab c", StringUtils.stripToNull("  ab c  "));
309         assertEquals(StringUtilsTest.NON_WHITESPACE,
310                 StringUtils.stripToNull(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
311     }
312 }