Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

java de/serialization test cases

  • Loading branch information...
commit ac4cc3b9d000c45ca1d132a3cb3a87a192a765bc 1 parent 359b127
@ropalka ropalka authored
View
54 modules/tests/src/test/java/test/fossnova/json/AbstractJsonValuesTestCase.java
@@ -26,6 +26,8 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import org.fossnova.json.JsonArray;
import org.fossnova.json.JsonBoolean;
@@ -44,19 +46,31 @@
*/
abstract class AbstractJsonValuesTestCase {
- static void assertRoundTrip( final JsonObject jsonObject ) throws IOException, JsonException {
- final String serializedJsonObject = serializeJson( jsonObject );
- final JsonValue deserializedJsonObject = deserializeJson( serializedJsonObject );
+ static void assertJsonSerializationRoundTrip( final JsonObject jsonObject ) throws IOException, JsonException {
+ final String serializedJsonObject = jsonSerialization( jsonObject );
+ final JsonValue deserializedJsonObject = jsonDeserialization( serializedJsonObject );
assertEquals( jsonObject, deserializedJsonObject );
}
- static void assertRoundTrip( final JsonArray jsonArray ) throws IOException, JsonException {
- final String serializedJsonObject = serializeJson( jsonArray );
- final JsonValue deserializedJsonObject = deserializeJson( serializedJsonObject );
+ static void assertJsonSerializationRoundTrip( final JsonArray jsonArray ) throws IOException, JsonException {
+ final String serializedJsonObject = jsonSerialization( jsonArray );
+ final JsonValue deserializedJsonObject = jsonDeserialization( serializedJsonObject );
assertEquals( jsonArray, deserializedJsonObject );
}
- static String serializeJson( final JsonObject jsonObject ) throws IOException, JsonException {
+ static void assertJavaSerializationRoundTrip( final JsonObject jsonObject ) throws IOException, ClassNotFoundException {
+ final byte[] serializedJsonObject = javaSerialization( jsonObject );
+ final JsonValue deserializedJsonObject = javaDeserialization( serializedJsonObject );
+ assertEquals( jsonObject, deserializedJsonObject );
+ }
+
+ static void assertJavaSerializationRoundTrip( final JsonArray jsonArray ) throws IOException, ClassNotFoundException {
+ final byte[] serializedJsonObject = javaSerialization( jsonArray );
+ final JsonValue deserializedJsonObject = javaDeserialization( serializedJsonObject );
+ assertEquals( jsonArray, deserializedJsonObject );
+ }
+
+ private static String jsonSerialization( final JsonObject jsonObject ) throws IOException, JsonException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final JsonWriter jsonWriter = JsonStreamFactory.newInstance().newJsonWriter( baos );
jsonObject.writeTo( jsonWriter );
@@ -64,7 +78,7 @@ static String serializeJson( final JsonObject jsonObject ) throws IOException, J
return new String( baos.toByteArray() );
}
- static String serializeJson( final JsonArray jsonArray ) throws IOException, JsonException {
+ private static String jsonSerialization( final JsonArray jsonArray ) throws IOException, JsonException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final JsonWriter jsonWriter = JsonStreamFactory.newInstance().newJsonWriter( baos );
jsonArray.writeTo( jsonWriter );
@@ -72,13 +86,35 @@ static String serializeJson( final JsonArray jsonArray ) throws IOException, Jso
return new String( baos.toByteArray() );
}
- static JsonValue deserializeJson( final String jsonString ) throws IOException, JsonException {
+ private static JsonValue jsonDeserialization( final String jsonString ) throws IOException, JsonException {
final ByteArrayInputStream bais = new ByteArrayInputStream( jsonString.getBytes() );
final JsonReader jsonReader = JsonStreamFactory.newInstance().newJsonReader( bais );
final JsonValueFactory jsonFactory = JsonValueFactory.newInstance();
return jsonFactory.readFrom( jsonReader );
}
+ private static JsonValue javaDeserialization( final byte[] jsonData ) throws IOException, ClassNotFoundException {
+ final ByteArrayInputStream bais = new ByteArrayInputStream( jsonData );
+ final ObjectInputStream ois = new ObjectInputStream( bais );
+ return ( JsonValue ) ois.readObject();
+ }
+
+ private static byte[] javaSerialization( final JsonObject jsonObject ) throws IOException {
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final ObjectOutputStream oos = new ObjectOutputStream( baos );
+ oos.writeObject( jsonObject );
+ oos.close();
+ return baos.toByteArray();
+ }
+
+ private static byte[] javaSerialization( final JsonArray jsonArray ) throws IOException {
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final ObjectOutputStream oos = new ObjectOutputStream( baos );
+ oos.writeObject( jsonArray );
+ oos.close();
+ return baos.toByteArray();
+ }
+
static JsonArray createSimpleArray() {
final JsonValueFactory jsonFactory = JsonValueFactory.newInstance();
final JsonArray jsonArray = jsonFactory.newJsonArray();
View
12 modules/tests/src/test/java/test/fossnova/json/JsonArrayTestCase.java
@@ -79,9 +79,15 @@ public void jsonStructureFactoryReadFrom() throws IOException, JsonException {
}
@Test
- public void roundTrip() throws IOException, JsonException {
- assertRoundTrip( createSimpleArray() );
- assertRoundTrip( createComplexArray() );
+ public void jsonSerializationRoundTrip() throws IOException, JsonException {
+ assertJsonSerializationRoundTrip( createSimpleArray() );
+ assertJsonSerializationRoundTrip( createComplexArray() );
+ }
+
+ @Test
+ public void javaSerializationRoundTrip() throws IOException, ClassNotFoundException {
+ assertJavaSerializationRoundTrip( createSimpleArray() );
+ assertJavaSerializationRoundTrip( createComplexArray() );
}
@Test
View
12 modules/tests/src/test/java/test/fossnova/json/JsonObjectTestCase.java
@@ -84,9 +84,15 @@ public void jsonStructureFactoryReadFrom() throws IOException, JsonException {
}
@Test
- public void roundTrip() throws IOException, JsonException {
- assertRoundTrip( createSimpleObject() );
- assertRoundTrip( createComplexObject() );
+ public void jsonSerializationRoundTrip() throws IOException, JsonException {
+ assertJsonSerializationRoundTrip( createSimpleObject() );
+ assertJsonSerializationRoundTrip( createComplexObject() );
+ }
+
+ @Test
+ public void javaSerializationRoundTrip() throws IOException, ClassNotFoundException {
+ assertJavaSerializationRoundTrip( createSimpleObject() );
+ assertJavaSerializationRoundTrip( createComplexObject() );
}
@Test
Please sign in to comment.
Something went wrong with that request. Please try again.