Skip to content

Commit 86f4bda

Browse files
committed
Merge pull request #13 from stleary/enum-support
support enum testing
2 parents 32ea7e0 + 6b03f1b commit 86f4bda

5 files changed

Lines changed: 131 additions & 1 deletion

File tree

EnumTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.json.junit;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.json.*;
6+
import org.junit.*;
7+
8+
/**
9+
* Documents how enum is handled by JSON-Java.
10+
*/
11+
public class EnumTest {
12+
13+
@Test
14+
public void simpleEnum() {
15+
/**
16+
* Nothing happens when a simple enum is parsed to JSONObject
17+
*/
18+
MyEnum myEnum = MyEnum.VAL2;
19+
JSONObject jsonObject = new JSONObject(myEnum);
20+
assertTrue("simple enum is not processed by JSONObject", jsonObject.length() == 0);
21+
/**
22+
* Nothing good happens when a simple enum is parsed to JSONArray
23+
*/
24+
try {
25+
new JSONArray(myEnum);
26+
} catch (JSONException e) {
27+
assertTrue("JSONArray throws exception when passed enum", true);
28+
}
29+
}
30+
31+
@Test
32+
public void enumWithField() {
33+
/**
34+
* enum with a getters is handled like a bean
35+
*/
36+
String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
37+
MyEnumField myEnum = MyEnumField.VAL2;
38+
JSONObject jsonObject = new JSONObject(myEnum);
39+
JSONObject expectedJsonObject = new JSONObject(expectedStr);
40+
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
41+
}
42+
43+
@Test
44+
public void enumInClass() {
45+
/**
46+
* class which contains enum instances.
47+
* The enum values in MyEnum are lost.
48+
* The string values in MyEnumFild are extracted and wrapped.
49+
*/
50+
String expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
51+
MyEnumClass myEnumClass = new MyEnumClass();
52+
myEnumClass.setMyEnum(MyEnum.VAL1);
53+
myEnumClass.setMyEnumField(MyEnumField.VAL3);
54+
JSONObject jsonObject = new JSONObject(myEnumClass);
55+
JSONObject expectedJsonObject = new JSONObject(expectedStr);
56+
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
57+
}
58+
59+
@Test
60+
public void enumValueToString() {
61+
String expectedStr1 = "\"VAL1\"";
62+
String expectedStr2 = "\"VAL1\"";
63+
String expectedStr3 = "\"org.json.junit.MyEnumClass@";
64+
MyEnum myEnum = MyEnum.VAL1;
65+
MyEnumField myEnumField = MyEnumField.VAL1;
66+
MyEnumClass myEnumClass = new MyEnumClass();
67+
myEnumClass.setMyEnum(MyEnum.VAL1);
68+
myEnumClass.setMyEnumField(MyEnumField.VAL1);
69+
70+
String str1 = JSONObject.valueToString(myEnum);
71+
assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1,
72+
str1.equals(expectedStr1));
73+
String str2 = JSONObject.valueToString(myEnumField);
74+
assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2,
75+
str2.equals(expectedStr2));
76+
String str3 = JSONObject.valueToString(myEnumClass);
77+
assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3,
78+
str3.startsWith(expectedStr3));
79+
}
80+
}

JunitTestSuite.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
HTTPTest.class,
1414
JSONStringerTest.class,
1515
JSONObjectTest.class,
16-
JSONArrayTest.class
16+
JSONArrayTest.class,
17+
EnumTest.class
1718
})
1819
public class JunitTestSuite {
1920
}

MyEnum.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.json.junit;
2+
3+
public enum MyEnum {
4+
VAL1,
5+
VAL2,
6+
VAL3;
7+
}

MyEnumClass.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.json.junit;
2+
3+
/**
4+
* this is simply a class that contains some enum instances
5+
*/
6+
public class MyEnumClass {
7+
private MyEnum myEnum;
8+
private MyEnumField myEnumField;
9+
10+
public MyEnum getMyEnum() {
11+
return myEnum;
12+
}
13+
public void setMyEnum(MyEnum myEnum) {
14+
this.myEnum = myEnum;
15+
}
16+
public MyEnumField getMyEnumField() {
17+
return myEnumField;
18+
}
19+
public void setMyEnumField(MyEnumField myEnumField) {
20+
this.myEnumField = myEnumField;
21+
}
22+
}

MyEnumField.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.json.junit;
2+
3+
public enum MyEnumField {
4+
VAL1(1, "val 1"),
5+
VAL2(2, "val 2"),
6+
VAL3(3, "val 3");
7+
8+
private String value;
9+
private Integer intVal;
10+
private MyEnumField(Integer intVal, String value) {
11+
this.value = value;
12+
this.intVal = intVal;
13+
}
14+
public String getValue() {
15+
return value;
16+
}
17+
public Integer getIntVal() {
18+
return intVal;
19+
}
20+
}

0 commit comments

Comments
 (0)