Skip to content

Commit 4c542d7

Browse files
committed
Add CharSequenceUtilsTest
The tests are ported from commons lang. The CharSequenceUtils class is based on an old version and some tests fail. The method is package private and the cases that fail are not exercised in the codec code.
1 parent fab180b commit 4c542d7

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

src/main/java/org/apache/commons/codec/binary/CharSequenceUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class CharSequenceUtils {
3131

3232
/**
3333
* Green implementation of regionMatches.
34+
*
35+
* <p>Note: This function differs from the current implementation in Apache Commons Lang
36+
* where the input indices are not valid. It is only used within this package.
3437
*
3538
* @param cs
3639
* the {@code CharSequence} to be processed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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.codec.binary;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
/**
27+
* Tests {@link org.apache.commons.codec.binary.CharSequenceUtils}.
28+
*
29+
* <p>Tests copied from Apache Commons Lang 3.11. The implementation in codec is based on
30+
* an earlier version of Lang and some tests fail. The CharSequenceUtils class is public but
31+
* the method is package private. The failing tests have been commented out and the
32+
* implementation left unchanged.
33+
*/
34+
public class CharSequenceUtilsTest {
35+
36+
static class TestData{
37+
final String source;
38+
final boolean ignoreCase;
39+
final int toffset;
40+
final String other;
41+
final int ooffset;
42+
final int len;
43+
final boolean expected;
44+
final Class<? extends Throwable> throwable;
45+
TestData(final String source, final boolean ignoreCase, final int toffset,
46+
final String other, final int ooffset, final int len, final boolean expected) {
47+
this.source = source;
48+
this.ignoreCase = ignoreCase;
49+
this.toffset = toffset;
50+
this.other = other;
51+
this.ooffset = ooffset;
52+
this.len = len;
53+
this.expected = expected;
54+
this.throwable = null;
55+
}
56+
TestData(final String source, final boolean ignoreCase, final int toffset,
57+
final String other, final int ooffset, final int len, final Class<? extends Throwable> throwable) {
58+
this.source = source;
59+
this.ignoreCase = ignoreCase;
60+
this.toffset = toffset;
61+
this.other = other;
62+
this.ooffset = ooffset;
63+
this.len = len;
64+
this.expected = false;
65+
this.throwable = throwable;
66+
}
67+
@Override
68+
public String toString() {
69+
final StringBuilder sb = new StringBuilder();
70+
sb.append(source).append("[").append(toffset).append("]");
71+
sb.append(ignoreCase? " caseblind ":" samecase ");
72+
sb.append(other).append("[").append(ooffset).append("]");
73+
sb.append(" ").append(len).append(" => ");
74+
if (throwable != null) {
75+
sb.append(throwable);
76+
} else {
77+
sb.append(expected);
78+
}
79+
return sb.toString();
80+
}
81+
}
82+
83+
// Note: The commented out tests fail due to the CharSequenceUtils method
84+
// being based on Lang 3.3.2 and the tests are from 3.11.
85+
86+
private static final TestData[] TEST_DATA = {
87+
// Source IgnoreCase Offset Other Offset Length Result
88+
//new TestData("", true, -1, "", -1, -1, false),
89+
//new TestData("", true, 0, "", 0, 1, false),
90+
new TestData("a", true, 0, "abc", 0, 0, true),
91+
new TestData("a", true, 0, "abc", 0, 1, true),
92+
//new TestData("a", true, 0, null, 0, 0, NullPointerException.class),
93+
//new TestData(null, true, 0, null, 0, 0, NullPointerException.class),
94+
//new TestData(null, true, 0, "", 0, 0, NullPointerException.class),
95+
new TestData("Abc", true, 0, "abc", 0, 3, true),
96+
new TestData("Abc", false, 0, "abc", 0, 3, false),
97+
new TestData("Abc", true, 1, "abc", 1, 2, true),
98+
new TestData("Abc", false, 1, "abc", 1, 2, true),
99+
new TestData("Abcd", true, 1, "abcD", 1, 2, true),
100+
new TestData("Abcd", false, 1, "abcD", 1, 2, true),
101+
};
102+
103+
private abstract static class RunTest {
104+
105+
abstract boolean invoke();
106+
107+
void run(final TestData data, final String id) {
108+
if (data.throwable != null) {
109+
final String msg = id + " Expected " + data.throwable;
110+
try {
111+
invoke();
112+
Assert.fail(msg + " but nothing was thrown.");
113+
} catch (Exception ex) {
114+
assertTrue(msg + " but was " + ex.getClass().getSimpleName(),
115+
data.throwable.isAssignableFrom(ex.getClass()));
116+
}
117+
} else {
118+
final boolean stringCheck = invoke();
119+
assertEquals(id + " Failed test " + data, data.expected, stringCheck);
120+
}
121+
}
122+
123+
}
124+
125+
@Test
126+
public void testRegionMatches() {
127+
for (final TestData data : TEST_DATA) {
128+
new RunTest() {
129+
@Override
130+
boolean invoke() {
131+
return data.source.regionMatches(data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
132+
}
133+
}.run(data, "String");
134+
new RunTest() {
135+
@Override
136+
boolean invoke() {
137+
return CharSequenceUtils.regionMatches(data.source, data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
138+
}
139+
}.run(data, "CSString");
140+
new RunTest() {
141+
@Override
142+
boolean invoke() {
143+
return CharSequenceUtils.regionMatches(new StringBuilder(data.source), data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
144+
}
145+
}.run(data, "CSNonString");
146+
}
147+
}
148+
149+
/**
150+
* Test the constructor exists. This is here for code coverage. The class ideally should
151+
* be package private, marked as final and have a private constructor to prevent instances.
152+
*/
153+
@SuppressWarnings("unused")
154+
@Test
155+
public void testConstructor() {
156+
new CharSequenceUtils();
157+
}
158+
}

0 commit comments

Comments
 (0)