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  
18  package org.apache.commons.lang3;
19  
20  import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link StringUtils#abbreviate(String, int)} and friends.
31   */
32  class StringUtilsAbbreviateTest extends AbstractLangTest {
33  
34      private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) {
35          final String abcdefghijklmno = "abcdefghijklmno";
36          final String message = "abbreviate(String,String,int,int) failed";
37          final String actual = StringUtils.abbreviate(abcdefghijklmno, abbrevMarker, offset, maxWidth);
38          if (offset >= 0 && offset < abcdefghijklmno.length()) {
39              assertTrue(actual.indexOf((char) ('a' + offset)) != -1, message + " -- should contain offset character");
40          }
41          assertTrue(actual.length() <= maxWidth, () -> message + " -- should not be greater than maxWidth");
42          assertEquals(expected, actual, message);
43      }
44  
45      private void assertAbbreviateWithOffset(final String expected, final int offset, final int maxWidth) {
46          final String abcdefghijklmno = "abcdefghijklmno";
47          final String message = "abbreviate(String,int,int) failed";
48          final String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
49          if (offset >= 0 && offset < abcdefghijklmno.length()) {
50              assertTrue(actual.indexOf((char) ('a' + offset)) != -1, message + " -- should contain offset character");
51          }
52          assertTrue(actual.length() <= maxWidth, () -> message + " -- should not be greater than maxWidth");
53          assertEquals(expected, actual, message);
54      }
55  
56      @Test
57      void testAbbreviate_StringInt() {
58          assertNull(StringUtils.abbreviate(null, 10));
59          assertEquals("", StringUtils.abbreviate("", 10));
60          assertEquals("short", StringUtils.abbreviate("short", 10));
61          assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
62          final String raspberry = "raspberry peach";
63          assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
64          assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
65          assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
66          assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6));
67          assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7));
68          assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8));
69          assertEquals("a...", StringUtils.abbreviate("abcdefg", 4));
70          assertEquals("", StringUtils.abbreviate("", 4));
71          assertIllegalArgumentException(() -> StringUtils.abbreviate("abc", 3), "StringUtils.abbreviate expecting IllegalArgumentException");
72      }
73  
74      @Test
75      void testAbbreviate_StringIntInt() {
76          assertNull(StringUtils.abbreviate(null, 10, 12));
77          assertEquals("", StringUtils.abbreviate("", 0, 10));
78          assertEquals("", StringUtils.abbreviate("", 2, 10));
79          assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 0, 3),
80                  "StringUtils.abbreviate expecting IllegalArgumentException");
81          assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 5, 6),
82                  "StringUtils.abbreviate expecting IllegalArgumentException");
83          final String raspberry = "raspberry peach";
84          assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
85          assertNull(StringUtils.abbreviate(null, 7, 14));
86          assertAbbreviateWithOffset("abcdefg...", -1, 10);
87          assertAbbreviateWithOffset("abcdefg...", 0, 10);
88          assertAbbreviateWithOffset("abcdefg...", 1, 10);
89          assertAbbreviateWithOffset("abcdefg...", 2, 10);
90          assertAbbreviateWithOffset("abcdefg...", 3, 10);
91          assertAbbreviateWithOffset("abcdefg...", 4, 10);
92          assertAbbreviateWithOffset("...fghi...", 5, 10);
93          assertAbbreviateWithOffset("...ghij...", 6, 10);
94          assertAbbreviateWithOffset("...hijk...", 7, 10);
95          assertAbbreviateWithOffset("...ijklmno", 8, 10);
96          assertAbbreviateWithOffset("...ijklmno", 9, 10);
97          assertAbbreviateWithOffset("...ijklmno", 10, 10);
98          assertAbbreviateWithOffset("...ijklmno", 11, 10);
99          assertAbbreviateWithOffset("...ijklmno", 12, 10);
100         assertAbbreviateWithOffset("...ijklmno", 13, 10);
101         assertAbbreviateWithOffset("...ijklmno", 14, 10);
102         assertAbbreviateWithOffset("...ijklmno", 15, 10);
103         assertAbbreviateWithOffset("...ijklmno", 16, 10);
104         assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
105         // abbreviating a shorter string allows maxWidth < 7
106         assertEquals("...efg", StringUtils.abbreviate("abcdefg", 5, 6));
107     }
108 
109     @Test
110     void testAbbreviate_StringStringInt() {
111         assertNull(StringUtils.abbreviate(null, null, 10));
112         assertNull(StringUtils.abbreviate(null, "...", 10));
113         assertEquals("paranaguac", StringUtils.abbreviate("paranaguacu", null, 10));
114         assertEquals("", StringUtils.abbreviate("", "...", 2));
115         assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
116         assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10));
117         final String raspberry = "raspberry peach";
118         assertEquals("raspberry pe..", StringUtils.abbreviate(raspberry, "..", 14));
119         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", "---*---", 15));
120         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", ".", 16));
121         assertEquals("abc()(", StringUtils.abbreviate("abcdefg", "()(", 6));
122         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", ";", 7));
123         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8));
124         assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4));
125         assertEquals("", StringUtils.abbreviate("", 4));
126         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "...", 3),
127                 "StringUtils.abbreviate expecting IllegalArgumentException");
128     }
129 
130     @Test
131     void testAbbreviate_StringStringIntInt() {
132         assertNull(StringUtils.abbreviate(null, null, 10, 12));
133         assertNull(StringUtils.abbreviate(null, "...", 10, 12));
134         assertEquals("", StringUtils.abbreviate("", null, 0, 10));
135         assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
136         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", null, 2, 10));
137         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "", 2, 10));
138         assertEquals("abc", StringUtils.abbreviate("abcdefg", null, 0, 3));
139         assertEquals("cde", StringUtils.abbreviate("abcdefg", null, 2, 3));
140         assertEquals("abc", StringUtils.abbreviate("abcdefg", "", 0, 3));
141         assertEquals("cde", StringUtils.abbreviate("abcdefg", "", 2, 3));
142         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "::", 0, 2),
143                 "StringUtils.abbreviate expecting IllegalArgumentException");
144         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "!!.", 5, 6),
145                 "StringUtils.abbreviate expecting IllegalArgumentException");
146         final String raspberry = "raspberry peach";
147         assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15));
148         assertNull(StringUtils.abbreviate(null, ";", 7, 14));
149         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh;;", ";;", -1, 10);
150         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi.", ".", 0, 10);
151         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh++", "++", 1, 10);
152         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi*", "*", 2, 10);
153         assertAbbreviateWithAbbrevMarkerAndOffset("abcdef{{{{", "{{{{", 4, 10);
154         assertAbbreviateWithAbbrevMarkerAndOffset("abcdef____", "____", 5, 10);
155         assertAbbreviateWithAbbrevMarkerAndOffset("==fghijk==", "==", 5, 10);
156         assertAbbreviateWithAbbrevMarkerAndOffset("___ghij___", "___", 6, 10);
157         assertAbbreviateWithAbbrevMarkerAndOffset("…ghijklmno", "…", 6, 10);
158         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 7, 10);
159         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 8, 10);
160         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 9, 10);
161         assertAbbreviateWithAbbrevMarkerAndOffset("///ijklmno", "///", 10, 10);
162         assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 10, 10);
163         assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 11, 10);
164         assertAbbreviateWithAbbrevMarkerAndOffset("...ijklmno", "...", 12, 10);
165         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 13, 10);
166         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 14, 10);
167         assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10);
168         assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10);
169         assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", Integer.MAX_VALUE, 10);
170         // abbreviating a shorter string allows maxWidth < abbrevMarker.length * 2 + 1
171         assertEquals("..de", StringUtils.abbreviate("abcde", "..", 4, 4));
172         assertEquals("....fg", StringUtils.abbreviate("abcdefg", "....", 5, 6));
173     }
174 
175     // Fixed LANG-1463
176     @Test
177     void testAbbreviateMarkerWithEmptyString() {
178         final String greaterThanMaxTest = "much too long text";
179         assertEquals("much too long", StringUtils.abbreviate(greaterThanMaxTest, "", 13));
180     }
181 
182     @Test
183     void testAbbreviateMiddle() {
184         // javadoc examples
185         assertNull(StringUtils.abbreviateMiddle(null, null, 0));
186         assertEquals("abc", StringUtils.abbreviateMiddle("abc", null, 0));
187         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 0));
188         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 3));
189         assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4));
190         // JIRA issue (LANG-405) example (slightly different than actual expected result)
191         assertEquals("A very long text with un...f the text is complete.", StringUtils.abbreviateMiddle(
192                 "A very long text with unimportant stuff in the middle but interesting start and end to see if the text is complete.", "...", 50));
193         // Test a much longer text :)
194         final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
195         assertEquals("Start text->Close text", StringUtils.abbreviateMiddle(longText, "->", 22));
196         // Test negative length
197         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
198         // Test boundaries
199         // Fails to change anything as method ensures first and last char are kept
200         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
201         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
202         // Test length of n=1
203         assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
204         // Test smallest length that can lead to success
205         assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
206         // More from LANG-405
207         assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
208         assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
209     }
210 
211     @Test
212     void testAbbreviateMiddleSurrogatePair() {
213         // U+1F600 GRINNING FACE is a single supplementary code point stored as a surrogate pair
214         final String grin = "😀";
215         // the head cut backs off the pair so the middle is never preceded by a lone high surrogate
216         assertEquals("a." + "b", StringUtils.abbreviateMiddle("a" + grin + grin + "b", ".", 4));
217         // the tail cut skips the orphaned low surrogate so the middle is never followed by one
218         assertEquals(grin + "..", StringUtils.abbreviateMiddle(grin + "abc" + grin, "..", 5));
219         // a cut that lands between two whole code points is unchanged
220         assertEquals("ab.d", StringUtils.abbreviateMiddle("ab" + grin + "cd", ".", 4));
221         assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4));
222         // results stay within length and never contain an unpaired surrogate
223         final String source = "a" + grin + "b" + grin + "cd" + grin + "ef";
224         for (int len = 3; len < source.length(); len++) {
225             final String result = StringUtils.abbreviateMiddle(source, ".", len);
226             assertTrue(result.length() <= len, () -> "result longer than length: " + result);
227             for (int i = 0; i < result.length(); i++) {
228                 final char ch = result.charAt(i);
229                 if (Character.isHighSurrogate(ch)) {
230                     assertTrue(i + 1 < result.length() && Character.isLowSurrogate(result.charAt(i + 1)), "lone high surrogate in: " + result);
231                     i++; // skip the paired low surrogate
232                 } else {
233                     assertFalse(Character.isLowSurrogate(ch), "lone low surrogate in: " + result);
234                 }
235             }
236         }
237     }
238 
239     @Test
240     void testAbbreviateSurrogatePair() {
241         // U+1F600 GRINNING FACE is a single supplementary code point stored as a surrogate pair
242         final String grin = "😀";
243         // the head cut backs off the pair so the marker is never preceded by a lone high surrogate
244         assertEquals("...", StringUtils.abbreviate(grin + "abcdef", 4));
245         assertEquals(grin + "...", StringUtils.abbreviate(grin + "abcdef", 5));
246         // a trailing supplementary code point is kept whole rather than sliced into a lone low surrogate
247         assertEquals("..." + grin, StringUtils.abbreviate("abcdef" + grin, 6, 5));
248         // an input that is only the pair is kept whole or dropped, never split into a lone surrogate
249         assertEquals(grin, StringUtils.abbreviate(grin, 4));
250         assertEquals(grin, StringUtils.abbreviate(grin, 0, 5));
251         assertEquals(grin, StringUtils.abbreviate(grin, 1, 4));
252         assertEquals(grin, StringUtils.abbreviate(grin, "x", 0, 2));
253         assertEquals(grin, StringUtils.abbreviate(grin, "x", 1, 2));
254         assertEquals(grin, StringUtils.abbreviate(grin, "", 0, 2));
255         assertEquals("", StringUtils.abbreviate(grin, "", 0, 1));
256         // results stay within maxWidth and never contain an unpaired surrogate
257         for (int width = 4; width <= 8; width++) {
258             final String result = StringUtils.abbreviate("a" + grin + "b" + grin + "cd", width);
259             assertTrue(result.length() <= width, () -> "result longer than maxWidth: " + result);
260             for (int i = 0; i < result.length(); i++) {
261                 final char ch = result.charAt(i);
262                 if (Character.isHighSurrogate(ch)) {
263                     assertTrue(i + 1 < result.length() && Character.isLowSurrogate(result.charAt(i + 1)), "lone high surrogate in: " + result);
264                     i++; // skip the paired low surrogate
265                 } else {
266                     assertFalse(Character.isLowSurrogate(ch), "lone low surrogate in: " + result);
267                 }
268             }
269         }
270     }
271 
272     /**
273      * Tests <a href="LANG-1770">https://issues.apache.org/jira/projects/LANG/issues/LANG-1770</a>.
274      */
275     @Test
276     void testEmoji() {
277         // @formatter:off
278         final String[] expectedResultsFox = {
279             "🦊...", // 4
280             "🦊🦊...",
281             "🦊🦊🦊...",
282             "🦊🦊🦊🦊...",
283             "🦊🦊🦊🦊🦊...",
284             "🦊🦊🦊🦊🦊🦊...",
285             "🦊🦊🦊🦊🦊🦊🦊...", // 10
286         };
287         final String[] expectedResultsFamilyWithCodepoints = {
288             "👩...",
289             "👩🏻...",
290             "👩🏻‍...", // zero width joiner
291             "👩🏻‍👨...",
292             "👩🏻‍👨🏻...",
293             "👩🏻‍👨🏻‍...",
294             "👩🏻‍👨🏻‍👦..."
295         };
296         final String[] expectedResultsFamilyWithGrapheme = {
297             "👩🏻‍👨🏻‍👦🏻‍👦🏻...", // 4
298             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼...",
299             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽...",
300             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾...",
301             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿...",
302             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻...",
303             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼..." // 10
304         };
305         // @formatter:on
306         for (int i = 4; i <= 10; i++) {
307             final String abbreviateResult = StringUtils.abbreviate("🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊", i);
308             assertNotNull(abbreviateResult);
309             // assertEquals(expectedResultsFox[i - 4], abbreviateResult);
310         }
311         for (int i = 4; i <= 10; i++) {
312             final String abbreviateResult = StringUtils.abbreviate(
313                     "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿",
314                     i);
315             assertNotNull(abbreviateResult);
316             // assertEquals(expectedResultsFamilyWithCodepoints[i - 4], abbreviateResult);
317         }
318     }
319 }