1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

support enum testing

This commit is contained in:
stleary 2015-06-03 22:50:08 -05:00
parent 32ea7e0ba3
commit 6b03f1bbe7
5 changed files with 131 additions and 1 deletions

80
EnumTest.java Normal file
View file

@ -0,0 +1,80 @@
package org.json.junit;
import static org.junit.Assert.*;
import org.json.*;
import org.junit.*;
/**
* Documents how enum is handled by JSON-Java.
*/
public class EnumTest {
@Test
public void simpleEnum() {
/**
* Nothing happens when a simple enum is parsed to JSONObject
*/
MyEnum myEnum = MyEnum.VAL2;
JSONObject jsonObject = new JSONObject(myEnum);
assertTrue("simple enum is not processed by JSONObject", jsonObject.length() == 0);
/**
* Nothing good happens when a simple enum is parsed to JSONArray
*/
try {
new JSONArray(myEnum);
} catch (JSONException e) {
assertTrue("JSONArray throws exception when passed enum", true);
}
}
@Test
public void enumWithField() {
/**
* enum with a getters is handled like a bean
*/
String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
MyEnumField myEnum = MyEnumField.VAL2;
JSONObject jsonObject = new JSONObject(myEnum);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
@Test
public void enumInClass() {
/**
* class which contains enum instances.
* The enum values in MyEnum are lost.
* The string values in MyEnumFild are extracted and wrapped.
*/
String expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
myEnumClass.setMyEnumField(MyEnumField.VAL3);
JSONObject jsonObject = new JSONObject(myEnumClass);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
@Test
public void enumValueToString() {
String expectedStr1 = "\"VAL1\"";
String expectedStr2 = "\"VAL1\"";
String expectedStr3 = "\"org.json.junit.MyEnumClass@";
MyEnum myEnum = MyEnum.VAL1;
MyEnumField myEnumField = MyEnumField.VAL1;
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
myEnumClass.setMyEnumField(MyEnumField.VAL1);
String str1 = JSONObject.valueToString(myEnum);
assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1,
str1.equals(expectedStr1));
String str2 = JSONObject.valueToString(myEnumField);
assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2,
str2.equals(expectedStr2));
String str3 = JSONObject.valueToString(myEnumClass);
assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3,
str3.startsWith(expectedStr3));
}
}

View file

@ -13,7 +13,8 @@ import org.junit.runners.Suite;
HTTPTest.class,
JSONStringerTest.class,
JSONObjectTest.class,
JSONArrayTest.class
JSONArrayTest.class,
EnumTest.class
})
public class JunitTestSuite {
}

7
MyEnum.java Normal file
View file

@ -0,0 +1,7 @@
package org.json.junit;
public enum MyEnum {
VAL1,
VAL2,
VAL3;
}

22
MyEnumClass.java Normal file
View file

@ -0,0 +1,22 @@
package org.json.junit;
/**
* this is simply a class that contains some enum instances
*/
public class MyEnumClass {
private MyEnum myEnum;
private MyEnumField myEnumField;
public MyEnum getMyEnum() {
return myEnum;
}
public void setMyEnum(MyEnum myEnum) {
this.myEnum = myEnum;
}
public MyEnumField getMyEnumField() {
return myEnumField;
}
public void setMyEnumField(MyEnumField myEnumField) {
this.myEnumField = myEnumField;
}
}

20
MyEnumField.java Normal file
View file

@ -0,0 +1,20 @@
package org.json.junit;
public enum MyEnumField {
VAL1(1, "val 1"),
VAL2(2, "val 2"),
VAL3(3, "val 3");
private String value;
private Integer intVal;
private MyEnumField(Integer intVal, String value) {
this.value = value;
this.intVal = intVal;
}
public String getValue() {
return value;
}
public Integer getIntVal() {
return intVal;
}
}