Skip to content

Commit 49117f3

Browse files
committed
Adds new tests for testing bean->JSONObject mapping
1 parent 0e3f23d commit 49117f3

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed

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

Lines changed: 80 additions & 0 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;
@@ -32,6 +33,8 @@
3233
import org.json.XML;
3334
import org.json.junit.data.BrokenToString;
3435
import org.json.junit.data.Fraction;
36+
import org.json.junit.data.GenericBean;
37+
import org.json.junit.data.GenericBeanInt;
3538
import org.json.junit.data.MyBean;
3639
import org.json.junit.data.MyBigNumberBean;
3740
import org.json.junit.data.MyEnum;
@@ -40,6 +43,9 @@
4043
import org.json.junit.data.MyNumber;
4144
import org.json.junit.data.MyNumberContainer;
4245
import org.json.junit.data.MyPublicClass;
46+
import org.json.junit.data.Singleton;
47+
import org.json.junit.data.SingletonEnum;
48+
import org.json.junit.data.WeirdList;
4349
import org.junit.Test;
4450

4551
import com.jayway.jsonpath.Configuration;
@@ -2583,4 +2589,78 @@ public void toMap() {
25832589
assertTrue("Removing a key should succeed", map.remove("key3") != null);
25842590
assertTrue("Map should have 2 elements", map.size() == 2);
25852591
}
2592+
2593+
@Test
2594+
public void testSingletonBean() {
2595+
final JSONObject jo = new JSONObject(Singleton.getInstance());
2596+
assertEquals(jo.keySet().toString(), 1, jo.length());
2597+
assertEquals(0, jo.get("someInt"));
2598+
assertEquals(null, jo.opt("someString"));
2599+
2600+
// Update the singleton values
2601+
Singleton.getInstance().setSomeInt(42);
2602+
Singleton.getInstance().setSomeString("Something");
2603+
final JSONObject jo2 = new JSONObject(Singleton.getInstance());
2604+
assertEquals(2, jo2.length());
2605+
assertEquals(42, jo2.get("someInt"));
2606+
assertEquals("Something", jo2.get("someString"));
2607+
2608+
// ensure our original jo hasn't changed.
2609+
assertEquals(0, jo.get("someInt"));
2610+
assertEquals(null, jo.opt("someString"));
2611+
}
2612+
@Test
2613+
public void testSingletonEnumBean() {
2614+
final JSONObject jo = new JSONObject(SingletonEnum.getInstance());
2615+
assertEquals(jo.keySet().toString(), 1, jo.length());
2616+
assertEquals(0, jo.get("someInt"));
2617+
assertEquals(null, jo.opt("someString"));
2618+
2619+
// Update the singleton values
2620+
SingletonEnum.getInstance().setSomeInt(42);
2621+
SingletonEnum.getInstance().setSomeString("Something");
2622+
final JSONObject jo2 = new JSONObject(SingletonEnum.getInstance());
2623+
assertEquals(2, jo2.length());
2624+
assertEquals(42, jo2.get("someInt"));
2625+
assertEquals("Something", jo2.get("someString"));
2626+
2627+
// ensure our original jo hasn't changed.
2628+
assertEquals(0, jo.get("someInt"));
2629+
assertEquals(null, jo.opt("someString"));
2630+
}
2631+
2632+
@Test
2633+
public void testGenericBean() {
2634+
GenericBean<Integer> bean = new GenericBean<>(42);
2635+
final JSONObject jo = new JSONObject(bean);
2636+
assertEquals(jo.keySet().toString(), 8, jo.length());
2637+
assertEquals(42, jo.get("genericValue"));
2638+
assertEquals("Expected the getter to only be called once",
2639+
1, bean.genericGetCounter);
2640+
assertEquals(0, bean.genericSetCounter);
2641+
}
2642+
2643+
@Test
2644+
public void testGenericIntBean() {
2645+
GenericBeanInt bean = new GenericBeanInt(42);
2646+
final JSONObject jo = new JSONObject(bean);
2647+
assertEquals(jo.keySet().toString(), 9, jo.length());
2648+
assertEquals(42, jo.get("genericValue"));
2649+
assertEquals("Expected the getter to only be called once",
2650+
1, bean.genericGetCounter);
2651+
assertEquals(0, bean.genericSetCounter);
2652+
}
2653+
2654+
@Test
2655+
public void testWierdListBean() {
2656+
WeirdList bean = new WeirdList(42, 43, 44);
2657+
final JSONObject jo = new JSONObject(bean);
2658+
// get() should have a key of 0 length
2659+
// get(int) should be ignored base on parameter count
2660+
// getInt(int) should also be ignored based on parameter count
2661+
// add(Integer) should be ignore as it doesn't start with get/is and also has a parameter
2662+
// getALL should be mapped
2663+
assertEquals("Expected 1 key to mapped "+jo.keySet().toString(), 1, jo.length());
2664+
assertNotNull(jo.get("ALL"));
2665+
}
25862666
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
public GenericBean(T genericValue) {
14+
super();
15+
this.genericValue = genericValue;
16+
}
17+
18+
/** */
19+
private T genericValue;
20+
/** to be used by the calling test to see how often the getter is called */
21+
public int genericGetCounter;
22+
/** to be used by the calling test to see how often the setter is called */
23+
public int genericSetCounter;
24+
25+
/** @return the genericValue */
26+
public T getGenericValue() {
27+
this.genericGetCounter++;
28+
return this.genericValue;
29+
}
30+
31+
/** sets the generic value */
32+
public void setGenericValue(T genericValue) {
33+
this.genericSetCounter++;
34+
this.genericValue = genericValue;
35+
}
36+
37+
@Override
38+
public Integer getIntKey() {
39+
return Integer.valueOf(42);
40+
}
41+
42+
@Override
43+
public Double getDoubleKey() {
44+
return Double.valueOf(4.2);
45+
}
46+
47+
@Override
48+
public String getStringKey() {
49+
return "MyString Key";
50+
}
51+
52+
@Override
53+
public String getEscapeStringKey() {
54+
return "\"My String with \"s";
55+
}
56+
57+
@Override
58+
public Boolean isTrueKey() {
59+
return Boolean.TRUE;
60+
}
61+
62+
@Override
63+
public Boolean isFalseKey() {
64+
return Boolean.FALSE;
65+
}
66+
67+
@Override
68+
public StringReader getStringReaderKey() {
69+
return new StringReader("Some String Value in a reader");
70+
}
71+
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/** return false. should not be beanable */
20+
public boolean getable() {
21+
return false;
22+
}
23+
24+
/** */
25+
public GenericBeanInt(Integer genericValue) {
26+
super(genericValue);
27+
}
28+
29+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.json.junit.data;
2+
3+
/**
4+
* Sample singleton for use with bean testing.
5+
*
6+
* @author John Aylward
7+
*
8+
*/
9+
public final class Singleton {
10+
/** */
11+
private int someInt;
12+
/** */
13+
private String someString;
14+
/** single instance. */
15+
private static final Singleton INSTANCE = new Singleton();
16+
17+
/** @return the singleton instance. */
18+
public static final Singleton getInstance() {
19+
return INSTANCE;
20+
}
21+
22+
/** */
23+
private Singleton() {
24+
if (INSTANCE != null) {
25+
throw new IllegalStateException("Already instantiated");
26+
}
27+
}
28+
29+
@Override
30+
protected Object clone() throws CloneNotSupportedException {
31+
return INSTANCE;
32+
}
33+
34+
/** @return someInt */
35+
public int getSomeInt() {
36+
return someInt;
37+
}
38+
39+
/** sets someInt */
40+
public void setSomeInt(int someInt) {
41+
this.someInt = someInt;
42+
}
43+
44+
/** @return someString */
45+
public String getSomeString() {
46+
return someString;
47+
}
48+
49+
/** sets someString */
50+
public void setSomeString(String someString) {
51+
this.someString = someString;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
final int prime = 31;
57+
int result = 1;
58+
result = prime * result + someInt;
59+
result = prime * result + ((someString == null) ? 0 : someString.hashCode());
60+
return result;
61+
}
62+
63+
@Override
64+
public boolean equals(Object obj) {
65+
if (this == obj)
66+
return true;
67+
if (obj == null)
68+
return false;
69+
if (getClass() != obj.getClass())
70+
return false;
71+
Singleton other = (Singleton) obj;
72+
if (someInt != other.someInt)
73+
return false;
74+
if (someString == null) {
75+
if (other.someString != null)
76+
return false;
77+
} else if (!someString.equals(other.someString))
78+
return false;
79+
return true;
80+
}
81+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.json.junit.data;
2+
3+
/**
4+
* Sample singleton done as an Enum for use with bean testing.
5+
*
6+
* @author John Aylward
7+
*
8+
*/
9+
public enum SingletonEnum {
10+
INSTANCE;
11+
/** */
12+
private int someInt;
13+
/** */
14+
private String someString;
15+
16+
/** single instance. */
17+
18+
/**
19+
* @return the singleton instance. I a real application, I'd hope no one did
20+
* this to an enum singleton.
21+
*/
22+
public static final SingletonEnum getInstance() {
23+
return INSTANCE;
24+
}
25+
26+
/** */
27+
private SingletonEnum() {
28+
}
29+
30+
/** @return someInt */
31+
public int getSomeInt() {
32+
return someInt;
33+
}
34+
35+
/** sets someInt */
36+
public void setSomeInt(int someInt) {
37+
this.someInt = someInt;
38+
}
39+
40+
/** @return someString */
41+
public String getSomeString() {
42+
return someString;
43+
}
44+
45+
/** sets someString */
46+
public void setSomeString(String someString) {
47+
this.someString = someString;
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
*
3+
*/
4+
package org.json.junit.data;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
/**
11+
* @author John Aylward
12+
*/
13+
public class WeirdList {
14+
/** */
15+
private final List<Integer> list = new ArrayList<>();
16+
17+
public WeirdList(Integer... vals) {
18+
this.list.addAll(Arrays.asList(vals));
19+
}
20+
21+
/** gets a copy of the list */
22+
public List<Integer> get() {
23+
return new ArrayList<>(this.list);
24+
}
25+
26+
/** gets a copy of the list */
27+
public List<Integer> getALL() {
28+
return new ArrayList<>(this.list);
29+
}
30+
31+
/** get an index */
32+
public Integer get(int i) {
33+
return this.list.get(i);
34+
}
35+
36+
/** get an index */
37+
public int getInt(int i) {
38+
return this.list.get(i);
39+
}
40+
41+
/** adds a new value to the end of the list */
42+
public void add(Integer value) {
43+
this.list.add(value);
44+
}
45+
}

0 commit comments

Comments
 (0)