From 7f1765f1fa447295cf4160eadbcadce05375d78f Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Wed, 10 Jul 2013 15:54:35 +0000 Subject: [PATCH] tests: use single quoted json for readability git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1501809 13f79535-47bb-0310-9956-ffa450edef68 --- .../TestManagedSchemaFieldResource.java | 14 +++++------ .../java/org/apache/solr/SolrTestCaseJ4.java | 24 +++++++++++++++++-- .../org/apache/solr/util/RestTestBase.java | 9 +++---- 3 files changed, 32 insertions(+), 15 deletions(-) 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.java index 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 { @Test public 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.\""); } @Test public 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 { @Test public 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 { @Test public 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.java index 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. *

* 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("(?