[TEST] Make sure that match assertion throws error if run against an object

We had a REST test that relied on matching a json response against a regex. It worked but the match wasn't done against the actual json object, but its java map representation converted into a string by calling `toString`. Since all other clients test runners don't work in this case, as they try to match a json object against a regex, we should do the same and prevent it from working.
This commit is contained in:
javanna 2015-02-02 19:25:33 +01:00 committed by Luca Cavanna
parent dfe67da013
commit 0e67dda15d
1 changed files with 5 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import java.util.regex.Pattern;
import static org.elasticsearch.test.hamcrest.RegexMatcher.matches; import static org.elasticsearch.test.hamcrest.RegexMatcher.matches;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -49,10 +50,12 @@ public class MatchAssertion extends Assertion {
if (expectedValue instanceof String) { if (expectedValue instanceof String) {
String expValue = ((String) expectedValue).trim(); String expValue = ((String) expectedValue).trim();
if (expValue.length() > 2 && expValue.startsWith("/") && expValue.endsWith("/")) { if (expValue.length() > 2 && expValue.startsWith("/") && expValue.endsWith("/")) {
assertThat("field [" + getField() + "] was expected to be of type String but is an instanceof [" + actualValue.getClass() + "]", actualValue, instanceOf(String.class));
String stringValue = (String) actualValue;
String regex = expValue.substring(1, expValue.length() - 1); String regex = expValue.substring(1, expValue.length() - 1);
logger.trace("assert that [{}] matches [{}]", actualValue, regex); logger.trace("assert that [{}] matches [{}]", stringValue, regex);
assertThat("field [" + getField() + "] was expected to match the provided regex but didn't", assertThat("field [" + getField() + "] was expected to match the provided regex but didn't",
actualValue.toString(), matches(regex, Pattern.COMMENTS)); stringValue, matches(regex, Pattern.COMMENTS));
return; return;
} }
} }