@Test public void testNoParams2() throws Exception { List list = new ArrayList(); list.add(ExpressionFactory.matchExp("k1", new ExpressionParameter("test1"))); list.add(ExpressionFactory.matchExp("k2", new ExpressionParameter("test2"))); list.add(ExpressionFactory.matchExp("k3", new ExpressionParameter("test3"))); list.add(ExpressionFactory.matchExp("k4", new ExpressionParameter("test4"))); Expression e1 = ExpressionFactory.or(list); Map params = new HashMap(); params.put("test2", "abc"); params.put("test3", "xyz"); Expression e2 = e1.params(params); // some expression nodes must be pruned assertNotNull(e2); assertEquals(2, e2.getOperandCount()); Expression k2 = (Expression) e2.getOperand(0); assertEquals("abc", k2.getOperand(1)); Expression k3 = (Expression) e2.getOperand(1); assertEquals("xyz", k3.getOperand(1)); }
@Test(expected = ExpressionException.class) public void testFailOnMissingParams() throws Exception { Expression e1 = ExpressionFactory.matchExp("k1", new ExpressionParameter("test")); e1 = e1.orExp(ExpressionFactory.matchExp("k2", "v2")); e1 = e1.orExp(ExpressionFactory.matchExp("k3", "v3")); e1.params(new HashMap(), false); }
private Object evaluateExpression(Node<?> context, Node<?> thisNode, String expr) throws InvalidExpressionException { ExpressionFactory expressionFactory = context.getRecord().getSurveyContext().getExpressionFactory(); DefaultValueExpression expression = expressionFactory.createDefaultValueExpression(expr); Object object = expression.evaluate(context, thisNode); return object; }
static ValueExpression restore(Object value, Class type, FacesContext context) { if (value == null) return null; String expr = (String) value; Application app = context.getApplication(); ExpressionFactory factory = app.getExpressionFactory(); return factory.createValueExpression(context.getELContext(), expr, type); }
/** Tests how parameter substitution algorithm works on an expression with no parameters. */ @Test public void testCopy1() throws Exception { Expression e1 = ExpressionFactory.matchExp("k1", "v1"); e1 = e1.orExp(ExpressionFactory.matchExp("k2", "v2")); e1 = e1.orExp(ExpressionFactory.matchExp("k3", "v3")); Expression e2 = e1.params(new HashMap()); TstTraversalHandler.compareExps(e1, e2); }
@Test public void testNullRequiredParameter2() throws Exception { Expression e1 = ExpressionFactory.exp("abc = 3 and x = $a"); Map params = new HashMap(); params.put("a", null); Expression e2 = e1.params(params, false); // null must be preserved assertEquals(ExpressionFactory.exp("abc = 3 and x = null"), e2); }
static ValueExpression restoreWithType(Object value, FacesContext context) { if (value == null) return null; Object[] state = (Object[]) value; String expr = (String) state[0]; Class type = (Class) state[1]; Application app = context.getApplication(); ExpressionFactory factory = app.getExpressionFactory(); return factory.createValueExpression(context.getELContext(), expr, type); }
/** @throws java.lang.Exception */ @Before public void setUp() throws Exception { solver = new YicesSolver(); esp = ExpressionFactory.createVariable("esp", 32); m1 = ExpressionFactory.createMemoryLocation( ExpressionFactory.createPlus(esp, ExpressionFactory.createNumber(4, 32)), 32); m2 = ExpressionFactory.createMemoryLocation( ExpressionFactory.createPlus(esp, ExpressionFactory.createNumber(8, 32)), 32); m3 = ExpressionFactory.createMemoryLocation( ExpressionFactory.createPlus(esp, ExpressionFactory.createNumber(4, 32)), 32); }
/** Tests how parameter substitution algorithm works on an expression with no parameters. */ @Test public void testCopy2() throws Exception { Expression andExp = ExpressionFactory.matchExp("k1", "v1"); andExp = andExp.andExp(ExpressionFactory.matchExp("k2", "v2")); andExp = andExp.andExp(ExpressionFactory.matchExp("k3", "v3")); List exprs = new ArrayList(); exprs.add(andExp); exprs.add(ExpressionFactory.matchExp("k1", "v1")); Expression e1 = ExpressionFactory.joinExp(Expression.OR, exprs); Expression e2 = e1.params(new HashMap()); TstTraversalHandler.compareExps(e1, e2); }
protected void parseParamStrings(String paramString, ExpressionFactory expressionFactory) { List paramStrings = Util.tokenizeIgnoringEnclosers(paramString, ','); for (Iterator itr = paramStrings.iterator(); itr.hasNext(); ) { String nextParam = ((String) itr.next()).trim(); if (!(nextParam.equals(""))) { Debug.logn("Find match for param " + nextParam, this); if (CallExpression.isVariable(this.context, nextParam)) { DNVariable v = CallExpression.getVariable(this.context, nextParam); parameters.add(v); } else if (expressionFactory.getExpression(nextParam, this.context) != null) { parameters.add(expressionFactory.getExpression(nextParam, this.context)); } } } }
/** * Creates a new expression that joins this object with another expression, using specified join * type. It is very useful for incrementally building chained expressions, like long AND or OR * statements. */ public Expression joinExp(int type, Expression exp) { Expression join = ExpressionFactory.expressionOfType(type); join.setOperand(0, this); join.setOperand(1, exp); join.flattenTree(); return join; }
@Test public void testNulls() { Expression e1 = ExpressionFactory.exp("x = null"); Expression e2 = e1.params(Collections.EMPTY_MAP, true); assertNotNull(e2); TstTraversalHandler.compareExps(e1, e2); }
@Test public void testNullOptionalParameter() throws Exception { Expression e = ExpressionFactory.exp("abc = 3 and x = $a"); Expression e1 = e.params(Collections.EMPTY_MAP, true); // $a must be pruned assertEquals(ExpressionFactory.exp("abc = 3"), e1); Map params = new HashMap(); params.put("a", null); Expression e2 = e.params(params, true); // null must be preserved assertEquals(ExpressionFactory.exp("abc = 3 and x = null"), e2); }
@Test public void testParams_Positional_Repeating() { Expression e = ExpressionFactory.exp("a = $a or x = $x and y = $x"); Expression ep = e.paramsArray("A", 5); assertNotSame(e, ep); assertEquals("(a = \"A\") or ((x = 5) and (y = 5))", ep.toString()); }
@Test public void testParams_NullHandling_CAY847() { Expression e = ExpressionFactory.exp("X = $x"); e = e.params(Collections.singletonMap("x", null)); assertEquals("X = null", e.toString()); }
private RTLExpression addClause(RTLExpression formula, RTLExpression clause) { if (formula != null) { return ExpressionFactory.createAnd(formula, clause); } else { return clause; } }
@Test public void testNoParams1_FromString() { Expression e1 = ExpressionFactory.exp("k1 = $test"); Expression e2 = e1.params(Collections.EMPTY_MAP); // all expression nodes must be pruned assertNull(e2); }
/** Tests how parameter substitution algorithm works on an expression with no parameters. */ @Test public void testInParameter() throws Exception { Expression inExp = ExpressionFactory.exp("k1 in $test"); Expression e1 = ExpressionFactory.exp("k1 in ('a', 'b')"); Expression transformed = inExp.params(Collections.singletonMap("test", new Object[] {"a", "b"})); TstTraversalHandler.compareExps(e1, transformed); // just in case manually check params DataObject o1 = new CayenneDataObject(); o1.writePropertyDirectly("k1", "a"); assertTrue(transformed.match(o1)); DataObject o2 = new CayenneDataObject(); o2.writePropertyDirectly("k1", "x"); assertFalse(transformed.match(o2)); }
@Test public void testNoParams1() throws Exception { Expression e1 = ExpressionFactory.matchExp("k1", new ExpressionParameter("test")); Expression e2 = e1.params(Collections.<String, Object>emptyMap()); // all expression nodes must be pruned assertNull(e2); }
private ExpressionEvaluator<V, D> createDefaultEvaluator( ExpressionFactory factory, String contextDescription, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException { ExpressionEvaluatorFactory evaluatorFactory = factory.getDefaultEvaluatorFactory(); if (evaluatorFactory == null) { throw new SystemException("Internal error: No default expression evaluator factory"); } return evaluatorFactory.createEvaluator( null, outputDefinition, contextDescription, task, result); }
/** * Lookup an expression factory used to coerce method parameters in context under key <code> * "javax.el.ExpressionFactory"</code>. If no expression factory can be found under that key, use * a default instance created with {@link ExpressionFactory#newInstance()}. * * @param context The context of this evaluation. * @return expression factory instance */ private ExpressionFactory getExpressionFactory(ELContext context) { Object obj = context.getContext(ExpressionFactory.class); if (obj instanceof ExpressionFactory) { return (ExpressionFactory) obj; } if (defaultFactory == null) { defaultFactory = ExpressionFactory.newInstance(); } return defaultFactory; }
@SuppressWarnings("serial") @Test(expected = ExpressionException.class) public void testParams_Map_Partial_NoPrune() { Expression e = ExpressionFactory.exp("a = $a or x = $x"); e.params( new HashMap<String, Object>() { { put("a", "A"); } }, false); }
@Test public void testParams2_FromString() { Expression e1 = ExpressionFactory.exp("k1 like $test"); Map<String, Object> map = new HashMap<String, Object>(); map.put("test", "xyz"); Expression e2 = e1.params(map, false); assertNotNull(e2); assertEquals(2, e2.getOperandCount()); assertEquals(Expression.LIKE, e2.getType()); assertEquals("xyz", e2.getOperand(1)); }
@Test public void testParams2() throws Exception { Expression e1 = ExpressionFactory.likeExp("k1", new ExpressionParameter("test")); Map map = new HashMap(); map.put("test", "xyz"); Expression e2 = e1.params(map, false); assertNotNull(e2); assertEquals(2, e2.getOperandCount()); assertEquals(Expression.LIKE, e2.getType()); assertEquals("xyz", e2.getOperand(1)); }
@Test public void testNoParams3() throws Exception { List list = new ArrayList(); list.add(ExpressionFactory.matchExp("k1", new ExpressionParameter("test1"))); list.add(ExpressionFactory.matchExp("k2", new ExpressionParameter("test2"))); list.add(ExpressionFactory.matchExp("k3", new ExpressionParameter("test3"))); list.add(ExpressionFactory.matchExp("k4", new ExpressionParameter("test4"))); Expression e1 = ExpressionFactory.joinExp(Expression.OR, list); Map params = new HashMap(); params.put("test4", "123"); Expression e2 = e1.params(params, true); // some expression nodes must be pruned assertNotNull(e2); assertTrue("List expression: " + e2, !(e2 instanceof ASTList)); assertEquals(2, e2.getOperandCount()); assertEquals("123", e2.getOperand(1)); assertEquals("k4", ((Expression) e2.getOperand(0)).getOperand(0)); }
@Test public void testConditionals() { RTLExpression c = ExpressionFactory.createConditionalExpression( ExpressionFactory.createGreaterThan(esp, ExpressionFactory.createNumber(10, 32)), ExpressionFactory.createNumber(10, 32), ExpressionFactory.createNumber(5, 32)); RTLExpression f = ExpressionFactory.createEqual(ExpressionFactory.createVariable("x", 32), c); solver.addAssertion(f); assertTrue(solver.isSatisfiable()); }
@Test public void testParams_Map_Partial_Prune() { Expression e = ExpressionFactory.exp("a = $a or x = $x"); @SuppressWarnings("serial") Expression ep = e.params( new HashMap<String, Object>() { { put("a", "A"); } }); assertNotSame(e, ep); assertEquals("(a = \"A\")", ep.toString()); }
@Test public void testNoParams3_FromString() { Expression e1 = ExpressionFactory.exp("k1 = $test1 or k2 = $test2 or k3 = $test3 or k4 = $test4"); Map<String, Object> params = new HashMap<String, Object>(); params.put("test4", "123"); Expression e2 = e1.params(params); // some expression nodes must be pruned assertNotNull(e2); assertEquals(2, e2.getOperandCount()); assertEquals("123", e2.getOperand(1)); assertEquals("k4", ((Expression) e2.getOperand(0)).getOperand(0)); }
@Test public void testInParameter_AsValues() throws Exception { Expression inExp = ExpressionFactory.exp("k1 in ($ap, $bp)"); String e1String = "k1 in (\"a\", \"b\")"; Expression e1 = ExpressionFactory.exp(e1String); Map<String, Object> params = new HashMap<String, Object>(); params.put("ap", "a"); params.put("bp", "b"); Expression transformed = inExp.params(params); TstTraversalHandler.compareExps(e1, transformed); assertEquals(e1String, transformed.toString()); // just in case manually check params DataObject o1 = new CayenneDataObject(); o1.writePropertyDirectly("k1", "a"); assertTrue(transformed.match(o1)); DataObject o2 = new CayenneDataObject(); o2.writePropertyDirectly("k1", "x"); assertFalse(transformed.match(o2)); }
@Test public void testIterationCondition() throws Exception { final String TEST_NAME = "testIterationCondition"; TestUtil.displayTestTile(TEST_NAME); // GIVEN OperationResult result = new OperationResult(TestExpression.class.getName() + "." + TEST_NAME); rememberScriptExecutionCount(); ExpressionType expressionType = PrismTestUtil.parseAtomicValue( EXPRESSION_ITERATION_CONDITION_FILE, ExpressionType.COMPLEX_TYPE); PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinitionImpl<Boolean>( ExpressionConstants.OUTPUT_ELMENT_NAME, DOMUtil.XSD_BOOLEAN, prismContext); Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, TEST_NAME, null, result); ExpressionVariables variables = new ExpressionVariables(); PrismObject<UserType> user = PrismTestUtil.parseObject(USER_JACK_FILE); variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, user); variables.addVariableDefinition(ExpressionConstants.VAR_USER, user); PrismObject<ShadowType> account = PrismTestUtil.parseObject(ACCOUNT_JACK_DUMMYFILE); variables.addVariableDefinition(ExpressionConstants.VAR_SHADOW, account); variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION, 1); variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION_TOKEN, "001"); ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(null, variables, TEST_NAME, null, result); // WHEN PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = expression.evaluate(expressionContext); // THEN assertNotNull(outputTriple); outputTriple.checkConsistence(); // Make sure that the script is executed only once. There is no delta in the variables, no need // to do it twice. assertScriptExecutionIncrement(1); }