use of org.mvel2.ast.Contains in project mvel by mikebrock.
the class PropertyAccessTests method testBindingCoercion.
public void testBindingCoercion() {
List list = new LinkedList();
list.add("Apple");
list.add("Peach");
list.add("Icing");
Cake cake = new Cake();
MVEL.setProperty(cake, "ingredients", list);
assertTrue(cake.getIngredients().contains("Apple"));
assertTrue(cake.getIngredients().contains("Peach"));
assertTrue(cake.getIngredients().contains("Icing"));
}
use of org.mvel2.ast.Contains in project mvel by mikebrock.
the class TypesAndInferenceTests method testAnalysisCompile.
public void testAnalysisCompile() {
ParserContext pCtx = new ParserContext();
ExpressionCompiler e = new ExpressionCompiler("foo.aValue = 'bar'");
e.setVerifyOnly(true);
e.compile(pCtx);
assertTrue(pCtx.getInputs().keySet().contains("foo"));
assertEquals(1, pCtx.getInputs().size());
assertEquals(0, pCtx.getVariables().size());
}
use of org.mvel2.ast.Contains in project mvel by mikebrock.
the class TypesAndInferenceTests method testVarInputs4.
public void testVarInputs4() {
ExpressionCompiler compiler = new ExpressionCompiler("System.out.println( message );");
compiler.compile();
assertTrue(compiler.getParserContextState().getInputs().keySet().contains("message"));
}
use of org.mvel2.ast.Contains in project mvel by mikebrock.
the class AbstractParser method reduce.
/**
* This method is called when we reach the point where we must subEval a trinary operation in the expression.
* (ie. val1 op val2). This is not the same as a binary operation, although binary operations would appear
* to have 3 structures as well. A binary structure (or also a junction in the expression) compares the
* current state against 2 downrange structures (usually an op and a val).
*/
protected void reduce() {
Object v1, v2;
int operator;
try {
switch(operator = (Integer) stk.pop()) {
case ADD:
case SUB:
case DIV:
case MULT:
case MOD:
case EQUAL:
case NEQUAL:
case GTHAN:
case LTHAN:
case GETHAN:
case LETHAN:
case POWER:
stk.op(operator);
break;
case AND:
v1 = stk.pop();
stk.push(((Boolean) stk.pop()) && ((Boolean) v1));
break;
case OR:
v1 = stk.pop();
stk.push(((Boolean) stk.pop()) || ((Boolean) v1));
break;
case CHOR:
v1 = stk.pop();
if (!isEmpty(v2 = stk.pop()) || !isEmpty(v1)) {
stk.clear();
stk.push(!isEmpty(v2) ? v2 : v1);
return;
} else
stk.push(null);
break;
case REGEX:
stk.push(java.util.regex.Pattern.compile(java.lang.String.valueOf(stk.pop())).matcher(java.lang.String.valueOf(stk.pop())).matches());
break;
case INSTANCEOF:
stk.push(((Class) stk.pop()).isInstance(stk.pop()));
break;
case CONVERTABLE_TO:
stk.push(org.mvel2.DataConversion.canConvert(stk.peek2().getClass(), (Class) stk.pop2()));
break;
case CONTAINS:
stk.push(containsCheck(stk.peek2(), stk.pop2()));
break;
case BW_AND:
stk.push(asInt(stk.peek2()) & asInt(stk.pop2()));
break;
case BW_OR:
stk.push(asInt(stk.peek2()) | asInt(stk.pop2()));
break;
case BW_XOR:
stk.push(asInt(stk.peek2()) ^ asInt(stk.pop2()));
break;
case BW_SHIFT_LEFT:
stk.push(asInt(stk.peek2()) << asInt(stk.pop2()));
break;
case BW_USHIFT_LEFT:
int iv2 = asInt(stk.peek2());
if (iv2 < 0)
iv2 *= -1;
stk.push(iv2 << asInt(stk.pop2()));
break;
case BW_SHIFT_RIGHT:
stk.push(asInt(stk.peek2()) >> asInt(stk.pop2()));
break;
case BW_USHIFT_RIGHT:
stk.push(asInt(stk.peek2()) >>> asInt(stk.pop2()));
break;
case SOUNDEX:
stk.push(soundex(java.lang.String.valueOf(stk.pop())).equals(soundex(java.lang.String.valueOf(stk.pop()))));
break;
case SIMILARITY:
stk.push(similarity(java.lang.String.valueOf(stk.pop()), java.lang.String.valueOf(stk.pop())));
break;
}
} catch (ClassCastException e) {
throw new CompileException("syntax error or incompatable types", expr, st, e);
} catch (ArithmeticException e) {
throw new CompileException("arithmetic error: " + e.getMessage(), expr, st, e);
} catch (Exception e) {
throw new CompileException("failed to subEval expression", expr, st, e);
}
}
use of org.mvel2.ast.Contains in project drools by kiegroup.
the class SecurityPolicyTest method testAccumulateFunctionMVEL.
@Test
public void testAccumulateFunctionMVEL() throws Exception {
String drl = "package org.foo.bar\n" + "import " + MaliciousExitHelper.class.getName().replace('$', '.') + " \n" + "rule testRule dialect \"mvel\" \n" + " when\n" + " Number() from accumulate(Object(), " + " sum(MaliciousExitHelper.exit()))\n" + " then\n" + "end";
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
ks.newKieBuilder(kfs).buildAll();
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
ksession.insert("foo");
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (PropertyAccessException e) {
// weak way of testing but couldn't find a better way
if (e.toString().contains("The security policy should have prevented")) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} else {
// test succeeded
}
} catch (Exception e) {
if (e.toString().contains("access denied (\"java.lang.RuntimePermission\" \"exitVM.0\")")) {
// test succeeded
} else {
throw e;
}
}
}
Aggregations