aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/lang3/text/translate
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/apache/commons/lang3/text/translate')
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/EntityArraysTest.java74
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/LookupTranslatorTest.java53
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java70
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java77
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java84
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.java57
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/UnicodeUnescaperTest.java60
-rw-r--r--src/test/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemoverTest.java51
8 files changed, 526 insertions, 0 deletions
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/EntityArraysTest.java b/src/test/java/org/apache/commons/lang3/text/translate/EntityArraysTest.java
new file mode 100644
index 000000000..58f832170
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/text/translate/EntityArraysTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.EntityArrays}.
+ */
+@Deprecated
+public class EntityArraysTest extends AbstractLangTest {
+
+ @Test
+ public void testConstructorExists() {
+ new EntityArrays();
+ }
+
+ // LANG-659 - check arrays for duplicate entries
+ @Test
+ public void testHTML40_EXTENDED_ESCAPE() {
+ final Set<String> col0 = new HashSet<>();
+ final Set<String> col1 = new HashSet<>();
+ final String [][] sa = EntityArrays.HTML40_EXTENDED_ESCAPE();
+ for (int i =0; i <sa.length; i++) {
+ assertTrue(col0.add(sa[i][0]), "Already added entry 0: "+i+" "+sa[i][0]);
+ assertTrue(col1.add(sa[i][1]), "Already added entry 1: "+i+" "+sa[i][1]);
+ }
+ }
+
+ // LANG-658 - check arrays for duplicate entries
+ @Test
+ public void testISO8859_1_ESCAPE() {
+ final Set<String> col0 = new HashSet<>();
+ final Set<String> col1 = new HashSet<>();
+ final String [][] sa = EntityArrays.ISO8859_1_ESCAPE();
+ boolean success = true;
+ for (int i =0; i <sa.length; i++) {
+ final boolean add0 = col0.add(sa[i][0]);
+ final boolean add1 = col1.add(sa[i][1]);
+ if (!add0) {
+ success = false;
+ System.out.println("Already added entry 0: "+i+" "+sa[i][0]+" "+sa[i][1]);
+ }
+ if (!add1) {
+ success = false;
+ System.out.println("Already added entry 1: "+i+" "+sa[i][0]+" "+sa[i][1]);
+ }
+ }
+ assertTrue(success, "One or more errors detected");
+ }
+
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/LookupTranslatorTest.java b/src/test/java/org/apache/commons/lang3/text/translate/LookupTranslatorTest.java
new file mode 100644
index 000000000..6549f335f
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/text/translate/LookupTranslatorTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.LookupTranslator}.
+ */
+@Deprecated
+public class LookupTranslatorTest extends AbstractLangTest {
+
+ @Test
+ public void testBasicLookup() throws IOException {
+ final LookupTranslator lt = new LookupTranslator(new CharSequence[][] { { "one", "two" } });
+ final StringWriter out = new StringWriter();
+ final int result = lt.translate("one", 0, out);
+ assertEquals(3, result, "Incorrect code point consumption");
+ assertEquals("two", out.toString(), "Incorrect value");
+ }
+
+ // Tests: https://issues.apache.org/jira/browse/LANG-882
+ @Test
+ public void testLang882() throws IOException {
+ final LookupTranslator lt = new LookupTranslator(new CharSequence[][] { { new StringBuffer("one"), new StringBuffer("two") } });
+ final StringWriter out = new StringWriter();
+ final int result = lt.translate(new StringBuffer("one"), 0, out);
+ assertEquals(3, result, "Incorrect code point consumption");
+ assertEquals("two", out.toString(), "Incorrect value");
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java
new file mode 100644
index 000000000..3b92c4640
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.NumericEntityEscaper}.
+ */
+@Deprecated
+public class NumericEntityEscaperTest extends AbstractLangTest {
+
+ @Test
+ public void testBelow() {
+ final NumericEntityEscaper nee = NumericEntityEscaper.below('F');
+
+ final String input = "ADFGZ";
+ final String result = nee.translate(input);
+ assertEquals("&#65;&#68;FGZ", result, "Failed to escape numeric entities via the below method");
+ }
+
+ @Test
+ public void testBetween() {
+ final NumericEntityEscaper nee = NumericEntityEscaper.between('F', 'L');
+
+ final String input = "ADFGZ";
+ final String result = nee.translate(input);
+ assertEquals("AD&#70;&#71;Z", result, "Failed to escape numeric entities via the between method");
+ }
+
+ @Test
+ public void testAbove() {
+ final NumericEntityEscaper nee = NumericEntityEscaper.above('F');
+
+ final String input = "ADFGZ";
+ final String result = nee.translate(input);
+ assertEquals("ADF&#71;&#90;", result, "Failed to escape numeric entities via the above method");
+ }
+
+ // See LANG-617
+ @Test
+ public void testSupplementary() {
+ final NumericEntityEscaper nee = new NumericEntityEscaper();
+ final String input = "\uD803\uDC22";
+ final String expected = "&#68642;";
+
+ final String result = nee.translate(input);
+ assertEquals(expected, result, "Failed to escape numeric entities supplementary characters");
+
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java
new file mode 100644
index 000000000..a1c00d371
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.NumericEntityUnescaper}.
+ */
+@Deprecated
+public class NumericEntityUnescaperTest extends AbstractLangTest {
+
+ @Test
+ public void testSupplementaryUnescaping() {
+ final NumericEntityUnescaper neu = new NumericEntityUnescaper();
+ final String input = "&#68642;";
+ final String expected = "\uD803\uDC22";
+
+ final String result = neu.translate(input);
+ assertEquals(expected, result, "Failed to unescape numeric entities supplementary characters");
+ }
+
+ @Test
+ public void testOutOfBounds() {
+ final NumericEntityUnescaper neu = new NumericEntityUnescaper();
+
+ assertEquals("Test &", neu.translate("Test &"), "Failed to ignore when last character is &");
+ assertEquals("Test &#", neu.translate("Test &#"), "Failed to ignore when last character is &");
+ assertEquals("Test &#x", neu.translate("Test &#x"), "Failed to ignore when last character is &");
+ assertEquals("Test &#X", neu.translate("Test &#X"), "Failed to ignore when last character is &");
+ }
+
+ @Test
+ public void testUnfinishedEntity() {
+ // parse it
+ NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional);
+ String input = "Test &#x30 not test";
+ String expected = "Test \u0030 not test";
+
+ String result = neu.translate(input);
+ assertEquals(expected, result, "Failed to support unfinished entities (i.e. missing semicolon)");
+
+ // ignore it
+ neu = new NumericEntityUnescaper();
+ input = "Test &#x30 not test";
+ expected = input;
+
+ result = neu.translate(input);
+ assertEquals(expected, result, "Failed to ignore unfinished entities (i.e. missing semicolon)");
+
+ // fail it
+ final NumericEntityUnescaper failingNeu =
+ new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon);
+ final String failingInput = "Test &#x30 not test";
+ assertThrows(IllegalArgumentException.class, () -> failingNeu.translate(failingInput));
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java
new file mode 100644
index 000000000..8fa2d4948
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.OctalUnescaper}.
+ */
+@Deprecated
+public class OctalUnescaperTest extends AbstractLangTest {
+
+ @Test
+ public void testBetween() {
+ final OctalUnescaper oue = new OctalUnescaper(); //.between("1", "377");
+
+ String input = "\\45";
+ String result = oue.translate(input);
+ assertEquals("\45", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\377";
+ result = oue.translate(input);
+ assertEquals("\377", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\377 and";
+ result = oue.translate(input);
+ assertEquals("\377 and", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\378 and";
+ result = oue.translate(input);
+ assertEquals("\37" + "8 and", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\378";
+ result = oue.translate(input);
+ assertEquals("\37" + "8", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\1";
+ result = oue.translate(input);
+ assertEquals("\1", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\036";
+ result = oue.translate(input);
+ assertEquals("\036", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\0365";
+ result = oue.translate(input);
+ assertEquals("\036" + "5", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\003";
+ result = oue.translate(input);
+ assertEquals("\003", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\0003";
+ result = oue.translate(input);
+ assertEquals("\000" + "3", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\279";
+ result = oue.translate(input);
+ assertEquals("\279", result, "Failed to unescape octal characters via the between method");
+
+ input = "\\999";
+ result = oue.translate(input);
+ assertEquals("\\999", result, "Failed to ignore an out of range octal character via the between method");
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.java
new file mode 100644
index 000000000..6e66bd84e
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.UnicodeEscaper}.
+ */
+@Deprecated
+public class UnicodeEscaperTest extends AbstractLangTest {
+
+ @Test
+ public void testBelow() {
+ final UnicodeEscaper ue = UnicodeEscaper.below('F');
+
+ final String input = "ADFGZ";
+ final String result = ue.translate(input);
+ assertEquals("\\u0041\\u0044FGZ", result, "Failed to escape Unicode characters via the below method");
+ }
+
+ @Test
+ public void testBetween() {
+ final UnicodeEscaper ue = UnicodeEscaper.between('F', 'L');
+
+ final String input = "ADFGZ";
+ final String result = ue.translate(input);
+ assertEquals("AD\\u0046\\u0047Z", result, "Failed to escape Unicode characters via the between method");
+ }
+
+ @Test
+ public void testAbove() {
+ final UnicodeEscaper ue = UnicodeEscaper.above('F');
+
+ final String input = "ADFGZ";
+ final String result = ue.translate(input);
+ assertEquals("ADF\\u0047\\u005A", result, "Failed to escape Unicode characters via the above method");
+ }
+}
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/UnicodeUnescaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/UnicodeUnescaperTest.java
new file mode 100644
index 000000000..54d1235ce
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.UnicodeEscaper}.
+ */
+@Deprecated
+public class UnicodeUnescaperTest extends AbstractLangTest {
+
+ // Requested in LANG-507
+ @Test
+ public void testUPlus() {
+ final UnicodeUnescaper uu = new UnicodeUnescaper();
+
+ final String input = "\\u+0047";
+ assertEquals("G", uu.translate(input), "Failed to unescape Unicode characters with 'u+' notation");
+ }
+
+ @Test
+ public void testUuuuu() {
+ final UnicodeUnescaper uu = new UnicodeUnescaper();
+
+ final String input = "\\uuuuuuuu0047";
+ final String result = uu.translate(input);
+ assertEquals("G", result, "Failed to unescape Unicode characters with many 'u' characters");
+ }
+
+ @Test
+ public void testLessThanFour() {
+ final UnicodeUnescaper uu = new UnicodeUnescaper();
+
+ final String input = "\\0047\\u006";
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> uu.translate(input),
+ "A lack of digits in a Unicode escape sequence failed to throw an exception");
+ }
+}
diff --git a/src/test/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemoverTest.java b/src/test/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemoverTest.java
new file mode 100644
index 000000000..8ff0e2f50
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemoverTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.lang3.text.translate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.CharArrayWriter;
+import java.io.IOException;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.UnicodeUnpairedSurrogateRemover}.
+ */
+@Deprecated
+public class UnicodeUnpairedSurrogateRemoverTest extends AbstractLangTest {
+ final UnicodeUnpairedSurrogateRemover subject = new UnicodeUnpairedSurrogateRemover();
+ final CharArrayWriter writer = new CharArrayWriter(); // nothing is ever written to it
+
+ @Test
+ public void testValidCharacters() throws IOException {
+ assertFalse(subject.translate(0xd7ff, writer));
+ assertFalse(subject.translate(0xe000, writer));
+ assertEquals(0, writer.size());
+ }
+
+ @Test
+ public void testInvalidCharacters() throws IOException {
+ assertTrue(subject.translate(0xd800, writer));
+ assertTrue(subject.translate(0xdfff, writer));
+ assertEquals(0, writer.size());
+ }
+}
+