mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Adds new tests for testing bean->JSONObject mapping
This commit is contained in:
parent
0e3f23d7a1
commit
49117f33dc
6 changed files with 356 additions and 0 deletions
|
@ -3,6 +3,7 @@ package org.json.junit;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
@ -32,6 +33,8 @@ import org.json.JSONPointerException;
|
||||||
import org.json.XML;
|
import org.json.XML;
|
||||||
import org.json.junit.data.BrokenToString;
|
import org.json.junit.data.BrokenToString;
|
||||||
import org.json.junit.data.Fraction;
|
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.MyBean;
|
||||||
import org.json.junit.data.MyBigNumberBean;
|
import org.json.junit.data.MyBigNumberBean;
|
||||||
import org.json.junit.data.MyEnum;
|
import org.json.junit.data.MyEnum;
|
||||||
|
@ -40,6 +43,9 @@ import org.json.junit.data.MyJsonString;
|
||||||
import org.json.junit.data.MyNumber;
|
import org.json.junit.data.MyNumber;
|
||||||
import org.json.junit.data.MyNumberContainer;
|
import org.json.junit.data.MyNumberContainer;
|
||||||
import org.json.junit.data.MyPublicClass;
|
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 org.junit.Test;
|
||||||
|
|
||||||
import com.jayway.jsonpath.Configuration;
|
import com.jayway.jsonpath.Configuration;
|
||||||
|
@ -2583,4 +2589,78 @@ public class JSONObjectTest {
|
||||||
assertTrue("Removing a key should succeed", map.remove("key3") != null);
|
assertTrue("Removing a key should succeed", map.remove("key3") != null);
|
||||||
assertTrue("Map should have 2 elements", map.size() == 2);
|
assertTrue("Map should have 2 elements", map.size() == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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 mapped "+jo.keySet().toString(), 1, jo.length());
|
||||||
|
assertNotNull(jo.get("ALL"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
72
src/test/java/org/json/junit/data/GenericBean.java
Normal file
72
src/test/java/org/json/junit/data/GenericBean.java
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
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 {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** sets the generic value */
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
src/test/java/org/json/junit/data/GenericBeanInt.java
Normal file
29
src/test/java/org/json/junit/data/GenericBeanInt.java
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** return false. should not be beanable */
|
||||||
|
public boolean getable() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
public GenericBeanInt(Integer genericValue) {
|
||||||
|
super(genericValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
81
src/test/java/org/json/junit/data/Singleton.java
Normal file
81
src/test/java/org/json/junit/data/Singleton.java
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
package org.json.junit.data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sample singleton for use with bean testing.
|
||||||
|
*
|
||||||
|
* @author John Aylward
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public final class Singleton {
|
||||||
|
/** */
|
||||||
|
private int someInt;
|
||||||
|
/** */
|
||||||
|
private String someString;
|
||||||
|
/** single instance. */
|
||||||
|
private static final Singleton INSTANCE = new Singleton();
|
||||||
|
|
||||||
|
/** @return the singleton instance. */
|
||||||
|
public static final Singleton getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
private Singleton() {
|
||||||
|
if (INSTANCE != null) {
|
||||||
|
throw new IllegalStateException("Already instantiated");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object clone() throws CloneNotSupportedException {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return someInt */
|
||||||
|
public int getSomeInt() {
|
||||||
|
return someInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** sets someInt */
|
||||||
|
public void setSomeInt(int someInt) {
|
||||||
|
this.someInt = someInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return someString */
|
||||||
|
public String getSomeString() {
|
||||||
|
return someString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** sets someString */
|
||||||
|
public void setSomeString(String someString) {
|
||||||
|
this.someString = someString;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + someInt;
|
||||||
|
result = prime * result + ((someString == null) ? 0 : someString.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Singleton other = (Singleton) obj;
|
||||||
|
if (someInt != other.someInt)
|
||||||
|
return false;
|
||||||
|
if (someString == null) {
|
||||||
|
if (other.someString != null)
|
||||||
|
return false;
|
||||||
|
} else if (!someString.equals(other.someString))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
49
src/test/java/org/json/junit/data/SingletonEnum.java
Normal file
49
src/test/java/org/json/junit/data/SingletonEnum.java
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package org.json.junit.data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sample singleton done as an Enum for use with bean testing.
|
||||||
|
*
|
||||||
|
* @author John Aylward
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum SingletonEnum {
|
||||||
|
INSTANCE;
|
||||||
|
/** */
|
||||||
|
private int someInt;
|
||||||
|
/** */
|
||||||
|
private String someString;
|
||||||
|
|
||||||
|
/** single instance. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the singleton instance. I a real application, I'd hope no one did
|
||||||
|
* this to an enum singleton.
|
||||||
|
*/
|
||||||
|
public static final SingletonEnum getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
private SingletonEnum() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return someInt */
|
||||||
|
public int getSomeInt() {
|
||||||
|
return someInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** sets someInt */
|
||||||
|
public void setSomeInt(int someInt) {
|
||||||
|
this.someInt = someInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return someString */
|
||||||
|
public String getSomeString() {
|
||||||
|
return someString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** sets someString */
|
||||||
|
public void setSomeString(String someString) {
|
||||||
|
this.someString = someString;
|
||||||
|
}
|
||||||
|
}
|
45
src/test/java/org/json/junit/data/WeirdList.java
Normal file
45
src/test/java/org/json/junit/data/WeirdList.java
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.json.junit.data;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author John Aylward
|
||||||
|
*/
|
||||||
|
public class WeirdList {
|
||||||
|
/** */
|
||||||
|
private final List<Integer> list = new ArrayList<>();
|
||||||
|
|
||||||
|
public WeirdList(Integer... vals) {
|
||||||
|
this.list.addAll(Arrays.asList(vals));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** gets a copy of the list */
|
||||||
|
public List<Integer> get() {
|
||||||
|
return new ArrayList<>(this.list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** gets a copy of the list */
|
||||||
|
public List<Integer> getALL() {
|
||||||
|
return new ArrayList<>(this.list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** get an index */
|
||||||
|
public Integer get(int i) {
|
||||||
|
return this.list.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** get an index */
|
||||||
|
public int getInt(int i) {
|
||||||
|
return this.list.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** adds a new value to the end of the list */
|
||||||
|
public void add(Integer value) {
|
||||||
|
this.list.add(value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue