diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java index 16995f60dffc9..9045a390f2ae3 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java @@ -1,5 +1,3 @@ -package org.elasticsearch.painless; - /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with @@ -19,7 +17,11 @@ * under the License. */ -import org.apache.lucene.util.Constants; +package org.elasticsearch.painless; + +import org.elasticsearch.test.ESTestCase; + +import static java.util.Collections.singletonMap; // TODO: Figure out a way to test autobox caching properly from methods such as Integer.valueOf(int); public class EqualsTests extends ScriptTestCase { @@ -132,11 +134,13 @@ public void testBranchEquals() { } public void testBranchEqualsDefAndPrimitive() { - assumeFalse("test fails on Windows", Constants.WINDOWS); - assertEquals(true, exec("def x = 1000; int y = 1000; return x == y;")); - assertEquals(false, exec("def x = 1000; int y = 1000; return x === y;")); - assertEquals(true, exec("def x = 1000; int y = 1000; return y == x;")); - assertEquals(false, exec("def x = 1000; int y = 1000; return y === x;")); + /* This test needs an Integer that isn't cached by Integer.valueOf so we draw one randomly. We can't use any fixed integer because + * we can never be sure that the JVM hasn't configured itself to cache that Integer. It is sneaky like that. */ + int uncachedAutoboxedInt = randomValueOtherThanMany(i -> Integer.valueOf(i) == Integer.valueOf(i), ESTestCase::randomInt); + assertEquals(true, exec("def x = params.i; int y = params.i; return x == y;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(false, exec("def x = params.i; int y = params.i; return x === y;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(true, exec("def x = params.i; int y = params.i; return y == x;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(false, exec("def x = params.i; int y = params.i; return y === x;", singletonMap("i", uncachedAutoboxedInt), true)); } public void testBranchNotEquals() { @@ -150,11 +154,13 @@ public void testBranchNotEquals() { } public void testBranchNotEqualsDefAndPrimitive() { - assumeFalse("test fails on Windows", Constants.WINDOWS); - assertEquals(false, exec("def x = 1000; int y = 1000; return x != y;")); - assertEquals(true, exec("def x = 1000; int y = 1000; return x !== y;")); - assertEquals(false, exec("def x = 1000; int y = 1000; return y != x;")); - assertEquals(true, exec("def x = 1000; int y = 1000; return y !== x;")); + /* This test needs an Integer that isn't cached by Integer.valueOf so we draw one randomly. We can't use any fixed integer because + * we can never be sure that the JVM hasn't configured itself to cache that Integer. It is sneaky like that. */ + int uncachedAutoboxedInt = randomValueOtherThanMany(i -> Integer.valueOf(i) == Integer.valueOf(i), ESTestCase::randomInt); + assertEquals(false, exec("def x = params.i; int y = params.i; return x != y;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(true, exec("def x = params.i; int y = params.i; return x !== y;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(false, exec("def x = params.i; int y = params.i; return y != x;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(true, exec("def x = params.i; int y = params.i; return y !== x;", singletonMap("i", uncachedAutoboxedInt), true)); } public void testRightHandNull() {