gson-comments/gson/src/test/java/com/google/gson/jf/LenientCollectionTest.java

46 lines
1.2 KiB
Java

package com.google.gson.jf;
import com.google.gson.*;
import junit.framework.TestCase;
import java.util.*;
import static org.junit.Assert.*;
import static com.google.gson.jf.ExtraAssert.*;
public class LenientCollectionTest extends TestCase {
private Gson lenient;
private Gson strict;
private static final String json = "{\"collection\": \"example\"}";
@Override
protected void setUp() throws Exception {
super.setUp();
lenient = new GsonBuilder().setLenient().create();
strict = new GsonBuilder().create();
}
public void testLenientArray() {
assertArrayEquals(new String[] {"example"}, lenient.fromJson(json, StringArrayBox.class).collection);
}
public void testLenientList() {
assertListEquals(List.of("example"), lenient.fromJson(json, StringListBox.class).collection);
}
public void testStrictArray() {
try {
strict.fromJson(json, StringArrayBox.class);
fail("Strict Gson should not deserialize array without brackets");
} catch (JsonParseException expected) {
}
}
public void testStrictList() {
try {
strict.fromJson(json, StringListBox.class);
fail("Strict Gson should not deserialize list without brackets");
} catch (JsonParseException expected) {
}
}
}