Class ArraySizeComparator
- java.lang.Object
-
- org.skyscreamer.jsonassert.comparator.AbstractComparator
-
- org.skyscreamer.jsonassert.comparator.DefaultComparator
-
- org.skyscreamer.jsonassert.comparator.ArraySizeComparator
-
- All Implemented Interfaces:
JSONComparator
public class ArraySizeComparator extends DefaultComparator
A JSONAssert array size comparator.Some typical usage idioms are listed below.
Assuming JSON to be verified is held in String variable ARRAY_OF_JSONOBJECTS and contains:
{a:[7, 8, 9]}then:
To verify that array 'a' contains 3 elements:
JSONAssert.assertEquals("{a:[3]}", ARRAY_OF_JSONOBJECTS, new ArraySizeComparator(JSONCompareMode.LENIENT));To verify that array 'a' contains between 2 and 6 elements:
JSONAssert.assertEquals("{a:[2,6]}", ARRAY_OF_JSONOBJECTS, new ArraySizeComparator(JSONCompareMode.LENIENT));- Author:
- Duncan Mackinder
-
-
Constructor Summary
Constructors Constructor Description ArraySizeComparator(JSONCompareMode mode)Create new ArraySizeComparator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcompareJSONArray(java.lang.String prefix, org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareResult result)Expected array should consist of either 1 or 2 integer values that define maximum and minimum valid lengths of the actual array.-
Methods inherited from class org.skyscreamer.jsonassert.comparator.DefaultComparator
areNotSameDoubles, areNumbers, compareJSON, compareValues
-
Methods inherited from class org.skyscreamer.jsonassert.comparator.AbstractComparator
checkJsonObjectKeysActualInExpected, checkJsonObjectKeysExpectedInActual, compareJSON, compareJSON, compareJSONArrayOfJsonObjects, compareJSONArrayOfSimpleValues, compareJSONArrayWithStrictOrder, recursivelyCompareJSONArray
-
-
-
-
Constructor Detail
-
ArraySizeComparator
public ArraySizeComparator(JSONCompareMode mode)
Create new ArraySizeComparator.- Parameters:
mode- comparison mode, has no impact on ArraySizeComparator but is used by instance of superclass DefaultComparator to control comparison of JSON items other than arrays.
-
-
Method Detail
-
compareJSONArray
public void compareJSONArray(java.lang.String prefix, org.json.JSONArray expected, org.json.JSONArray actual, JSONCompareResult result) throws org.json.JSONExceptionExpected array should consist of either 1 or 2 integer values that define maximum and minimum valid lengths of the actual array. If expected array contains a single integer value, then the actual array must contain exactly that number of elements.- Specified by:
compareJSONArrayin interfaceJSONComparator- Overrides:
compareJSONArrayin classDefaultComparator- Parameters:
prefix- the path in the json where the comparison happensexpected- the expected JSON arrayactual- the actual JSON arrayresult- stores the actual state of the comparison result- Throws:
org.json.JSONException- JSON parsing error
-
-