org.skyscreamer.jsonassert.comparator
Class ArraySizeComparator

java.lang.Object
  extended by org.skyscreamer.jsonassert.comparator.AbstractComparator
      extended by org.skyscreamer.jsonassert.comparator.DefaultComparator
          extended by 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
ArraySizeComparator(JSONCompareMode mode)
          Create new ArraySizeComparator.
 
Method Summary
 void compareJSONArray(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
compareJSON, compareValues
 
Methods inherited from class org.skyscreamer.jsonassert.comparator.AbstractComparator
checkJsonObjectKeysActualInExpected, checkJsonObjectKeysExpectedInActual, compareJSON, compareJSON, compareJSONArrayOfJsonObjects, compareJSONArrayOfSimpleValues, compareJSONArrayWithStrictOrder, recursivelyCompareJSONArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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(String prefix,
                             org.json.JSONArray expected,
                             org.json.JSONArray actual,
                             JSONCompareResult result)
                      throws org.json.JSONException
Expected 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:
compareJSONArray in interface JSONComparator
Overrides:
compareJSONArray in class DefaultComparator
Throws:
org.json.JSONException


Copyright © 2014. All Rights Reserved.