Pārlūkot izejas kodu

ZOOKEEPER-3205: Add test cases for jute

Adding some test cases for o.a.jute BinaryInputArchive .  Issue ZOOKEEPER-3205 contains the patch as well.

Author: Karthik K <karthiknetworklink@gmail.com>

Reviewers: fangmin@apache.org, eolivelli@apache.org

Closes #726 from akkumar/jutetest
Karthik K 6 gadi atpakaļ
vecāks
revīzija
f4667d6fdf

+ 4 - 0
.gitignore

@@ -43,6 +43,10 @@ nbdist/
 nb-configuration.xml
 nbactions.xml
 
+# vscode
+classes
+.vscode
+
 # Other
 *~
 logs/

+ 98 - 4
zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java

@@ -17,12 +17,16 @@
  */
 package org.apache.jute;
 
-import org.junit.Assert;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertArrayEquals;
+
 import org.junit.Test;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
-
+import java.nio.charset.StandardCharsets;
 
 // TODO: introduce JuteTestCase as in ZKTestCase
 public class BinaryInputArchiveTest {
@@ -35,10 +39,100 @@ public class BinaryInputArchiveTest {
         BinaryInputArchive ia = BinaryInputArchive.getArchive(is);
         try {
             ia.readString("");
-            Assert.fail("Should have thrown an IOException");
+            fail("Should have thrown an IOException");
         } catch (IOException e) {
-            Assert.assertTrue("Not 'Unreasonable length' exception: " + e,
+            assertTrue("Not 'Unreasonable length' exception: " + e,
                     e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH));
         }
     }
+
+    private void checkWriterAndReader(TestWriter writer, TestReader reader) {
+        TestCheckWriterReader.checkWriterAndReader(
+                BinaryOutputArchive::getArchive,
+                BinaryInputArchive::getArchive,
+                writer,
+                reader
+        );
+    }
+
+    @Test
+    public void testInt() {
+        final int expected = 4;
+        final String tag = "tag1";
+        checkWriterAndReader(
+            (oa) -> oa.writeInt(expected, tag),
+            (ia) -> {
+                int actual = ia.readInt(tag);
+                assertEquals(expected, actual);
+            }
+        );
+    }
+
+    @Test
+    public void testBool() {
+        final boolean expected = false;
+        final String tag = "tag1";
+        checkWriterAndReader(
+            (oa) -> oa.writeBool(expected, tag),
+            (ia) -> {
+                    boolean actual = ia.readBool(tag);
+                    assertEquals(expected, actual);
+            }
+        );
+    }
+
+    @Test
+    public void testString() {
+        final String expected = "hello";
+        final String tag = "tag1";
+        checkWriterAndReader(
+            (oa) -> oa.writeString(expected, tag),
+            (ia) -> {
+                String actual = ia.readString(tag);
+                assertEquals(expected, actual);
+            }
+        );
+    }
+
+    @Test
+    public void testFloat() {
+        final float expected = 3.14159f;
+        final String tag = "tag1";
+        final float delta = 1e-10f;
+        checkWriterAndReader(
+            (oa) -> oa.writeFloat(expected, tag),
+            (ia) -> {
+                    float actual = ia.readFloat(tag);
+                    assertEquals(expected, actual, delta);
+            }
+        );
+    }
+
+    @Test
+    public void testDouble() {
+        final double expected = 3.14159f;
+        final String tag = "tag1";
+        final float delta = 1e-20f;
+        checkWriterAndReader(
+            (oa) -> oa.writeDouble(expected, tag),
+            (ia) -> {
+                    double actual = ia.readDouble(tag);
+                    assertEquals(expected, actual, delta);
+            }
+        );
+    }
+
+    @Test
+    public void testBuffer() {
+        final byte[] expected = "hello-world".getBytes(StandardCharsets.UTF_8);
+        final String tag = "tag1";
+        checkWriterAndReader(
+                (oa) -> oa.writeBuffer(expected, tag),
+                (ia) -> {
+                    byte [] actual = ia.readBuffer(tag);
+                    assertArrayEquals(expected, actual);
+                }
+        );
+    }
+
 }

+ 62 - 0
zookeeper-jute/src/test/java/org/apache/jute/TestCheckWriterReader.java

@@ -0,0 +1,62 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.jute;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import static org.junit.Assert.fail;
+
+/**
+ * TestOutputArchive creates an output archive from a given outputstream.
+ */
+interface TestOutputArchive {
+
+    OutputArchive getArchive(OutputStream os) throws IOException;
+}
+
+interface TestInputArchive {
+
+    InputArchive getArchive(InputStream is) throws IOException;
+}
+
+class TestCheckWriterReader {
+
+    static void checkWriterAndReader(
+            TestOutputArchive output, TestInputArchive input,
+            TestWriter writer, TestReader reader) {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            OutputArchive oa = output.getArchive(baos);
+            writer.write(oa);
+        } catch (IOException e) {
+            fail("Should not throw IOException while writing");
+        }
+        InputStream is = new ByteArrayInputStream(baos.toByteArray());
+        try {
+            InputArchive ia = input.getArchive(is);
+            reader.read(ia);
+        } catch (IOException e) {
+            fail("Should not throw IOException while reading back");
+        }
+    }
+
+}

+ 24 - 0
zookeeper-jute/src/test/java/org/apache/jute/TestReader.java

@@ -0,0 +1,24 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.jute;
+
+import java.io.IOException;
+
+public interface TestReader {
+    void read(InputArchive ia) throws IOException;
+}

+ 29 - 0
zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java

