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
This commit is contained in:
Yonik Seeley 2013-07-10 15:54:35 +00:00
parent 2310ab22f0
commit 7f1765f1fa
3 changed files with 32 additions and 15 deletions

View File

@ -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"

View File

@ -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();

View File

@ -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;