18
18
package org .apache .commons .lang3 ;
19
19
20
20
import static org .junit .jupiter .api .Assertions .assertEquals ;
21
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
21
22
import static org .junit .jupiter .api .Assertions .assertNull ;
22
23
import static org .junit .jupiter .api .Assertions .assertThrows ;
23
24
import static org .junit .jupiter .api .Assertions .assertTrue ;
29
30
*/
30
31
public class StringUtilsAbbreviateTest {
31
32
33
+ /**
34
+ * Tests <a href="LANG-1770">https://issues.apache.org/jira/projects/LANG/issues/LANG-1770</a>.
35
+ */
36
+ @ Test
37
+ public void testEmoji () {
38
+ // @formatter:off
39
+ final String [] expectedResultsFox = {
40
+ "🦊..." , // 4
41
+ "🦊🦊..." ,
42
+ "🦊🦊🦊..." ,
43
+ "🦊🦊🦊🦊..." ,
44
+ "🦊🦊🦊🦊🦊..." ,
45
+ "🦊🦊🦊🦊🦊🦊..." ,
46
+ "🦊🦊🦊🦊🦊🦊🦊..." , // 10
47
+ };
48
+ final String [] expectedResultsFamilyWithCodepoints = {
49
+ "👩..." ,
50
+ "👩🏻..." ,
51
+ "👩🏻..." , // zero width joiner
52
+ "👩🏻👨..." ,
53
+ "👩🏻👨🏻..." ,
54
+ "👩🏻👨🏻..." ,
55
+ "👩🏻👨🏻👦..."
56
+ };
57
+ final String [] expectedResultsFamilyWithGrapheme = {
58
+ "👩🏻👨🏻👦🏻👦🏻..." , // 4
59
+ "👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼..." ,
60
+ "👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽..." ,
61
+ "👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾..." ,
62
+ "👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿..." ,
63
+ "👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿👩🏻👨🏻👦🏻👦🏻..." ,
64
+ "👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼..." // 10
65
+ };
66
+ // @formatter:on
67
+ for (int i = 4 ; i <= 10 ; i ++) {
68
+ final String abbreviateResult = StringUtils .abbreviate ("🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊🦊" , i );
69
+ assertNotNull (abbreviateResult );
70
+ // assertEquals(expectedResultsFox[i - 4], abbreviateResult);
71
+ }
72
+ for (int i = 4 ; i <= 10 ; i ++) {
73
+ final String abbreviateResult = StringUtils .abbreviate (
74
+ "👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿👩🏻👨🏻👦🏻👦🏻👩🏼👨🏼👦🏼👦🏼👩🏽👨🏽👦🏽👦🏽👩🏾👨🏾👦🏾👦🏾👩🏿👨🏿👦🏿👦🏿" ,
75
+ i );
76
+ assertNotNull (abbreviateResult );
77
+ // assertEquals(expectedResultsFamilyWithCodepoints[i - 4], abbreviateResult);
78
+ }
79
+ }
80
+
32
81
private void assertAbbreviateWithAbbrevMarkerAndOffset (final String expected , final String abbrevMarker , final int offset , final int maxWidth ) {
33
82
final String abcdefghijklmno = "abcdefghijklmno" ;
34
83
final String message = "abbreviate(String,String,int,int) failed" ;
35
84
final String actual = StringUtils .abbreviate (abcdefghijklmno , abbrevMarker , offset , maxWidth );
36
85
if (offset >= 0 && offset < abcdefghijklmno .length ()) {
37
- assertTrue (actual .indexOf ((char ) ('a' + offset )) != -1 ,
38
- message + " -- should contain offset character" );
86
+ assertTrue (actual .indexOf ((char ) ('a' + offset )) != -1 , message + " -- should contain offset character" );
39
87
}
40
88
assertTrue (actual .length () <= maxWidth , () -> message + " -- should not be greater than maxWidth" );
41
89
assertEquals (expected , actual , message );
@@ -46,8 +94,7 @@ private void assertAbbreviateWithOffset(final String expected, final int offset,
46
94
final String message = "abbreviate(String,int,int) failed" ;
47
95
final String actual = StringUtils .abbreviate (abcdefghijklmno , offset , maxWidth );
48
96
if (offset >= 0 && offset < abcdefghijklmno .length ()) {
49
- assertTrue (actual .indexOf ((char ) ('a' + offset )) != -1 ,
50
- message + " -- should contain offset character" );
97
+ assertTrue (actual .indexOf ((char ) ('a' + offset )) != -1 , message + " -- should contain offset character" );
51
98
}
52
99
assertTrue (actual .length () <= maxWidth , () -> message + " -- should not be greater than maxWidth" );
53
100
assertEquals (expected , actual , message );
@@ -59,7 +106,6 @@ public void testAbbreviate_StringInt() {
59
106
assertEquals ("" , StringUtils .abbreviate ("" , 10 ));
60
107
assertEquals ("short" , StringUtils .abbreviate ("short" , 10 ));
61
108
assertEquals ("Now is ..." , StringUtils .abbreviate ("Now is the time for all good men to come to the aid of their party." , 10 ));
62
-
63
109
final String raspberry = "raspberry peach" ;
64
110
assertEquals ("raspberry p..." , StringUtils .abbreviate (raspberry , 14 ));
65
111
assertEquals ("raspberry peach" , StringUtils .abbreviate ("raspberry peach" , 15 ));
@@ -69,31 +115,20 @@ public void testAbbreviate_StringInt() {
69
115
assertEquals ("abcdefg" , StringUtils .abbreviate ("abcdefg" , 8 ));
70
116
assertEquals ("a..." , StringUtils .abbreviate ("abcdefg" , 4 ));
71
117
assertEquals ("" , StringUtils .abbreviate ("" , 4 ));
72
-
73
- assertThrows (
74
- IllegalArgumentException .class ,
75
- () -> StringUtils .abbreviate ("abc" , 3 ),
76
- "StringUtils.abbreviate expecting IllegalArgumentException" );
118
+ assertThrows (IllegalArgumentException .class , () -> StringUtils .abbreviate ("abc" , 3 ), "StringUtils.abbreviate expecting IllegalArgumentException" );
77
119
}
78
120
79
121
@ Test
80
122
public void testAbbreviate_StringIntInt () {
81
123
assertNull (StringUtils .abbreviate (null , 10 , 12 ));
82
124
assertEquals ("" , StringUtils .abbreviate ("" , 0 , 10 ));
83
125
assertEquals ("" , StringUtils .abbreviate ("" , 2 , 10 ));
84
-
85
- assertThrows (
86
- IllegalArgumentException .class ,
87
- () -> StringUtils .abbreviate ("abcdefghij" , 0 , 3 ),
126
+ assertThrows (IllegalArgumentException .class , () -> StringUtils .abbreviate ("abcdefghij" , 0 , 3 ),
88
127
"StringUtils.abbreviate expecting IllegalArgumentException" );
89
- assertThrows (
90
- IllegalArgumentException .class ,
91
- () -> StringUtils .abbreviate ("abcdefghij" , 5 , 6 ),
128
+ assertThrows (IllegalArgumentException .class , () -> StringUtils .abbreviate ("abcdefghij" , 5 , 6 ),
92
129
"StringUtils.abbreviate expecting IllegalArgumentException" );
93
-
94
130
final String raspberry = "raspberry peach" ;
95
131
assertEquals ("raspberry peach" , StringUtils .abbreviate (raspberry , 11 , 15 ));
96
-
97
132
assertNull (StringUtils .abbreviate (null , 7 , 14 ));
98
133
assertAbbreviateWithOffset ("abcdefg..." , -1 , 10 );
99
134
assertAbbreviateWithOffset ("abcdefg..." , 0 , 10 );
@@ -124,7 +159,6 @@ public void testAbbreviate_StringStringInt() {
124
159
assertEquals ("" , StringUtils .abbreviate ("" , "..." , 2 ));
125
160
assertEquals ("wai**" , StringUtils .abbreviate ("waiheke" , "**" , 5 ));
126
161
assertEquals ("And af,,,," , StringUtils .abbreviate ("And after a long time, he finally met his son." , ",,,," , 10 ));
127
-
128
162
final String raspberry = "raspberry peach" ;
129
163
assertEquals ("raspberry pe.." , StringUtils .abbreviate (raspberry , ".." , 14 ));
130
164
assertEquals ("raspberry peach" , StringUtils .abbreviate ("raspberry peach" , "---*---" , 15 ));
@@ -134,10 +168,7 @@ public void testAbbreviate_StringStringInt() {
134
168
assertEquals ("abcdefg" , StringUtils .abbreviate ("abcdefg" , "_-" , 8 ));
135
169
assertEquals ("abc." , StringUtils .abbreviate ("abcdefg" , "." , 4 ));
136
170
assertEquals ("" , StringUtils .abbreviate ("" , 4 ));
137
-
138
- assertThrows (
139
- IllegalArgumentException .class ,
140
- () -> StringUtils .abbreviate ("abcdefghij" , "..." , 3 ),
171
+ assertThrows (IllegalArgumentException .class , () -> StringUtils .abbreviate ("abcdefghij" , "..." , 3 ),
141
172
"StringUtils.abbreviate expecting IllegalArgumentException" );
142
173
}
143
174
@@ -147,19 +178,12 @@ public void testAbbreviate_StringStringIntInt() {
147
178
assertNull (StringUtils .abbreviate (null , "..." , 10 , 12 ));
148
179
assertEquals ("" , StringUtils .abbreviate ("" , null , 0 , 10 ));
149
180
assertEquals ("" , StringUtils .abbreviate ("" , "..." , 2 , 10 ));
150
-
151
- assertThrows (
152
- IllegalArgumentException .class ,
153
- () -> StringUtils .abbreviate ("abcdefghij" , "::" , 0 , 2 ),
181
+ assertThrows (IllegalArgumentException .class , () -> StringUtils .abbreviate ("abcdefghij" , "::" , 0 , 2 ),
154
182
"StringUtils.abbreviate expecting IllegalArgumentException" );
155
- assertThrows (
156
- IllegalArgumentException .class ,
157
- () -> StringUtils .abbreviate ("abcdefghij" , "!!!" , 5 , 6 ),
183
+ assertThrows (IllegalArgumentException .class , () -> StringUtils .abbreviate ("abcdefghij" , "!!!" , 5 , 6 ),
158
184
"StringUtils.abbreviate expecting IllegalArgumentException" );
159
-
160
185
final String raspberry = "raspberry peach" ;
161
186
assertEquals ("raspberry peach" , StringUtils .abbreviate (raspberry , "--" , 12 , 15 ));
162
-
163
187
assertNull (StringUtils .abbreviate (null , ";" , 7 , 14 ));
164
188
assertAbbreviateWithAbbrevMarkerAndOffset ("abcdefgh;;" , ";;" , -1 , 10 );
165
189
assertAbbreviateWithAbbrevMarkerAndOffset ("abcdefghi." , "." , 0 , 10 );
@@ -183,7 +207,7 @@ public void testAbbreviate_StringStringIntInt() {
183
207
assertAbbreviateWithAbbrevMarkerAndOffset ("+ghijklmno" , "+" , Integer .MAX_VALUE , 10 );
184
208
}
185
209
186
- //Fixed LANG-1463
210
+ // Fixed LANG-1463
187
211
@ Test
188
212
public void testAbbreviateMarkerWithEmptyString () {
189
213
final String greaterThanMaxTest = "much too long text" ;
@@ -198,34 +222,22 @@ public void testAbbreviateMiddle() {
198
222
assertEquals ("abc" , StringUtils .abbreviateMiddle ("abc" , "." , 0 ));
199
223
assertEquals ("abc" , StringUtils .abbreviateMiddle ("abc" , "." , 3 ));
200
224
assertEquals ("ab.f" , StringUtils .abbreviateMiddle ("abcdef" , "." , 4 ));
201
-
202
225
// JIRA issue (LANG-405) example (slightly different than actual expected result)
203
- assertEquals (
204
- "A very long text with un...f the text is complete." ,
205
- StringUtils .abbreviateMiddle (
206
- "A very long text with unimportant stuff in the middle but interesting start and " +
207
- "end to see if the text is complete." , "..." , 50 ));
208
-
226
+ assertEquals ("A very long text with un...f the text is complete." , StringUtils .abbreviateMiddle (
227
+ "A very long text with unimportant stuff in the middle but interesting start and " + "end to see if the text is complete." , "..." , 50 ));
209
228
// Test a much longer text :)
210
229
final String longText = "Start text" + StringUtils .repeat ("x" , 10000 ) + "Close text" ;
211
- assertEquals (
212
- "Start text->Close text" ,
213
- StringUtils .abbreviateMiddle (longText , "->" , 22 ));
214
-
230
+ assertEquals ("Start text->Close text" , StringUtils .abbreviateMiddle (longText , "->" , 22 ));
215
231
// Test negative length
216
232
assertEquals ("abc" , StringUtils .abbreviateMiddle ("abc" , "." , -1 ));
217
-
218
233
// Test boundaries
219
234
// Fails to change anything as method ensures first and last char are kept
220
235
assertEquals ("abc" , StringUtils .abbreviateMiddle ("abc" , "." , 1 ));
221
236
assertEquals ("abc" , StringUtils .abbreviateMiddle ("abc" , "." , 2 ));
222
-
223
237
// Test length of n=1
224
238
assertEquals ("a" , StringUtils .abbreviateMiddle ("a" , "." , 1 ));
225
-
226
239
// Test smallest length that can lead to success
227
240
assertEquals ("a.d" , StringUtils .abbreviateMiddle ("abcd" , "." , 3 ));
228
-
229
241
// More from LANG-405
230
242
assertEquals ("a..f" , StringUtils .abbreviateMiddle ("abcdef" , ".." , 4 ));
231
243
assertEquals ("ab.ef" , StringUtils .abbreviateMiddle ("abcdef" , "." , 5 ));
0 commit comments