|
@@ -17,12 +17,16 @@
|
|
*/
|
|
*/
|
|
package org.apache.jute;
|
|
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 org.junit.Test;
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
// TODO: introduce JuteTestCase as in ZKTestCase
|
|
// TODO: introduce JuteTestCase as in ZKTestCase
|
|
public class BinaryInputArchiveTest {
|
|
public class BinaryInputArchiveTest {
|
|
@@ -35,10 +39,100 @@ public class BinaryInputArchiveTest {
|
|
BinaryInputArchive ia = BinaryInputArchive.getArchive(is);
|
|
BinaryInputArchive ia = BinaryInputArchive.getArchive(is);
|
|
try {
|
|
try {
|
|
ia.readString("");
|
|
ia.readString("");
|
|
- Assert.fail("Should have thrown an IOException");
|
|
|
|
|
|
+ fail("Should have thrown an IOException");
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- Assert.assertTrue("Not 'Unreasonable length' exception: " + e,
|
|
|
|
|
|
+ assertTrue("Not 'Unreasonable length' exception: " + e,
|
|
e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH));
|
|
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);
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|