Repository: commons-text Updated Branches: refs/heads/master 348aa51c1 -> c7cf533d2
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java new file mode 100644 index 0000000..8019599 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java @@ -0,0 +1,99 @@ +/* + * 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.text.similarity; + +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link LongestCommonSubsequence}. + */ +public class LongestCommonSubsequenceTest { + + private static LongestCommonSubsequence subject; + + @BeforeClass + public static void setup() { + subject = new LongestCommonSubsequence(); + } + + @Test + public void testLongestCommonSubsequenceApply() { + assertEquals(Integer.valueOf(0), subject.apply("", "")); + assertEquals(Integer.valueOf(0), subject.apply("left", "")); + assertEquals(Integer.valueOf(0), subject.apply("", "right")); + assertEquals(Integer.valueOf(3), subject.apply("frog", "fog")); + assertEquals(Integer.valueOf(0), subject.apply("fly", "ant")); + assertEquals(Integer.valueOf(1), subject.apply("elephant", "hippo")); + assertEquals(Integer.valueOf(8), subject.apply("ABC Corporation", "ABC Corp")); + assertEquals(Integer.valueOf(20), subject.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.")); + assertEquals(Integer.valueOf(24), subject.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness")); + assertEquals(Integer.valueOf(11), subject.apply("PENNSYLVANIA", "PENNCISYLVNIA")); + assertEquals(Integer.valueOf(1), subject.apply("left", "right")); + assertEquals(Integer.valueOf(4), subject.apply("leettteft", "ritttght")); + assertEquals(Integer.valueOf(15), subject.apply("the same string", "the same string")); + } + + @Test(expected = IllegalArgumentException.class) + public void testGettingLongestCommonSubsequenceApplyNullNull() throws Exception { + subject.apply(null, null); + } + + @Test(expected = IllegalArgumentException.class) + public void testGettingLongestCommonSubsequenceApplyStringNull() throws Exception { + subject.apply(" ", null); + } + + @Test(expected = IllegalArgumentException.class) + public void testGettingLongestCommonSubsequenceApplyNullString() throws Exception { + subject.apply(null, "right"); + } + + @Test + public void testLongestCommonSubsequence() { + assertEquals("", subject.logestCommonSubsequence("", "")); + assertEquals("", subject.logestCommonSubsequence("left", "")); + assertEquals("", subject.logestCommonSubsequence("", "right")); + assertEquals("fog", subject.logestCommonSubsequence("frog", "fog")); + assertEquals("", subject.logestCommonSubsequence("fly", "ant")); + assertEquals("h", subject.logestCommonSubsequence("elephant", "hippo")); + assertEquals("ABC Corp", subject.logestCommonSubsequence("ABC Corporation", "ABC Corp")); + assertEquals("D H Enterprises Inc", subject.logestCommonSubsequence("D N H Enterprises Inc", "D & H Enterprises, Inc.")); + assertEquals("My Gym Childrens Fitness", subject.logestCommonSubsequence("My Gym Children's Fitness Center", "My Gym. Childrens Fitness")); + assertEquals("PENNSYLVNIA", subject.logestCommonSubsequence("PENNSYLVANIA", "PENNCISYLVNIA")); + assertEquals("t", subject.logestCommonSubsequence("left", "right")); + assertEquals("tttt", subject.logestCommonSubsequence("leettteft", "ritttght")); + assertEquals("the same string", subject.logestCommonSubsequence("the same string", "the same string")); + } + + @Test(expected = IllegalArgumentException.class) + public void testGettingLongestCommonSubsequenceNullNull() throws Exception { + subject.logestCommonSubsequence(null, null); + } + + @Test(expected = IllegalArgumentException.class) + public void testGettingLongestCommonSubsequenceStringNull() throws Exception { + subject.logestCommonSubsequence(" ", null); + } + + @Test(expected = IllegalArgumentException.class) + public void testGettingLongestCommonSubsequenceNullString() throws Exception { + subject.logestCommonSubsequence(null, "right"); + } +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/similarity/ParameterizedEditDistanceFromTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/ParameterizedEditDistanceFromTest.java b/src/test/java/org/apache/commons/text/similarity/ParameterizedEditDistanceFromTest.java new file mode 100644 index 0000000..0201ca0 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/ParameterizedEditDistanceFromTest.java @@ -0,0 +1,90 @@ +/* + * 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.text.similarity; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Unit tests for {@link EditDistanceFrom}. + * + * @param <R> The {@link EditDistance} return type. + */ +@RunWith(Parameterized.class) +public class ParameterizedEditDistanceFromTest<R> { + + private final EditDistance<R> editDistance; + private final CharSequence left; + private final CharSequence right; + private final R distance; + + public ParameterizedEditDistanceFromTest( + final EditDistance<R> editDistance, + final CharSequence left, final CharSequence right, + final R distance) { + + this.editDistance = editDistance; + this.left = left; + this.right = right; + this.distance = distance; + } + + @Parameters + public static Iterable<Object[]> parameters() { + return Arrays.asList( new Object[][] { + + { new HammingDistance(), "Sam I am.", "Ham I am.", 1 }, + { new HammingDistance(), "Japtheth, Ham, Shem", "Japtheth, HAM, Shem", 2 }, + { new HammingDistance(), "Hamming", "Hamming", 0 }, + + { new LevenshteinDistance(), "Apache", "a patchy", 4 }, + { new LevenshteinDistance(), "go", "no go", 3 }, + { new LevenshteinDistance(), "go", "go", 0 }, + + { new LevenshteinDistance(4), "Apache", "a patchy", 4 }, + { new LevenshteinDistance(4), "go", "no go", 3 }, + { new LevenshteinDistance(0), "go", "go", 0 }, + + { + new EditDistance<Boolean>() { + @Override + public Boolean apply(final CharSequence left, final CharSequence right) { + return left == right || (left != null && left.equals(right)); + } + }, + "Bob's your uncle.", + "Every good boy does fine.", + false + } + + } ); + } + + @Test + public void test() { + final EditDistanceFrom<R> editDistanceFrom = new EditDistanceFrom<>(editDistance, left); + assertThat(editDistanceFrom.apply(right), equalTo(distance)); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/similarity/ParameterizedLevenshteinDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/ParameterizedLevenshteinDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/ParameterizedLevenshteinDistanceTest.java new file mode 100644 index 0000000..0ef18d0 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/ParameterizedLevenshteinDistanceTest.java @@ -0,0 +1,125 @@ +/* + * 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.text.similarity; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Unit tests for {@link LevenshteinDistance}. + */ +@RunWith(Parameterized.class) +public class ParameterizedLevenshteinDistanceTest { + + private final Integer distance; + private final CharSequence left; + private final CharSequence right; + private final Integer threshold; + + public ParameterizedLevenshteinDistanceTest( + final Integer threshold, + final CharSequence left, final CharSequence right, + final Integer distance) { + + this.threshold = threshold; + this.left = left; + this.right = right; + this.distance = distance; + } + + @Parameters + public static Iterable<Object[]> parameters() { + return Arrays.asList( new Object[][] { + + /* empty strings */ + { 0, "", "", 0 }, + { 8, "aaapppp", "", 7 }, + { 7, "aaapppp", "", 7 }, + { 6, "aaapppp", "", -1 }, + + /* unequal strings, zero threshold */ + { 0, "b", "a", -1 }, + { 0, "a", "b", -1 }, + + /* equal strings */ + { 0, "aa", "aa", 0 }, + { 2, "aa", "aa", 0 }, + + /* same length */ + { 2, "aaa", "bbb", -1 }, + { 3, "aaa", "bbb", 3 }, + + /* big stripe */ + { 10, "aaaaaa", "b", 6 }, + + /* distance less than threshold */ + { 8, "aaapppp", "b", 7 }, + { 4, "a", "bbb", 3 }, + + /* distance equal to threshold */ + { 7, "aaapppp", "b", 7 }, + { 3, "a", "bbb", 3 }, + + /* distance greater than threshold */ + { 2, "a", "bbb", -1 }, + { 2, "bbb", "a", -1 }, + { 6, "aaapppp", "b", -1 }, + + /* stripe runs off array, strings not similar */ + { 1, "a", "bbb", -1 }, + { 1, "bbb", "a", -1 }, + + /* stripe runs off array, strings are similar */ + { 1, "12345", "1234567", -1 }, + { 1, "1234567", "12345", -1 }, + + /* old getLevenshteinDistance test cases */ + { 1, "frog", "fog", 1 }, + { 3, "fly", "ant", 3 }, + { 7, "elephant", "hippo", 7 }, + { 6, "elephant", "hippo", -1 }, + { 7, "hippo", "elephant", 7 }, + { 6, "hippo", "elephant", -1 }, + { 8, "hippo", "zzzzzzzz", 8 }, + { 8, "zzzzzzzz", "hippo", 8 }, + { 1, "hello", "hallo", 1 }, + + { Integer.MAX_VALUE, "frog", "fog", 1 }, + { Integer.MAX_VALUE, "fly", "ant", 3 }, + { Integer.MAX_VALUE, "elephant", "hippo", 7 }, + { Integer.MAX_VALUE, "hippo", "elephant", 7 }, + { Integer.MAX_VALUE, "hippo", "zzzzzzzz", 8 }, + { Integer.MAX_VALUE, "zzzzzzzz", "hippo", 8 }, + { Integer.MAX_VALUE, "hello", "hallo", 1 } + + } ); + } + + @Test + public void test() { + final LevenshteinDistance metric = new LevenshteinDistance(threshold); + assertThat(metric.apply(left, right), equalTo(distance)); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/similarity/ParameterizedSimilarityScoreFromTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/ParameterizedSimilarityScoreFromTest.java b/src/test/java/org/apache/commons/text/similarity/ParameterizedSimilarityScoreFromTest.java new file mode 100644 index 0000000..654ae4e --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/ParameterizedSimilarityScoreFromTest.java @@ -0,0 +1,81 @@ +/* + * 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.text.similarity; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Arrays; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Unit tests for {@link SimilarityScoreFrom}. + * + * @param <R> The {@link SimilarityScore} return type. + */ +@RunWith(Parameterized.class) +public class ParameterizedSimilarityScoreFromTest<R> { + + private final SimilarityScore<R> similarityScore; + private final CharSequence left; + private final CharSequence right; + private final R distance; + + public ParameterizedSimilarityScoreFromTest( + final SimilarityScore<R> similarityScore, + final CharSequence left, final CharSequence right, + final R distance) { + + this.similarityScore = similarityScore; + this.left = left; + this.right = right; + this.distance = distance; + } + + @Parameters + public static Iterable<Object[]> parameters() { + return Arrays.asList( new Object[][] { + + { new JaroWinklerDistance(), "elephant", "hippo", 0.44 }, + { new JaroWinklerDistance(), "hippo", "elephant", 0.44 }, + { new JaroWinklerDistance(), "hippo", "zzzzzzzz", 0.0 }, + + { + new SimilarityScore<Boolean>() { + @Override + public Boolean apply(final CharSequence left, final CharSequence right) { + return left == right || (left != null && left.equals(right)); + } + }, + "Bob's your uncle.", + "Every good boy does fine.", + false + } + + } ); + } + + @Test + public void test() { + final SimilarityScoreFrom<R> similarityScoreFrom = new SimilarityScoreFrom<>(similarityScore, left); + assertThat(similarityScoreFrom.apply(right), equalTo(distance)); + } +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/similarity/StringMetricFromTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/StringMetricFromTest.java b/src/test/java/org/apache/commons/text/similarity/StringMetricFromTest.java new file mode 100644 index 0000000..2b6e7a8 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/StringMetricFromTest.java @@ -0,0 +1,66 @@ +/* + * 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.text.similarity; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +/** + * Unit tests for {@link EditDistanceFrom}. + */ +public class StringMetricFromTest { + + @Test + public void testEquivalence() { + final EditDistance<Integer> metric = new LevenshteinDistance(); + final String left = "Apache"; + final String right = "a patchy"; + final Integer distance = 4; + final EditDistanceFrom<Integer> metricFrom = new EditDistanceFrom<>(metric, left); + + assertThat(metricFrom.apply(right), equalTo(distance)); + assertThat(metricFrom.apply(right), equalTo(metric.apply(left, right))); + } + + @Test + public void testJavadocExample() { + final EditDistance<Integer> metric = new LevenshteinDistance(); + final String target = "Apache"; + final EditDistanceFrom<Integer> metricFrom = + new EditDistanceFrom<>(metric, target); + String mostSimilar = null; + Integer shortestDistance = null; + + for (final String test : new String[] { "Appaloosa", "a patchy", "apple" }) { + final Integer distance = metricFrom.apply(test); + if (shortestDistance == null || distance < shortestDistance) { + shortestDistance = distance; + mostSimilar = test; + } + } + assertThat(mostSimilar, equalTo("a patchy")); + assertThat(shortestDistance, equalTo(4)); + } + + @Test(expected = IllegalArgumentException.class) + public void testMissingMetric() { + new EditDistanceFrom<Number>(null, "no go"); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/AggregateTranslatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/AggregateTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/AggregateTranslatorTest.java new file mode 100644 index 0000000..f1a4639 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/AggregateTranslatorTest.java @@ -0,0 +1,66 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link AggregateTranslator}. + */ +public class AggregateTranslatorTest { + + @Test + public void testNullConstructor() throws Exception { + final String testString = "foo"; + final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator[]) null); + assertEquals(testString, subject.translate(testString)); + } + + @Test + public void testNullVarargConstructor() throws Exception { + final String testString = "foo"; + final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator) null); + assertEquals(testString, subject.translate(testString)); + } + + @Test + public void testNonNull() throws IOException{ + final Map<CharSequence, CharSequence> oneTwoMap = new HashMap<>(); + oneTwoMap.put("one", "two"); + final Map<CharSequence, CharSequence> threeFourMap = new HashMap<>(); + threeFourMap.put("three", "four"); + final CharSequenceTranslator translator1 = new LookupTranslator(oneTwoMap); + final CharSequenceTranslator translator2 = new LookupTranslator(threeFourMap); + final AggregateTranslator subject = new AggregateTranslator(translator1, translator2); + final StringWriter out1 = new StringWriter(); + final int result1 = subject.translate(new StringBuffer("one"), 0, out1); + assertEquals("Incorrect codepoint consumption", 3, result1); + assertEquals("Incorrect value", "two", out1.toString()); + final StringWriter out2 = new StringWriter(); + final int result2 = subject.translate(new StringBuffer("three"), 0, out2); + assertEquals("Incorrect codepoint consumption", 5, result2); + assertEquals("Incorrect value", "four", out2.toString()); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/EntityArraysTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/EntityArraysTest.java b/src/test/java/org/apache/commons/text/translate/EntityArraysTest.java new file mode 100644 index 0000000..6ab42a2 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/EntityArraysTest.java @@ -0,0 +1,129 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link EntityArrays}. + */ +public class EntityArraysTest { + + @Test + public void testConstructorExists() { + new EntityArrays(); + } + + // LANG-659, LANG-658 - avoid duplicate entries + @Test + public void testForDuplicatedDeclaredMapKeys() throws Exception { + String packageDirectory = EntityArraysTest.class.getPackage().getName().replace(".", "/"); + try (BufferedReader br = new BufferedReader(new FileReader("src/main/java/" + packageDirectory + "/EntityArrays.java"))) { + String line; + int mapDeclarationCounter = 0; + while ((line = br.readLine()) != null) { + //Start with map declaration and count put lines + if (line.contains("new HashMap<>();")) { + mapDeclarationCounter = 0; + } else if (line.contains(".put(")) { + mapDeclarationCounter++; + } else if (line.contains("Collections.unmodifiableMap(initialMap);")) { + String mapVariableName = line.split("=")[0].trim(); + @SuppressWarnings("unchecked") // This is test code + Map<String,String> mapValue = (Map<String, String>)EntityArrays.class.getDeclaredField(mapVariableName).get(EntityArrays.class); + // Validate that we are not inserting into the same key twice in the map declaration. If this, + // indeed was the case the keySet().size() would be smaller than the number of put() statements + assertEquals(mapDeclarationCounter, mapValue.keySet().size()); + } + } + } + } + + @Test + public void testForDuplicateDeclaredMapValuesISO8859Map() { + assertEquals(EntityArrays.ISO8859_1_ESCAPE.keySet().size(), + EntityArrays.ISO8859_1_UNESCAPE.keySet().size()); + } + + @Test + public void testISO8859Map() { + testEscapeVsUnescapeMaps(EntityArrays.ISO8859_1_ESCAPE, EntityArrays.ISO8859_1_UNESCAPE); + } + + @Test + public void testForDuplicateDeclaredMapValuesHtml40ExtendedMap() { + assertEquals(EntityArrays.HTML40_EXTENDED_ESCAPE.keySet().size(), + EntityArrays.HTML40_EXTENDED_UNESCAPE.keySet().size()); + } + + @Test + public void testHtml40ExtendedMap() { + testEscapeVsUnescapeMaps(EntityArrays.HTML40_EXTENDED_ESCAPE, EntityArrays.HTML40_EXTENDED_UNESCAPE); + } + + @Test + public void testForDuplicateDeclaredMapValuesAposMap() { + assertEquals(EntityArrays.APOS_ESCAPE.keySet().size(), + EntityArrays.APOS_UNESCAPE.keySet().size()); + } + + @Test + public void testAposMap() { + testEscapeVsUnescapeMaps(EntityArrays.APOS_ESCAPE, EntityArrays.APOS_UNESCAPE); + } + + @Test + public void testForDuplicateDeclaredMapValuesBasicMap() { + assertEquals(EntityArrays.BASIC_ESCAPE.keySet().size(), + EntityArrays.BASIC_UNESCAPE.keySet().size()); + } + + @Test + public void testBasicMap() { + testEscapeVsUnescapeMaps(EntityArrays.BASIC_ESCAPE, EntityArrays.BASIC_UNESCAPE); + } + + @Test + public void testForDuplicateDeclaredMapValuesJavaCtrlCharsMap() { + assertEquals(EntityArrays.JAVA_CTRL_CHARS_ESCAPE.keySet().size(), + EntityArrays.JAVA_CTRL_CHARS_UNESCAPE.keySet().size()); + } + + @Test + public void testJavaCntrlCharsMap() { + testEscapeVsUnescapeMaps(EntityArrays.JAVA_CTRL_CHARS_ESCAPE, EntityArrays.JAVA_CTRL_CHARS_UNESCAPE); + } + + private void testEscapeVsUnescapeMaps(final Map<CharSequence, CharSequence> escapeMap, + final Map<CharSequence, CharSequence> unescapeMap) { + for (final CharSequence escapeKey : escapeMap.keySet()) { + for (final CharSequence unescapeKey : unescapeMap.keySet()) { + if (escapeKey == unescapeMap.get(unescapeKey)) { + assertEquals(escapeMap.get(escapeKey), unescapeKey); + } + } + } + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/JavaUnicodeEscaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/JavaUnicodeEscaperTest.java b/src/test/java/org/apache/commons/text/translate/JavaUnicodeEscaperTest.java new file mode 100644 index 0000000..f2d4bea --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/JavaUnicodeEscaperTest.java @@ -0,0 +1,65 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import java.io.UnsupportedEncodingException; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link JavaUnicodeEscaper}. + */ +public class JavaUnicodeEscaperTest { + + @Test + public void testBelow() { + final JavaUnicodeEscaper jue = JavaUnicodeEscaper.below('F'); + + final String input = "ADFGZ"; + final String result = jue.translate(input); + assertEquals("Failed to escape Unicode characters via the below method", "\\u0041\\u0044FGZ", result); + } + + @Test + public void testBetween() { + final JavaUnicodeEscaper jue = JavaUnicodeEscaper.between('F', 'L'); + + final String input = "ADFGZ"; + final String result = jue.translate(input); + assertEquals("Failed to escape Unicode characters via the between method", "AD\\u0046\\u0047Z", result); + } + + @Test + public void testAbove() { + final JavaUnicodeEscaper jue = JavaUnicodeEscaper.above('F'); + + final String input = "ADFGZ"; + final String result = jue.translate(input); + assertEquals("Failed to escape Unicode characters via the above method", "ADF\\u0047\\u005A", result); + } + + @Test + public void testToUtf16Escape() throws UnsupportedEncodingException { + final JavaUnicodeEscaper jue = JavaUnicodeEscaper.below('F'); + // According to https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B10000..U.2B10FFFF, + // Character ?, U+24B62, Binary Code Point 0010 0100 1011 0110 0010, Binary UTF-167 1101 1000 0101 0010 1101 1111 0110 0010, UTF-16 Hex Code Units D852 DF62 + final String encoding = jue.toUtf16Escape(Integer.parseInt("024B62", 16)); + assertEquals("\\uD852\\uDF62",encoding); + } +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java new file mode 100644 index 0000000..ebf7af5 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java @@ -0,0 +1,57 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link LookupTranslator}. + */ +public class LookupTranslatorTest { + + @Test + public void testBasicLookup() throws IOException { + final Map<CharSequence, CharSequence> translatorMap = new HashMap<>(); + translatorMap.put("one", "two"); + final LookupTranslator lt = new LookupTranslator(translatorMap); + final StringWriter out = new StringWriter(); + final int result = lt.translate("one", 0, out); + assertEquals("Incorrect codepoint consumption", 3, result); + assertEquals("Incorrect value", "two", out.toString()); + } + + // Tests: https://issues.apache.org/jira/browse/LANG-882 + @Test + public void testLang882() throws IOException { + final Map<CharSequence, CharSequence> translatorMap = new HashMap<>(); + translatorMap.put(new StringBuffer("one"), new StringBuffer("two")); + final LookupTranslator lt = new LookupTranslator(translatorMap); + final StringWriter out = new StringWriter(); + final int result = lt.translate(new StringBuffer("one"), 0, out); + assertEquals("Incorrect codepoint consumption", 3, result); + assertEquals("Incorrect value", "two", out.toString()); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/NumericEntityEscaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/NumericEntityEscaperTest.java b/src/test/java/org/apache/commons/text/translate/NumericEntityEscaperTest.java new file mode 100644 index 0000000..efd3ddd --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/NumericEntityEscaperTest.java @@ -0,0 +1,68 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link NumericEntityEscaper}. + */ +public class NumericEntityEscaperTest { + + @Test + public void testBelow() { + final NumericEntityEscaper nee = NumericEntityEscaper.below('F'); + + final String input = "ADFGZ"; + final String result = nee.translate(input); + assertEquals("Failed to escape numeric entities via the below method", "ADFGZ", result); + } + + @Test + public void testBetween() { + final NumericEntityEscaper nee = NumericEntityEscaper.between('F', 'L'); + + final String input = "ADFGZ"; + final String result = nee.translate(input); + assertEquals("Failed to escape numeric entities via the between method", "ADFGZ", result); + } + + @Test + public void testAbove() { + final NumericEntityEscaper nee = NumericEntityEscaper.above('F'); + + final String input = "ADFGZ"; + final String result = nee.translate(input); + assertEquals("Failed to escape numeric entities via the above method", "ADFGZ", result); + } + + // See LANG-617 + @Test + public void testSupplementary() { + final NumericEntityEscaper nee = new NumericEntityEscaper(); + final String input = "\uD803\uDC22"; + final String expected = "𐰢"; + + final String result = nee.translate(input); + assertEquals("Failed to escape numeric entities supplementary characters", expected, result); + + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/NumericEntityUnescaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/NumericEntityUnescaperTest.java b/src/test/java/org/apache/commons/text/translate/NumericEntityUnescaperTest.java new file mode 100644 index 0000000..f62386e --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/NumericEntityUnescaperTest.java @@ -0,0 +1,80 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Unit tests for {@link NumericEntityUnescaper}. + */ +public class NumericEntityUnescaperTest { + + @Test + public void testSupplementaryUnescaping() { + final NumericEntityUnescaper neu = new NumericEntityUnescaper(); + final String input = "𐰢"; + final String expected = "\uD803\uDC22"; + + final String result = neu.translate(input); + assertEquals("Failed to unescape numeric entities supplementary characters", expected, result); + } + + @Test + public void testOutOfBounds() { + final NumericEntityUnescaper neu = new NumericEntityUnescaper(); + + assertEquals("Failed to ignore when last character is &", "Test &", neu.translate("Test &")); + assertEquals("Failed to ignore when last character is &", "Test &#", neu.translate("Test &#")); + assertEquals("Failed to ignore when last character is &", "Test &#x", neu.translate("Test &#x")); + assertEquals("Failed to ignore when last character is &", "Test &#X", neu.translate("Test &#X")); + } + + @Test + public void testUnfinishedEntity() { + // parse it + NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional); + String input = "Test 0 not test"; + String expected = "Test \u0030 not test"; + + String result = neu.translate(input); + assertEquals("Failed to support unfinished entities (i.e. missing semi-colon)", expected, result); + + // ignore it + neu = new NumericEntityUnescaper(); + input = "Test 0 not test"; + expected = input; + + result = neu.translate(input); + assertEquals("Failed to ignore unfinished entities (i.e. missing semi-colon)", expected, result); + + // fail it + neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon); + input = "Test 0 not test"; + + try { + result = neu.translate(input); + fail("IllegalArgumentException expected"); + } catch(final IllegalArgumentException iae) { + // expected + } + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java b/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java new file mode 100644 index 0000000..6b02dc8 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java @@ -0,0 +1,82 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link OctalUnescaper}. + */ +public class OctalUnescaperTest { + + @Test + public void testBetween() { + final OctalUnescaper oue = new OctalUnescaper(); //.between("1", "377"); + + String input = "\\45"; + String result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\45", result); + + input = "\\377"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\377", result); + + input = "\\377 and"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\377 and", result); + + input = "\\378 and"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\37" + "8 and", result); + + input = "\\378"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\37" + "8", result); + + input = "\\1"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\1", result); + + input = "\\036"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\036", result); + + input = "\\0365"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\036" + "5", result); + + input = "\\003"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\003", result); + + input = "\\0003"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\000" + "3", result); + + input = "\\279"; + result = oue.translate(input); + assertEquals("Failed to unescape octal characters via the between method", "\279", result); + + input = "\\999"; + result = oue.translate(input); + assertEquals("Failed to ignore an out of range octal character via the between method", "\\999", result); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java new file mode 100644 index 0000000..d36d6e2 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java @@ -0,0 +1,57 @@ +/* + * 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.text.translate; + +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; + +import static org.junit.Assert.assertEquals; + +/** + * Unit test for {@link SinglePassTranslator} + */ +public class SinglePassTranslatorTest { + + private final SinglePassTranslator dummyTranslator = new SinglePassTranslator() { + @Override + void translateWhole(final CharSequence input, final Writer out) throws IOException { + } + }; + + private StringWriter out; + + @Before + public void before() { + out = new StringWriter(); + } + + @Test + public void codePointsAreReturned() throws Exception { + assertEquals(0, dummyTranslator.translate("", 0, out)); + assertEquals(3, dummyTranslator.translate("abc", 0, out)); + assertEquals(7, dummyTranslator.translate("abcdefg", 0, out)); + } + + @Test(expected = IllegalArgumentException.class) + public void indexIsValidated() throws Exception { + dummyTranslator.translate("abc", 1, out); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/UnicodeEscaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/UnicodeEscaperTest.java b/src/test/java/org/apache/commons/text/translate/UnicodeEscaperTest.java new file mode 100644 index 0000000..8f35802 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/UnicodeEscaperTest.java @@ -0,0 +1,55 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link UnicodeEscaper}. + */ +public class UnicodeEscaperTest { + + @Test + public void testBelow() { + final UnicodeEscaper ue = UnicodeEscaper.below('F'); + + final String input = "ADFGZ"; + final String result = ue.translate(input); + assertEquals("Failed to escape Unicode characters via the below method", "\\u0041\\u0044FGZ", result); + } + + @Test + public void testBetween() { + final UnicodeEscaper ue = UnicodeEscaper.between('F', 'L'); + + final String input = "ADFGZ"; + final String result = ue.translate(input); + assertEquals("Failed to escape Unicode characters via the between method", "AD\\u0046\\u0047Z", result); + } + + @Test + public void testAbove() { + final UnicodeEscaper ue = UnicodeEscaper.above('F'); + + final String input = "ADFGZ"; + final String result = ue.translate(input); + assertEquals("Failed to escape Unicode characters via the above method", "ADF\\u0047\\u005A", result); + } +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java b/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java new file mode 100644 index 0000000..4be0c98 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java @@ -0,0 +1,60 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Unit tests for {@link UnicodeEscaper}. + */ +public class UnicodeUnescaperTest { + + // Requested in LANG-507 + @Test + public void testUPlus() { + final UnicodeUnescaper uu = new UnicodeUnescaper(); + + final String input = "\\u+0047"; + assertEquals("Failed to unescape Unicode characters with 'u+' notation", "G", uu.translate(input)); + } + + @Test + public void testUuuuu() { + final UnicodeUnescaper uu = new UnicodeUnescaper(); + + final String input = "\\uuuuuuuu0047"; + final String result = uu.translate(input); + assertEquals("Failed to unescape Unicode characters with many 'u' characters", "G", result); + } + + @Test + public void testLessThanFour() { + final UnicodeUnescaper uu = new UnicodeUnescaper(); + + final String input = "\\0047\\u006"; + try { + uu.translate(input); + fail("A lack of digits in a Unicode escape sequence failed to throw an exception"); + } catch(final IllegalArgumentException iae) { + // expected + } + } +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemoverTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemoverTest.java b/src/test/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemoverTest.java new file mode 100644 index 0000000..6f67e11 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/UnicodeUnpairedSurrogateRemoverTest.java @@ -0,0 +1,47 @@ +/* + * 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.text.translate; + +import org.junit.Test; + +import java.io.CharArrayWriter; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link UnicodeUnpairedSurrogateRemover}. + */ +public class UnicodeUnpairedSurrogateRemoverTest { + final UnicodeUnpairedSurrogateRemover subject = new UnicodeUnpairedSurrogateRemover(); + final CharArrayWriter writer = new CharArrayWriter(); // nothing is ever written to it + + @Test + public void testValidCharacters() throws IOException { + assertEquals(false, subject.translate(0xd7ff, writer)); + assertEquals(false, subject.translate(0xe000, writer)); + assertEquals(0, writer.size()); + } + + @Test + public void testInvalidCharacters() throws IOException { + assertEquals(true, subject.translate(0xd800, writer)); + assertEquals(true, subject.translate(0xdfff, writer)); + assertEquals(0, writer.size()); + } +} +