|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.commons.lang3; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +/** |
| 28 | + * Tests {@link StringUtils#abbreviate(String, int)} and friends. |
| 29 | + */ |
| 30 | +public class StringUtilsAbbreviateTest { |
| 31 | + |
| 32 | + private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) { |
| 33 | + final String abcdefghijklmno = "abcdefghijklmno"; |
| 34 | + final String message = "abbreviate(String,String,int,int) failed"; |
| 35 | + final String actual = StringUtils.abbreviate(abcdefghijklmno, abbrevMarker, offset, maxWidth); |
| 36 | + if (offset >= 0 && offset < abcdefghijklmno.length()) { |
| 37 | + assertTrue(actual.indexOf((char) ('a' + offset)) != -1, |
| 38 | + 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, |
| 50 | + 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 | + public 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 | + |
| 63 | + final String raspberry = "raspberry peach"; |
| 64 | + assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14)); |
| 65 | + assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15)); |
| 66 | + assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16)); |
| 67 | + assertEquals("abc...", StringUtils.abbreviate("abcdefg", 6)); |
| 68 | + assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 7)); |
| 69 | + assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8)); |
| 70 | + assertEquals("a...", StringUtils.abbreviate("abcdefg", 4)); |
| 71 | + assertEquals("", StringUtils.abbreviate("", 4)); |
| 72 | + |
| 73 | + assertThrows( |
| 74 | + IllegalArgumentException.class, |
| 75 | + () -> StringUtils.abbreviate("abc", 3), |
| 76 | + "StringUtils.abbreviate expecting IllegalArgumentException"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testAbbreviate_StringIntInt() { |
| 81 | + assertNull(StringUtils.abbreviate(null, 10, 12)); |
| 82 | + assertEquals("", StringUtils.abbreviate("", 0, 10)); |
| 83 | + assertEquals("", StringUtils.abbreviate("", 2, 10)); |
| 84 | + |
| 85 | + assertThrows( |
| 86 | + IllegalArgumentException.class, |
| 87 | + () -> StringUtils.abbreviate("abcdefghij", 0, 3), |
| 88 | + "StringUtils.abbreviate expecting IllegalArgumentException"); |
| 89 | + assertThrows( |
| 90 | + IllegalArgumentException.class, |
| 91 | + () -> StringUtils.abbreviate("abcdefghij", 5, 6), |
| 92 | + "StringUtils.abbreviate expecting IllegalArgumentException"); |
| 93 | + |
| 94 | + final String raspberry = "raspberry peach"; |
| 95 | + assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15)); |
| 96 | + |
| 97 | + assertNull(StringUtils.abbreviate(null, 7, 14)); |
| 98 | + assertAbbreviateWithOffset("abcdefg...", -1, 10); |
| 99 | + assertAbbreviateWithOffset("abcdefg...", 0, 10); |
| 100 | + assertAbbreviateWithOffset("abcdefg...", 1, 10); |
| 101 | + assertAbbreviateWithOffset("abcdefg...", 2, 10); |
| 102 | + assertAbbreviateWithOffset("abcdefg...", 3, 10); |
| 103 | + assertAbbreviateWithOffset("abcdefg...", 4, 10); |
| 104 | + assertAbbreviateWithOffset("...fghi...", 5, 10); |
| 105 | + assertAbbreviateWithOffset("...ghij...", 6, 10); |
| 106 | + assertAbbreviateWithOffset("...hijk...", 7, 10); |
| 107 | + assertAbbreviateWithOffset("...ijklmno", 8, 10); |
| 108 | + assertAbbreviateWithOffset("...ijklmno", 9, 10); |
| 109 | + assertAbbreviateWithOffset("...ijklmno", 10, 10); |
| 110 | + assertAbbreviateWithOffset("...ijklmno", 11, 10); |
| 111 | + assertAbbreviateWithOffset("...ijklmno", 12, 10); |
| 112 | + assertAbbreviateWithOffset("...ijklmno", 13, 10); |
| 113 | + assertAbbreviateWithOffset("...ijklmno", 14, 10); |
| 114 | + assertAbbreviateWithOffset("...ijklmno", 15, 10); |
| 115 | + assertAbbreviateWithOffset("...ijklmno", 16, 10); |
| 116 | + assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testAbbreviate_StringStringInt() { |
| 121 | + assertNull(StringUtils.abbreviate(null, null, 10)); |
| 122 | + assertNull(StringUtils.abbreviate(null, "...", 10)); |
| 123 | + assertEquals("paranaguacu", StringUtils.abbreviate("paranaguacu", null, 10)); |
| 124 | + assertEquals("", StringUtils.abbreviate("", "...", 2)); |
| 125 | + assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5)); |
| 126 | + assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10)); |
| 127 | + |
| 128 | + final String raspberry = "raspberry peach"; |
| 129 | + assertEquals("raspberry pe..", StringUtils.abbreviate(raspberry, "..", 14)); |
| 130 | + assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", "---*---", 15)); |
| 131 | + assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", ".", 16)); |
| 132 | + assertEquals("abc()(", StringUtils.abbreviate("abcdefg", "()(", 6)); |
| 133 | + assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", ";", 7)); |
| 134 | + assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8)); |
| 135 | + assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4)); |
| 136 | + assertEquals("", StringUtils.abbreviate("", 4)); |
| 137 | + |
| 138 | + assertThrows( |
| 139 | + IllegalArgumentException.class, |
| 140 | + () -> StringUtils.abbreviate("abcdefghij", "...", 3), |
| 141 | + "StringUtils.abbreviate expecting IllegalArgumentException"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testAbbreviate_StringStringIntInt() { |
| 146 | + assertNull(StringUtils.abbreviate(null, null, 10, 12)); |
| 147 | + assertNull(StringUtils.abbreviate(null, "...", 10, 12)); |
| 148 | + assertEquals("", StringUtils.abbreviate("", null, 0, 10)); |
| 149 | + assertEquals("", StringUtils.abbreviate("", "...", 2, 10)); |
| 150 | + |
| 151 | + assertThrows( |
| 152 | + IllegalArgumentException.class, |
| 153 | + () -> StringUtils.abbreviate("abcdefghij", "::", 0, 2), |
| 154 | + "StringUtils.abbreviate expecting IllegalArgumentException"); |
| 155 | + assertThrows( |
| 156 | + IllegalArgumentException.class, |
| 157 | + () -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6), |
| 158 | + "StringUtils.abbreviate expecting IllegalArgumentException"); |
| 159 | + |
| 160 | + final String raspberry = "raspberry peach"; |
| 161 | + assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15)); |
| 162 | + |
| 163 | + assertNull(StringUtils.abbreviate(null, ";", 7, 14)); |
| 164 | + assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh;;", ";;", -1, 10); |
| 165 | + assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi.", ".", 0, 10); |
| 166 | + assertAbbreviateWithAbbrevMarkerAndOffset("abcdefgh++", "++", 1, 10); |
| 167 | + assertAbbreviateWithAbbrevMarkerAndOffset("abcdefghi*", "*", 2, 10); |
| 168 | + assertAbbreviateWithAbbrevMarkerAndOffset("abcdef{{{{", "{{{{", 4, 10); |
| 169 | + assertAbbreviateWithAbbrevMarkerAndOffset("abcdef____", "____", 5, 10); |
| 170 | + assertAbbreviateWithAbbrevMarkerAndOffset("==fghijk==", "==", 5, 10); |
| 171 | + assertAbbreviateWithAbbrevMarkerAndOffset("___ghij___", "___", 6, 10); |
| 172 | + assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 7, 10); |
| 173 | + assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 8, 10); |
| 174 | + assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 9, 10); |
| 175 | + assertAbbreviateWithAbbrevMarkerAndOffset("///ijklmno", "///", 10, 10); |
| 176 | + assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 10, 10); |
| 177 | + assertAbbreviateWithAbbrevMarkerAndOffset("//hijklmno", "//", 11, 10); |
| 178 | + assertAbbreviateWithAbbrevMarkerAndOffset("...ijklmno", "...", 12, 10); |
| 179 | + assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 13, 10); |
| 180 | + assertAbbreviateWithAbbrevMarkerAndOffset("/ghijklmno", "/", 14, 10); |
| 181 | + assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10); |
| 182 | + assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10); |
| 183 | + assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", Integer.MAX_VALUE, 10); |
| 184 | + } |
| 185 | + |
| 186 | + //Fixed LANG-1463 |
| 187 | + @Test |
| 188 | + public void testAbbreviateMarkerWithEmptyString() { |
| 189 | + final String greaterThanMaxTest = "much too long text"; |
| 190 | + assertEquals("much too long", StringUtils.abbreviate(greaterThanMaxTest, "", 13)); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testAbbreviateMiddle() { |
| 195 | + // javadoc examples |
| 196 | + assertNull(StringUtils.abbreviateMiddle(null, null, 0)); |
| 197 | + assertEquals("abc", StringUtils.abbreviateMiddle("abc", null, 0)); |
| 198 | + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 0)); |
| 199 | + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 3)); |
| 200 | + assertEquals("ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4)); |
| 201 | + |
| 202 | + // 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 | + |
| 209 | + // Test a much longer text :) |
| 210 | + final String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text"; |
| 211 | + assertEquals( |
| 212 | + "Start text->Close text", |
| 213 | + StringUtils.abbreviateMiddle(longText, "->", 22)); |
| 214 | + |
| 215 | + // Test negative length |
| 216 | + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1)); |
| 217 | + |
| 218 | + // Test boundaries |
| 219 | + // Fails to change anything as method ensures first and last char are kept |
| 220 | + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1)); |
| 221 | + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2)); |
| 222 | + |
| 223 | + // Test length of n=1 |
| 224 | + assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1)); |
| 225 | + |
| 226 | + // Test smallest length that can lead to success |
| 227 | + assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3)); |
| 228 | + |
| 229 | + // More from LANG-405 |
| 230 | + assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4)); |
| 231 | + assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5)); |
| 232 | + } |
| 233 | +} |
0 commit comments