1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
171 assertEquals("..de", StringUtils.abbreviate("abcde", "..", 4, 4));
172 assertEquals("....fg", StringUtils.abbreviate("abcdefg", "....", 5, 6));
173 }
174
175
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
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
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
194 final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
195 assertEquals("Start text->Close text", StringUtils.abbreviateMiddle(longText, "->", 22));
196
197 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
198
199
200 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
201 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
202
203 assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
204
205 assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
206
207 assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
208 assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
209 }
210
211 @Test
212 void testAbbreviateMiddleSurrogatePair() {
213
214 final String grin = "ð";
215
216 assertEquals("a." + "b", StringUtils.abbreviateMiddle("a" + grin + grin + "b", ".", 4));
217
218 assertEquals(grin + "..", StringUtils.abbreviateMiddle(grin + "abc" + grin, "..", 5));
219
220 assertEquals("ab.d", StringUtils.abbreviateMiddle("ab" + grin + "cd", ".", 4));
221 assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4));
222
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++;
232 } else {
233 assertFalse(Character.isLowSurrogate(ch), "lone low surrogate in: " + result);
234 }
235 }
236 }
237 }
238
239 @Test
240 void testAbbreviateSurrogatePair() {
241
242 final String grin = "ð";
243
244 assertEquals("...", StringUtils.abbreviate(grin + "abcdef", 4));
245 assertEquals(grin + "...", StringUtils.abbreviate(grin + "abcdef", 5));
246
247 assertEquals("..." + grin, StringUtils.abbreviate("abcdef" + grin, 6, 5));
248
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
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++;
265 } else {
266 assertFalse(Character.isLowSurrogate(ch), "lone low surrogate in: " + result);
267 }
268 }
269 }
270 }
271
272
273
274
275 @Test
276 void testEmoji() {
277
278 final String[] expectedResultsFox = {
279 "ð¦...",
280 "ð¦ð¦...",
281 "ð¦ð¦ð¦...",
282 "ð¦ð¦ð¦ð¦...",
283 "ð¦ð¦ð¦ð¦ð¦...",
284 "ð¦ð¦ð¦ð¦ð¦ð¦...",
285 "ð¦ð¦ð¦ð¦ð¦ð¦ð¦...",
286 };
287 final String[] expectedResultsFamilyWithCodepoints = {
288 "ð©...",
289 "ð©ð»...",
290 "ð©ð»â...",
291 "ð©ð»âð¨...",
292 "ð©ð»âð¨ð»...",
293 "ð©ð»âð¨ð»â...",
294 "ð©ð»âð¨ð»âð¦..."
295 };
296 final String[] expectedResultsFamilyWithGrapheme = {
297 "ð©ð»âð¨ð»âð¦ð»âð¦ð»...",
298 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼...",
299 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½...",
300 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾...",
301 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿...",
302 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»...",
303 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼..."
304 };
305
306 for (int i = 4; i <= 10; i++) {
307 final String abbreviateResult = StringUtils.abbreviate("ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦", i);
308 assertNotNull(abbreviateResult);
309
310 }
311 for (int i = 4; i <= 10; i++) {
312 final String abbreviateResult = StringUtils.abbreviate(
313 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿",
314 i);
315 assertNotNull(abbreviateResult);
316
317 }
318 }
319 }