You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by fa...@apache.org on 2019/01/02 14:10:10 UTC

[zookeeper] branch master updated: ZOOKEEPER-3205: Add test cases for jute

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

fangmin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new f4667d6  ZOOKEEPER-3205: Add test cases for jute
f4667d6 is described below

commit f4667d6fdf6e29ed7ec3c3ea18e0f4cd956cd360
Author: Karthik K <ka...@gmail.com>
AuthorDate: Wed Jan 2 22:10:02 2019 +0800

    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 <ka...@gmail.com>
    
    Reviewers: fangmin@apache.org, eolivelli@apache.org
    
    Closes #726 from akkumar/jutetest
---
 .gitignore                                         |   4 +
 .../org/apache/jute/BinaryInputArchiveTest.java    | 102 ++++++++++++++++-
 .../org/apache/jute/TestCheckWriterReader.java     |  62 ++++++++++
 ...BinaryInputArchiveTest.java => TestReader.java} |  24 +---
 ...BinaryInputArchiveTest.java => TestWriter.java} |  27 +----
 .../src/test/java/org/apache/jute/UtilsTest.java   |  96 ++++++++++++++++
 .../java/org/apache/jute/XmlInputArchiveTest.java  | 127 +++++++++++++++++++++
 7 files changed, 395 insertions(+), 47 deletions(-)

diff --git a/.gitignore b/.gitignore
index 5fe044f..cd94fca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -43,6 +43,10 @@ nbdist/
 nb-configuration.xml
 nbactions.xml
 
+# vscode
+classes
+.vscode
+
 # Other
 *~
 logs/
diff --git a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java b/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
index 52fdae9..9226466 100644
--- a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
+++ b/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);
+                }
+        );
+    }
+
 }
diff --git a/zookeeper-jute/src/test/java/org/apache/jute/TestCheckWriterReader.java b/zookeeper-jute/src/test/java/org/apache/jute/TestCheckWriterReader.java
new file mode 100644
index 0000000..115f58b
--- /dev/null
+++ b/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");
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java b/zookeeper-jute/src/test/java/org/apache/jute/TestReader.java
similarity index 52%
copy from zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
copy to zookeeper-jute/src/test/java/org/apache/jute/TestReader.java
index 52fdae9..a528b59 100644
--- a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
+++ b/zookeeper-jute/src/test/java/org/apache/jute/TestReader.java
@@ -17,28 +17,8 @@
  */
 package org.apache.jute;
 
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 
-
-// TODO: introduce JuteTestCase as in ZKTestCase
-public class BinaryInputArchiveTest {
-
-    @Test
-    public void testReadStringCheckLength() {
-        byte[] buf = new byte[]{
-                Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE};
-        ByteArrayInputStream is = new ByteArrayInputStream(buf);
-        BinaryInputArchive ia = BinaryInputArchive.getArchive(is);
-        try {
-            ia.readString("");
-            Assert.fail("Should have thrown an IOException");
-        } catch (IOException e) {
-            Assert.assertTrue("Not 'Unreasonable length' exception: " + e,
-                    e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH));
-        }
-    }
+public interface TestReader {
+    void read(InputArchive ia) throws IOException;
 }
diff --git a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java b/zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java
similarity index 52%
copy from zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
copy to zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java
index 52fdae9..ffaa9bc 100644
--- a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java
+++ b/zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java
@@ -17,28 +17,13 @@
  */
 package org.apache.jute;
 
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 
 
-// TODO: introduce JuteTestCase as in ZKTestCase
-public class BinaryInputArchiveTest {
+public interface TestWriter {
 
-    @Test
-    public void testReadStringCheckLength() {
-        byte[] buf = new byte[]{
-                Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE};
-        ByteArrayInputStream is = new ByteArrayInputStream(buf);
-        BinaryInputArchive ia = BinaryInputArchive.getArchive(is);
-        try {
-            ia.readString("");
-            Assert.fail("Should have thrown an IOException");
-        } catch (IOException e) {
-            Assert.assertTrue("Not 'Unreasonable length' exception: " + e,
-                    e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH));
-        }
-    }
-}
+    /**
+     * Write to the given output archive.
+     */
+    void write(OutputArchive oa) throws IOException;
+}
\ No newline at end of file
diff --git a/zookeeper-jute/src/test/java/org/apache/jute/UtilsTest.java b/zookeeper-jute/src/test/java/org/apache/jute/UtilsTest.java
new file mode 100644
index 0000000..ac42083
--- /dev/null
+++ b/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());
+    }
+
+}
\ No newline at end of file
diff --git a/zookeeper-jute/src/test/java/org/apache/jute/XmlInputArchiveTest.java b/zookeeper-jute/src/test/java/org/apache/jute/XmlInputArchiveTest.java
new file mode 100644
index 0000000..2c9dda2
--- /dev/null
+++ b/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);
+                }
+        );
+    }
+
+}
\ No newline at end of file