Skip to content

Commit

Permalink
Merge pull request #75 from johnjaylward/PopulateMapMoreStrict
Browse files Browse the repository at this point in the history
Populate map more strict
  • Loading branch information
stleary authored Jul 19, 2017
2 parents 1aeadd1 + 38d1122 commit cf411b3
Show file tree
Hide file tree
Showing 22 changed files with 560 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/test/java/org/json/junit/EnumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.junit.data.MyEnum;
import org.json.junit.data.MyEnumClass;
import org.json.junit.data.MyEnumField;
import org.junit.Test;

import com.jayway.jsonpath.Configuration;
Expand Down Expand Up @@ -195,7 +198,7 @@ public void enumValueToString() {
* However, an enum within another class will not be rendered
* unless that class overrides default toString()
*/
String expectedStr3 = "\"org.json.junit.MyEnumClass@";
String expectedStr3 = "\"org.json.junit.data.MyEnumClass@";
myEnumClass.setMyEnum(MyEnum.VAL1);
myEnumClass.setMyEnumField(MyEnumField.VAL1);
String str3 = JSONObject.valueToString(myEnumClass);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/json/junit/JSONObjectLocaleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.*;

import org.json.*;
import org.json.junit.data.MyLocaleBean;
import org.junit.*;

/**
Expand Down
126 changes: 117 additions & 9 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -30,6 +31,22 @@
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;
Expand Down Expand Up @@ -484,7 +501,7 @@ public void jsonObjectByObjectAndNames() {
@Test
public void jsonObjectByResourceBundle() {
JSONObject jsonObject = new
JSONObject("org.json.junit.StringsResourceBundle",
JSONObject("org.json.junit.data.StringsResourceBundle",
Locale.getDefault());

// validate JSON
Expand Down Expand Up @@ -2575,18 +2592,109 @@ public void toMap() {
// 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 that validates a singleton can be serialized as a bean.
*/
@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 class for verifying write errors.
* @author John Aylward
*
* Test to validate that a generic class can be serialized as a bean.
*/
private static class BrokenToString {
@Override
public String toString() {
throw new IllegalStateException("Something went horribly wrong!");
}
@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"));
}
}
13 changes: 13 additions & 0 deletions src/test/java/org/json/junit/data/BrokenToString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.json.junit.data;

/**
* test class for verifying write errors.
* @author John Aylward
*
*/
public class BrokenToString {
@Override
public String toString() {
throw new IllegalStateException("Something went horribly wrong!");
}
}
69 changes: 69 additions & 0 deletions src/test/java/org/json/junit/data/ExceptionalBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
*
*/
package org.json.junit.data;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.json.JSONObject;

/**
* Object for testing the exception handling in {@link JSONObject#populateMap}.
*
* @author John Aylward
*/
public class ExceptionalBean {
/**
* @return a closeable.
*/
public Closeable getCloseable() {
// anonymous inner class did not work...
return new MyCloseable();
}

/**
* @return Nothing really. Just can't be void.
* @throws IllegalAccessException
* always thrown
*/
public int getIllegalAccessException() throws IllegalAccessException {
throw new IllegalAccessException("Yup, it's illegal");
}

/**
* @return Nothing really. Just can't be void.
* @throws IllegalArgumentException
* always thrown
*/
public int getIllegalArgumentException() throws IllegalArgumentException {
throw new IllegalArgumentException("Yup, it's illegal");
}

/**
* @return Nothing really. Just can't be void.
* @throws InvocationTargetException
* always thrown
*/
public int getInvocationTargetException() throws InvocationTargetException {
throw new InvocationTargetException(new Exception("Yup, it's illegal"));
}

/** My closeable class. */
public static final class MyCloseable implements Closeable {

/**
* @return a string
*/
@SuppressWarnings("unused")
public String getString() {
return "Yup, it's closeable";
}

@Override
public void close() throws IOException {
throw new IOException("Closing is too hard!");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.json.junit;
package org.json.junit.data;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/org/json/junit/data/GenericBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.json.junit.data;

import java.io.StringReader;

/**
*
* @author John Aylward
*
* @param <T>
* generic number value
*/
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
/**
* @param genericValue
* value to initiate with
*/
public GenericBean(T genericValue) {
super();
this.genericValue = genericValue;
}

/** */
private T genericValue;
/** to be used by the calling test to see how often the getter is called */
public int genericGetCounter;
/** to be used by the calling test to see how often the setter is called */
public int genericSetCounter;

/** @return the genericValue */
public T getGenericValue() {
this.genericGetCounter++;
return this.genericValue;
}

/**
* @param genericValue
* generic value to set
*/
public void setGenericValue(T genericValue) {
this.genericSetCounter++;
this.genericValue = genericValue;
}

@Override
public Integer getIntKey() {
return Integer.valueOf(42);
}

@Override
public Double getDoubleKey() {
return Double.valueOf(4.2);
}

@Override
public String getStringKey() {
return "MyString Key";
}

@Override
public String getEscapeStringKey() {
return "\"My String with \"s";
}

@Override
public Boolean isTrueKey() {
return Boolean.TRUE;
}

@Override
public Boolean isFalseKey() {
return Boolean.FALSE;
}

@Override
public StringReader getStringReaderKey() {
return new StringReader("Some String Value in a reader");
}

}
42 changes: 42 additions & 0 deletions src/test/java/org/json/junit/data/GenericBeanInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
*
*/
package org.json.junit.data;

/**
* @author john
*
*/
public class GenericBeanInt extends GenericBean<Integer> {
/** */
final char a = 'A';

/** @return the a */
public char getA() {
return a;
}

/**
* Should not be beanable
*
* @return false
*/
public boolean getable() {
return false;
}

/**
* @param genericValue
* the value to initiate with.
*/
public GenericBeanInt(Integer genericValue) {
super(genericValue);
}

/** override to generate a bridge method */
@Override
public Integer getGenericValue() {
return super.getGenericValue();
}

}
Loading

0 comments on commit cf411b3

Please sign in to comment.