forked from apache/commons-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtilsAbbreviateTest.java
More file actions
256 lines (243 loc) · 15.4 KB
/
StringUtilsAbbreviateTest.java
File metadata and controls
256 lines (243 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3;
import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
* Tests {@link StringUtils#abbreviate(String, int)} and friends.
*/
class StringUtilsAbbreviateTest {
private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) {
final String abcdefghijklmno = "abcdefghijklmno";
final String message = "abbreviate(String,String,int,int) failed";
final String actual = StringUtils.abbreviate(abcdefghijklmno, abbrevMarker, offset, maxWidth);
if (offset >= 0 && offset < abcdefghijklmno.length()) {
assertTrue(actual.indexOf((char) ('a' + offset)) != -1, message + " -- should contain offset character");
}
assertTrue(actual.length() <= maxWidth, () -> message + " -- should not be greater than maxWidth");
assertEquals(expected, actual, message);
}
private void assertAbbreviateWithOffset(final String expected, final int offset, final int maxWidth) {
final String abcdefghijklmno = "abcdefghijklmno";
final String message = "abbreviate(String,int,int) failed";
final String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
if (offset >= 0 && offset < abcdefghijklmno.length()) {
assertTrue(actual.indexOf((char) ('a' + offset)) != -1, message + " -- should contain offset character");
}
assertTrue(actual.length() <= maxWidth, () -> message + " -- should not be greater than maxWidth");
assertEquals(expected, actual, message);
}
@Test
void testAbbreviate_StringInt() {
assertNull(StringUtils.abbreviate(null, 10));
assertEquals("", StringUtils.abbreviate("", 10));
assertEquals("short", StringUtils.abbreviate("short", 10));
assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
final String raspberry = "raspberry peach";
assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6));
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7));
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8));
assertEquals("a...", StringUtils.abbreviate("abcdefg", 4));
assertEquals("", StringUtils.abbreviate("", 4));
assertIllegalArgumentException(() -> StringUtils.abbreviate("abc", 3), "StringUtils.abbreviate expecting IllegalArgumentException");
}
@Test
void testAbbreviate_StringIntInt() {
assertNull(StringUtils.abbreviate(null, 10, 12));
assertEquals("", StringUtils.abbreviate("", 0, 10));
assertEquals("", StringUtils.abbreviate("", 2, 10));
assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 0, 3),
"StringUtils.abbreviate expecting IllegalArgumentException");
assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 5, 6),
"StringUtils.abbreviate expecting IllegalArgumentException");
final String raspberry = "raspberry peach";
assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
assertNull(StringUtils.abbreviate(null, 7, 14));
assertAbbreviateWithOffset("abcdefg...", -1, 10);
assertAbbreviateWithOffset("abcdefg...", 0, 10);
assertAbbreviateWithOffset("abcdefg...", 1, 10);
assertAbbreviateWithOffset("abcdefg...", 2, 10);
assertAbbreviateWithOffset("abcdefg...", 3, 10);
assertAbbreviateWithOffset("abcdefg...", 4, 10);
assertAbbreviateWithOffset("...fghi...", 5, 10);
assertAbbreviateWithOffset("...ghij...", 6, 10);
assertAbbreviateWithOffset("...hijk...", 7, 10);
assertAbbreviateWithOffset("...ijklmno", 8, 10);
assertAbbreviateWithOffset("...ijklmno", 9, 10);
assertAbbreviateWithOffset("...ijklmno", 10, 10);
assertAbbreviateWithOffset("...ijklmno", 11, 10);
assertAbbreviateWithOffset("...ijklmno", 12, 10);
assertAbbreviateWithOffset("...ijklmno", 13, 10);
assertAbbreviateWithOffset("...ijklmno", 14, 10);
assertAbbreviateWithOffset("...ijklmno", 15, 10);
assertAbbreviateWithOffset("...ijklmno", 16, 10);
assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
// abbreviating a shorter string allows maxWidth < 7
assertEquals("...efg", StringUtils.abbreviate("abcdefg", 5, 6));
}
@Test
void testAbbreviate_StringStringInt() {
assertNull(StringUtils.abbreviate(null, null, 10));
assertNull(StringUtils.abbreviate(null, "...", 10));
assertEquals("paranaguac", StringUtils.abbreviate("paranaguacu", null, 10));
assertEquals("", StringUtils.abbreviate("", "...", 2));
assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10));
final String raspberry = "raspberry peach";
assertEquals("raspberry pe..", StringUtils.abbreviate(raspberry, "..", 14));
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", "---*---", 15));
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", ".", 16));
assertEquals("abc()(", StringUtils.abbreviate("abcdefg", "()(", 6));
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", ";", 7));
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8));
assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4));
assertEquals("", StringUtils.abbreviate("", 4));
assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "...", 3),
"StringUtils.abbreviate expecting IllegalArgumentException");
}
@Test
void testAbbreviate_StringStringIntInt() {
assertNull(StringUtils.abbreviate(null, null, 10, 12));
assertNull(StringUtils.abbreviate(null, "...", 10, 12));
assertEquals("", StringUtils.abbreviate("", null, 0, 10));
assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", null, 2, 10));
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "", 2, 10));
assertEquals("abc", StringUtils.abbreviate("abcdefg", null, 0, 3));
assertEquals("cde", StringUtils.abbreviate("abcdefg", null, 2, 3));
assertEquals("abc", StringUtils.abbreviate("abcdefg", "", 0, 3));
assertEquals("cde", StringUtils.abbreviate("abcdefg", "", 2, 3));
assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "::", 0, 2),
"StringUtils.abbreviate expecting IllegalArgumentException");
assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6),
"StringUtils.abbreviate expecting IllegalArgumentException");
final String raspberry = "raspberry peach";
assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15));
assertNull(StringUtils.abbreviate(null, ";", 7, 14));
assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh;;", ";;", -1, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi.", ".", 0, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh++", "++", 1, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi*", "*", 2, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("abcdef{{{{", "{{{{", 4, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("abcdef____", "____", 5, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("==fghijk==", "==", 5, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("___ghij___", "___", 6, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 7, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 8, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 9, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("///ijklmno", "///", 10, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 10, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 11, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("...ijklmno", "...", 12, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 13, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 14, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10);
assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", Integer.MAX_VALUE, 10);
// abbreviating a shorter string allows maxWidth < abbrevMarker.length * 2 + 1
assertEquals("..de", StringUtils.abbreviate("abcde", "..", 4, 4));
assertEquals("....fg", StringUtils.abbreviate("abcdefg", "....", 5, 6));
}
// Fixed LANG-1463
@Test
void testAbbreviateMarkerWithEmptyString() {
final String greaterThanMaxTest = "much too long text";
assertEquals("much too long", StringUtils.abbreviate(greaterThanMaxTest, "", 13));
}
@Test
void testAbbreviateMiddle() {
// javadoc examples
assertNull(StringUtils.abbreviateMiddle(null, null, 0));
assertEquals("abc", StringUtils.abbreviateMiddle("abc", null, 0));
assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 0));
assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 3));
assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4));
// JIRA issue (LANG-405) example (slightly different than actual expected result)
assertEquals("A very long text with un...f the text is complete.", StringUtils.abbreviateMiddle(
"A very long text with unimportant stuff in the middle but interesting start and end to see if the text is complete.", "...", 50));
// Test a much longer text :)
final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text";
assertEquals("Start text->Close text", StringUtils.abbreviateMiddle(longText, "->", 22));
// Test negative length
assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1));
// Test boundaries
// Fails to change anything as method ensures first and last char are kept
assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1));
assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2));
// Test length of n=1
assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1));
// Test smallest length that can lead to success
assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3));
// More from LANG-405
assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4));
assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5));
}
/**
* Tests <a href="LANG-1770">https://issues.apache.org/jira/projects/LANG/issues/LANG-1770</a>.
*/
@Test
void testEmoji() {
// @formatter:off
final String[] expectedResultsFox = {
"🦊...", // 4
"🦊🦊...",
"🦊🦊🦊...",
"🦊🦊🦊🦊...",
"🦊🦊🦊🦊🦊...",
"🦊🦊🦊🦊🦊🦊...",
"🦊🦊🦊🦊🦊🦊🦊...", // 10
};
final String[] expectedResultsFamilyWithCodepoints = {
"👩...",
"👩🏻...",
"👩🏻...", // zero width joiner
"👩🏻👨...",
"👩🏻👨🏻...",
"👩🏻👨🏻...",
"👩🏻👨🏻👦..."
};
final String[] expectedResultsFamilyWithGrapheme = {
"👩🏻👨🏻👦🏻👦🏻...", // 4
"👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼...",
"👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽...",
"👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾...",
"👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿...",
"👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿👩🏻👨🏻👦🏻👦🏻...",
"👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼..." // 10
};
// @formatter:on
for (int i = 4; i <= 10; i++) {
final String abbreviateResult = StringUtils.abbreviate("🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊", i);
assertNotNull(abbreviateResult);
// assertEquals(expectedResultsFox[i - 4], abbreviateResult);
}
for (int i = 4; i <= 10; i++) {
final String abbreviateResult = StringUtils.abbreviate(
"👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿",
i);
assertNotNull(abbreviateResult);
// assertEquals(expectedResultsFamilyWithCodepoints[i - 4], abbreviateResult);
}
}
}