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         // abbreviating a shorter string allows maxWidth < 7
105         assertEquals("...efg", StringUtils.abbreviate("abcdefg", 5, 6));
106     }
107 
108     @Test
109     void testAbbreviate_StringStringInt() {
110         assertNull(StringUtils.abbreviate(null, null, 10));
111         assertNull(StringUtils.abbreviate(null, "...", 10));
112         assertEquals("paranaguac", StringUtils.abbreviate("paranaguacu", null, 10));
113         assertEquals("", StringUtils.abbreviate("", "...", 2));
114         assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
115         assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10));
116         final String raspberry = "raspberry peach";
117         assertEquals("raspberry pe..", StringUtils.abbreviate(raspberry, "..", 14));
118         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", "---*---", 15));
119         assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", ".", 16));
120         assertEquals("abc()(", StringUtils.abbreviate("abcdefg", "()(", 6));
121         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", ";", 7));
122         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8));
123         assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4));
124         assertEquals("", StringUtils.abbreviate("", 4));
125         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "...", 3),
126                 "StringUtils.abbreviate expecting IllegalArgumentException");
127     }
128 
129     @Test
130     void testAbbreviate_StringStringIntInt() {
131         assertNull(StringUtils.abbreviate(null, null, 10, 12));
132         assertNull(StringUtils.abbreviate(null, "...", 10, 12));
133         assertEquals("", StringUtils.abbreviate("", null, 0, 10));
134         assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
135         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", null, 2, 10));
136         assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "", 2, 10));
137         assertEquals("abc", StringUtils.abbreviate("abcdefg", null, 0, 3));
138         assertEquals("cde", StringUtils.abbreviate("abcdefg", null, 2, 3));
139         assertEquals("abc", StringUtils.abbreviate("abcdefg", "", 0, 3));
140         assertEquals("cde", StringUtils.abbreviate("abcdefg", "", 2, 3));
141         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "::", 0, 2),
142                 "StringUtils.abbreviate expecting IllegalArgumentException");
143         assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6),
144                 "StringUtils.abbreviate expecting IllegalArgumentException");
145         final String raspberry = "raspberry peach";
146         assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15));
147         assertNull(StringUtils.abbreviate(null, ";", 7, 14));
148         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh;;", ";;", -1, 10);
149         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi.", ".", 0, 10);
150         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh++", "++", 1, 10);
151         assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi*", "*", 2, 10);
152         assertAbbreviateWithAbbrevMarkerAndOffset("abcdef{{{{", "{{{{", 4, 10);
153         assertAbbreviateWithAbbrevMarkerAndOffset("abcdef____", "____", 5, 10);
154         assertAbbreviateWithAbbrevMarkerAndOffset("==fghijk==", "==", 5, 10);
155         assertAbbreviateWithAbbrevMarkerAndOffset("___ghij___", "___", 6, 10);
156         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 7, 10);
157         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 8, 10);
158         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 9, 10);
159         assertAbbreviateWithAbbrevMarkerAndOffset("///ijklmno", "///", 10, 10);
160         assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 10, 10);
161         assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 11, 10);
162         assertAbbreviateWithAbbrevMarkerAndOffset("...ijklmno", "...", 12, 10);
163         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 13, 10);
164         assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 14, 10);
165         assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10);
166         assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10);
167         assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", Integer.MAX_VALUE, 10);
168         // abbreviating a shorter string allows maxWidth < abbrevMarker.length * 2 + 1
169         assertEquals("..de", StringUtils.abbreviate("abcde", "..", 4, 4));
170         assertEquals("....fg", StringUtils.abbreviate("abcdefg", "....", 5, 6));
171     }
172 
173     // Fixed LANG-1463
174     @Test
175     void testAbbreviateMarkerWithEmptyString() {
176         final String greaterThanMaxTest = "much too long text";
177         assertEquals("much too long", StringUtils.abbreviate(greaterThanMaxTest, "", 13));
178     }
179 
180     @Test
181     void testAbbreviateMiddle() {
182         // javadoc examples
183         assertNull(StringUtils.abbreviateMiddle(null, null, 0));
184         assertEquals("abc", StringUtils.abbreviateMiddle("abc", null, 0));
185         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 0));
186         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 3));
187         assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4));
188         // JIRA issue (LANG-405) example (slightly different than actual expected result)
189         assertEquals("A very long text with un...f the text is complete.", StringUtils.abbreviateMiddle(
190                 "A very long text with unimportant stuff in the middle but interesting start and end to see if the text is complete.", "...", 50));
191         // Test a much longer text :)
192         final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
193         assertEquals("Start text->Close text", StringUtils.abbreviateMiddle(longText, "->", 22));
194         // Test negative length
195         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
196         // Test boundaries
197         // Fails to change anything as method ensures first and last char are kept
198         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
199         assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
200         // Test length of n=1
201         assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
202         // Test smallest length that can lead to success
203         assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
204         // More from LANG-405
205         assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
206         assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
207     }
208 
209     /**
210      * Tests <a href="LANG-1770">https://issues.apache.org/jira/projects/LANG/issues/LANG-1770</a>.
211      */
212     @Test
213     void testEmoji() {
214         // @formatter:off
215         final String[] expectedResultsFox = {
216             "🦊...", // 4
217             "🦊🦊...",
218             "🦊🦊🦊...",
219             "🦊🦊🦊🦊...",
220             "🦊🦊🦊🦊🦊...",
221             "🦊🦊🦊🦊🦊🦊...",
222             "🦊🦊🦊🦊🦊🦊🦊...", // 10
223         };
224         final String[] expectedResultsFamilyWithCodepoints = {
225             "👩...",
226             "👩🏻...",
227             "👩🏻‍...", // zero width joiner
228             "👩🏻‍👨...",
229             "👩🏻‍👨🏻...",
230             "👩🏻‍👨🏻‍...",
231             "👩🏻‍👨🏻‍👦..."
232         };
233         final String[] expectedResultsFamilyWithGrapheme = {
234             "👩🏻‍👨🏻‍👦🏻‍👦🏻...", // 4
235             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼...",
236             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽...",
237             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾...",
238             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿...",
239             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻...",
240             "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼..." // 10
241         };
242         // @formatter:on
243         for (int i = 4; i <= 10; i++) {
244             final String abbreviateResult = StringUtils.abbreviate("🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊", i);
245             assertNotNull(abbreviateResult);
246             // assertEquals(expectedResultsFox[i - 4], abbreviateResult);
247         }
248         for (int i = 4; i <= 10; i++) {
249             final String abbreviateResult = StringUtils.abbreviate(
250                     "👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿👩🏻‍👨🏻‍👦🏻‍👦🏻👩🏼‍👨🏼‍👦🏼‍👦🏼👩🏽‍👨🏽‍👦🏽‍👦🏽👩🏾‍👨🏾‍👦🏾‍👦🏾👩🏿‍👨🏿‍👦🏿‍👦🏿",
251                     i);
252             assertNotNull(abbreviateResult);
253             // assertEquals(expectedResultsFamilyWithCodepoints[i - 4], abbreviateResult);
254         }
255     }
256 }