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.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link StringUtils#abbreviate(String, int)} and friends.
30   */
31  class StringUtilsAbbreviateTest {
32  
33      private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) {
34          final String abcdefghijklmno = "abcdefghijklmno";
35          final String message = "abbreviate(String,String,int,int) failed";
36          final String actual = StringUtils.abbreviate(abcdefghijklmno, abbrevMarker, offset, maxWidth);
37          if (offset >= 0 && offset < abcdefghijklmno.length()) {
38              assertTrue(actual.indexOf((char) ('a' + offset)) != -1, message + " -- should contain offset character");
39          }
40          assertTrue(actual.length() <= maxWidth, () -> message + " -- should not be greater than maxWidth");
41          assertEquals(expected, actual, message);
42      }
43  
44      private void assertAbbreviateWithOffset(final String expected, final int offset, final int maxWidth) {
45          final String abcdefghijklmno = "abcdefghijklmno";
46          final String message = "abbreviate(String,int,int) failed";
47          final String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
48          if (offset >= 0 && offset < abcdefghijklmno.length()) {
49              assertTrue(actual.indexOf((char) ('a' + offset)) != -1, message + " -- should contain offset character");
50          }
51          assertTrue(actual.length() <= maxWidth, () -> message + " -- should not be greater than maxWidth");
52          assertEquals(expected, actual, message);
53      }
54  
55      @Test
56      void testAbbreviate_StringInt() {
57          assertNull(StringUtils.abbreviate(null, 10));
58          assertEquals("", StringUtils.abbreviate("", 10));
59          assertEquals("short", StringUtils.abbreviate("short", 10));
60          assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
61          final String raspberry = "raspberry peach";
62          assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
63          assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
64          assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
65          assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6));
66          assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7));
67          assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8));
68          assertEquals("a...", StringUtils.abbreviate("abcdefg", 4));
69          assertEquals("", StringUtils.abbreviate("", 4));
70          assertIllegalArgumentException(() -> StringUtils.abbreviate("abc", 3), "StringUtils.abbreviate expecting IllegalArgumentException");
71      }
72  
73      @Test
74      void testAbbreviate_StringIntInt() {
75          assertNull(StringUtils.abbreviate(null, 10, 12));
76          assertEquals("", StringUtils.abbreviate("", 0, 10));
77          assertEquals("", StringUtils.abbreviate("", 2, 10));
78          assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 0, 3),
79                  "StringUtils.abbreviate expecting IllegalArgumentException");
80          assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 5, 6),
81                  "StringUtils.abbreviate expecting IllegalArgumentException");
82          final String raspberry = "raspberry peach";
83          assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
84          assertNull(StringUtils.abbreviate(null, 7, 14));
85          assertAbbreviateWithOffset("abcdefg...", -1, 10);
86          assertAbbreviateWithOffset("abcdefg...", 0, 10);
87          assertAbbreviateWithOffset("abcdefg...", 1, 10);
88          assertAbbreviateWithOffset("abcdefg...", 2, 10);
89          assertAbbreviateWithOffset("abcdefg...", 3, 10);
90          assertAbbreviateWithOffset("abcdefg...", 4, 10);
91          assertAbbreviateWithOffset("...fghi...", 5, 10);
92          assertAbbreviateWithOffset("...ghij...", 6, 10);
93          assertAbbreviateWithOffset("...hijk...", 7, 10);
94          assertAbbreviateWithOffset("...ijklmno", 8, 10);
95          assertAbbreviateWithOffset("...ijklmno", 9, 10);
96          assertAbbreviateWithOffset("...ijklmno", 10, 10);
97          assertAbbreviateWithOffset("...ijklmno", 11, 10);
98          assertAbbreviateWithOffset("...ijklmno", 12, 10);
99          assertAbbreviateWithOffset("...ijklmno", 13, 10);
100         assertAbbreviateWithOffset("...ijklmno", 14, 10);
101         assertAbbreviateWithOffset("...ijklmno", 15, 10);
102         assertAbbreviateWithOffset("...ijklmno", 16, 10);
103         assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
104     }
105 
106     @Test
107     void testAbbreviate_StringStringInt() {
108         assertNull(StringUtils.abbreviate(null, null, 10));
109         assertNull(StringUtils.abbreviate(null, "...", 10));
110         assertEquals("paranaguacu", StringUtils.abbreviate("paranaguacu", null, 10));
111         assertEquals("", StringUtils.abbreviate("", "...", 2));
112         assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
113         assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10));
114         final String raspberry = "raspberry peach";
115         assertEquals("raspberry pe..", StringUtils.abbreviate(raspberry, "..", 14));
116         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", "---*---", 15));
117         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", ".", 16));
118         assertEquals("abc()(", StringUtils.abbreviate("abcdefg", "()(", 6));
119         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", ";", 7));
120         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8));
121         assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4));
122         assertEquals("", StringUtils.abbreviate("", 4));
123         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "...", 3),
124                 "StringUtils.abbreviate expecting IllegalArgumentException");
125     }
126 
127     @Test
128     void testAbbreviate_StringStringIntInt() {
129         assertNull(StringUtils.abbreviate(null, null, 10, 12));
130         assertNull(StringUtils.abbreviate(null, "...", 10, 12));
131         assertEquals("", StringUtils.abbreviate("", null, 0, 10));
132         assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
133         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "::", 0, 2),
134                 "StringUtils.abbreviate expecting IllegalArgumentException");
135         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6),
136                 "StringUtils.abbreviate expecting IllegalArgumentException");
137         final String raspberry = "raspberry peach";
138         assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15));
139         assertNull(StringUtils.abbreviate(null, ";", 7, 14));
140         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh;;", ";;", -1, 10);
141         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi.", ".", 0, 10);
142         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh++", "++", 1, 10);
143         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi*", "*", 2, 10);
144         assertAbbreviateWithAbbrevMarkerAndOffset("abcdef{{{{", "{{{{", 4, 10);
145         assertAbbreviateWithAbbrevMarkerAndOffset("abcdef____", "____", 5, 10);
146         assertAbbreviateWithAbbrevMarkerAndOffset("==fghijk==", "==", 5, 10);
147         assertAbbreviateWithAbbrevMarkerAndOffset("___ghij___", "___", 6, 10);
148         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 7, 10);
149         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 8, 10);
150         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 9, 10);
151         assertAbbreviateWithAbbrevMarkerAndOffset("///ijklmno", "///", 10, 10);
152         assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 10, 10);
153         assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 11, 10);
154         assertAbbreviateWithAbbrevMarkerAndOffset("...ijklmno", "...", 12, 10);
155         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 13, 10);
156         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 14, 10);
157         assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10);
158         assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10);
159         assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", Integer.MAX_VALUE, 10);
160     }
161 
162     // Fixed LANG-1463
163     @Test
164     void testAbbreviateMarkerWithEmptyString() {
165         final String greaterThanMaxTest = "much too long text";
166         assertEquals("much too long", StringUtils.abbreviate(greaterThanMaxTest, "", 13));
167     }
168 
169     @Test
170     void testAbbreviateMiddle() {
171         // javadoc examples
172         assertNull(StringUtils.abbreviateMiddle(null, null, 0));
173         assertEquals("abc", StringUtils.abbreviateMiddle("abc", null, 0));
174         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 0));
175         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 3));
176         assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4));
177         // JIRA issue (LANG-405) example (slightly different than actual expected result)
178         assertEquals("A very long text with un...f the text is complete.", StringUtils.abbreviateMiddle(
179                 "A very long text with unimportant stuff in the middle but interesting start and end to see if the text is complete.", "...", 50));
180         // Test a much longer text :)
181         final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
182         assertEquals("Start text->Close text", StringUtils.abbreviateMiddle(longText, "->", 22));
183         // Test negative length
184         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
185         // Test boundaries
186         // Fails to change anything as method ensures first and last char are kept
187         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
188         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
189         // Test length of n=1
190         assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
191         // Test smallest length that can lead to success
192         assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
193         // More from LANG-405
194         assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
195         assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
196     }
197 
198     /**
199      * Tests <a href="LANG-1770">https://issues.apache.org/jira/projects/LANG/issues/LANG-1770</a>.
200      */
201     @Test
202     void testEmoji() {
203         // @formatter:off
204         final String[] expectedResultsFox = {
205             "🦊...", // 4
206             "🦊🦊...",
207             "🦊🦊🦊...",
208             "🦊🦊🦊🦊...",
209             "🦊🦊🦊🦊🦊...",
210             "🦊🦊🦊🦊🦊🦊...",
211             "🦊🦊🦊🦊🦊🦊🦊...", // 10
212         };
213         final String[] expectedResultsFamilyWithCodepoints = {
214             "👩...",
215             "👩🏻...",
216             "👩🏻‍...", // zero width joiner
217             "👩🏻‍👨...",
218             "👩🏻‍👨🏻...",
219             "👩🏻‍👨🏻‍...",
220             "👩🏻‍👨🏻‍👦..."
221         };
222         final String[] expectedResultsFamilyWithGrapheme = {
223             "👩🏻‍👨🏻‍👦🏻‍👦🏻...", // 4
224             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼...",
225             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽...",
226             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾...",
227             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿...",
228             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻...",
229             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼..." // 10
230         };
231         // @formatter:on
232         for (int i = 4; i <= 10; i++) {
233             final String abbreviateResult = StringUtils.abbreviate("🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊", i);
234             assertNotNull(abbreviateResult);
235             // assertEquals(expectedResultsFox[i - 4], abbreviateResult);
236         }
237         for (int i = 4; i <= 10; i++) {
238             final String abbreviateResult = StringUtils.abbreviate(
239                     "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿",
240                     i);
241             assertNotNull(abbreviateResult);
242             // assertEquals(expectedResultsFamilyWithCodepoints[i - 4], abbreviateResult);
243         }
244     }
245 }