|
| 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 | +} |
0 commit comments