Skip to content

Commit

Permalink
ZOOKEEPER-3205: Add test cases for jute
Browse files Browse the repository at this point in the history
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 apache#726 from akkumar/jutetest
  • Loading branch information
akkumar authored and lvfangmin committed Jan 2, 2019
1 parent 2a3800f commit f4667d6
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ nbdist/
nb-configuration.xml
nbactions.xml

# vscode
classes
.vscode

# Other
*~
logs/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,10 +39,100 @@ public void testReadStringCheckLength() {
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);
}
);
}

}
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions zookeeper-jute/src/test/java/org/apache/jute/TestReader.java
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java
Original file line number Diff line number Diff line change
@@ -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 changes: 96 additions & 0 deletions zookeeper-jute/src/test/java/org/apache/jute/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit f4667d6

Please sign in to comment.