Search in sources :

Example 21 with ValueExpression

use of jakarta.el.ValueExpression in project tomcat by apache.

the class TestValueExpressionImpl method testGetValueReference.

@Test
public void testGetValueReference() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var = factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);
    ValueExpression ve = factory.createValueExpression(context, "${beanB.name}", String.class);
    // First check the basics work
    String result = (String) ve.getValue(context);
    Assert.assertEquals("Tomcat", result);
    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    Assert.assertNotNull(vr);
    Assert.assertEquals(beanB, vr.getBase());
    Assert.assertEquals("name", vr.getProperty());
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) ValueReference(jakarta.el.ValueReference) Test(org.junit.Test)

Example 22 with ValueExpression

use of jakarta.el.ValueExpression in project tomcat by apache.

the class TestValueExpressionImpl method testBug51177ObjectMap.

@Test
public void testBug51177ObjectMap() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    Object o1 = "String value";
    Object o2 = Integer.valueOf(32);
    Map<Object, Object> map = new HashMap<>();
    map.put("key1", o1);
    map.put("key2", o2);
    ValueExpression var = factory.createValueExpression(map, Map.class);
    context.getVariableMapper().setVariable("map", var);
    ValueExpression ve1 = factory.createValueExpression(context, "${map.key1}", Object.class);
    ve1.setValue(context, o2);
    Assert.assertEquals(o2, ve1.getValue(context));
    ValueExpression ve2 = factory.createValueExpression(context, "${map.key2}", Object.class);
    ve2.setValue(context, o1);
    Assert.assertEquals(o1, ve2.getValue(context));
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) HashMap(java.util.HashMap) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) Test(org.junit.Test)

Example 23 with ValueExpression

use of jakarta.el.ValueExpression in project tomcat by apache.

the class TestValueExpressionImpl method testBug50105.

@Test
public void testBug50105() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    TesterEnum testEnum = TesterEnum.APPLE;
    ValueExpression var = factory.createValueExpression(testEnum, TesterEnum.class);
    context.getVariableMapper().setVariable("testEnum", var);
    // When coercing an Enum to a String, name() should always be used.
    ValueExpression ve1 = factory.createValueExpression(context, "${testEnum}", String.class);
    String result1 = (String) ve1.getValue(context);
    Assert.assertEquals("APPLE", result1);
    ValueExpression ve2 = factory.createValueExpression(context, "foo${testEnum}bar", String.class);
    String result2 = (String) ve2.getValue(context);
    Assert.assertEquals("fooAPPLEbar", result2);
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) Test(org.junit.Test)

Example 24 with ValueExpression

use of jakarta.el.ValueExpression in project tomcat by apache.

the class TestValueExpressionImpl method testBug51544Direct.

/*
     * Test using list directly as variable.
     */
@Test
public void testBug51544Direct() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    List<?> list = Collections.emptyList();
    ValueExpression var = factory.createValueExpression(list, List.class);
    context.getVariableMapper().setVariable("list", var);
    ValueExpression ve = factory.createValueExpression(context, "${list.size()}", Integer.class);
    Integer result = (Integer) ve.getValue(context);
    Assert.assertEquals(Integer.valueOf(0), result);
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) Test(org.junit.Test)

Example 25 with ValueExpression

use of jakarta.el.ValueExpression in project tomcat by apache.

the class TestValueExpressionImpl method testBug49345.

@Test
public void testBug49345() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    TesterBeanA beanA = new TesterBeanA();
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    beanA.setBean(beanB);
    ValueExpression var = factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);
    ValueExpression ve = factory.createValueExpression(context, "${beanA.bean.name}", String.class);
    // First check the basics work
    String result = (String) ve.getValue(context);
    Assert.assertEquals("Tomcat", result);
    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    Assert.assertNotNull(vr);
    Assert.assertEquals(beanB, vr.getBase());
    Assert.assertEquals("name", vr.getProperty());
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) ValueReference(jakarta.el.ValueReference) Test(org.junit.Test)

Aggregations

ValueExpression (jakarta.el.ValueExpression)42 Test (org.junit.Test)28 ELContext (jakarta.el.ELContext)24 ExpressionFactory (jakarta.el.ExpressionFactory)24 ELContextImpl (org.apache.jasper.el.ELContextImpl)17 VariableMapper (jakarta.el.VariableMapper)8 ELProcessor (jakarta.el.ELProcessor)7 MethodExpression (jakarta.el.MethodExpression)7 ELException (jakarta.el.ELException)6 PropertyNotFoundException (jakarta.el.PropertyNotFoundException)4 ValueReference (jakarta.el.ValueReference)3 ELClass (jakarta.el.ELClass)2 ELManager (jakarta.el.ELManager)1 FunctionMapper (jakarta.el.FunctionMapper)1 LambdaExpression (jakarta.el.LambdaExpression)1 MethodNotFoundException (jakarta.el.MethodNotFoundException)1 TesterELContext (jakarta.el.TesterELContext)1 ELParseException (jakarta.servlet.jsp.el.ELParseException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1