1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-18 16:24:19 -07:00

Merge pull request #75 from johnjaylward/PopulateMapMoreStrict

Populate map more strict
This commit is contained in:
Sean Leary 2017-07-19 18:57:55 -05:00 committed by GitHub
commit cf411b3187
22 changed files with 561 additions and 26 deletions

View file

@ -3,6 +3,7 @@ package org.json.junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -30,6 +31,22 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONPointerException;
import org.json.XML;
import org.json.junit.data.BrokenToString;
import org.json.junit.data.ExceptionalBean;
import org.json.junit.data.Fraction;
import org.json.junit.data.GenericBean;
import org.json.junit.data.GenericBeanInt;
import org.json.junit.data.MyBean;
import org.json.junit.data.MyBigNumberBean;
import org.json.junit.data.MyEnum;
import org.json.junit.data.MyEnumField;
import org.json.junit.data.MyJsonString;
import org.json.junit.data.MyNumber;
import org.json.junit.data.MyNumberContainer;
import org.json.junit.data.MyPublicClass;
import org.json.junit.data.Singleton;
import org.json.junit.data.SingletonEnum;
import org.json.junit.data.WeirdList;
import org.junit.Test;
import com.jayway.jsonpath.Configuration;
@ -484,7 +501,7 @@ public class JSONObjectTest {
@Test
public void jsonObjectByResourceBundle() {
JSONObject jsonObject = new
JSONObject("org.json.junit.StringsResourceBundle",
JSONObject("org.json.junit.data.StringsResourceBundle",
Locale.getDefault());
// validate JSON
@ -2575,18 +2592,109 @@ public class JSONObjectTest {
// assert that the new map is mutable
assertTrue("Removing a key should succeed", map.remove("key3") != null);
assertTrue("Map should have 2 elements", map.size() == 2);
}
/**
* test class for verifying write errors.
* @author John Aylward
*
* test that validates a singleton can be serialized as a bean.
*/
private static class BrokenToString {
@Override
public String toString() {
throw new IllegalStateException("Something went horribly wrong!");
}
@Test
public void testSingletonBean() {
final JSONObject jo = new JSONObject(Singleton.getInstance());
assertEquals(jo.keySet().toString(), 1, jo.length());
assertEquals(0, jo.get("someInt"));
assertEquals(null, jo.opt("someString"));
// Update the singleton values
Singleton.getInstance().setSomeInt(42);
Singleton.getInstance().setSomeString("Something");
final JSONObject jo2 = new JSONObject(Singleton.getInstance());
assertEquals(2, jo2.length());
assertEquals(42, jo2.get("someInt"));
assertEquals("Something", jo2.get("someString"));
// ensure our original jo hasn't changed.
assertEquals(0, jo.get("someInt"));
assertEquals(null, jo.opt("someString"));
}
/**
* test that validates a singleton can be serialized as a bean.
*/
@Test
public void testSingletonEnumBean() {
final JSONObject jo = new JSONObject(SingletonEnum.getInstance());
assertEquals(jo.keySet().toString(), 1, jo.length());
assertEquals(0, jo.get("someInt"));
assertEquals(null, jo.opt("someString"));
// Update the singleton values
SingletonEnum.getInstance().setSomeInt(42);
SingletonEnum.getInstance().setSomeString("Something");
final JSONObject jo2 = new JSONObject(SingletonEnum.getInstance());
assertEquals(2, jo2.length());
assertEquals(42, jo2.get("someInt"));
assertEquals("Something", jo2.get("someString"));
// ensure our original jo hasn't changed.
assertEquals(0, jo.get("someInt"));
assertEquals(null, jo.opt("someString"));
}
/**
* Test to validate that a generic class can be serialized as a bean.
*/
@Test
public void testGenericBean() {
GenericBean<Integer> bean = new GenericBean<>(42);
final JSONObject jo = new JSONObject(bean);
assertEquals(jo.keySet().toString(), 8, jo.length());
assertEquals(42, jo.get("genericValue"));
assertEquals("Expected the getter to only be called once",
1, bean.genericGetCounter);
assertEquals(0, bean.genericSetCounter);
}
/**
* Test to validate that a generic class can be serialized as a bean.
*/
@Test
public void testGenericIntBean() {
GenericBeanInt bean = new GenericBeanInt(42);
final JSONObject jo = new JSONObject(bean);
assertEquals(jo.keySet().toString(), 9, jo.length());
assertEquals(42, jo.get("genericValue"));
assertEquals("Expected the getter to only be called once",
1, bean.genericGetCounter);
assertEquals(0, bean.genericSetCounter);
}
/**
* Test to verify <code>key</code> limitations in the JSONObject bean serializer.
*/
@Test
public void testWierdListBean() {
WeirdList bean = new WeirdList(42, 43, 44);
final JSONObject jo = new JSONObject(bean);
// get() should have a key of 0 length
// get(int) should be ignored base on parameter count
// getInt(int) should also be ignored based on parameter count
// add(Integer) should be ignore as it doesn't start with get/is and also has a parameter
// getALL should be mapped
assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
1, jo.length());
assertNotNull(jo.get("ALL"));
}
/**
* Tests the exception portions of populateMap.
*/
@Test
public void testExceptionalBean() {
ExceptionalBean bean = new ExceptionalBean();
final JSONObject jo = new JSONObject(bean);
assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
1, jo.length());
assertTrue(jo.get("closeable") instanceof JSONObject);
assertTrue(jo.getJSONObject("closeable").has("string"));
}
}