TEXT-23: Adding unit tests to o.a.c.t.translate, from lang
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/feeab276 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/feeab276 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/feeab276 Branch: refs/heads/master Commit: feeab27693c01a8789df89f9fad9a6f6edb9ccb5 Parents: 617869b Author: Rob Tompkins <chtom...@gmail.com> Authored: Mon Nov 7 21:11:24 2016 -0500 Committer: Rob Tompkins <chtom...@gmail.com> Committed: Mon Nov 7 21:11:24 2016 -0500 ---------------------------------------------------------------------- .../text/translate/EntityArraysTest.java | 72 +++++++++++++++++ .../text/translate/LookupTranslatorTest.java | 51 ++++++++++++ .../translate/NumericEntityEscaperTest.java | 68 ++++++++++++++++ .../translate/NumericEntityUnescaperTest.java | 80 +++++++++++++++++++ .../text/translate/OctalUnescaperTest.java | 82 ++++++++++++++++++++ .../text/translate/UnicodeEscaperTest.java | 55 +++++++++++++ .../text/translate/UnicodeUnescaperTest.java | 60 ++++++++++++++ .../UnicodeUnpairedSurrogateRemoverTest.java | 47 +++++++++++ 8 files changed, 515 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/feeab276/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..9b3dd4d --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/EntityArraysTest.java @@ -0,0 +1,72 @@ +/* + * 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.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertTrue; + +/** + * Unit tests for {@link org.apache.commons.text.translate.EntityArrays}. + */ +public class EntityArraysTest { + + @Test + public void testConstructorExists() { + new org.apache.commons.text.translate.EntityArrays(); + } + + // LANG-659 - check arrays for duplicate entries + @Test + public void testHTML40_EXTENDED_ESCAPE(){ + final Set<String> col0 = new HashSet<String>(); + final Set<String> col1 = new HashSet<String>(); + final String [][] sa = org.apache.commons.text.translate.EntityArrays.HTML40_EXTENDED_ESCAPE(); + for(int i =0; i <sa.length; i++){ + assertTrue("Already added entry 0: "+i+" "+sa[i][0],col0.add(sa[i][0])); + assertTrue("Already added entry 1: "+i+" "+sa[i][1],col1.add(sa[i][1])); + } + } + + // LANG-658 - check arrays for duplicate entries + @Test + public void testISO8859_1_ESCAPE(){ + final Set<String> col0 = new HashSet<String>(); + final Set<String> col1 = new HashSet<String>(); + 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("One or more errors detected",success); + } + + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/feeab276/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..e9559a5 --- /dev/null +++ b/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.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.text.translate; + +import org.junit.Test; + +import java.io.IOException; +import java.io.StringWriter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link org.apache.commons.text.translate.LookupTranslator}. + */ +public class LookupTranslatorTest { + + @Test + public void testBasicLookup() throws IOException { + final org.apache.commons.text.translate.LookupTranslator lt = new org.apache.commons.text.translate.LookupTranslator(new CharSequence[][] { { "one", "two" } }); + 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 org.apache.commons.text.translate.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("Incorrect codepoint consumption", 3, result); + assertEquals("Incorrect value", "two", out.toString()); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/feeab276/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..c0a09a6 --- /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 org.apache.commons.text.translate.NumericEntityEscaper}. + */ +public class NumericEntityEscaperTest { + + @Test + public void testBelow() { + final org.apache.commons.text.translate.NumericEntityEscaper nee = org.apache.commons.text.translate.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 org.apache.commons.text.translate.NumericEntityEscaper nee = org.apache.commons.text.translate.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 org.apache.commons.text.translate.NumericEntityEscaper nee = org.apache.commons.text.translate.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 org.apache.commons.text.translate.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/feeab276/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..8c6d7ec --- /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 org.apache.commons.text.translate.NumericEntityUnescaper}. + */ +public class NumericEntityUnescaperTest { + + @Test + public void testSupplementaryUnescaping() { + final org.apache.commons.text.translate.NumericEntityUnescaper neu = new org.apache.commons.text.translate.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 org.apache.commons.text.translate.NumericEntityUnescaper neu = new org.apache.commons.text.translate.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 + org.apache.commons.text.translate.NumericEntityUnescaper neu = new org.apache.commons.text.translate.NumericEntityUnescaper(org.apache.commons.text.translate.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 org.apache.commons.text.translate.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 org.apache.commons.text.translate.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/feeab276/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..8f94a48 --- /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 org.apache.commons.text.translate.OctalUnescaper}. + */ +public class OctalUnescaperTest { + + @Test + public void testBetween() { + final org.apache.commons.text.translate.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/feeab276/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..9be2484 --- /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 org.apache.commons.text.translate.UnicodeEscaper}. + */ +public class UnicodeEscaperTest { + + @Test + public void testBelow() { + final org.apache.commons.text.translate.UnicodeEscaper ue = org.apache.commons.text.translate.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 org.apache.commons.text.translate.UnicodeEscaper ue = org.apache.commons.text.translate.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 org.apache.commons.text.translate.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/feeab276/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..e596ec4 --- /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 org.apache.commons.text.translate.UnicodeEscaper}. + */ +public class UnicodeUnescaperTest { + + // Requested in LANG-507 + @Test + public void testUPlus() { + final org.apache.commons.text.translate.UnicodeUnescaper uu = new org.apache.commons.text.translate.UnicodeUnescaper(); + + final String input = "\\u+0047"; + assertEquals("Failed to unescape Unicode characters with 'u+' notation", "G", uu.translate(input)); + } + + @Test + public void testUuuuu() { + final org.apache.commons.text.translate.UnicodeUnescaper uu = new org.apache.commons.text.translate.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 org.apache.commons.text.translate.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/feeab276/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..e9b4aa9 --- /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 org.apache.commons.text.translate.UnicodeUnpairedSurrogateRemover}. + */ +public class UnicodeUnpairedSurrogateRemoverTest { + final org.apache.commons.text.translate.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()); + } +} +