refactored String related functional tests out in a separate test class.
This commit is contained in:
parent
0b9c739a7b
commit
3b8404dac5
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
115
gson/src/test/java/com/google/gson/functional/StringTest.java
Normal file
115
gson/src/test/java/com/google/gson/functional/StringTest.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user