View Javadoc
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    *      https://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.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   * Tests {@link CharSequenceUtils}.
28   *
29   * <p>
30   * Tests copied from Apache Commons Lang 3.11. The implementation in codec is based on
31   * an earlier version of Lang and some tests fail. The CharSequenceUtils class is public but
32   * the method is package private. The failing tests have been commented out and the
33   * implementation left unchanged.
34   * </p>
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      // Note: The commented out tests fail due to the CharSequenceUtils method
61      // being based on Lang 3.3.2 and the tests are from 3.11.
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             //          Source  IgnoreCase Offset Other  Offset Length Result
116             //new TestData("",    true,      -1,    "",    -1,    -1,    false),
117             //new TestData("",    true,      0,     "",    0,     1,     false),
118             new TestData("a",   true,      0,     "abc", 0,     0,     true),
119             new TestData("a",   true,      0,     "abc", 0,     1,     true),
120             //new TestData("a",   true,      0,     null,  0,     0,     NullPointerException.class),
121             //new TestData(null,  true,      0,     null,  0,     0,     NullPointerException.class),
122             //new TestData(null,  true,      0,     "",    0,     0,     NullPointerException.class),
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      * Test the constructor exists. This is here for code coverage. The class ideally should
133      * be package private, marked as final and have a private constructor to prevent instances.
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 }