Fix painless's out of bounds assertions in java 9

Java 9's exception message when lists have an out of bounds index
is much better than java 8 but the painless code asserted on the
java 8 message. Now it'll accept either.

I'm tempted to weaken the assertion but I like asserting that the
message is readable.
This commit is contained in:
Nik Everett 2016-10-29 22:19:48 -04:00
parent 3a7a218e8f
commit 1bbd3c5400
3 changed files with 22 additions and 21 deletions

View File

@ -39,9 +39,9 @@ public abstract class ArrayLikeObjectTestCase extends ScriptTestCase {
*/
protected abstract String valueCtorCall(String valueType, int size);
/**
* The type of the exception thrown by out of bounds accesses;
* Matcher for the message of the out of bounds exceptions thrown for too negative or too positive offsets.
*/
protected abstract Matcher<? super IndexOutOfBoundsException> outOfBoundsExceptionMatcher(int index, int size);
protected abstract Matcher<String> outOfBoundsExceptionMessageMatcher(int index, int size);
private void arrayLoadStoreTestCase(boolean declareAsDef, String valueType, Object val, @Nullable Number valPlusOne) {
String declType = declareAsDef ? "def" : declType(valueType);
@ -80,7 +80,7 @@ public abstract class ArrayLikeObjectTestCase extends ScriptTestCase {
IndexOutOfBoundsException e = expectScriptThrows(IndexOutOfBoundsException.class,
() -> exec(script, singletonMap("val", val), true));
try {
assertThat(e, outOfBoundsExceptionMatcher(index, 5));
assertThat(e.getMessage(), outOfBoundsExceptionMessageMatcher(index, 5));
} catch (AssertionError ae) {
ae.addSuppressed(e); // Mark the exception we are testing as suppressed so we get its stack trace. If it has one :(
throw ae;

View File

@ -24,9 +24,7 @@ import org.hamcrest.Matcher;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.equalTo;
/** Tests for working with arrays. */
public class ArrayTests extends ArrayLikeObjectTestCase {
@ -41,9 +39,8 @@ public class ArrayTests extends ArrayLikeObjectTestCase {
}
@Override
protected Matcher<? super IndexOutOfBoundsException> outOfBoundsExceptionMatcher(int index, int size) {
return both(instanceOf(ArrayIndexOutOfBoundsException.class))
.and(hasToString("java.lang.ArrayIndexOutOfBoundsException: " + index));
protected Matcher<String> outOfBoundsExceptionMessageMatcher(int index, int size) {
return equalTo(Integer.toString(index));
}
public void testArrayLengthHelper() throws Throwable {

View File

@ -23,10 +23,9 @@ import org.hamcrest.Matcher;
import java.util.Arrays;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
/** Tests for working with lists. */
public class ListTests extends ArrayLikeObjectTestCase {
@ -55,15 +54,20 @@ public class ListTests extends ArrayLikeObjectTestCase {
}
@Override
protected Matcher<? super IndexOutOfBoundsException> outOfBoundsExceptionMatcher(int index, int size) {
if (index > size) {
return hasToString("java.lang.IndexOutOfBoundsException: Index: " + index + ", Size: " + size);
protected Matcher<String> outOfBoundsExceptionMessageMatcher(int index, int size) {
if ("1.8".equals(Runtime.class.getPackage().getSpecificationVersion())) {
// 1.8 and below aren't as clean as 1.9+
if (index > size) {
return equalTo("Index: " + index + ", Size: " + size);
} else {
Matcher<String> m = equalTo(Integer.toString(index));
// If we set -XX:-OmitStackTraceInFastThrow we wouldn't need this
m = anyOf(m, nullValue());
return m;
}
} else {
Matcher<? super IndexOutOfBoundsException> m = both(instanceOf(ArrayIndexOutOfBoundsException.class))
.and(hasToString("java.lang.ArrayIndexOutOfBoundsException: " + index));
// If we set -XX:-OmitStackTraceInFastThrow we wouldn't need this
m = either(m).or(instanceOf(ArrayIndexOutOfBoundsException.class));
return m;
// Starting with 1.9 it gets nicer
return equalTo("Index " + index + " out-of-bounds for length " + size);
}
}