|
52 | 52 | * JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
53 | 53 | * </code> |
54 | 54 | * |
| 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<Object> regExValueMatcher = new RegularExpressionValueMatcher<Object>("\\d+"); // matches one or more digits<br/> |
| 62 | + * Customization[] customizations = new Customization[aLength];<br/> |
| 63 | + * for (int i=0; i<aLength; i++) {<br/> |
| 64 | + * String contextPath = "a["+i+"].id";<br/> |
| 65 | + * customizations[i] = new Customization(contextPath, regExValueMatcher);<br/> |
| 66 | + * }<br/> |
| 67 | + * CustomComparator regExComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, customizations);<br/> |
| 68 | + * ArrayValueMatcher<Object> regExArrayValueMatcher = new ArrayValueMatcher<Object>(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 | + * |
55 | 74 | * <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> |
56 | 75 | * |
57 | 76 | * <code> |
|
79 | 98 | * <p>To verify that the second elements of JSON array 'a' is a JSON array whose first element has the value 9:</p> |
80 | 99 | * |
81 | 100 | * <code> |
82 | | - * Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher<Object>(comparator, 0));<br/> |
| 101 | + * JSONComparator innerComparator = new DefaultComparator(JSONCompareMode.LENIENT);<br/> |
| 102 | + * Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher<Object>(innerComparator, 0));<br/> |
83 | 103 | * JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization);<br/> |
84 | 104 | * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1));<br/> |
85 | 105 | * JSONAssert.assertEquals("{a:[[9]]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
|
0 commit comments