From 6b03f1bbe745a8574ca8ca3d71b02e05c3b5a151 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 3 Jun 2015 22:50:08 -0500 Subject: [PATCH] support enum testing --- EnumTest.java | 80 +++++++++++++++++++++++++++++++++++++++++++++ JunitTestSuite.java | 3 +- MyEnum.java | 7 ++++ MyEnumClass.java | 22 +++++++++++++ MyEnumField.java | 20 ++++++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 EnumTest.java create mode 100644 MyEnum.java create mode 100644 MyEnumClass.java create mode 100644 MyEnumField.java diff --git a/EnumTest.java b/EnumTest.java new file mode 100644 index 0000000..6b2a4bc --- /dev/null +++ b/EnumTest.java @@ -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)); + } +} diff --git a/JunitTestSuite.java b/JunitTestSuite.java index a91426d..ceff6f1 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -13,7 +13,8 @@ import org.junit.runners.Suite; HTTPTest.class, JSONStringerTest.class, JSONObjectTest.class, - JSONArrayTest.class + JSONArrayTest.class, + EnumTest.class }) public class JunitTestSuite { } \ No newline at end of file diff --git a/MyEnum.java b/MyEnum.java new file mode 100644 index 0000000..d2fe08d --- /dev/null +++ b/MyEnum.java @@ -0,0 +1,7 @@ +package org.json.junit; + +public enum MyEnum { + VAL1, + VAL2, + VAL3; +} diff --git a/MyEnumClass.java b/MyEnumClass.java new file mode 100644 index 0000000..8e71663 --- /dev/null +++ b/MyEnumClass.java @@ -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; + } +} diff --git a/MyEnumField.java b/MyEnumField.java new file mode 100644 index 0000000..7efec54 --- /dev/null +++ b/MyEnumField.java @@ -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; + } +}