From 3b8404dac56c365cbf77034ec7d96588d89e9798 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 10 Oct 2008 02:29:06 +0000 Subject: [PATCH] refactored String related functional tests out in a separate test class. --- .../google/gson/functional/ObjectTest.java | 44 ------- .../google/gson/functional/PrimitiveTest.java | 51 +------- .../google/gson/functional/StringTest.java | 115 ++++++++++++++++++ 3 files changed, 116 insertions(+), 94 deletions(-) create mode 100644 gson/src/test/java/com/google/gson/functional/StringTest.java diff --git a/gson/src/test/java/com/google/gson/functional/ObjectTest.java b/gson/src/test/java/com/google/gson/functional/ObjectTest.java index 48563d93..56c68ea4 100644 --- a/gson/src/test/java/com/google/gson/functional/ObjectTest.java +++ b/gson/src/test/java/com/google/gson/functional/ObjectTest.java @@ -299,43 +299,11 @@ public class ObjectTest extends TestCase { assertEquals(BagOfPrimitives.DEFAULT_VALUE, target.longValue); } - public void testReallyLongStringsDeserialization() throws Exception { - StringBuilder sb = new StringBuilder(8096); - sb.append("Once upon a time there was a really long string that caused a StackOverFlowError\n"); - sb.append("and now it is fixed and instead throws a JsonParserException.....Yippie!!!\n"); - sb.append("Wow....that is a really long string that is meant to be an exception stack trace, "); - sb.append("but is not :( \n\n\n\n\n\n."); - sb.append("lalalalala \n\n\n."); - sb.append("C'est la vie!!! \n\n\n\n\n"); - - for (int i = 0; i < 10; i++) { - sb.append(sb.toString()); - } - - while (true) { - try { - String stackTrace = sb.toString(); - sb.append(stackTrace); - String json = "{\"message\":\"Error message.\"," - + "\"stackTrace\":\"" + stackTrace + "\"}"; - parseLongJson(json); - } catch (JsonParseException expected) { - break; - } - } - } - public void testEmptyCollectionInAnObjectSerialization() throws Exception { ContainsReferenceToSelfType target = new ContainsReferenceToSelfType(); assertEquals("{\"children\":[]}", gson.toJson(target)); } - private void parseLongJson(String json) throws JsonParseException { - ExceptionHolder target = gson.fromJson(json, ExceptionHolder.class); - assertTrue(target.message.contains("Error")); - assertTrue(target.stackTrace.contains("Yippie")); - } - public void testCircularSerialization() throws Exception { ContainsReferenceToSelfType a = new ContainsReferenceToSelfType(); ContainsReferenceToSelfType b = new ContainsReferenceToSelfType(); @@ -502,16 +470,4 @@ public class ObjectTest extends TestCase { a = 10; } } - - private static class ExceptionHolder { - public final String message; - public final String stackTrace; - public ExceptionHolder() { - this("", ""); - } - public ExceptionHolder(String message, String stackTrace) { - this.message = message; - this.stackTrace = stackTrace; - } - } } diff --git a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java index 68410017..54edebb9 100644 --- a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java @@ -25,7 +25,7 @@ import java.math.BigDecimal; import java.math.BigInteger; /** - * Functional tests for Json primitive values: String, integers, and floating point numbers. + * Functional tests for Json primitive values: integers, and floating point numbers. * * @author Inderjeet Singh * @author Joel Leitch @@ -39,55 +39,6 @@ public class PrimitiveTest extends TestCase { gson = new Gson(); } - public void testStringValueSerialization() throws Exception { - String value = "someRandomStringValue"; - assertEquals('"' + value + '"', gson.toJson(value)); - } - - public void testStringValueDeserialization() throws Exception { - String value = "someRandomStringValue"; - String actual = gson.fromJson("\"" + value + "\"", String.class); - assertEquals(value, actual); - } - - public void testSingleQuoteInStringSerialization() throws Exception { - String valueWithQuotes = "beforeQuote'afterQuote"; - String jsonRepresentation = gson.toJson(valueWithQuotes); - assertEquals(valueWithQuotes, gson.fromJson(jsonRepresentation, String.class)); - } - - public void testSingleQuoteInStringDeserialization() throws Exception { - String value = "beforeQuote'afterQuote"; - String actual = gson.fromJson("\"" + value + "\"", String.class); - assertEquals(value, actual); - } - - public void testEscapingQuotesInStringSerialization() throws Exception { - String valueWithQuotes = "beforeQuote\"afterQuote"; - String jsonRepresentation = gson.toJson(valueWithQuotes); - String target = gson.fromJson(jsonRepresentation, String.class); - assertEquals(valueWithQuotes, target); - } - - public void testEscapingQuotesInStringDeserialization() throws Exception { - String value = "beforeQuote\\\"afterQuote"; - String actual = gson.fromJson("\"" + value + "\"", String.class); - String expected = "beforeQuote\"afterQuote"; - assertEquals(expected, actual); - } - - public void testStringValueAsSingleElementArraySerialization() throws Exception { - String[] target = {"abc"}; - assertEquals("[\"abc\"]", gson.toJson(target)); - assertEquals("[\"abc\"]", gson.toJson(target, String[].class)); - } - - public void testStringValueAsSingleElementArrayDeserialization() throws Exception { - String value = "someRandomStringValue"; - String actual = gson.fromJson("[\"" + value + "\"]", String.class); - assertEquals(value, actual); - } - public void testPrimitiveIntegerAutoboxedSerialization() { assertEquals("1", gson.toJson(1)); } diff --git a/gson/src/test/java/com/google/gson/functional/StringTest.java b/gson/src/test/java/com/google/gson/functional/StringTest.java new file mode 100644 index 00000000..c4fd5a3e --- /dev/null +++ b/gson/src/test/java/com/google/gson/functional/StringTest.java @@ -0,0 +1,115 @@ +package com.google.gson.functional; + +import com.google.gson.Gson; +import com.google.gson.JsonParseException; + +import junit.framework.TestCase; + +/** + * Functional tests for Json serialization and deserialization of strings. + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +public class StringTest extends TestCase { + private Gson gson; + + @Override + protected void setUp() throws Exception { + super.setUp(); + gson = new Gson(); + } + + public void testStringValueSerialization() throws Exception { + String value = "someRandomStringValue"; + assertEquals('"' + value + '"', gson.toJson(value)); + } + + public void testStringValueDeserialization() throws Exception { + String value = "someRandomStringValue"; + String actual = gson.fromJson("\"" + value + "\"", String.class); + assertEquals(value, actual); + } + + public void testSingleQuoteInStringSerialization() throws Exception { + String valueWithQuotes = "beforeQuote'afterQuote"; + String jsonRepresentation = gson.toJson(valueWithQuotes); + assertEquals(valueWithQuotes, gson.fromJson(jsonRepresentation, String.class)); + } + + public void testSingleQuoteInStringDeserialization() throws Exception { + String value = "beforeQuote'afterQuote"; + String actual = gson.fromJson("\"" + value + "\"", String.class); + assertEquals(value, actual); + } + + public void testEscapingQuotesInStringSerialization() throws Exception { + String valueWithQuotes = "beforeQuote\"afterQuote"; + String jsonRepresentation = gson.toJson(valueWithQuotes); + String target = gson.fromJson(jsonRepresentation, String.class); + assertEquals(valueWithQuotes, target); + } + + public void testEscapingQuotesInStringDeserialization() throws Exception { + String value = "beforeQuote\\\"afterQuote"; + String actual = gson.fromJson("\"" + value + "\"", String.class); + String expected = "beforeQuote\"afterQuote"; + assertEquals(expected, actual); + } + + public void testStringValueAsSingleElementArraySerialization() throws Exception { + String[] target = {"abc"}; + assertEquals("[\"abc\"]", gson.toJson(target)); + assertEquals("[\"abc\"]", gson.toJson(target, String[].class)); + } + + public void testStringValueAsSingleElementArrayDeserialization() throws Exception { + String value = "someRandomStringValue"; + String actual = gson.fromJson("[\"" + value + "\"]", String.class); + assertEquals(value, actual); + } + + public void testReallyLongStringsDeserialization() throws Exception { + StringBuilder sb = new StringBuilder(8096); + sb.append("Once upon a time there was a really long string that caused a StackOverFlowError\n"); + sb.append("and now it is fixed and instead throws a JsonParserException.....Yippie!!!\n"); + sb.append("Wow....that is a really long string that is meant to be an exception stack trace, "); + sb.append("but is not :( \n\n\n\n\n\n."); + sb.append("lalalalala \n\n\n."); + sb.append("C'est la vie!!! \n\n\n\n\n"); + + for (int i = 0; i < 10; i++) { + sb.append(sb.toString()); + } + + while (true) { + try { + String stackTrace = sb.toString(); + sb.append(stackTrace); + String json = "{\"message\":\"Error message.\"," + + "\"stackTrace\":\"" + stackTrace + "\"}"; + parseLongJson(json); + } catch (JsonParseException expected) { + break; + } + } + } + + private void parseLongJson(String json) throws JsonParseException { + ExceptionHolder target = gson.fromJson(json, ExceptionHolder.class); + assertTrue(target.message.contains("Error")); + assertTrue(target.stackTrace.contains("Yippie")); + } + + private static class ExceptionHolder { + public final String message; + public final String stackTrace; + public ExceptionHolder() { + this("", ""); + } + public ExceptionHolder(String message, String stackTrace) { + this.message = message; + this.stackTrace = stackTrace; + } + } +}