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
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
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
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
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
181 final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
182 assertEquals("Start text->Close text", StringUtils.abbreviateMiddle(longText, "->", 22));
183
184 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
185
186
187 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
188 assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
189
190 assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
191
192 assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
193
194 assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
195 assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
196 }
197
198
199
200
201 @Test
202 void testEmoji() {
203
204 final String[] expectedResultsFox = {
205 "ð¦...",
206 "ð¦ð¦...",
207 "ð¦ð¦ð¦...",
208 "ð¦ð¦ð¦ð¦...",
209 "ð¦ð¦ð¦ð¦ð¦...",
210 "ð¦ð¦ð¦ð¦ð¦ð¦...",
211 "ð¦ð¦ð¦ð¦ð¦ð¦ð¦...",
212 };
213 final String[] expectedResultsFamilyWithCodepoints = {
214 "ð©...",
215 "ð©ð»...",
216 "ð©ð»â...",
217 "ð©ð»âð¨...",
218 "ð©ð»âð¨ð»...",
219 "ð©ð»âð¨ð»â...",
220 "ð©ð»âð¨ð»âð¦..."
221 };
222 final String[] expectedResultsFamilyWithGrapheme = {
223 "ð©ð»âð¨ð»âð¦ð»âð¦ð»...",
224 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼...",
225 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½...",
226 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾...",
227 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿...",
228 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»...",
229 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼..."
230 };
231
232 for (int i = 4; i <= 10; i++) {
233 final String abbreviateResult = StringUtils.abbreviate("ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦ð¦", i);
234 assertNotNull(abbreviateResult);
235
236 }
237 for (int i = 4; i <= 10; i++) {
238 final String abbreviateResult = StringUtils.abbreviate(
239 "ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿ð©ð»âð¨ð»âð¦ð»âð¦ð»ð©ð¼âð¨ð¼âð¦ð¼âð¦ð¼ð©ð½âð¨ð½âð¦ð½âð¦ð½ð©ð¾âð¨ð¾âð¦ð¾âð¦ð¾ð©ð¿âð¨ð¿âð¦ð¿âð¦ð¿",
240 i);
241 assertNotNull(abbreviateResult);
242
243 }
244 }
245 }