Skip to content

Commit ff46e31

Browse files
committed
Fix Checkstyle
Cover a few lines/branches Add some more tests to cover a bit more. Also apply some IDE-suggestions, assert-order, simplify assertions.
1 parent 5a6af9e commit ff46e31

11 files changed

+159
-7
lines changed

src/test/java/org/apache/commons/text/StringSubstitutorTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void setUp() throws Exception {
193193
}
194194

195195
@AfterEach
196-
public void tearDown() throws Exception {
196+
public void tearDown() {
197197
values = null;
198198
}
199199

@@ -1085,4 +1085,11 @@ void testSubstitutePreserveEscape() throws IOException {
10851085
assertEqualsCharSeq("value $${escaped}", replace(sub, org));
10861086
}
10871087

1088+
@Test
1089+
void testToString() {
1090+
final StringSubstitutor s = new StringSubstitutor(null, "prefix", "suffix");
1091+
String str = s.toString();
1092+
assertTrue(str.contains("\"prefix\""),
1093+
"Had: " + str);
1094+
}
10881095
}

src/test/java/org/apache/commons/text/TextStringBuilderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,13 @@ void testConstructorCharSequence() {
550550
assertEquals(length, sb.toCharArray().length);
551551
}
552552

553+
@Test
554+
void testConstructorCharSequenceNull() {
555+
final TextStringBuilder sb = new TextStringBuilder((CharSequence) null);
556+
assertEquals(TextStringBuilder.CAPACITY, sb.capacity());
557+
assertEquals(0, sb.toCharArray().length);
558+
}
559+
553560
@Test
554561
void testConstructorDefault() {
555562
final TextStringBuilder sb = new TextStringBuilder();

src/test/java/org/apache/commons/text/WordUtilsTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,16 @@ void testConstructor() {
177177

178178
@Test
179179
void testContainsAllWords_StringString() {
180-
assertFalse(WordUtils.containsAllWords(null, (String) null));
180+
assertFalse(WordUtils.containsAllWords(null));
181181
assertFalse(WordUtils.containsAllWords(null, ""));
182182
assertFalse(WordUtils.containsAllWords(null, "ab"));
183183

184+
assertFalse(WordUtils.containsAllWords(""));
184185
assertFalse(WordUtils.containsAllWords("", (String) null));
185186
assertFalse(WordUtils.containsAllWords("", ""));
186187
assertFalse(WordUtils.containsAllWords("", "ab"));
187188

189+
assertFalse(WordUtils.containsAllWords("foo"));
188190
assertFalse(WordUtils.containsAllWords("foo", (String) null));
189191
assertFalse(WordUtils.containsAllWords("bar", ""));
190192
assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by"));
@@ -321,6 +323,32 @@ void testInitialsSurrogatePairs() {
321323
new char[] { '\uD800', '\uDF14', '\uD800', '\uDF18' }));
322324
}
323325

326+
@Deprecated
327+
@Test
328+
void testIsDelimiter() {
329+
assertFalse(WordUtils.isDelimiter('.', null));
330+
assertTrue(WordUtils.isDelimiter(' ', null));
331+
332+
assertFalse(WordUtils.isDelimiter(' ', new char[] { '.' }));
333+
assertTrue(WordUtils.isDelimiter('.', new char[] { '.' }));
334+
335+
assertFalse(WordUtils.isDelimiter(' ', new char[] { '.', '_', 'a' }));
336+
assertTrue(WordUtils.isDelimiter('.', new char[] { '.', '_', 'a', '.' }));
337+
}
338+
339+
@Deprecated
340+
@Test
341+
void testIsDelimiterCodePoint() {
342+
assertFalse(WordUtils.isDelimiter((int) '.', null));
343+
assertTrue(WordUtils.isDelimiter((int) ' ', null));
344+
345+
assertFalse(WordUtils.isDelimiter((int) ' ', new char[] { '.' }));
346+
assertTrue(WordUtils.isDelimiter((int) '.', new char[] { '.' }));
347+
348+
assertFalse(WordUtils.isDelimiter((int) ' ', new char[] { '.', '_', 'a' }));
349+
assertTrue(WordUtils.isDelimiter((int) '.', new char[] { '.', '_', 'a', '.' }));
350+
}
351+
324352
@Test
325353
void testLANG1292() {
326354
// Prior to fix, this was throwing StringIndexOutOfBoundsException

src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ void testSingletons() {
257257
assertSame(XmlEncoderStringLookup.INSTANCE, stringLookupFactory.xmlEncoderStringLookup());
258258
}
259259

260+
/**
261+
* Tests that we return the singleton.
262+
*/
263+
@Deprecated
264+
@Test
265+
void testDeprecatedSingletons() {
266+
final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE;
267+
assertSame(StringLookupFactory.INSTANCE_BASE64_DECODER, stringLookupFactory.base64StringLookup());
268+
}
269+
260270
@Test
261271
void testXmlStringLookup() {
262272
final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE;
@@ -279,4 +289,9 @@ void testXmlStringLookupExternalEntityOn() {
279289
assertEquals(XmlStringLookupTest.DATA, StringLookupFactory.INSTANCE.xmlStringLookup(XmlStringLookupTest.EMPTY_MAP).apply(key).trim());
280290
}
281291

292+
@Test
293+
void testClear() {
294+
// this will clear out the global cache in ConstantStringLookup
295+
StringLookupFactory.clear();
296+
}
282297
}

src/test/java/org/apache/commons/text/similarity/IntersectionResultTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void testEquals() {
3636
};
3737

3838
// Test a different instance with same values
39-
Assertions.assertEquals(results[0], new IntersectionResult(0, 0, 0));
39+
Assertions.assertEquals(new IntersectionResult(0, 0, 0), results[0]);
4040

4141
final Object something = new Object();
4242
for (int i = 0; i < results.length; i++) {
@@ -45,6 +45,15 @@ void testEquals() {
4545
for (int j = 0; j < results.length; j++) {
4646
Assertions.assertEquals(results[i].equals(results[j]), i == j);
4747
}
48+
49+
// IntelliJ would like to optimize this, but it would make
50+
// the test useless as assertNotEquals() handles null itself
51+
//noinspection ConstantValue,SimplifiableAssertion
52+
Assertions.assertFalse(results[i].equals(null),
53+
"Should not be Equal to null");
54+
//noinspection AssertBetweenInconvertibleTypes
55+
Assertions.assertNotEquals("Test", results[i],
56+
"Should not be Equal to a different type of object");
4857
}
4958
}
5059

src/test/java/org/apache/commons/text/similarity/LevenshteinResultsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ void testEqualsReturningFalse() {
4040
assertFalse(levenshteinResults.equals(levenshteinResultsTwo));
4141
}
4242

43+
@Test
44+
void testEqualsDifferentDistance() {
45+
final Integer integerOne = 1662;
46+
final Integer integerTwo = 1164;
47+
final LevenshteinResults levenshteinResults = new LevenshteinResults(integerOne, integerOne, integerOne, integerOne);
48+
final LevenshteinResults levenshteinResultsTwo = new LevenshteinResults(integerTwo, integerOne, integerOne, integerOne);
49+
assertFalse(levenshteinResults.equals(levenshteinResultsTwo));
50+
}
51+
4352
@Test
4453
void testEqualsSameObject() {
4554
final Integer integer = 1662;
@@ -62,4 +71,12 @@ void testEqualsWithNull() {
6271
assertFalse(levenshteinResults.equals(null));
6372
}
6473

74+
@Test
75+
void testEqualsWithDifferentObject() {
76+
final Integer integer = -647;
77+
final LevenshteinResults levenshteinResults = new LevenshteinResults(integer, null, null, integer);
78+
//noinspection EqualsBetweenInconvertibleTypes
79+
assertFalse(levenshteinResults.equals("Test"));
80+
}
81+
6582
}

src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void testLogestCommonSubsequence() {
8989
assertEquals("", subject.logestCommonSubsequence("", ""));
9090
assertEquals("", subject.logestCommonSubsequence("left", ""));
9191
assertEquals("", subject.logestCommonSubsequence("", "right"));
92+
assertEquals("", subject.logestCommonSubsequence("l", "a"));
93+
assertEquals("", subject.logestCommonSubsequence("left", "a"));
9294
assertEquals("fog", subject.logestCommonSubsequence("frog", "fog"));
9395
assertEquals("", subject.logestCommonSubsequence("fly", "ant"));
9496
assertEquals("h", subject.logestCommonSubsequence("elephant", "hippo"));
@@ -106,6 +108,8 @@ void testLongestCommonSubsequence() {
106108
assertEquals("", subject.longestCommonSubsequence("", ""));
107109
assertEquals("", subject.longestCommonSubsequence("left", ""));
108110
assertEquals("", subject.longestCommonSubsequence("", "right"));
111+
assertEquals("", subject.longestCommonSubsequence("l", "a"));
112+
assertEquals("", subject.longestCommonSubsequence("left", "a"));
109113
assertEquals("fog", subject.longestCommonSubsequence("frog", "fog"));
110114
assertEquals("", subject.longestCommonSubsequence("fly", "ant"));
111115
assertEquals("h", subject.longestCommonSubsequence("elephant", "hippo"));
@@ -136,6 +140,7 @@ void testLongestCommonSubsequenceApply() {
136140
}
137141

138142
@Test
143+
@Deprecated
139144
void testLongestCommonSubstringLengthArray() {
140145
assertArrayEquals(new int[][]{ {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 1, 2, 2}}, subject.longestCommonSubstringLengthArray("ab", "abc"));
141146
}

src/test/java/org/apache/commons/text/translate/CharSequenceTranslatorTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.apache.commons.text.translate;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

2223
import java.io.IOException;
24+
import java.io.UncheckedIOException;
2325
import java.io.Writer;
2426

2527
import org.junit.jupiter.api.Test;
@@ -46,8 +48,20 @@ void testWith() throws IOException {
4648
final CharSequenceTranslator charSequenceTranslatorThree = new TestCharSequenceTranslator();
4749
final CharSequenceTranslator aggregatedTranslator = charSequenceTranslatorOne.with(charSequenceTranslatorTwo, charSequenceTranslatorThree);
4850
aggregatedTranslator.translate("", 0, null);
49-
assertTrue(aggregatedTranslator instanceof AggregateTranslator);
51+
assertInstanceOf(AggregateTranslator.class, aggregatedTranslator);
5052
assertEquals(3, translateInvocationCounter);
5153
}
5254

55+
@Test
56+
void testIOException() {
57+
final CharSequenceTranslator translator = new CharSequenceTranslator() {
58+
@Override
59+
public int translate(CharSequence input, int index, Writer writer) throws IOException {
60+
throw new IOException("Test exception");
61+
}
62+
};
63+
64+
assertThrows(UncheckedIOException.class,
65+
() -> translator.translate("."));
66+
}
5367
}

src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ void testBetween() {
3838
result = oue.translate(input);
3939
assertEquals("\377", result, "Failed to unescape octal characters via the between method");
4040

41+
input = "\\777";
42+
result = oue.translate(input);
43+
assertEquals("\777", result, "Failed to unescape octal characters via the between method");
44+
4145
input = "\\377 and";
4246
result = oue.translate(input);
4347
assertEquals("\377 and", result, "Failed to unescape octal characters via the between method");
@@ -79,4 +83,33 @@ void testBetween() {
7983
assertEquals("\\999", result, "Failed to ignore an out of range octal character via the between method");
8084
}
8185

86+
@Test
87+
void testInvalid() {
88+
final OctalUnescaper oue = new OctalUnescaper();
89+
90+
String input = "\\4a";
91+
String result = oue.translate(input);
92+
assertEquals("\4a", result, "Failed to unescape octal characters via the between method");
93+
}
94+
95+
@Test
96+
void testHighLowSurrogate() {
97+
final OctalUnescaper oue = new OctalUnescaper();
98+
99+
String input = "\\377\uD800and";
100+
String result = oue.translate(input);
101+
assertEquals("\377\uD800and", result, "Failed to unescape octal characters via the between method");
102+
103+
input = "\\377\uD83D\uDE80and";
104+
result = oue.translate(input);
105+
assertEquals("\377\uD83D\uDE80and", result, "Failed to unescape octal characters via the between method");
106+
107+
input = "\\377\uD83D\uDC00and";
108+
result = oue.translate(input);
109+
assertEquals("\377\uD83D\uDC00and", result, "Failed to unescape octal characters via the between method");
110+
111+
input = "\\377\uD83D";
112+
result = oue.translate(input);
113+
assertEquals("\377\uD83D", result, "Failed to unescape octal characters via the between method");
114+
}
82115
}

src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
2121

22-
import java.io.IOException;
2322
import java.io.StringWriter;
2423
import java.io.Writer;
2524

@@ -34,7 +33,7 @@ class SinglePassTranslatorTest {
3433
private final SinglePassTranslator dummyTranslator = new SinglePassTranslator() {
3534

3635
@Override
37-
void translateWhole(final CharSequence input, final Writer writer) throws IOException {
36+
void translateWhole(final CharSequence input, final Writer writer) {
3837
// noop
3938
}
4039
};
@@ -63,4 +62,15 @@ void testTranslateThrowsIllegalArgumentException() {
6362
assertThrows(IllegalArgumentException.class, () -> dummyTranslator.translate("(,Fk", 647, null));
6463
}
6564

65+
@Test
66+
void testTranslateThrowsIllegalArgumentExceptionWithNonAnonymousClass() {
67+
assertThrows(IllegalArgumentException.class, () -> new TestTranslator().translate("(,Fk", 647, null));
68+
}
69+
70+
private final static class TestTranslator extends SinglePassTranslator {
71+
@Override
72+
void translateWhole(final CharSequence input, final Writer writer) {
73+
// noop
74+
}
75+
}
6676
}

0 commit comments

Comments
 (0)