@@ -0,0 +1,29 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.jute;
+
+import java.io.IOException;
+
+
+public interface TestWriter {
+
+    /**
+     * Write to the given output archive.
+     */
+    void write(OutputArchive oa) throws IOException;
+}

+ 96 - 0
zookeeper-jute/src/test/java/org/apache/jute/UtilsTest.java

@@ -0,0 +1,96 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.jute;
+
+import org.junit.Assert;
+import org.junit.Test;
+import java.io.IOException;
+
+
+public class UtilsTest {
+
+    void assertXMLString(String input) {
+        String xmlString = Utils.toXMLString(input);
+        String actual = Utils.fromXMLString(xmlString);
+        Assert.assertEquals(input, actual);
+    }
+
+    @Test
+    public void testXMLString() {
+        assertXMLString("hello");
+        assertXMLString(",}%"); // special characters
+    }
+
+    void assertXMLBuffer(byte[] input) {
+        String xmlString = Utils.toXMLBuffer(input);
+        try {
+            byte[] actual = Utils.fromXMLBuffer(xmlString);
+            Assert.assertArrayEquals (input, actual);
+        } catch (IOException ioex) {
+            Assert.fail("Should not be throwing an IOException");
+        }
+    }
+
+    @Test
+    public void testXMLBuffer() {
+        assertXMLBuffer("hello".getBytes());
+    }
+
+
+    void assertCSVString(String input) {
+        String csvString = Utils.toCSVString(input);
+        try {
+            String actual = Utils.fromCSVString(csvString);
+            Assert.assertEquals(input, actual);
+        } catch (IOException ioex) {
+            Assert.fail("Should not be throwing an IOException");
+        }
+    }
+
+    @Test
+    public void testCSVString() {
+        assertCSVString("hello");
+        assertCSVString(",}%");        // special characters
+    }
+
+    @Test
+    public void testFromCSVString() {
+        try {
+            Utils.fromCSVString("1"); // not starting with '
+            Assert.fail("Should have thrown an IOException");
+        } catch (IOException ioex) {
+
+        }
+    }
+
+    void assertCSVBuffer(byte[] input) {
+        String csvBuffer = Utils.toCSVBuffer(input);
+        try {
+            byte[] actual = Utils.fromCSVBuffer(csvBuffer);
+            Assert.assertArrayEquals(input, actual);
+        } catch (IOException ioex) {
+            Assert.fail("Should not have thrown IOException during assertCSVBuffer");
+        }
+    }
+
+    @Test
+    public void testCSVBuffer() {
+        assertCSVBuffer("universe".getBytes());
+    }
+
+}

+ 127 - 0
zookeeper-jute/src/test/java/org/apache/jute/XmlInputArchiveTest.java

@@ -0,0 +1,127 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.jute;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
+
+import org.junit.Test;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import java.nio.charset.StandardCharsets;
+import java.io.IOException;
+
+public class XmlInputArchiveTest {
+
+    private void checkWriterAndReader(TestWriter writer, TestReader reader) {
+        TestCheckWriterReader.checkWriterAndReader(
+                XmlOutputArchive::getArchive,
+                (is) -> {
+                    try {
+                        return XmlInputArchive.getArchive(is);
+                    } catch (ParserConfigurationException|SAXException e) {
+                        throw new IOException(e);
+                    }
+                },
+                writer,
+                reader
+        );
+    }
+
+    @Test
+    public void testWriteInt() {
+        final int expected = 4;
+        final String tag = "tag1";
+        checkWriterAndReader(
+                (oa) -> oa.writeInt(expected, tag),
+                (ia) -> {
+                    int actual = ia.readInt(tag);
+                    assertEquals(expected, actual);
+                }
+        );
+    }
+
+    @Test
+    public void testWriteBool() {
+        final boolean expected = false;
+        final String tag = "tag1";
+        checkWriterAndReader(
+                (oa) -> oa.writeBool(expected, tag),
+                (ia) -> {
+                    boolean actual = ia.readBool(tag);
+                    assertEquals(expected, actual);
+                }
+        );
+    }
+
+    @Test
+    public void testWriteString() {
+        final String expected = "hello";
+        final String tag = "tag1";
+        checkWriterAndReader(
+                (oa) -> oa.writeString(expected, tag),
+                (ia) -> {
+                    String actual = ia.readString(tag);
+                    assertEquals(expected, actual);
+                }
+        );
+    }
+
+    @Test
+    public void testWriteFloat() {
+        final float expected = 3.14159f;
+        final String tag = "tag1";
+        final float delta = 1e-10f;
+        checkWriterAndReader(
+                (oa) -> oa.writeFloat(expected, tag),
+                (ia) -> {
+                    float actual = ia.readFloat(tag);
+                    assertEquals(expected, actual, delta);
+                }
+        );
+    }
+
+    @Test
+    public void testWriteDouble() {
+        final double expected = 3.14159f;
+        final String tag = "tag1";
+        final float delta = 1e-20f;
+        checkWriterAndReader(
+                (oa) -> oa.writeDouble(expected, tag),
+                (ia) -> {
+                    double actual = ia.readDouble(tag);
+                    assertEquals(expected, actual, delta);
+                }
+        );
+    }
+
+    @Test
+    public void testBuffer() {
+        final byte[] expected = "hello-world".getBytes(StandardCharsets.UTF_8);
+        final String tag = "tag1";
+        checkWriterAndReader(
+                (oa) -> oa.writeBuffer(expected, tag),
+                (ia) -> {
+                    byte [] actual = ia.readBuffer(tag);
+                    assertArrayEquals(expected, actual);
+                }
+        );
+    }
+
+}