22
33import static org .junit .Assert .*;
44
5+ import java .util .*;
6+
57import org .json .*;
68import org .junit .*;
79
10+ import com .jayway .jsonpath .*;
11+
812/**
913 * Enums are not explicitly supported in JSON-Java. But because enums act like
1014 * classes, all required behavior is already be present in some form.
@@ -25,23 +29,37 @@ public void jsonObjectFromEnum() {
2529 assertTrue ("simple enum has no getters" , jsonObject .length () == 0 );
2630
2731 // enum with a getters should create a non-empty object
28- String expectedStr = "{\" value\" :\" val 2\" , \" intVal\" :2}" ;
2932 MyEnumField myEnumField = MyEnumField .VAL2 ;
3033 jsonObject = new JSONObject (myEnumField );
31- JSONObject expectedJsonObject = new JSONObject (expectedStr );
32- Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
34+
35+ // validate JSON content
36+ Object doc = Configuration .defaultConfiguration ().jsonProvider ()
37+ .parse (jsonObject .toString ());
38+ assertTrue ("expecting 2 items in top level object" ,
39+ ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
40+ assertTrue ("expecting val 2" , "val 2" .equals (JsonPath .read (doc , "$.value" )));
41+ assertTrue ("expecting 2" , Integer .valueOf (2 ).equals (JsonPath .read (doc , "$.intVal" )));
3342
3443 /**
3544 * class which contains enum instances. Each enum should be stored
3645 * in its own JSONObject
3746 */
38- expectedStr = "{\" myEnumField\" :{\" intVal\" :3,\" value\" :\" val 3\" },\" myEnum\" :{}}" ;
3947 MyEnumClass myEnumClass = new MyEnumClass ();
4048 myEnumClass .setMyEnum (MyEnum .VAL1 );
4149 myEnumClass .setMyEnumField (MyEnumField .VAL3 );
4250 jsonObject = new JSONObject (myEnumClass );
43- expectedJsonObject = new JSONObject (expectedStr );
44- Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
51+
52+ // validate JSON content
53+ doc = Configuration .defaultConfiguration ().jsonProvider ()
54+ .parse (jsonObject .toString ());
55+ assertTrue ("expecting 2 items in top level object" ,
56+ ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
57+ assertTrue ("expecting 2 items in myEnumField object" ,
58+ ((Map <?,?>)(JsonPath .read (doc , "$.myEnumField" ))).size () == 2 );
59+ assertTrue ("expecting 0 items in myEnum object" ,
60+ ((Map <?,?>)(JsonPath .read (doc , "$.myEnum" ))).size () == 0 );
61+ assertTrue ("expecting 3" , Integer .valueOf (3 ).equals (JsonPath .read (doc , "$.myEnumField.intVal" )));
62+ assertTrue ("expecting val 3" , "val 3" .equals (JsonPath .read (doc , "$.myEnumField.value" )));
4563 }
4664
4765 /**
@@ -51,28 +69,30 @@ public void jsonObjectFromEnum() {
5169 @ Test
5270 public void jsonObjectFromEnumWithNames () {
5371 String [] names ;
54- String expectedStr ;
5572 JSONObject jsonObject ;
56- JSONObject finalJsonObject ;
57- JSONObject expectedJsonObject ;
5873
59- expectedStr = "{\" VAL1\" :\" VAL1\" ,\" VAL2\" :\" VAL2\" ,\" VAL3\" :\" VAL3\" }" ;
6074 MyEnum myEnum = MyEnum .VAL1 ;
6175 names = JSONObject .getNames (myEnum );
62- // The values will be MyEnmField fields, so need to convert back to string for comparison
76+ // The values will be MyEnum fields
6377 jsonObject = new JSONObject (myEnum , names );
64- finalJsonObject = new JSONObject (jsonObject .toString ());
65- expectedJsonObject = new JSONObject (expectedStr );
66- Util .compareActualVsExpectedJsonObjects (finalJsonObject , expectedJsonObject );
6778
68- expectedStr = "{\" VAL1\" :\" VAL1\" ,\" VAL2\" :\" VAL2\" ,\" VAL3\" :\" VAL3\" }" ;
79+ // validate JSON object
80+ Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
81+ assertTrue ("expected 3 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 3 );
82+ assertTrue ("expected VAL1" , "VAL1" .equals (JsonPath .read (doc , "$.VAL1" )));
83+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$.VAL2" )));
84+ assertTrue ("expected VAL3" , "VAL3" .equals (JsonPath .read (doc , "$.VAL3" )));
85+
6986 MyEnumField myEnumField = MyEnumField .VAL3 ;
7087 names = JSONObject .getNames (myEnumField );
71- // The values will be MyEnmField fields, so need to convert back to string for comparison
88+ // The values will be MyEnmField fields
7289 jsonObject = new JSONObject (myEnumField , names );
73- finalJsonObject = new JSONObject (jsonObject .toString ());
74- expectedJsonObject = new JSONObject (expectedStr );
75- Util .compareActualVsExpectedJsonObjects (finalJsonObject , expectedJsonObject );
90+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
91+ assertTrue ("expected 3 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 3 );
92+ assertTrue ("expected VAL1" , "VAL1" .equals (JsonPath .read (doc , "$.VAL1" )));
93+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$.VAL2" )));
94+ assertTrue ("expected VAL3" , "VAL3" .equals (JsonPath .read (doc , "$.VAL3" )));
95+
7696 }
7797
7898 /**
@@ -81,29 +101,33 @@ public void jsonObjectFromEnumWithNames() {
81101 */
82102 @ Test
83103 public void enumPut () {
84- String expectedFinalStr = "{\" myEnum\" :\" VAL2\" , \" myEnumField\" :\" VAL1\" }" ;
85104 JSONObject jsonObject = new JSONObject ();
86105 MyEnum myEnum = MyEnum .VAL2 ;
87106 jsonObject .put ("myEnum" , myEnum );
88- assertTrue ("expecting myEnum value" , MyEnum .VAL2 .equals (jsonObject .get ("myEnum" )));
89- assertTrue ("expecting myEnum value" , MyEnum .VAL2 .equals (jsonObject .opt ("myEnum" )));
90107 MyEnumField myEnumField = MyEnumField .VAL1 ;
91108 jsonObject .putOnce ("myEnumField" , myEnumField );
92- assertTrue ("expecting myEnumField value" , MyEnumField .VAL1 .equals (jsonObject .get ("myEnumField" )));
93- assertTrue ("expecting myEnumField value" , MyEnumField .VAL1 .equals (jsonObject .opt ("myEnumField" )));
94- JSONObject finalJsonObject = new JSONObject (jsonObject .toString ());
95- JSONObject expectedFinalJsonObject = new JSONObject (expectedFinalStr );
96- Util .compareActualVsExpectedJsonObjects (finalJsonObject , expectedFinalJsonObject );
97109
110+ // validate JSON content
111+ Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
112+ assertTrue ("expected 2 top level objects" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
113+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$.myEnum" )));
114+ assertTrue ("expected VAL1" , "VAL1" .equals (JsonPath .read (doc , "$.myEnumField" )));
115+
98116 JSONArray jsonArray = new JSONArray ();
99117 jsonArray .put (myEnum );
100118 jsonArray .put (1 , myEnumField );
119+
120+ // validate JSON content
121+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonArray .toString ());
122+ assertTrue ("expected 2 top level objects" , ((List <?>)(JsonPath .read (doc , "$" ))).size () == 2 );
123+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$[0]" )));
124+ assertTrue ("expected VAL1" , "VAL1" .equals (JsonPath .read (doc , "$[1]" )));
125+
126+ /**
127+ * Leaving these tests because they exercise get, opt, and remove
128+ */
101129 assertTrue ("expecting myEnum value" , MyEnum .VAL2 .equals (jsonArray .get (0 )));
102130 assertTrue ("expecting myEnumField value" , MyEnumField .VAL1 .equals (jsonArray .opt (1 )));
103- JSONArray expectedJsonArray = new JSONArray ();
104- expectedJsonArray .put (MyEnum .VAL2 );
105- expectedJsonArray .put (MyEnumField .VAL1 );
106- Util .compareActualVsExpectedJsonArrays (jsonArray , expectedJsonArray );
107131 assertTrue ("expecting myEnumField value" , MyEnumField .VAL1 .equals (jsonArray .remove (1 )));
108132 }
109133
@@ -151,49 +175,66 @@ public void enumToString() {
151175
152176 MyEnumField myEnumField = MyEnumField .VAL2 ;
153177 jsonObject = new JSONObject (myEnumField );
154- expectedStr = "{\" value\" :\" val 2\" , \" intVal\" :2}" ;
155- JSONObject actualJsonObject = new JSONObject (jsonObject .toString ());
156- JSONObject expectedJsonObject = new JSONObject (expectedStr );
157- Util .compareActualVsExpectedJsonObjects (actualJsonObject , expectedJsonObject );
158178
159- expectedStr = "{\" myEnumField\" :{\" intVal\" :3,\" value\" :\" val 3\" },\" myEnum\" :{}}" ;
179+ // validate JSON content
180+ Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
181+ assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
182+ assertTrue ("expected val 2" , "val 2" .equals (JsonPath .read (doc , "$.value" )));
183+ assertTrue ("expected 2" , Integer .valueOf (2 ).equals (JsonPath .read (doc , "$.intVal" )));
184+
160185 MyEnumClass myEnumClass = new MyEnumClass ();
161186 myEnumClass .setMyEnum (MyEnum .VAL1 );
162187 myEnumClass .setMyEnumField (MyEnumField .VAL3 );
163188 jsonObject = new JSONObject (myEnumClass );
164- actualJsonObject = new JSONObject (jsonObject .toString ());
165- expectedJsonObject = new JSONObject (expectedStr );
166- Util .compareActualVsExpectedJsonObjects (actualJsonObject , expectedJsonObject );
167189
168- expectedStr = "{\" VAL1\" :\" VAL1\" ,\" VAL2\" :\" VAL2\" ,\" VAL3\" :\" VAL3\" }" ;
190+ // validate JSON content
191+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
192+ assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
193+ assertTrue ("expected 2 myEnumField items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnumField" ))).size () == 2 );
194+ assertTrue ("expected 0 myEnum items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnum" ))).size () == 0 );
195+ assertTrue ("expected 3" , Integer .valueOf (3 ).equals (JsonPath .read (doc , "$.myEnumField.intVal" )));
196+ assertTrue ("expected val 3" , "val 3" .equals (JsonPath .read (doc , "$.myEnumField.value" )));
197+
169198 String [] names = JSONObject .getNames (myEnum );
170199 jsonObject = new JSONObject (myEnum , names );
171- actualJsonObject = new JSONObject (jsonObject .toString ());
172- expectedJsonObject = new JSONObject (expectedStr );
173- Util .compareActualVsExpectedJsonObjects (actualJsonObject , expectedJsonObject );
174200
175- expectedStr = "{\" VAL1\" :\" VAL1\" ,\" VAL2\" :\" VAL2\" ,\" VAL3\" :\" VAL3\" }" ;
201+ // validate JSON content
202+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
203+ assertTrue ("expected 3 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 3 );
204+ assertTrue ("expected VAL1" , "VAL1" .equals (JsonPath .read (doc , "$.VAL1" )));
205+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$.VAL2" )));
206+ assertTrue ("expected VAL3" , "VAL3" .equals (JsonPath .read (doc , "$.VAL3" )));
207+
176208 names = JSONObject .getNames (myEnumField );
177209 jsonObject = new JSONObject (myEnumField , names );
178- actualJsonObject = new JSONObject (jsonObject .toString ());
179- expectedJsonObject = new JSONObject (expectedStr );
180- Util .compareActualVsExpectedJsonObjects (actualJsonObject , expectedJsonObject );
210+
211+ // validate JSON content
212+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
213+ assertTrue ("expected 3 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 3 );
214+ assertTrue ("expected VAL1" , "VAL1" .equals (JsonPath .read (doc , "$.VAL1" )));
215+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$.VAL2" )));
216+ assertTrue ("expected VAL3" , "VAL3" .equals (JsonPath .read (doc , "$.VAL3" )));
181217
182218 expectedStr = "{\" myEnum\" :\" VAL2\" , \" myEnumField\" :\" VAL2\" }" ;
183219 jsonObject = new JSONObject ();
184220 jsonObject .putOpt ("myEnum" , myEnum );
185221 jsonObject .putOnce ("myEnumField" , myEnumField );
186- actualJsonObject = new JSONObject (jsonObject .toString ());
187- expectedJsonObject = new JSONObject (expectedStr );
188- Util .compareActualVsExpectedJsonObjects (actualJsonObject , expectedJsonObject );
189222
190- expectedStr = "[\" VAL2\" , \" VAL2\" ]" ;
223+ // validate JSON content
224+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
225+ assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
226+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$.myEnum" )));
227+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$.myEnumField" )));
228+
191229 JSONArray jsonArray = new JSONArray ();
192230 jsonArray .put (myEnum );
193231 jsonArray .put (1 , myEnumField );
194- JSONArray actualJsonArray = new JSONArray (jsonArray .toString ());
195- JSONArray expectedJsonArray = new JSONArray (expectedStr );
196- Util .compareActualVsExpectedJsonArrays (actualJsonArray , expectedJsonArray );
232+
233+ // validate JSON content
234+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonArray .toString ());
235+ assertTrue ("expected 2 top level items" , ((List <?>)(JsonPath .read (doc , "$" ))).size () == 2 );
236+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$[0]" )));
237+ assertTrue ("expected VAL2" , "VAL2" .equals (JsonPath .read (doc , "$[1]" )));
197238 }
198239
199240 /**
@@ -206,19 +247,28 @@ public void wrap() {
206247 JSONObject jsonObject = (JSONObject )JSONObject .wrap (myEnum );
207248 assertTrue ("simple enum has no getters" , jsonObject .length () == 0 );
208249
209- String expectedStr = "{\" value\" :\" val 2\" , \" intVal\" :2}" ;
210250 MyEnumField myEnumField = MyEnumField .VAL2 ;
211251 jsonObject = (JSONObject )JSONObject .wrap (myEnumField );
212- JSONObject expectedJsonObject = new JSONObject (expectedStr );
213- Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
214252
215- expectedStr = "{\" myEnumField\" :{\" intVal\" :3,\" value\" :\" val 3\" },\" myEnum\" :{}}" ;
253+ // validate JSON content
254+ Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
255+ assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
256+ assertTrue ("expected val 2" , "val 2" .equals (JsonPath .read (doc , "$.value" )));
257+ assertTrue ("expected 2" , Integer .valueOf (2 ).equals (JsonPath .read (doc , "$.intVal" )));
258+
216259 MyEnumClass myEnumClass = new MyEnumClass ();
217260 myEnumClass .setMyEnum (MyEnum .VAL1 );
218261 myEnumClass .setMyEnumField (MyEnumField .VAL3 );
219262 jsonObject = (JSONObject )JSONObject .wrap (myEnumClass );
220- expectedJsonObject = new JSONObject (expectedStr );
221- Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
263+
264+ // validate JSON content
265+ doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
266+ assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
267+ assertTrue ("expected 2 myEnumField items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnumField" ))).size () == 2 );
268+ assertTrue ("expected 0 myEnum items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnum" ))).size () == 0 );
269+ assertTrue ("expected 3" , Integer .valueOf (3 ).equals (JsonPath .read (doc , "$.myEnumField.intVal" )));
270+ assertTrue ("expected val 3" , "val 3" .equals (JsonPath .read (doc , "$.myEnumField.value" )));
271+
222272 }
223273
224274 /**
0 commit comments