tests: give json matching tests a way to fail on repeated keys instead of overwriting with the last key

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1666438 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2015-03-13 13:17:56 +00:00
parent 40a1d7e22c
commit d4cba95df4
1 changed files with 20 additions and 3 deletions

View File

@ -17,9 +17,11 @@
package org.apache.solr;
import org.noggit.JSONParser;
import org.noggit.ObjectBuilder;
import org.apache.solr.common.util.StrUtils;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -31,6 +33,7 @@ public class JSONTestUtil {
* Default delta used in numeric equality comparisons for floats and doubles.
*/
public final static double DEFAULT_DELTA = 1e-5;
public static boolean failRepeatedKeys = false;
/**
* comparison using default delta
@ -78,11 +81,25 @@ public class JSONTestUtil {
* @param delta tollerance allowed in comparing float/double values
*/
public static String match(String path, String input, String expected, double delta) throws Exception {
Object inputObj = ObjectBuilder.fromJSON(input);
Object expectObj = ObjectBuilder.fromJSON(expected);
Object inputObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(input)).getVal() : ObjectBuilder.fromJSON(input);
Object expectObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(expected)).getVal() : ObjectBuilder.fromJSON(expected);
return matchObj(path, inputObj, expectObj, delta);
}
static class NoDupsObjectBuilder extends ObjectBuilder {
public NoDupsObjectBuilder(JSONParser parser) throws IOException {
super(parser);
}
@Override
public void addKeyVal(Object map, Object key, Object val) throws IOException {
Object prev = ((Map<Object, Object>) map).put(key, val);
if (prev != null) {
throw new RuntimeException("REPEATED JSON OBJECT KEY: key=" + key + " prevValue=" + prev + " thisValue" + val);
}
}
}
/**
* @param path JSON path expression
* @param input JSON Structure
@ -243,7 +260,7 @@ class CollectionTester {
if (a >= expectedList.size() || b >=v.size()) {
popPath();
setErr("List size mismatch (expected: " + expectedList.size() + ", got: " + v.size() + ")");
setErr("List size mismatch");
return false;
}