1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.binary;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.junit.jupiter.api.Assertions.fail;
23
24 import org.junit.jupiter.api.Test;
25
26
27
28
29
30
31
32
33
34 public class CharSequenceUtilsTest {
35
36 private abstract static class RunTest {
37
38 abstract boolean invoke();
39
40 void run(final TestData data, final String id) {
41 if (data.throwable != null) {
42 final String msg = id + " Expected " + data.throwable;
43 try {
44 invoke();
45 fail(msg + " but nothing was thrown.");
46 } catch (final Exception ex) {
47 assertTrue(data.throwable.isAssignableFrom(ex.getClass()),
48 msg + " but was " + ex.getClass().getSimpleName());
49 }
50 } else {
51 final boolean stringCheck = invoke();
52 assertEquals(data.expected, stringCheck, id + " Failed test " + data);
53 }
54 }
55
56 }
57
58
59
60
61 static class TestData {
62
63 final String source;
64 final boolean ignoreCase;
65 final int toffset;
66 final String other;
67 final int ooffset;
68 final int len;
69 final boolean expected;
70 final Class<? extends Throwable> throwable;
71
72 TestData(final String source, final boolean ignoreCase, final int toffset, final String other, final int ooffset, final int len,
73 final boolean expected) {
74 this.source = source;
75 this.ignoreCase = ignoreCase;
76 this.toffset = toffset;
77 this.other = other;
78 this.ooffset = ooffset;
79 this.len = len;
80 this.expected = expected;
81 this.throwable = null;
82 }
83
84 TestData(final String source, final boolean ignoreCase, final int toffset, final String other, final int ooffset, final int len,
85 final Class<? extends Throwable> throwable) {
86 this.source = source;
87 this.ignoreCase = ignoreCase;
88 this.toffset = toffset;
89 this.other = other;
90 this.ooffset = ooffset;
91 this.len = len;
92 this.expected = false;
93 this.throwable = throwable;
94 }
95
96 @Override
97 public String toString() {
98 final StringBuilder sb = new StringBuilder();
99 sb.append(source).append("[").append(toffset).append("]");
100 sb.append(ignoreCase ? " caseblind " : " samecase ");
101 sb.append(other).append("[").append(ooffset).append("]");
102 sb.append(" ").append(len).append(" => ");
103 if (throwable != null) {
104 sb.append(throwable);
105 } else {
106 sb.append(expected);
107 }
108 return sb.toString();
109 }
110 }
111
112 private static final TestData[] TEST_DATA = {
113
114
115
116 new TestData("a", true, 0, "abc", 0, 0, true),
117 new TestData("a", true, 0, "abc", 0, 1, true),
118
119
120
121 new TestData("Abc", true, 0, "abc", 0, 3, true),
122 new TestData("Abc", false, 0, "abc", 0, 3, false),
123 new TestData("Abc", true, 1, "abc", 1, 2, true),
124 new TestData("Abc", false, 1, "abc", 1, 2, true),
125 new TestData("Abcd", true, 1, "abcD", 1, 2, true),
126 new TestData("Abcd", false, 1, "abcD", 1, 2, true),
127 };
128
129
130
131
132
133 @SuppressWarnings("unused")
134 @Test
135 public void testConstructor() {
136 new CharSequenceUtils();
137 }
138
139 @Test
140 public void testRegionMatches() {
141 for (final TestData data : TEST_DATA) {
142 new RunTest() {
143 @Override
144 boolean invoke() {
145 return data.source.regionMatches(data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
146 }
147 }.run(data, "String");
148 new RunTest() {
149 @Override
150 boolean invoke() {
151 return CharSequenceUtils.regionMatches(data.source, data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
152 }
153 }.run(data, "CSString");
154 new RunTest() {
155 @Override
156 boolean invoke() {
157 return CharSequenceUtils.regionMatches(new StringBuilder(data.source), data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
158 }
159 }.run(data, "CSNonString");
160 }
161 }
162 }