From 4c53c0640e493a3f30879790b69706b615d055bb Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 3 Sep 2010 07:24:59 +0000 Subject: [PATCH] Exhaustive test coverage for mixed streaming & binding. --- .../java/com/google/gson/MixedStreamTest.java | 147 +++++++++++++++++- 1 file changed, 146 insertions(+), 1 deletion(-) diff --git a/gson/src/test/java/com/google/gson/MixedStreamTest.java b/gson/src/test/java/com/google/gson/MixedStreamTest.java index ed9e07d3..de123b36 100644 --- a/gson/src/test/java/com/google/gson/MixedStreamTest.java +++ b/gson/src/test/java/com/google/gson/MixedStreamTest.java @@ -16,11 +16,15 @@ package com.google.gson; +import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.List; import junit.framework.TestCase; public final class MixedStreamTest extends TestCase { @@ -69,6 +73,146 @@ public final class MixedStreamTest extends TestCase { assertEquals(RED_MIATA, gson.fromJson(jsonReader, Car.class)); jsonReader.endArray(); } + + public void testReaderDoesNotMutateState() throws IOException { + Gson gson = new Gson(); + JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON)); + jsonReader.beginArray(); + + jsonReader.setLenient(false); + gson.fromJson(jsonReader, Car.class); + assertFalse(jsonReader.isLenient()); + + jsonReader.setLenient(true); + gson.fromJson(jsonReader, Car.class); + assertTrue(jsonReader.isLenient()); + } + + public void testWriteDoesNotMutateState() throws IOException { + Gson gson = new Gson(); + JsonWriter jsonWriter = new JsonWriter(new StringWriter()); + jsonWriter.beginArray(); + + jsonWriter.setHtmlSafe(true); + jsonWriter.setLenient(true); + gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter); + assertTrue(jsonWriter.isHtmlSafe()); + assertTrue(jsonWriter.isLenient()); + + jsonWriter.setHtmlSafe(false); + jsonWriter.setLenient(false); + gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter); + assertFalse(jsonWriter.isHtmlSafe()); + assertFalse(jsonWriter.isLenient()); + } + + public void testReadInvalidState() throws IOException { + Gson gson = new Gson(); + JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON)); + jsonReader.beginArray(); + jsonReader.beginObject(); + try { + gson.fromJson(jsonReader, String.class); + fail(); + } catch (IllegalArgumentException expected) { + } + } + + public void testReadClosed() throws IOException { + Gson gson = new Gson(); + JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON)); + jsonReader.close(); + try { + gson.fromJson(jsonReader, new TypeToken>() {}.getType()); + fail(); + } catch (IllegalStateException expected) { + } + } + + public void testWriteInvalidState() throws IOException { + Gson gson = new Gson(); + JsonWriter jsonWriter = new JsonWriter(new StringWriter()); + jsonWriter.beginObject(); + try { + gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter); + fail(); + } catch (IllegalStateException expected) { + } + } + + public void testWriteClosed() throws IOException { + Gson gson = new Gson(); + JsonWriter jsonWriter = new JsonWriter(new StringWriter()); + jsonWriter.beginArray(); + jsonWriter.endArray(); + jsonWriter.close(); + try { + gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter); + fail(); + } catch (IllegalStateException expected) { + } + } + + public void testWriteNulls() { + Gson gson = new Gson(); + try { + gson.toJson(new JsonPrimitive("hello"), (JsonWriter) null); + fail(); + } catch (NullPointerException expected) { + } + + StringWriter stringWriter = new StringWriter(); + gson.toJson(null, new JsonWriter(stringWriter)); + assertEquals("", stringWriter.toString()); + } + + public void testReadNulls() { + Gson gson = new Gson(); + try { + gson.fromJson((JsonReader) null, Integer.class); + fail(); + } catch (NullPointerException expected) { + } + try { + gson.fromJson(new JsonReader(new StringReader("true")), null); + fail(); + } catch (IllegalArgumentException expected) { + } + } + + public void testWriteHtmlSafe() { + List contents = Arrays.asList("<", ">", "&", "=", "'"); + Type type = new TypeToken>() {}.getType(); + + StringWriter writer = new StringWriter(); + new Gson().toJson(contents, type, new JsonWriter(writer)); + assertEquals("[\"\\u003c\",\"\\u003e\",\"\\u0026\",\"\\u003d\",\"\\u0027\"]", + writer.toString()); + + writer = new StringWriter(); + new GsonBuilder().disableHtmlEscaping().create() + .toJson(contents, type, new JsonWriter(writer)); + assertEquals("[\"<\",\">\",\"&\",\"=\",\"'\"]", + writer.toString()); + } + + public void testWriteLenient() { + List doubles = Arrays.asList(Double.NaN, Double.NEGATIVE_INFINITY, + Double.POSITIVE_INFINITY, -0.0d, 0.5d, 0.0d); + Type type = new TypeToken>() {}.getType(); + + StringWriter writer = new StringWriter(); + JsonWriter jsonWriter = new JsonWriter(writer); + new GsonBuilder().serializeSpecialFloatingPointValues().create() + .toJson(doubles, type, jsonWriter); + assertEquals("[NaN,-Infinity,Infinity,-0.0,0.5,0.0]", writer.toString()); + + try { + new Gson().toJson(doubles, type, new JsonWriter(new StringWriter())); + fail(); + } catch (IllegalArgumentException expected) { + } + } static final class Car { String name; @@ -79,7 +223,8 @@ public final class MixedStreamTest extends TestCase { this.color = color; } - Car() {} // for Gson + @SuppressWarnings("unused") // used by Gson + Car() {} @Override public int hashCode() { return name.hashCode() ^ color;