You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/08/27 23:19:02 UTC

[commons-codec] 02/08: Add CharSequenceUtilsTest

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 4c542d7c7a2f9072803d42bcde9748bd36a80ffe
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu Aug 27 22:50:57 2020 +0100

    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.
---
 .../commons/codec/binary/CharSequenceUtils.java    |   3 +
 .../codec/binary/CharSequenceUtilsTest.java        | 158 +++++++++++++++++++++
 2 files changed, 161 insertions(+)

diff --git a/src/main/java/org/apache/commons/codec/binary/CharSequenceUtils.java b/src/main/java/org/apache/commons/codec/binary/CharSequenceUtils.java
index c3f37e8..7cc9acd 100644
--- a/src/main/java/org/apache/commons/codec/binary/CharSequenceUtils.java
+++ b/src/main/java/org/apache/commons/codec/binary/CharSequenceUtils.java
@@ -31,6 +31,9 @@ public class CharSequenceUtils {
 
     /**
      * Green implementation of regionMatches.
+     * 
+     * <p>Note: This function differs from the current implementation in Apache Commons Lang
+     * where the input indices are not valid. It is only used within this package.
      *
      * @param cs
      *            the {@code CharSequence} to be processed
diff --git a/src/test/java/org/apache/commons/codec/binary/CharSequenceUtilsTest.java b/src/test/java/org/apache/commons/codec/binary/CharSequenceUtilsTest.java
new file mode 100644
index 0000000..ada5865
--- /dev/null
+++ b/src/test/java/org/apache/commons/codec/binary/CharSequenceUtilsTest.java
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.binary;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests {@link org.apache.commons.codec.binary.CharSequenceUtils}.
+ *
+ * <p>Tests copied from Apache Commons Lang 3.11. The implementation in codec is based on
+ * an earlier version of Lang and some tests fail. The CharSequenceUtils class is public but 
+ * the method is package private. The failing tests have been commented out and the 
+ * implementation left unchanged.
+ */
+public class CharSequenceUtilsTest {
+
+    static class TestData{
+        final String source;
+        final boolean ignoreCase;
+        final int toffset;
+        final String other;
+        final int ooffset;
+        final int len;
+        final boolean expected;
+        final Class<? extends Throwable> throwable;
+        TestData(final String source, final boolean ignoreCase, final int toffset,
+                final String other, final int ooffset, final int len, final boolean expected) {
+            this.source = source;
+            this.ignoreCase = ignoreCase;
+            this.toffset = toffset;
+            this.other = other;
+            this.ooffset = ooffset;
+            this.len = len;
+            this.expected = expected;
+            this.throwable = null;
+        }
+        TestData(final String source, final boolean ignoreCase, final int toffset,
+                final String other, final int ooffset, final int len, final Class<? extends Throwable> throwable) {
+            this.source = source;
+            this.ignoreCase = ignoreCase;
+            this.toffset = toffset;
+            this.other = other;
+            this.ooffset = ooffset;
+            this.len = len;
+            this.expected = false;
+            this.throwable = throwable;
+        }
+        @Override
+        public String toString() {
+            final StringBuilder sb = new StringBuilder();
+            sb.append(source).append("[").append(toffset).append("]");
+            sb.append(ignoreCase? " caseblind ":" samecase ");
+            sb.append(other).append("[").append(ooffset).append("]");
+            sb.append(" ").append(len).append(" => ");
+            if (throwable != null) {
+                sb.append(throwable);
+            } else {
+                sb.append(expected);
+            }
+            return sb.toString();
+        }
+    }
+
+    // Note: The commented out tests fail due to the CharSequenceUtils method
+    // being based on Lang 3.3.2 and the tests are from 3.11.
+
+    private static final TestData[] TEST_DATA = {
+            //          Source  IgnoreCase Offset Other  Offset Length Result
+            //new TestData("",    true,      -1,    "",    -1,    -1,    false),
+            //new TestData("",    true,      0,     "",    0,     1,     false),
+            new TestData("a",   true,      0,     "abc", 0,     0,     true),
+            new TestData("a",   true,      0,     "abc", 0,     1,     true),
+            //new TestData("a",   true,      0,     null,  0,     0,     NullPointerException.class),
+            //new TestData(null,  true,      0,     null,  0,     0,     NullPointerException.class),
+            //new TestData(null,  true,      0,     "",    0,     0,     NullPointerException.class),
+            new TestData("Abc", true,      0,     "abc", 0,     3,     true),
+            new TestData("Abc", false,     0,     "abc", 0,     3,     false),
+            new TestData("Abc", true,      1,     "abc", 1,     2,     true),
+            new TestData("Abc", false,     1,     "abc", 1,     2,     true),
+            new TestData("Abcd", true,      1,     "abcD", 1,     2,     true),
+            new TestData("Abcd", false,     1,     "abcD", 1,     2,     true),
+    };
+
+    private abstract static class RunTest {
+
+        abstract boolean invoke();
+
+        void run(final TestData data, final String id) {
+            if (data.throwable != null) {
+                final String msg = id + " Expected " + data.throwable;
+                try {
+                    invoke();
+                    Assert.fail(msg + " but nothing was thrown.");
+                } catch (Exception ex) {
+                    assertTrue(msg + " but was " + ex.getClass().getSimpleName(), 
+                            data.throwable.isAssignableFrom(ex.getClass()));
+                }
+            } else {
+                final boolean stringCheck = invoke();
+                assertEquals(id + " Failed test " + data, data.expected, stringCheck);
+            }
+        }
+
+    }
+
+    @Test
+    public void testRegionMatches() {
+        for (final TestData data : TEST_DATA) {
+            new RunTest() {
+                @Override
+                boolean invoke() {
+                    return data.source.regionMatches(data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
+                }
+            }.run(data, "String");
+            new RunTest() {
+                @Override
+                boolean invoke() {
+                    return CharSequenceUtils.regionMatches(data.source, data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
+                }
+            }.run(data, "CSString");
+            new RunTest() {
+                @Override
+                boolean invoke() {
+                    return CharSequenceUtils.regionMatches(new StringBuilder(data.source), data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
+                }
+            }.run(data, "CSNonString");
+        }
+    }
+
+    /**
+     * Test the constructor exists. This is here for code coverage. The class ideally should
+     * be package private, marked as final and have a private constructor to prevent instances.
+     */
+    @SuppressWarnings("unused")
+    @Test
+    public void testConstructor() {
+        new CharSequenceUtils();
+    }
+}