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.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
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 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
169 assertEquals("..de", StringUtils.abbreviate("abcde", "..", 4, 4));
170 assertEquals("....fg", StringUtils.abbreviate("abcdefg", "....", 5, 6));
171 }
172
173
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
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
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
192 final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
193 assertEquals("Start text->Close text", StringUtils.abbreviateMiddle(longText, "->", 22));
194
195 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
196
197
198 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
199 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
200
201 assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
202
203 assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
204
205 assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
206 assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
207 }
208
209
210
211
212 @Test
213 void testEmoji() {
214
215 final String[] expectedResultsFox = {
216 "ð¦...",
217 "ð¦ð¦...",
218 "ð¦ð¦ð¦...",
219 "ð¦ð¦ð¦ð¦...",
220 "ð¦ð¦ð¦ð¦ð¦...",
221 "ð¦ð¦ð¦ð¦ð¦ð¦...",
222 "ð¦ð¦ð¦ð¦ð¦ð¦ð¦...",
223 };
224 final String[] expectedResultsFamilyWithCodepoints = {
225 "ð©...",
226 "ð©ð»...",
227 "ð©ð»â...",
228 "ð©ð»âð¨...",
229 "ð©ð»âð¨ð»...",
230 "ð©ð»âð¨ð»â...",
231 "ð©ð»âð¨ð»âð¦..."
232 };
233 final String[] expectedResultsFamilyWithGrapheme = {
234 "ð©ð»âð¨ð»âð¦ð»âð¦ð»...",
235 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼...",
236 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½...",
237 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾...",
238 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿...",
239 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»...",
240 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼..."
241 };
242
243 for (int i = 4; i <= 10; i++) {
244 final String abbreviateResult = StringUtils.abbreviate("ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦", i);
245 assertNotNull(abbreviateResult);
246
247 }
248 for (int i = 4; i <= 10; i++) {
249 final String abbreviateResult = StringUtils.abbreviate(
250 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿",
251 i);
252 assertNotNull(abbreviateResult);
253
254 }
255 }
256 }