AbstractExpression implements Expression<T>, Serializable, PortableObject { public T evaluate(Object target) { return evaluate(target, null); } protected  AbstractExpression(); protected  AbstractExpression(String expression); protected  AbstractExpression(InputStream expression); T evaluate(Object target); void readExternal(PofReader reader); void writeExternal(PofWriter writer); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testLiteralExpression() { Expression exp = createExpression("5 + 5"); assertEquals(10, ((Number) exp.evaluate(null)).intValue()); }
@Test public void testSimplePropertyExpression() { Person person = new Person(1L, "Homer"); Expression exp = createExpression("name"); assertEquals("Homer", exp.evaluate(person)); }
@Test public void testSetExpression() { Person person = new Person(1L, "Homer"); Expression exp = createExpression("name"); exp.evaluateAndSet(person, "HOMER"); assertEquals("HOMER", exp.evaluate(person)); }
@Test public void testNestedPropertyExpression() { Person person = new Person(1L, "Homer", null, new Address("111 Main St", "Springfield", "USA")); Expression exp = createExpression("address.city"); assertEquals("Springfield", exp.evaluate(person)); }
@Test public void testExpressionWithVariables() { Map vars = new HashMap(); vars.put("x", 5); vars.put("y", 5); Expression exp = createExpression("x * y"); assertEquals(25, ((Number) exp.evaluate(null, vars)).intValue()); }
@Test public void testExpressionWithTargetAndVariables() { Person person = new Person(1L, "Homer"); Map vars = new HashMap(); vars.put("lastName", "Simpson"); Expression exp = createExpression("name + ' ' + lastName"); assertEquals("Homer Simpson", exp.evaluate(person, vars)); }
@Test public void testPerformance() { Person person = new Person(1L, "Homer"); Expression exp = createExpression("name"); final int COUNT = 1000000; for (int i = 0; i < 1000; i++) { exp.evaluate(person); } long start = System.nanoTime(); for (int i = 0; i < COUNT; i++) { exp.evaluate(person); } long duration = System.nanoTime() - start; System.out.println(getLanguage() + ": " + duration / 1000000 + " ms"); }