Skip to content

Commit

Permalink
Move test classes to test root in Painless (elastic#40873)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jdconrad authored and Gurkan Kaymak committed May 27, 2019
1 parent b6fbd08 commit dd469f1
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ public void testMap_GroupBy() {
}

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)"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void testPrimitivesHaveMethods() {
}

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;"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ public void testNullSafeDeref() {
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
Expand All @@ -222,7 +222,7 @@ public void testNullSafeDeref() {
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));
Expand Down Expand Up @@ -254,8 +254,8 @@ public void testNullSafeDeref() {
+ "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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

/** 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;
Expand Down Expand Up @@ -51,11 +51,11 @@ public static int staticNumberTest(Number number) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public void testVirtualMethodReferenceDef() {

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() {
Expand Down Expand Up @@ -133,31 +133,31 @@ public void testCapturingMethodReferenceMultipleLambdas() {
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);"));
}

public void testCapturingMethodReferenceMultipleLambdasDefImpls() {
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);"));
}

public void testCapturingMethodReferenceMultipleLambdasDefInterface() {
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);"));
}

public void testCapturingMethodReferenceMultipleLambdasDefEverywhere() {
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);"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ public void testBoxedDefCalls() {
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);"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void testVoidReturnDef() {

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))"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public void testMethodDynamic() {
}

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);"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
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;
Expand All @@ -45,8 +45,6 @@
* 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
Expand All @@ -66,7 +64,9 @@ protected Settings scriptEngineSettings() {
*/
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;
}

Expand All @@ -91,12 +91,13 @@ public Object exec(String script, Map<String, Object> vars, boolean picky) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,8 +35,10 @@
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;
Expand All @@ -49,7 +51,6 @@
* 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(
Expand Down Expand Up @@ -379,10 +380,11 @@ public void testPField() {
+ "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");
}
Expand Down Expand Up @@ -497,10 +499,10 @@ public void testPSubMapShortcut() {

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))",
Expand Down Expand Up @@ -892,6 +894,14 @@ public void testSTryAndSCatch() {
+ "}");
}

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());
}
Expand Down
Loading

0 comments on commit dd469f1

Please sign in to comment.