Move test classes to test root in Painless (#40873)

This moves several test classes that were part of the main root to the test root. 
These were part of the main root due to limitations prior to whitelist 
customization. Without whitelist customization these can be moved to a test 
context and removed from the base whitelists as they should not be user 
facing.
This commit is contained in:
Jack Conradson 2019-04-05 15:15:14 -07:00
parent ac58b9bded
commit fd51780de2
15 changed files with 82 additions and 76 deletions

View File

@ -240,30 +240,3 @@ class org.elasticsearch.index.query.IntervalFilterScript$Interval {
int getEnd()
int getGaps()
}
# for testing
class org.elasticsearch.painless.FeatureTest no_import {
int z
()
(int,int)
int getX()
int getY()
Integer getI()
void setX(int)
void setY(int)
void setI(Integer)
boolean overloadedStatic()
boolean overloadedStatic(boolean)
int staticNumberTest(Number)
Double mixedAdd(int, Byte, char, Float)
Object twoFunctionsOfX(Function,Function)
void listInput(List)
int org.elasticsearch.painless.FeatureTestAugmentation getTotal()
int org.elasticsearch.painless.FeatureTestAugmentation addToTotal(int)
}
# for testing
static_import {
int staticAddIntsTest(int, int) from_class org.elasticsearch.painless.StaticTest
float staticAddFloatsTest(float, float) from_class org.elasticsearch.painless.FeatureTest
}

View File

@ -190,13 +190,13 @@ public class AugmentationTests extends ScriptTestCase {
}
public void testFeatureTest() {
assertEquals(5, exec("org.elasticsearch.painless.FeatureTest ft = new org.elasticsearch.painless.FeatureTest();" +
assertEquals(5, exec("org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" +
" ft.setX(3); ft.setY(2); return ft.getTotal()"));
assertEquals(5, exec("def ft = new org.elasticsearch.painless.FeatureTest();" +
assertEquals(5, exec("def ft = new org.elasticsearch.painless.FeatureTestObject();" +
" ft.setX(3); ft.setY(2); return ft.getTotal()"));
assertEquals(8, exec("org.elasticsearch.painless.FeatureTest ft = new org.elasticsearch.painless.FeatureTest();" +
assertEquals(8, exec("org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" +
" ft.setX(3); ft.setY(2); return ft.addToTotal(3)"));
assertEquals(8, exec("def ft = new org.elasticsearch.painless.FeatureTest();" +
assertEquals(8, exec("def ft = new org.elasticsearch.painless.FeatureTestObject();" +
" ft.setX(3); ft.setY(2); return ft.addToTotal(3)"));
}
}

View File

@ -127,7 +127,7 @@ public class BasicAPITests extends ScriptTestCase {
}
public void testPublicMemberAccess() {
assertEquals(5, exec("org.elasticsearch.painless.FeatureTest ft = new org.elasticsearch.painless.FeatureTest();" +
assertEquals(5, exec("org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" +
"ft.z = 5; return ft.z;"));
}

View File

@ -191,11 +191,11 @@ public class BasicExpressionTests extends ScriptTestCase {
assertNull( exec("def a = null; return a?.length()"));
assertEquals(3, exec("def a = 'foo'; return a?.length()"));
// Read shortcut
assertMustBeNullable( "org.elasticsearch.painless.FeatureTest a = null; return a?.x");
assertMustBeNullable( "org.elasticsearch.painless.FeatureTestObject a = null; return a?.x");
assertMustBeNullable(
"org.elasticsearch.painless.FeatureTest a = new org.elasticsearch.painless.FeatureTest(); return a?.x");
"org.elasticsearch.painless.FeatureTestObject a = new org.elasticsearch.painless.FeatureTestObject(); return a?.x");
assertNull( exec("def a = null; return a?.x"));
assertEquals(0, exec("def a = new org.elasticsearch.painless.FeatureTest(); return a?.x"));
assertEquals(0, exec("def a = new org.elasticsearch.painless.FeatureTestObject(); return a?.x"));
// Maps
// Call
@ -222,7 +222,7 @@ public class BasicExpressionTests extends ScriptTestCase {
assertEquals(2, exec("def a = new int[] {2, 3}; return a?.length"));
// Results from maps (should just work but let's test anyway)
FeatureTest t = new FeatureTest();
FeatureTestObject t = new FeatureTestObject();
assertNull( exec("Map a = ['thing': params.t]; return a.other?.getX()", singletonMap("t", t), true));
assertNull( exec("Map a = ['thing': params.t]; return a.other?.x", singletonMap("t", t), true));
assertNull( exec("def a = ['thing': params.t]; return a.other?.getX()", singletonMap("t", t), true));
@ -254,8 +254,8 @@ public class BasicExpressionTests extends ScriptTestCase {
+ "return a.missing_length", true));
// Writes, all unsupported at this point
// assertEquals(null, exec("org.elasticsearch.painless.FeatureTest a = null; return a?.x")); // Read field
// assertEquals(null, exec("org.elasticsearch.painless.FeatureTest a = null; a?.x = 7; return a?.x")); // Write field
// assertEquals(null, exec("org.elasticsearch.painless.FeatureTestObject a = null; return a?.x")); // Read field
// assertEquals(null, exec("org.elasticsearch.painless.FeatureTestObject a = null; a?.x = 7; return a?.x")); // Write field
// assertEquals(null, exec("Map a = null; a?.other = 'wow'; return a?.other")); // Write shortcut
// assertEquals(null, exec("def a = null; a?.other = 'cat'; return a?.other")); // Write shortcut
// assertEquals(null, exec("Map a = ['thing': 'bar']; a.other?.cat = 'no'; return a.other?.cat"));

View File

@ -19,14 +19,14 @@
package org.elasticsearch.painless;
public class FeatureTestAugmentation {
public static int getTotal(FeatureTest ft) {
public class FeatureTestAugmentationObject {
public static int getTotal(FeatureTestObject ft) {
return ft.getX() + ft.getY();
}
public static int addToTotal(FeatureTest ft, int add) {
public static int addToTotal(FeatureTestObject ft, int add) {
return getTotal(ft) + add;
}
private FeatureTestAugmentation() {}
private FeatureTestAugmentationObject() {}
}

View File

@ -23,7 +23,7 @@ import java.util.function.Function;
*/
/** Currently just a dummy class for testing a few features not yet exposed by whitelist! */
public class FeatureTest {
public class FeatureTestObject {
/** static method that returns true */
public static boolean overloadedStatic() {
return true;
@ -51,11 +51,11 @@ public class FeatureTest {
private Integer i;
/** empty ctor */
public FeatureTest() {
public FeatureTestObject() {
}
/** ctor with params */
public FeatureTest(int x, int y) {
public FeatureTestObject(int x, int y) {
this.x = x;
this.y = y;
}

View File

@ -46,12 +46,12 @@ public class FunctionRefTests extends ScriptTestCase {
public void testQualifiedStaticMethodReference() {
assertEquals(true,
exec("List l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTest::overloadedStatic).findFirst().get()"));
exec("List l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTestObject::overloadedStatic).findFirst().get()"));
}
public void testQualifiedStaticMethodReferenceDef() {
assertEquals(true,
exec("def l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTest::overloadedStatic).findFirst().get()"));
exec("def l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTestObject::overloadedStatic).findFirst().get()"));
}
public void testQualifiedVirtualMethodReference() {
@ -133,7 +133,7 @@ public class FunctionRefTests extends ScriptTestCase {
assertEquals("testingcdefg", exec(
"String x = 'testing';" +
"String y = 'abcdefg';" +
"org.elasticsearch.painless.FeatureTest test = new org.elasticsearch.painless.FeatureTest(2,3);" +
"org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" +
"return test.twoFunctionsOfX(x::concat, y::substring);"));
}
@ -141,7 +141,7 @@ public class FunctionRefTests extends ScriptTestCase {
assertEquals("testingcdefg", exec(
"def x = 'testing';" +
"def y = 'abcdefg';" +
"org.elasticsearch.painless.FeatureTest test = new org.elasticsearch.painless.FeatureTest(2,3);" +
"org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" +
"return test.twoFunctionsOfX(x::concat, y::substring);"));
}
@ -149,7 +149,7 @@ public class FunctionRefTests extends ScriptTestCase {
assertEquals("testingcdefg", exec(
"String x = 'testing';" +
"String y = 'abcdefg';" +
"def test = new org.elasticsearch.painless.FeatureTest(2,3);" +
"def test = new org.elasticsearch.painless.FeatureTestObject(2,3);" +
"return test.twoFunctionsOfX(x::concat, y::substring);"));
}
@ -157,7 +157,7 @@ public class FunctionRefTests extends ScriptTestCase {
assertEquals("testingcdefg", exec(
"def x = 'testing';" +
"def y = 'abcdefg';" +
"def test = new org.elasticsearch.painless.FeatureTest(2,3);" +
"def test = new org.elasticsearch.painless.FeatureTestObject(2,3);" +
"return test.twoFunctionsOfX(x::concat, y::substring);"));
}

View File

@ -333,15 +333,15 @@ public class GeneralCastTests extends ScriptTestCase {
assertEquals(1, exec("def y = 2.0; y.compareTo(1);"));
assertEquals(1, exec("int x = 1; def y = 2.0; y.compareTo(x);"));
assertEquals(-1, exec("Integer x = Integer.valueOf(3); def y = 2.0; y.compareTo(x);"));
assertEquals(2, exec("def f = new org.elasticsearch.painless.FeatureTest(); f.i = (byte)2; f.i"));
assertEquals(2, exec("def f = new org.elasticsearch.painless.FeatureTestObject(); f.i = (byte)2; f.i"));
assertEquals(4.0, exec(
"def x = new org.elasticsearch.painless.FeatureTest(); " +
"def x = new org.elasticsearch.painless.FeatureTestObject(); " +
"Byte i = Byte.valueOf(3); " +
"byte j = 1;" +
"Short s = Short.valueOf(-2);" +
"x.mixedAdd(j, i, (char)2, s)"
));
assertNull(exec("def f = new org.elasticsearch.painless.FeatureTest(); f.i = null; f.i"));
assertNull(exec("def f = new org.elasticsearch.painless.FeatureTestObject(); f.i = null; f.i"));
expectScriptThrows(ClassCastException.class, () -> exec("def x = 2.0; def y = 1; y.compareTo(x);"));
expectScriptThrows(ClassCastException.class, () -> exec("float f = 1.0f; def y = 1; y.compareTo(f);"));
}

View File

@ -112,7 +112,7 @@ public class LambdaTests extends ScriptTestCase {
public void testTwoLambdas() {
assertEquals("testingcdefg", exec(
"org.elasticsearch.painless.FeatureTest test = new org.elasticsearch.painless.FeatureTest(2,3);" +
"org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" +
"return test.twoFunctionsOfX(x -> 'testing'.concat(x), y -> 'abcdefg'.substring(y))"));
}

View File

@ -41,14 +41,14 @@ public class OverloadTests extends ScriptTestCase {
}
public void testConstructor() {
assertEquals(true, exec("org.elasticsearch.painless.FeatureTest f = new org.elasticsearch.painless.FeatureTest();" +
assertEquals(true, exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject();" +
"return f.x == 0 && f.y == 0;"));
assertEquals(true, exec("org.elasticsearch.painless.FeatureTest f = new org.elasticsearch.painless.FeatureTest(1, 2);" +
assertEquals(true, exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(1, 2);" +
"return f.x == 1 && f.y == 2;"));
}
public void testStatic() {
assertEquals(true, exec("return org.elasticsearch.painless.FeatureTest.overloadedStatic();"));
assertEquals(false, exec("return org.elasticsearch.painless.FeatureTest.overloadedStatic(false);"));
assertEquals(true, exec("return org.elasticsearch.painless.FeatureTestObject.overloadedStatic();"));
assertEquals(false, exec("return org.elasticsearch.painless.FeatureTestObject.overloadedStatic(false);"));
}
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.painless;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.painless.lookup.PainlessClass;

View File

@ -22,14 +22,14 @@ package org.elasticsearch.painless;
import junit.framework.AssertionFailedError;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.painless.antlr.Walker;
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupBuilder;
import org.elasticsearch.painless.spi.Whitelist;
import org.elasticsearch.painless.spi.WhitelistLoader;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptException;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -45,8 +45,6 @@ import static org.hamcrest.Matchers.hasSize;
* Typically just asserts the output of {@code exec()}
*/
public abstract class ScriptTestCase extends ESTestCase {
private static final PainlessLookup PAINLESS_LOOKUP = PainlessLookupBuilder.buildFromWhitelists(Whitelist.BASE_WHITELISTS);
protected PainlessScriptEngine scriptEngine;
@Before
@ -66,7 +64,9 @@ public abstract class ScriptTestCase extends ESTestCase {
*/
protected Map<ScriptContext<?>, List<Whitelist>> scriptContexts() {
Map<ScriptContext<?>, List<Whitelist>> contexts = new HashMap<>();
contexts.put(PainlessTestScript.CONTEXT, Whitelist.BASE_WHITELISTS);
List<Whitelist> whitelists = new ArrayList<>(Whitelist.BASE_WHITELISTS);
whitelists.add(WhitelistLoader.loadFromResourceFiles(Whitelist.class, "org.elasticsearch.painless.test"));
contexts.put(PainlessTestScript.CONTEXT, whitelists);
return contexts;
}
@ -91,12 +91,13 @@ public abstract class ScriptTestCase extends ESTestCase {
public Object exec(String script, Map<String, Object> vars, Map<String,String> compileParams, boolean picky) {
// test for ambiguity errors before running the actual script if picky is true
if (picky) {
ScriptClassInfo scriptClassInfo = new ScriptClassInfo(PAINLESS_LOOKUP, PainlessTestScript.class);
ScriptClassInfo scriptClassInfo =
new ScriptClassInfo(scriptEngine.getContextsToLookups().get(PainlessTestScript.CONTEXT), PainlessTestScript.class);
CompilerSettings pickySettings = new CompilerSettings();
pickySettings.setPicky(true);
pickySettings.setRegexesEnabled(CompilerSettings.REGEX_ENABLED.get(scriptEngineSettings()));
Walker.buildPainlessTree(
scriptClassInfo, new MainMethodReserved(), getTestName(), script, pickySettings, PAINLESS_LOOKUP, null);
Walker.buildPainlessTree(scriptClassInfo, new MainMethodReserved(), getTestName(), script, pickySettings,
scriptEngine.getContextsToLookups().get(PainlessTestScript.CONTEXT), null);
}
// test actual script execution
PainlessTestScript.Factory factory = scriptEngine.compile(null, script, PainlessTestScript.CONTEXT, compileParams);

View File

@ -19,7 +19,7 @@
package org.elasticsearch.painless;
public class StaticTest {
public class StaticTestObject {
public static int staticAddIntsTest(int x, int y) {
return x + y;
}

View File

@ -20,12 +20,12 @@
package org.elasticsearch.painless.node;
import org.elasticsearch.painless.CompilerSettings;
import org.elasticsearch.painless.FeatureTest;
import org.elasticsearch.painless.FeatureTestObject;
import org.elasticsearch.painless.Locals.Variable;
import org.elasticsearch.painless.Location;
import org.elasticsearch.painless.Operation;
import org.elasticsearch.painless.action.PainlessExecuteAction.PainlessTestScript;
import org.elasticsearch.painless.ScriptClassInfo;
import org.elasticsearch.painless.action.PainlessExecuteAction.PainlessTestScript;
import org.elasticsearch.painless.antlr.Walker;
import org.elasticsearch.painless.lookup.PainlessCast;
import org.elasticsearch.painless.lookup.PainlessClass;
@ -35,8 +35,10 @@ import org.elasticsearch.painless.lookup.PainlessLookupBuilder;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.spi.Whitelist;
import org.elasticsearch.painless.spi.WhitelistLoader;
import org.elasticsearch.test.ESTestCase;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -49,7 +51,6 @@ import static org.elasticsearch.painless.node.SSource.MainMethodReserved;
* Tests {@link Object#toString} implementations on all extensions of {@link ANode}.
*/
public class NodeToStringTests extends ESTestCase {
private final PainlessLookup painlessLookup = PainlessLookupBuilder.buildFromWhitelists(Whitelist.BASE_WHITELISTS);
public void testEAssignment() {
assertToString(
@ -379,10 +380,11 @@ public class NodeToStringTests extends ESTestCase {
+ "return a.length");
assertToString(
"(SSource\n"
+ " (SDeclBlock (SDeclaration org.elasticsearch.painless.FeatureTest a (ENewObj org.elasticsearch.painless.FeatureTest)))\n"
+ " (SDeclBlock (SDeclaration org.elasticsearch.painless.FeatureTestObject a"
+ " (ENewObj org.elasticsearch.painless.FeatureTestObject)))\n"
+ " (SExpression (EAssignment (PField (EVariable a) x) = (ENumeric 10)))\n"
+ " (SReturn (PField (EVariable a) x)))",
"org.elasticsearch.painless.FeatureTest a = new org.elasticsearch.painless.FeatureTest();\n"
"org.elasticsearch.painless.FeatureTestObject a = new org.elasticsearch.painless.FeatureTestObject();\n"
+ "a.x = 10;\n"
+ "return a.x");
}
@ -497,10 +499,10 @@ public class NodeToStringTests extends ESTestCase {
public void testPSubShortcut() {
Location l = new Location(getTestName(), 0);
PainlessClass s = painlessLookup.lookupPainlessClass(FeatureTest.class);
PainlessClass s = painlessLookup.lookupPainlessClass(FeatureTestObject.class);
PainlessMethod getter = s.methods.get(PainlessLookupUtility.buildPainlessMethodKey("getX", 0));
PainlessMethod setter = s.methods.get(PainlessLookupUtility.buildPainlessMethodKey("setX", 1));
PSubShortcut node = new PSubShortcut(l, "x", FeatureTest.class.getName(), getter, setter);
PSubShortcut node = new PSubShortcut(l, "x", FeatureTestObject.class.getName(), getter, setter);
node.prefix = new EVariable(l, "a");
assertEquals("(PSubShortcut (EVariable a) x)", node.toString());
assertEquals("(PSubNullSafeCallInvoke (PSubShortcut (EVariable a) x))",
@ -892,6 +894,14 @@ public class NodeToStringTests extends ESTestCase {
+ "}");
}
private final PainlessLookup painlessLookup;
public NodeToStringTests() {
List<Whitelist> whitelists = new ArrayList<>(Whitelist.BASE_WHITELISTS);
whitelists.add(WhitelistLoader.loadFromResourceFiles(Whitelist.class, "org.elasticsearch.painless.test"));
painlessLookup = PainlessLookupBuilder.buildFromWhitelists(whitelists);
}
private void assertToString(String expected, String code) {
assertEquals(expected, walk(code).toString());
}

View File

@ -2,7 +2,29 @@
class org.elasticsearch.painless.BindingsTests$BindingsTestScript {
}
class org.elasticsearch.painless.FeatureTestObject no_import {
int z
()
(int,int)
int getX()
int getY()
Integer getI()
void setX(int)
void setY(int)
void setI(Integer)
boolean overloadedStatic()
boolean overloadedStatic(boolean)
int staticNumberTest(Number)
Double mixedAdd(int, Byte, char, Float)
Object twoFunctionsOfX(Function,Function)
void listInput(List)
int org.elasticsearch.painless.FeatureTestAugmentationObject getTotal()
int org.elasticsearch.painless.FeatureTestAugmentationObject addToTotal(int)
}
static_import {
int staticAddIntsTest(int, int) from_class org.elasticsearch.painless.StaticTestObject
float staticAddFloatsTest(float, float) from_class org.elasticsearch.painless.FeatureTestObject
int addWithState(int, int, int, double) bound_to org.elasticsearch.painless.BindingsTests$BindingTestClass
int addThisWithState(BindingsTests.BindingsTestScript, int, int, int, double) bound_to org.elasticsearch.painless.BindingsTests$ThisBindingTestClass
int addEmptyThisWithState(BindingsTests.BindingsTestScript, int) bound_to org.elasticsearch.painless.BindingsTests$EmptyThisBindingTestClass