Skip to content

Commit cf411b3

Browse files
authored
Merge pull request stleary#75 from johnjaylward/PopulateMapMoreStrict
Populate map more strict
2 parents 1aeadd1 + 38d1122 commit cf411b3

22 files changed

+560
-25
lines changed

src/test/java/org/json/junit/EnumTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
import org.json.JSONArray;
1212
import org.json.JSONObject;
13+
import org.json.junit.data.MyEnum;
14+
import org.json.junit.data.MyEnumClass;
15+
import org.json.junit.data.MyEnumField;
1316
import org.junit.Test;
1417

1518
import com.jayway.jsonpath.Configuration;
@@ -195,7 +198,7 @@ public void enumValueToString() {
195198
* However, an enum within another class will not be rendered
196199
* unless that class overrides default toString()
197200
*/
198-
String expectedStr3 = "\"org.json.junit.MyEnumClass@";
201+
String expectedStr3 = "\"org.json.junit.data.MyEnumClass@";
199202
myEnumClass.setMyEnum(MyEnum.VAL1);
200203
myEnumClass.setMyEnumField(MyEnumField.VAL1);
201204
String str3 = JSONObject.valueToString(myEnumClass);

src/test/java/org/json/junit/JSONObjectLocaleTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.*;
66

77
import org.json.*;
8+
import org.json.junit.data.MyLocaleBean;
89
import org.junit.*;
910

1011
/**

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 117 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertNotEquals;
6+
import static org.junit.Assert.assertNotNull;
67
import static org.junit.Assert.assertNull;
78
import static org.junit.Assert.assertTrue;
89
import static org.junit.Assert.fail;
@@ -30,6 +31,22 @@
3031
import org.json.JSONObject;
3132
import org.json.JSONPointerException;
3233
import org.json.XML;
34+
import org.json.junit.data.BrokenToString;
35+
import org.json.junit.data.ExceptionalBean;
36+
import org.json.junit.data.Fraction;
37+
import org.json.junit.data.GenericBean;
38+
import org.json.junit.data.GenericBeanInt;
39+
import org.json.junit.data.MyBean;
40+
import org.json.junit.data.MyBigNumberBean;
41+
import org.json.junit.data.MyEnum;
42+
import org.json.junit.data.MyEnumField;
43+
import org.json.junit.data.MyJsonString;
44+
import org.json.junit.data.MyNumber;
45+
import org.json.junit.data.MyNumberContainer;
46+
import org.json.junit.data.MyPublicClass;
47+
import org.json.junit.data.Singleton;
48+
import org.json.junit.data.SingletonEnum;
49+
import org.json.junit.data.WeirdList;
3350
import org.junit.Test;
3451

3552
import com.jayway.jsonpath.Configuration;
@@ -484,7 +501,7 @@ public void jsonObjectByObjectAndNames() {
484501
@Test
485502
public void jsonObjectByResourceBundle() {
486503
JSONObject jsonObject = new
487-
JSONObject("org.json.junit.StringsResourceBundle",
504+
JSONObject("org.json.junit.data.StringsResourceBundle",
488505
Locale.getDefault());
489506

490507
// validate JSON
@@ -2575,18 +2592,109 @@ public void toMap() {
25752592
// assert that the new map is mutable
25762593
assertTrue("Removing a key should succeed", map.remove("key3") != null);
25772594
assertTrue("Map should have 2 elements", map.size() == 2);
2595+
}
2596+
2597+
/**
2598+
* test that validates a singleton can be serialized as a bean.
2599+
*/
2600+
@Test
2601+
public void testSingletonBean() {
2602+
final JSONObject jo = new JSONObject(Singleton.getInstance());
2603+
assertEquals(jo.keySet().toString(), 1, jo.length());
2604+
assertEquals(0, jo.get("someInt"));
2605+
assertEquals(null, jo.opt("someString"));
2606+
2607+
// Update the singleton values
2608+
Singleton.getInstance().setSomeInt(42);
2609+
Singleton.getInstance().setSomeString("Something");
2610+
final JSONObject jo2 = new JSONObject(Singleton.getInstance());
2611+
assertEquals(2, jo2.length());
2612+
assertEquals(42, jo2.get("someInt"));
2613+
assertEquals("Something", jo2.get("someString"));
25782614

2615+
// ensure our original jo hasn't changed.
2616+
assertEquals(0, jo.get("someInt"));
2617+
assertEquals(null, jo.opt("someString"));
2618+
}
2619+
2620+
/**
2621+
* test that validates a singleton can be serialized as a bean.
2622+
*/
2623+
@Test
2624+
public void testSingletonEnumBean() {
2625+
final JSONObject jo = new JSONObject(SingletonEnum.getInstance());
2626+
assertEquals(jo.keySet().toString(), 1, jo.length());
2627+
assertEquals(0, jo.get("someInt"));
2628+
assertEquals(null, jo.opt("someString"));
2629+
2630+
// Update the singleton values
2631+
SingletonEnum.getInstance().setSomeInt(42);
2632+
SingletonEnum.getInstance().setSomeString("Something");
2633+
final JSONObject jo2 = new JSONObject(SingletonEnum.getInstance());
2634+
assertEquals(2, jo2.length());
2635+
assertEquals(42, jo2.get("someInt"));
2636+
assertEquals("Something", jo2.get("someString"));
2637+
2638+
// ensure our original jo hasn't changed.
2639+
assertEquals(0, jo.get("someInt"));
2640+
assertEquals(null, jo.opt("someString"));
25792641
}
25802642

25812643
/**
2582-
* test class for verifying write errors.
2583-
* @author John Aylward
2584-
*
2644+
* Test to validate that a generic class can be serialized as a bean.
25852645
*/
2586-
private static class BrokenToString {
2587-
@Override
2588-
public String toString() {
2589-
throw new IllegalStateException("Something went horribly wrong!");
2590-
}
2646+
@Test
2647+
public void testGenericBean() {
2648+
GenericBean<Integer> bean = new GenericBean<>(42);
2649+
final JSONObject jo = new JSONObject(bean);
2650+
assertEquals(jo.keySet().toString(), 8, jo.length());
2651+
assertEquals(42, jo.get("genericValue"));
2652+
assertEquals("Expected the getter to only be called once",
2653+
1, bean.genericGetCounter);
2654+
assertEquals(0, bean.genericSetCounter);
2655+
}
2656+
2657+
/**
2658+
* Test to validate that a generic class can be serialized as a bean.
2659+
*/
2660+
@Test
2661+
public void testGenericIntBean() {
2662+
GenericBeanInt bean = new GenericBeanInt(42);
2663+
final JSONObject jo = new JSONObject(bean);
2664+
assertEquals(jo.keySet().toString(), 9, jo.length());
2665+
assertEquals(42, jo.get("genericValue"));
2666+
assertEquals("Expected the getter to only be called once",
2667+
1, bean.genericGetCounter);
2668+
assertEquals(0, bean.genericSetCounter);
2669+
}
2670+
2671+
/**
2672+
* Test to verify <code>key</code> limitations in the JSONObject bean serializer.
2673+
*/
2674+
@Test
2675+
public void testWierdListBean() {
2676+
WeirdList bean = new WeirdList(42, 43, 44);
2677+
final JSONObject jo = new JSONObject(bean);
2678+
// get() should have a key of 0 length
2679+
// get(int) should be ignored base on parameter count
2680+
// getInt(int) should also be ignored based on parameter count
2681+
// add(Integer) should be ignore as it doesn't start with get/is and also has a parameter
2682+
// getALL should be mapped
2683+
assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
2684+
1, jo.length());
2685+
assertNotNull(jo.get("ALL"));
2686+
}
2687+
2688+
/**
2689+
* Tests the exception portions of populateMap.
2690+
*/
2691+
@Test
2692+
public void testExceptionalBean() {
2693+
ExceptionalBean bean = new ExceptionalBean();
2694+
final JSONObject jo = new JSONObject(bean);
2695+
assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
2696+
1, jo.length());
2697+
assertTrue(jo.get("closeable") instanceof JSONObject);
2698+
assertTrue(jo.getJSONObject("closeable").has("string"));
25912699
}
25922700
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.json.junit.data;
2+
3+
/**
4+
* test class for verifying write errors.
5+
* @author John Aylward
6+
*
7+
*/
8+
public class BrokenToString {
9+
@Override
10+
public String toString() {
11+
throw new IllegalStateException("Something went horribly wrong!");
12+
}
13+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
*
3+
*/
4+
package org.json.junit.data;
5+
6+
import java.io.Closeable;
7+
import java.io.IOException;
8+
import java.lang.reflect.InvocationTargetException;
9+
10+
import org.json.JSONObject;
11+
12+
/**
13+
* Object for testing the exception handling in {@link JSONObject#populateMap}.
14+
*
15+
* @author John Aylward
16+
*/
17+
public class ExceptionalBean {
18+
/**
19+
* @return a closeable.
20+
*/
21+
public Closeable getCloseable() {
22+
// anonymous inner class did not work...
23+
return new MyCloseable();
24+
}
25+
26+
/**
27+
* @return Nothing really. Just can't be void.
28+
* @throws IllegalAccessException
29+
* always thrown
30+
*/
31+
public int getIllegalAccessException() throws IllegalAccessException {
32+
throw new IllegalAccessException("Yup, it's illegal");
33+
}
34+
35+
/**
36+
* @return Nothing really. Just can't be void.
37+
* @throws IllegalArgumentException
38+
* always thrown
39+
*/
40+
public int getIllegalArgumentException() throws IllegalArgumentException {
41+
throw new IllegalArgumentException("Yup, it's illegal");
42+
}
43+
44+
/**
45+
* @return Nothing really. Just can't be void.
46+
* @throws InvocationTargetException
47+
* always thrown
48+
*/
49+
public int getInvocationTargetException() throws InvocationTargetException {
50+
throw new InvocationTargetException(new Exception("Yup, it's illegal"));
51+
}
52+
53+
/** My closeable class. */
54+
public static final class MyCloseable implements Closeable {
55+
56+
/**
57+
* @return a string
58+
*/
59+
@SuppressWarnings("unused")
60+
public String getString() {
61+
return "Yup, it's closeable";
62+
}
63+
64+
@Override
65+
public void close() throws IOException {
66+
throw new IOException("Closing is too hard!");
67+
}
68+
}
69+
}

src/test/java/org/json/junit/Fraction.java renamed to src/test/java/org/json/junit/data/Fraction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.json.junit;
1+
package org.json.junit.data;
22

33
import java.math.BigDecimal;
44
import java.math.BigInteger;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.json.junit.data;
2+
3+
import java.io.StringReader;
4+
5+
/**
6+
*
7+
* @author John Aylward
8+
*
9+
* @param <T>
10+
* generic number value
11+
*/
12+
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
13+
/**
14+
* @param genericValue
15+
* value to initiate with
16+
*/
17+
public GenericBean(T genericValue) {
18+
super();
19+
this.genericValue = genericValue;
20+
}
21+
22+
/** */
23+
private T genericValue;
24+
/** to be used by the calling test to see how often the getter is called */
25+
public int genericGetCounter;
26+
/** to be used by the calling test to see how often the setter is called */
27+
public int genericSetCounter;
28+
29+
/** @return the genericValue */
30+
public T getGenericValue() {
31+
this.genericGetCounter++;
32+
return this.genericValue;
33+
}
34+
35+
/**
36+
* @param genericValue
37+
* generic value to set
38+
*/
39+
public void setGenericValue(T genericValue) {
40+
this.genericSetCounter++;
41+
this.genericValue = genericValue;
42+
}
43+
44+
@Override
45+
public Integer getIntKey() {
46+
return Integer.valueOf(42);
47+
}
48+
49+
@Override
50+
public Double getDoubleKey() {
51+
return Double.valueOf(4.2);
52+
}
53+
54+
@Override
55+
public String getStringKey() {
56+
return "MyString Key";
57+
}
58+
59+
@Override
60+
public String getEscapeStringKey() {
61+
return "\"My String with \"s";
62+
}
63+
64+
@Override
65+
public Boolean isTrueKey() {
66+
return Boolean.TRUE;
67+
}
68+
69+
@Override
70+
public Boolean isFalseKey() {
71+
return Boolean.FALSE;
72+
}
73+
74+
@Override
75+
public StringReader getStringReaderKey() {
76+
return new StringReader("Some String Value in a reader");
77+
}
78+
79+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
*
3+
*/
4+
package org.json.junit.data;
5+
6+
/**
7+
* @author john
8+
*
9+
*/
10+
public class GenericBeanInt extends GenericBean<Integer> {
11+
/** */
12+
final char a = 'A';
13+
14+
/** @return the a */
15+
public char getA() {
16+
return a;
17+
}
18+
19+
/**
20+
* Should not be beanable
21+
*
22+
* @return false
23+
*/
24+
public boolean getable() {
25+
return false;
26+
}
27+
28+
/**
29+
* @param genericValue
30+
* the value to initiate with.
31+
*/
32+
public GenericBeanInt(Integer genericValue) {
33+
super(genericValue);
34+
}
35+
36+
/** override to generate a bridge method */
37+
@Override
38+
public Integer getGenericValue() {
39+
return super.getGenericValue();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)