Apache Lucene open-source search software
tests: use single quoted json for readability
Yonik Seeley7f1765f1fa44
diff --git a/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.java b/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.javaindex cd408cf82f4..77bdf5259ec 100644--- a/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.java+++ b/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.java@@ -58,14 +58,14 @@ public class TestManagedSchemaFieldResource extends RestTestBase {@Testpublic void testAddFieldBadFieldType() throws Exception {assertJPut("/schema/fields/newfield",- "{\"type\":\"not_in_there_at_all\",\"stored\":\"false\"}",+ json( "{'type':'not_in_there_at_all','stored':'false'}" ),"/error/msg==\"Field \\'newfield\\': Field type \\'not_in_there_at_all\\' not found.\"");}@Testpublic void testAddFieldMismatchedName() throws Exception {assertJPut("/schema/fields/newfield",- "{\"name\":\"something_else\",\"type\":\"text\",\"stored\":\"false\"}",+ json( "{'name':'something_else','type':'text','stored':'false'}" ),"/error/msg==\"Field name in the request body \\'something_else\\'"+ " doesn\\'t match field name in the request URL \\'newfield\\'\"");}@@ -73,7 +73,7 @@ public class TestManagedSchemaFieldResource extends RestTestBase {@Testpublic void testAddFieldBadProperty() throws Exception {assertJPut("/schema/fields/newfield",- "{\"type\":\"text\",\"no_property_with_this_name\":\"false\"}",+ json( "{'type':'text','no_property_with_this_name':'false'}" ),"/error/msg==\"java.lang.IllegalArgumentException: Invalid field property: no_property_with_this_name\"");}@@ -85,7 +85,7 @@ public class TestManagedSchemaFieldResource extends RestTestBase {"/response/lst[@name='error']/int[@name='code'] = '404'");assertJPut("/schema/fields/newfield",- "{\"type\":\"text\",\"stored\":\"false\"}",+ json( "{'type':'text','stored':'false'}" ),"/responseHeader/status==0");assertQ("/schema/fields/newfield?indent=on&wt=xml",@@ -176,9 +176,9 @@ public class TestManagedSchemaFieldResource extends RestTestBase {@Testpublic void testPostCopy() throws Exception {assertJPost("/schema/fields",- "[{\"name\":\"fieldA\",\"type\":\"text\",\"stored\":\"false\"},"- + "{\"name\":\"fieldB\",\"type\":\"text\",\"stored\":\"false\"},"- + " {\"name\":\"fieldC\",\"type\":\"text\",\"stored\":\"false\", \"copyFields\":[\"fieldB\"]}]",+ json( "[{'name':'fieldA','type':'text','stored':'false'},"+ + " {'name':'fieldB','type':'text','stored':'false'},"+ + " {'name':'fieldC','type':'text','stored':'false', 'copyFields':['fieldB']}]" ),"/responseHeader/status==0");assertQ("/schema/copyfields/?indent=on&wt=xml&source.fl=fieldC","count(/response/arr[@name='copyFields']/lst) = 1"diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.javaindex 0e48733f1a1..d5b558be22c 100755--- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java+++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java@@ -76,6 +76,8 @@ import java.util.Map;import java.util.logging.ConsoleHandler;import java.util.logging.Handler;import java.util.logging.Level;+import java.util.regex.Matcher;+import java.util.regex.Pattern;import static com.google.common.base.Preconditions.checkNotNull;@@ -642,7 +644,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {/*** Validates a query matches some JSON test expressions and closes the* query. The text expression is of the form path:JSON. To facilitate- * easy embedding in Java strings, the JSON can have double quotes+ * easy embedding in Java strings, the JSON tests can have double quotes* replaced with single quotes.* <p>* Please use this with care: this makes it easy to match complete@@ -677,7 +679,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {for (String test : tests) {if (test == null || test.length()==0) continue;- String testJSON = test.replace('\'', '"');+ String testJSON = json(test);try {failed = true;@@ -934,6 +936,24 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {return sd;}+ /** Converts "test JSON" and returns standard JSON.+ * Currently this only consists of changing unescaped single quotes to double quotes,+ * and escaped single quotes to single quotes.+ *+ * The primary purpose is to be able to easily embed JSON strings in a JAVA string+ * with the best readability.+ *+ * This transformation is automatically applied to JSON test srings (like assertJQ).+ */+ public static String json(String testJSON) {+ testJSON = nonEscapedSingleQuotePattern.matcher(testJSON).replaceAll("\"");+ testJSON = escapedSingleQuotePattern.matcher(testJSON).replaceAll("'");+ return testJSON;+ }+ private static Pattern nonEscapedSingleQuotePattern = Pattern.compile("(?<!\\\\)\'");+ private static Pattern escapedSingleQuotePattern = Pattern.compile("\\\\\'");++/** Creates JSON from a SolrInputDocument. Doesn't currently handle boosts. */public static String json(SolrInputDocument doc) {CharArr out = new CharArr();diff --git a/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java b/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.javaindex 9041f0bfa57..9d551a99101 100644--- a/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java+++ b/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java@@ -229,8 +229,7 @@ abstract public class RestTestBase extends SolrJettyTestBase {for (String test : tests) {if (null == test || 0 == test.length()) continue;- String testJSON = test.replaceAll("(?<!\\\\)\'", "\"");- testJSON = testJSON.replaceAll("\\\\\'", "'");+ String testJSON = json(test);try {failed = true;@@ -311,8 +310,7 @@ abstract public class RestTestBase extends SolrJettyTestBase {for (String test : tests) {if (null == test || 0 == test.length()) continue;- String testJSON = test.replaceAll("(?<!\\\\)\'", "\"");- testJSON = testJSON.replaceAll("\\\\\'", "'");+ String testJSON = json(test);try {failed = true;@@ -391,8 +389,7 @@ abstract public class RestTestBase extends SolrJettyTestBase {for (String test : tests) {if (null == test || 0 == test.length()) continue;- String testJSON = test.replaceAll("(?<!\\\\)\'", "\"");- testJSON = testJSON.replaceAll("\\\\\'", "'");+ String testJSON = json(test);try {failed = true;