|
22 | 22 | * |
23 | 23 | * <p>Assuming JSON to be verified is held in String variable ARRAY_OF_JSONOBJECTS and contains:</p> |
24 | 24 | * |
25 | | - * <code>{a:[{background:white,id:1,type:row}, {background:grey,id:2,type:row}, {background:white,id:3,type:row}, {background:grey,id:4,type:row}]}</code> |
| 25 | + * <pre>{@code |
| 26 | + * {a:[{background:white, id:1, type:row}, |
| 27 | + * {background:grey, id:2, type:row}, |
| 28 | + * {background:white, id:3, type:row}, |
| 29 | + * {background:grey, id:4, type:row}]} |
| 30 | + * }</pre> |
26 | 31 | * |
27 | 32 | * <p>then:</p> |
28 | 33 | * |
29 | 34 | * <p>To verify that the 'id' attribute of first element of array 'a' is '1':</p> |
30 | 35 | * |
31 | | - * <code> |
32 | | - * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);<br/> |
33 | | - * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 0));<br/> |
34 | | - * JSONAssert.assertEquals("{a:[{id:1}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
35 | | - * </code> |
| 36 | + * <pre>{@code |
| 37 | + * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); |
| 38 | + * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 0)); |
| 39 | + * JSONAssert.assertEquals("{a:[{id:1}]}", ARRAY_OF_JSONOBJECTS, |
| 40 | + * new CustomComparator(JSONCompareMode.LENIENT, customization)); |
| 41 | + * }</pre> |
36 | 42 | * |
37 | 43 | * <p>To simplify complexity of expected JSON string, the value <code>"a:[{id:1}]}"</code> may be replaced by <code>"a:{id:1}}"</code></p> |
38 | 44 | * |
39 | 45 | * <p>To verify that the 'type' attribute of second and third elements of array 'a' is 'row':</p> |
40 | 46 | * |
41 | | - * <code> |
42 | | - * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);<br/> |
43 | | - * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1, 2));<br/> |
44 | | - * JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
45 | | - * </code> |
| 47 | + * <pre>{@code |
| 48 | + * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); |
| 49 | + * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1, 2)); |
| 50 | + * JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, |
| 51 | + * new CustomComparator(JSONCompareMode.LENIENT, customization)); |
| 52 | + * }</pre> |
46 | 53 | * |
47 | 54 | * <p>To verify that the 'type' attribute of every element of array 'a' is 'row':</p> |
48 | 55 | * |
49 | | - * <code> |
50 | | - * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);<br/> |
51 | | - * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator));<br/> |
52 | | - * JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
53 | | - * </code> |
| 56 | + * <pre>{@code |
| 57 | + * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); |
| 58 | + * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator)); |
| 59 | + * JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, |
| 60 | + * new CustomComparator(JSONCompareMode.LENIENT, customization)); |
| 61 | + * }</pre> |
54 | 62 | * |
55 | 63 | * <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 | 64 | * |
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> |
| 65 | + * <pre>{@code |
| 66 | + * // get length of array we will verify |
| 67 | + * int aLength = ((JSONArray)((JSONObject)JSONParser.parseJSON(ARRAY_OF_JSONOBJECTS)).get("a")).length(); |
| 68 | + * // create array of customizations one for each array element |
| 69 | + * RegularExpressionValueMatcher<Object> regExValueMatcher = |
| 70 | + * new RegularExpressionValueMatcher<Object>("\\d+"); // matches one or more digits |
| 71 | + * Customization[] customizations = new Customization[aLength]; |
| 72 | + * for (int i=0; i<aLength; i++) { |
| 73 | + * String contextPath = "a["+i+"].id"; |
| 74 | + * customizations[i] = new Customization(contextPath, regExValueMatcher); |
| 75 | + * } |
| 76 | + * CustomComparator regExComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, customizations); |
| 77 | + * ArrayValueMatcher<Object> regExArrayValueMatcher = new ArrayValueMatcher<Object>(regExComparator); |
| 78 | + * Customization regExArrayValueCustomization = new Customization("a", regExArrayValueMatcher); |
| 79 | + * CustomComparator regExCustomArrayValueComparator = |
| 80 | + * new CustomComparator(JSONCompareMode.STRICT_ORDER, new Customization[] { regExArrayValueCustomization }); |
| 81 | + * JSONAssert.assertEquals("{a:[{id:X}]}", ARRAY_OF_JSONOBJECTS, regExCustomArrayValueComparator); |
| 82 | + * }</pre> |
73 | 83 | * |
74 | 84 | * <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> |
75 | 85 | * |
76 | | - * <code> |
77 | | - * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);<br/> |
78 | | - * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator));<br/> |
79 | | - * JSONAssert.assertEquals("{a:[{background:white},{background:grey}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
80 | | - * </code> |
| 86 | + * <pre>{@code |
| 87 | + * JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); |
| 88 | + * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator)); |
| 89 | + * JSONAssert.assertEquals("{a:[{background:white},{background:grey}]}", ARRAY_OF_JSONOBJECTS, |
| 90 | + * new CustomComparator(JSONCompareMode.LENIENT, customization)); |
| 91 | + * }</pre> |
81 | 92 | * |
82 | 93 | * <p>Assuming JSON to be verified is held in String variable ARRAY_OF_JSONARRAYS and contains:</p> |
83 | 94 | * |
|
87 | 98 | * |
88 | 99 | * <p>To verify that the first three elements of JSON array 'a' are JSON arrays of length 3:</p> |
89 | 100 | * |
90 | | - * <code> |
91 | | - * JSONComparator comparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER);<br/> |
92 | | - * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 0, 2));<br/> |
| 101 | + * <pre>{@code |
| 102 | + * JSONComparator comparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER); |
| 103 | + * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 0, 2)); |
93 | 104 | * JSONAssert.assertEquals("{a:[[3]]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
94 | | - * </code> |
| 105 | + * }</pre> |
95 | 106 | * |
96 | 107 | * <p>NOTE: simplified expected JSON strings are not possible in this case as ArraySizeComparator does not support them.</p> |
97 | 108 | * |
98 | 109 | * <p>To verify that the second elements of JSON array 'a' is a JSON array whose first element has the value 9:</p> |
99 | 110 | * |
100 | | - * <code> |
101 | | - * JSONComparator innerComparator = new DefaultComparator(JSONCompareMode.LENIENT);<br/> |
102 | | - * Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher<Object>(innerComparator, 0));<br/> |
103 | | - * JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization);<br/> |
104 | | - * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1));<br/> |
| 111 | + * <pre>{@code |
| 112 | + * JSONComparator innerComparator = new DefaultComparator(JSONCompareMode.LENIENT); |
| 113 | + * Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher<Object>(innerComparator, 0)); |
| 114 | + * JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization); |
| 115 | + * Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1)); |
105 | 116 | * JSONAssert.assertEquals("{a:[[9]]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization)); |
106 | | - * </code> |
| 117 | + * }</pre> |
107 | 118 | * |
108 | 119 | * <p>To simplify complexity of expected JSON string, the value <code>"{a:[[9]]}"</code> may be replaced by <code>"{a:[9]}"</code> or <code>"{a:9}"</code></p> |
109 | 120 | * |
|
0 commit comments