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