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    *      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.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 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      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      // Note: The commented out tests fail due to the CharSequenceUtils method
59      // being based on Lang 3.3.2 and the tests are from 3.11.
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             //          Source  IgnoreCase Offset Other  Offset Length Result
114             //new TestData("",    true,      -1,    "",    -1,    -1,    false),
115             //new TestData("",    true,      0,     "",    0,     1,     false),
116             new TestData("a",   true,      0,     "abc", 0,     0,     true),
117             new TestData("a",   true,      0,     "abc", 0,     1,     true),
118             //new TestData("a",   true,      0,     null,  0,     0,     NullPointerException.class),
119             //new TestData(null,  true,      0,     null,  0,     0,     NullPointerException.class),
120             //new TestData(null,  true,      0,     "",    0,     0,     NullPointerException.class),
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      * Test the constructor exists. This is here for code coverage. The class ideally should
131      * be package private, marked as final and have a private constructor to prevent instances.
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 }