Skip to content

Commit 78e30a0

Browse files
Duncan MackinderDuncan Mackinder
authored andcommitted
Fix & improve ArrayValueMatcher JavaDoc
Fix final JavaDoc example and add new example showing how to verify every array element using a custom comparator
1 parent 7c096c6 commit 78e30a0

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/main/java/org/skyscreamer/jsonassert/ArrayValueMatcher.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@
5252
* JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization));
5353
* </code>
5454
*
55+
* <p>To verify that the 'id' attribute of every element of array 'a' matches regular expression '\d+'. This requires a custom comparator to specify regular expression to be used to validate each array element, hence the array of Customization instances:</p>
56+
*
57+
* <code>
58+
* // get length of array we will verify<br/>
59+
* int aLength = ((JSONArray)((JSONObject)JSONParser.parseJSON(ARRAY_OF_JSONOBJECTS)).get("a")).length();<br/>
60+
* // create array of customizations one for each array element<br/>
61+
* RegularExpressionValueMatcher&lt;Object&gt; regExValueMatcher = new RegularExpressionValueMatcher&lt;Object&gt;("\\d+"); // matches one or more digits<br/>
62+
* Customization[] customizations = new Customization[aLength];<br/>
63+
* for (int i=0; i&lt;aLength; i++) {<br/>
64+
* &nbsp;&nbsp;String contextPath = "a["+i+"].id";<br/>
65+
* &nbsp;&nbsp;customizations[i] = new Customization(contextPath, regExValueMatcher);<br/>
66+
* }<br/>
67+
* CustomComparator regExComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, customizations);<br/>
68+
* ArrayValueMatcher&lt;Object&gt; regExArrayValueMatcher = new ArrayValueMatcher&lt;Object&gt;(regExComparator);<br/>
69+
* Customization regExArrayValueCustomization = new Customization("a", regExArrayValueMatcher);<br/>
70+
* CustomComparator regExCustomArrayValueComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, new Customization[] { regExArrayValueCustomization });<br/>
71+
* JSONAssert.assertEquals("{a:[{id:X}]}", ARRAY_OF_JSONOBJECTS, regExCustomArrayValueComparator);<br/>
72+
* </code>
73+
*
5574
* <p>To verify that the 'background' attribute of every element of array 'a' alternates between 'white' and 'grey' starting with first element 'background' being 'white':</p>
5675
*
5776
* <code>
@@ -79,7 +98,8 @@
7998
* <p>To verify that the second elements of JSON array 'a' is a JSON array whose first element has the value 9:</p>
8099
*
81100
* <code>
82-
* Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher&lt;Object&gt;(comparator, 0));<br/>
101+
* JSONComparator innerComparator = new DefaultComparator(JSONCompareMode.LENIENT);<br/>
102+
* Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher&lt;Object&gt;(innerComparator, 0));<br/>
83103
* JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization);<br/>
84104
* Customization customization = new Customization("a", new ArrayValueMatcher&lt;Object&gt;(comparator, 1));<br/>
85105
* JSONAssert.assertEquals("{a:[[9]]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization));

src/test/java/org/skyscreamer/jsonassert/ArrayValueMatcherTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ public void verifyTypeAttributeOfEveryArrayElementMatchesRow() throws JSONExcept
209209
JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization));
210210
}
211211

212+
@Test
213+
public void verifyEveryArrayElementWithCustomComparator() throws JSONException {
214+
// get length of array we will verify
215+
int aLength = ((JSONArray)((JSONObject)JSONParser.parseJSON(ARRAY_OF_JSONOBJECTS)).get("a")).length();
216+
// create array of customizations one for each array element
217+
RegularExpressionValueMatcher<Object> regExValueMatcher = new RegularExpressionValueMatcher<Object>("\\d+"); // matches one or more digits
218+
Customization[] customizations = new Customization[aLength];
219+
for (int i=0; i<aLength; i++) {
220+
String contextPath = "a["+i+"].id";
221+
customizations[i] = new Customization(contextPath, regExValueMatcher);
222+
}
223+
CustomComparator regExComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, customizations);
224+
ArrayValueMatcher<Object> regExArrayValueMatcher = new ArrayValueMatcher<Object>(regExComparator);
225+
Customization regExArrayValueCustomization = new Customization("a", regExArrayValueMatcher);
226+
CustomComparator regExCustomArrayValueComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, new Customization[] { regExArrayValueCustomization });
227+
JSONAssert.assertEquals("{a:[{id:X}]}", ARRAY_OF_JSONOBJECTS, regExCustomArrayValueComparator);
228+
}
229+
212230
@Test
213231
public void verifyBackgroundAttributesOfEveryArrayElementAlternateBetweenWhiteAndGrey() throws JSONException {
214232
JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);

0 commit comments

Comments
 (0)