|
| 1 | +package org.skyscreamer.jsonassert; |
| 2 | + |
| 3 | +import java.text.MessageFormat; |
| 4 | + |
| 5 | +import org.json.JSONArray; |
| 6 | +import org.json.JSONException; |
| 7 | +import org.skyscreamer.jsonassert.comparator.JSONComparator; |
| 8 | + |
| 9 | +/** |
| 10 | + * A value matcher for arrays. This operates like STRICT_ORDER array match, |
| 11 | + * however if expected array has less elements than actual array the matching |
| 12 | + * process loops through the expected array to get expected elements for the |
| 13 | + * additional actual elements. In general the expected array will contain a |
| 14 | + * single element which is matched against each actual array element in turn. |
| 15 | + * This allows simple verification of constant array element components and |
| 16 | + * coupled with RegularExpressionValueMatcher can be used to match specific |
| 17 | + * array element components against a regular expression pattern. If the |
| 18 | + * expected object is not an array, a one element expected array is created |
| 19 | + * containing whatever is provided as the expected value. |
| 20 | + * |
| 21 | + * @author Duncan Mackinder |
| 22 | + * |
| 23 | + */ |
| 24 | +public class ArrayValueMatcher<T> implements LocationAwareValueMatcher<T> { |
| 25 | + private final JSONComparator comparator; |
| 26 | + private final int from; |
| 27 | + private final int to; |
| 28 | + |
| 29 | + /** |
| 30 | + * Create ArrayValueMatcher to match every element in actual array against |
| 31 | + * elements taken in sequence from expected array, repeating from start of |
| 32 | + * expected array if necessary. |
| 33 | + * |
| 34 | + * @param comparator |
| 35 | + * comparator to use to compare elements |
| 36 | + */ |
| 37 | + public ArrayValueMatcher(JSONComparator comparator) { |
| 38 | + this(comparator, 0, Integer.MAX_VALUE); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Create ArrayValueMatcher to match specified element in actual array |
| 43 | + * against first element of expected array. |
| 44 | + * |
| 45 | + * @param comparator |
| 46 | + * comparator to use to compare elements |
| 47 | + */ |
| 48 | + public ArrayValueMatcher(JSONComparator comparator, int i) { |
| 49 | + this(comparator, i, i); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create ArrayValueMatcher to match every element in specified range |
| 54 | + * (inclusive) from actual array against elements taken in sequence from |
| 55 | + * expected array, repeating from start of expected array if necessary. |
| 56 | + * |
| 57 | + * @param comparator |
| 58 | + * comparator to use to compare elements |
| 59 | + * @from first element in actual array to compare |
| 60 | + * @to last element in actual array to compare |
| 61 | + */ |
| 62 | + public ArrayValueMatcher(JSONComparator comparator, int from, int to) { |
| 63 | + assert comparator != null : "comparator null"; |
| 64 | + assert from >= 0 : MessageFormat.format("from({0}) < 0", from); |
| 65 | + assert to >= from : MessageFormat.format("to({0}) < from({1})", to, |
| 66 | + from); |
| 67 | + this.comparator = comparator; |
| 68 | + this.from = from; |
| 69 | + this.to = to; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + /* |
| 74 | + * NOTE: method defined as required by ValueMatcher interface but will never |
| 75 | + * be called so defined simply to indicate match failure |
| 76 | + */ |
| 77 | + public boolean equal(T o1, T o2) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean equal(String prefix, T actual, T expected, JSONCompareResult result) { |
| 83 | + if (!(actual instanceof JSONArray)) { |
| 84 | + throw new IllegalArgumentException("ArrayValueMatcher applied to non-array actual value"); |
| 85 | + } |
| 86 | + try { |
| 87 | + JSONArray actualArray = (JSONArray) actual; |
| 88 | + JSONArray expectedArray = expected instanceof JSONArray ? (JSONArray) expected: new JSONArray(new Object[] { expected }); |
| 89 | + int first = Math.max(0, from); |
| 90 | + int last = Math.min(actualArray.length() - 1, to); |
| 91 | + int expectedLen = expectedArray.length(); |
| 92 | + for (int i = first; i <= last; i++) { |
| 93 | + String elementPrefix = MessageFormat.format("{0}[{1}]", prefix, i); |
| 94 | + Object actualElement = actualArray.get(i); |
| 95 | + Object expectedElement = expectedArray.get((i - first) % expectedLen); |
| 96 | + comparator.compareValues(elementPrefix, expectedElement, actualElement, result); |
| 97 | + } |
| 98 | + // values must match since no exceptions thrown |
| 99 | + return true; |
| 100 | + } |
| 101 | + catch (JSONException e) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments