Exemplo n.º 1
0
  @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));
  }
Exemplo n.º 2
0
  @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);
  }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
  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);
  }
Exemplo n.º 5
0
  /** 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);
  }
Exemplo n.º 6
0
  @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);
  }
Exemplo n.º 7
0
  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);
  }
Exemplo n.º 8
0
 /** @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);
 }
Exemplo n.º 9
0
  /** 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);
  }
Exemplo n.º 10
0
  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));
        }
      }
    }
  }
Exemplo n.º 11
0
 /**
  * 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;
 }
Exemplo n.º 12
0
 @Test
 public void testNulls() {
   Expression e1 = ExpressionFactory.exp("x = null");
   Expression e2 = e1.params(Collections.EMPTY_MAP, true);
   assertNotNull(e2);
   TstTraversalHandler.compareExps(e1, e2);
 }
Exemplo n.º 13
0
  @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);
  }
Exemplo n.º 14
0
 @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());
 }
Exemplo n.º 15
0
  @Test
  public void testParams_NullHandling_CAY847() {
    Expression e = ExpressionFactory.exp("X = $x");

    e = e.params(Collections.singletonMap("x", null));
    assertEquals("X = null", e.toString());
  }
Exemplo n.º 16
0
 private RTLExpression addClause(RTLExpression formula, RTLExpression clause) {
   if (formula != null) {
     return ExpressionFactory.createAnd(formula, clause);
   } else {
     return clause;
   }
 }
Exemplo n.º 17
0
  @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);
  }
Exemplo n.º 18
0
  /** 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));
  }
Exemplo n.º 19
0
  @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);
  }
Exemplo n.º 20
0
 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);
 }
Exemplo n.º 21
0
 /**
  * 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;
 }
Exemplo n.º 22
0
 @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);
 }
Exemplo n.º 23
0
  @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));
  }
Exemplo n.º 24
0
  @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));
  }
Exemplo n.º 25
0
  @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));
  }
Exemplo n.º 26
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());
  }
Exemplo n.º 27
0
 @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());
 }
Exemplo n.º 28
0
  @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));
  }
Exemplo n.º 29
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));
  }
Exemplo n.º 30
0
  @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);
  }