@@ -83,11 +83,10 @@ private static void compareJSON(String prefix, JSONObject expected, JSONObject a
8383 Object expectedValue = expected .get (key );
8484 if (actual .has (key )) {
8585 Object actualValue = actual .get (key );
86- String fullKey = prefix + key ;
87- compareValues (fullKey , expectedValue , actualValue , mode , result );
86+ compareValues (qualify (prefix , key ), expectedValue , actualValue , mode , result );
8887 }
8988 else {
90- result .fail ( "Does not contain expected key: " + prefix + key );
89+ result .missing ( prefix , key );
9190 }
9291 }
9392
@@ -96,20 +95,24 @@ private static void compareJSON(String prefix, JSONObject expected, JSONObject a
9695 Set <String > actualKeys = getKeys (actual );
9796 for (String key : actualKeys ) {
9897 if (!expected .has (key )) {
99- result .fail ("Got unexpected field: " + prefix + key );
98+ result .fail ("Got unexpected field: " + qualify ( prefix , key ) );
10099 }
101100 }
102101 }
103102 }
104103
104+ private static String qualify (String prefix , String key ) {
105+ return "" .equals (prefix ) ? key : prefix + "." + key ;
106+ }
107+
105108 private static void compareValues (String fullKey , Object expectedValue , Object actualValue , JSONCompareMode mode , JSONCompareResult result ) throws JSONException
106109 {
107110 if (expectedValue .getClass ().isAssignableFrom (actualValue .getClass ())) {
108111 if (expectedValue instanceof JSONArray ) {
109112 compareJSONArray (fullKey , (JSONArray )expectedValue , (JSONArray )actualValue , mode , result );
110113 }
111114 else if (expectedValue instanceof JSONObject ) {
112- compareJSON (fullKey + "." , (JSONObject ) expectedValue , (JSONObject ) actualValue , mode , result );
115+ compareJSON (fullKey , (JSONObject ) expectedValue , (JSONObject ) actualValue , mode , result );
113116 }
114117 else if (!expectedValue .equals (actualValue )) {
115118 result .fail (fullKey , expectedValue , actualValue );
@@ -143,7 +146,7 @@ else if (allSimpleValues(expected)) {
143146 Map <Object , Integer > actualCount = CollectionUtils .getCardinalityMap (jsonArrayToList (actual ));
144147 for (Object o : expectedCount .keySet ()) {
145148 if (!actualCount .containsKey (o )) {
146- result .fail (key + "[]: Expected " + o + ", but not found" );
149+ result .missing (key + "[]" , o );
147150 }
148151 else if (actualCount .get (o ) != expectedCount .get (o )) {
149152 result .fail (key + "[]: Expected " + expectedCount .get (o ) + " occurrence(s) of " + o
@@ -167,12 +170,12 @@ else if (allJSONObjects(expected)) {
167170 Map <Object , JSONObject > actualValueMap = arrayOfJsonObjectToMap (actual , uniqueKey );
168171 for (Object id : expectedValueMap .keySet ()) {
169172 if (!actualValueMap .containsKey (id )) {
170- result .fail ( key + "[]: Expected but did not find object where " + uniqueKey + "=" + id );
173+ result .missing ( formatUniqueKey ( key , uniqueKey , id ), expectedValueMap . get ( id ) );
171174 continue ;
172175 }
173176 JSONObject expectedValue = expectedValueMap .get (id );
174177 JSONObject actualValue = actualValueMap .get (id );
175- compareValues (key + "[" + uniqueKey + "=" + id + "]" , expectedValue , actualValue , mode , result );
178+ compareValues (formatUniqueKey ( key , uniqueKey , id ) , expectedValue , actualValue , mode , result );
176179 }
177180 for (Object id : actualValueMap .keySet ()) {
178181 if (!expectedValueMap .containsKey (id )) {
@@ -192,6 +195,10 @@ else if (allJSONArrays(expected)) {
192195 }
193196 }
194197
198+ private static String formatUniqueKey (String key , String uniqueKey , Object value ) {
199+ return key + "[" + uniqueKey + "=" + value + "]" ;
200+ }
201+
195202 // This is expensive (O(n^2) -- yuck), but may be the only resort for some cases with loose array ordering, and no
196203 // easy way to uniquely identify each element.
197204 private static void recursivelyCompareJSONArray (String key , JSONArray expected , JSONArray actual ,
0 commit comments