lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r1666438 - /lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java
Date Fri, 13 Mar 2015 13:17:56 GMT
Author: yonik
Date: Fri Mar 13 13:17:56 2015
New Revision: 1666438

URL: http://svn.apache.org/r1666438
Log:
tests: give json matching tests a way to fail on repeated keys instead of overwriting with
the last key

Modified:
    lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java

Modified: lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java?rev=1666438&r1=1666437&r2=1666438&view=diff
==============================================================================
--- lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java (original)
+++ lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java Fri Mar
13 13:17:56 2015
@@ -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;
       }
 



Mime
View raw message