org.apache.jasper.el.ELContextImpl Java Examples

The following examples show how to use org.apache.jasper.el.ELContextImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestValueExpressionImpl.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Test returning an empty list as a bean property.
 */
@Test
public void testBug51544Bean() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setValList(Collections.emptyList());

    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanA.valList.size()}", Integer.class);

    Integer result = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(0), result);
}
 
Example #2
Source File: TestValueExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@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);
}
 
Example #3
Source File: TestBeanELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a valid FeatureDescriptors are returned.
 */
@Test
public void testGetFeatureDescriptors02() {
    BeanELResolver resolver = new BeanELResolver();
    ELContext context = new ELContextImpl();

    Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(context, new Bean());

    while (result.hasNext()) {
        PropertyDescriptor featureDescriptor = (PropertyDescriptor) result.next();
        Assert.assertEquals(featureDescriptor.getPropertyType(),
                featureDescriptor.getValue(ELResolver.TYPE));
        Assert.assertEquals(Boolean.TRUE,
                featureDescriptor.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
    }
}
 
Example #4
Source File: TestListELResolver.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that if the ListELResolver is constructed with readOnly the method
 * will return always true, otherwise false.
 */
@Test
public void testIsReadOnly03() {
    ListELResolver resolver = new ListELResolver();
    ELContext context = new ELContextImpl();

    List<String> list = new ArrayList<String>();
    list.add("key");
    boolean result = resolver.isReadOnly(context, list, new Integer(0));

    Assert.assertFalse(result);
    Assert.assertTrue(context.isPropertyResolved());

    resolver = new ListELResolver(true);

    result = resolver.isReadOnly(context, list, new Integer(0));

    Assert.assertTrue(result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #5
Source File: TestArrayELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a result is returned even when a coercion cannot be performed.
 */
@Test
public void testIsReadOnly05() {
    ArrayELResolver resolver = new ArrayELResolver();
    ELContext context = new ELContextImpl();

    String[] base = new String[] { "element" };
    boolean result = resolver.isReadOnly(context, base, "key");

    Assert.assertFalse(result);
    Assert.assertTrue(context.isPropertyResolved());

    resolver = new ArrayELResolver(true);

    result = resolver.isReadOnly(context, base, "key");

    Assert.assertTrue(result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #6
Source File: TestELParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void bug56185() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);

    TesterBeanC beanC = new TesterBeanC();
    ValueExpression var =
        factory.createValueExpression(beanC, TesterBeanC.class);
    context.getVariableMapper().setVariable("myBean", var);

    ValueExpression ve = factory.createValueExpression(context,
        "${(myBean.int1 > 1 and myBean.myBool) or "+
        "((myBean.myBool or myBean.myBool1) and myBean.int1 > 1)}",
        Boolean.class);
    Assert.assertEquals(Boolean.FALSE, ve.getValue(context));
    beanC.setInt1(2);
    beanC.setMyBool1(true);
    Assert.assertEquals(Boolean.TRUE, ve.getValue(context));
}
 
Example #7
Source File: TestArrayELResolver.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the propertyResolved is false if base is not an array.
 */
@Test
public void testIsReadOnly02() {
    ArrayELResolver resolver = new ArrayELResolver();
    ELContext context = new ELContextImpl();

    boolean result = resolver.isReadOnly(context, new Object(),
            new Object());

    Assert.assertFalse(result);
    Assert.assertFalse(context.isPropertyResolved());

    resolver = new ArrayELResolver(true);

    result = resolver.isReadOnly(context, new Object(), new Object());

    Assert.assertTrue(result);
    Assert.assertFalse(context.isPropertyResolved());
}
 
Example #8
Source File: TestArrayELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the propertyResolved is false if base is not an array.
 */
@Test
public void testIsReadOnly02() {
    ArrayELResolver resolver = new ArrayELResolver();
    ELContext context = new ELContextImpl();

    boolean result = resolver.isReadOnly(context, new Object(),
            new Object());

    Assert.assertFalse(result);
    Assert.assertFalse(context.isPropertyResolved());

    resolver = new ArrayELResolver(true);

    result = resolver.isReadOnly(context, new Object(), new Object());

    Assert.assertTrue(result);
    Assert.assertFalse(context.isPropertyResolved());
}
 
Example #9
Source File: TestMapELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a valid FeatureDescriptors are returned.
 */
@Test
public void testGetFeatureDescriptors02() {
    MapELResolver mapELResolver = new MapELResolver();
    ELContext context = new ELContextImpl();

    Map<String, String> map = new HashMap<String, String>();
    map.put("key", "value");
    Iterator<FeatureDescriptor> result = mapELResolver
            .getFeatureDescriptors(context, map);

    while (result.hasNext()) {
        FeatureDescriptor featureDescriptor = result.next();
        Assert.assertEquals("key", featureDescriptor.getDisplayName());
        Assert.assertEquals("key", featureDescriptor.getName());
        Assert.assertEquals("", featureDescriptor.getShortDescription());
        Assert.assertFalse(featureDescriptor.isExpert());
        Assert.assertFalse(featureDescriptor.isHidden());
        Assert.assertTrue(featureDescriptor.isPreferred());
        Assert.assertEquals("key".getClass(),
                featureDescriptor.getValue(ELResolver.TYPE));
        Assert.assertEquals(Boolean.TRUE, featureDescriptor
                .getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
    }
}
 
Example #10
Source File: TestListELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the key is out of bounds and null will be returned.
 */
@Test
public void testGetValue05() {
    ListELResolver resolver = new ListELResolver();
    ELContext context = new ELContextImpl();

    List<String> list = new ArrayList<String>();
    list.add("key");
    Object result = resolver.getValue(context, list, new Integer(1));

    Assert.assertNull(result);
    Assert.assertTrue(context.isPropertyResolved());

    result = resolver.getValue(context, list, new Integer(-1));

    Assert.assertNull(result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #11
Source File: TestListELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a result is returned even when a coercion cannot be performed.
 */
@Test
public void testIsReadOnly06() {
    ListELResolver resolver = new ListELResolver();
    ELContext context = new ELContextImpl();

    List<String> list = new ArrayList<String>();
    list.add("key");
    boolean result = resolver.isReadOnly(context, list, "key");

    Assert.assertFalse(result);
    Assert.assertTrue(context.isPropertyResolved());

    resolver = new ListELResolver(true);

    result = resolver.isReadOnly(context, list, "key");

    Assert.assertTrue(result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #12
Source File: TestMapELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that if the MapELResolver is constructed with readOnly the method
 * will return always true, otherwise false.
 */
@Test
public void testIsReadOnly03() {
    MapELResolver mapELResolver = new MapELResolver();
    ELContext context = new ELContextImpl();

    boolean result = mapELResolver.isReadOnly(context,
            new HashMap<Object, Object>(), new Object());

    Assert.assertFalse(result);
    Assert.assertTrue(context.isPropertyResolved());

    mapELResolver = new MapELResolver(true);

    result = mapELResolver.isReadOnly(context,
            new HashMap<Object, Object>(), new Object());

    Assert.assertTrue(result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #13
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Test returning an empty list as a bean property.
 */
@Test
public void testBug51544Bean() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setValList(Collections.emptyList());

    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);

    ValueExpression ve = factory.createValueExpression(
            context, "${beanA.valList.size()}", Integer.class);

    Integer result = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(0), result);
}
 
Example #14
Source File: TestELParser.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaKeyWordIdentifier() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    TesterBeanA beanA = new TesterBeanA();
    beanA.setInt("five");
    ValueExpression var =
        factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("this", var);

    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${this}", String.class);
    } catch (ELException ele) {
        e = ele;
    }
    assertNotNull(e);
}
 
Example #15
Source File: TestMapELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the propertyResolved is false if base is not Map.
 */
@Test
public void testIsReadOnly02() {
    MapELResolver mapELResolver = new MapELResolver();
    ELContext context = new ELContextImpl();

    boolean result = mapELResolver.isReadOnly(context, new Object(),
            new Object());

    Assert.assertFalse(result);
    Assert.assertFalse(context.isPropertyResolved());

    mapELResolver = new MapELResolver(true);

    result = mapELResolver.isReadOnly(context, new Object(), new Object());

    Assert.assertTrue(result);
    Assert.assertFalse(context.isPropertyResolved());
}
 
Example #16
Source File: TestResourceBundleELResolver.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a valid property is resolved.
 */
@Test
public void testGetValue03() {
    ResourceBundleELResolver resolver = new ResourceBundleELResolver();
    ELContext context = new ELContextImpl();

    ResourceBundle resourceBundle = new TesterResourceBundle();
    Object result = resolver.getValue(context, resourceBundle, "key1");

    Assert.assertEquals("value1", result);
    Assert.assertTrue(context.isPropertyResolved());

    result = resolver.getValue(context, resourceBundle, "unknown-key");

    Assert.assertEquals("???unknown-key???", result);
    Assert.assertTrue(context.isPropertyResolved());

    result = resolver.getValue(context, resourceBundle, null);

    Assert.assertNull(result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #17
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug50105() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    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);
    assertEquals("APPLE", result1);

    ValueExpression ve2 = factory.createValueExpression(
            context, "foo${testEnum}bar", String.class);
    String result2 = (String) ve2.getValue(context);
    assertEquals("fooAPPLEbar", result2);
}
 
Example #18
Source File: TestValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetValueReference() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    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);
    assertEquals("Tomcat", result);

    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    assertNotNull(vr);

    assertEquals(beanB, vr.getBase());
    assertEquals("name", vr.getProperty());
}
 
Example #19
Source File: TestArrayELResolver.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a valid property is set.
 */
@Test
public void testSetValue04() {
    ArrayELResolver resolver = new ArrayELResolver();
    ELContext context = new ELContextImpl();

    String[] base = new String[] { "element" };
    resolver.setValue(context, base, new Integer(0), "new-element");

    Assert.assertEquals("new-element",
            resolver.getValue(context, base, new Integer(0)));
    Assert.assertTrue(context.isPropertyResolved());

    resolver.setValue(context, base, new Integer(0), null);

    Assert.assertEquals(null,
            resolver.getValue(context, base, new Integer(0)));
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #20
Source File: TestResourceBundleELResolver.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a valid FeatureDescriptors are returned.
 */
@Test
public void testGetFeatureDescriptors02() {
    ResourceBundleELResolver resolver = new ResourceBundleELResolver();
    ELContext context = new ELContextImpl();

    ResourceBundle resourceBundle = new TesterResourceBundle(
            new Object[][] { { "key", "value" } });
    @SuppressWarnings("unchecked")
    Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(
            context, resourceBundle);

    while (result.hasNext()) {
        FeatureDescriptor featureDescriptor = result.next();
        Assert.assertEquals("key", featureDescriptor.getDisplayName());
        Assert.assertEquals("key", featureDescriptor.getName());
        Assert.assertEquals("", featureDescriptor.getShortDescription());
        Assert.assertFalse(featureDescriptor.isExpert());
        Assert.assertFalse(featureDescriptor.isHidden());
        Assert.assertTrue(featureDescriptor.isPreferred());
        Assert.assertEquals(String.class,
                featureDescriptor.getValue(ELResolver.TYPE));
        Assert.assertEquals(Boolean.TRUE, featureDescriptor
                .getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
    }
}
 
Example #21
Source File: TestBeanELResolver.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a valid FeatureDescriptors are returned.
 */
@Test
public void testGetFeatureDescriptors02() {
    BeanELResolver resolver = new BeanELResolver();
    ELContext context = new ELContextImpl();

    Iterator<FeatureDescriptor> result = resolver.getFeatureDescriptors(context, new Bean());

    while (result.hasNext()) {
        PropertyDescriptor featureDescriptor = (PropertyDescriptor) result.next();
        Assert.assertEquals(featureDescriptor.getPropertyType(),
                featureDescriptor.getValue(ELResolver.TYPE));
        Assert.assertEquals(Boolean.TRUE,
                featureDescriptor.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
    }
}
 
Example #22
Source File: TestListELResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the readOnly is true always when the map is not modifiable.
 */
@Test
public void testIsReadOnly04() {
    ListELResolver resolver = new ListELResolver();
    ELContext context = new ELContextImpl();

    List<String> list = new ArrayList<String>();
    list.add("key");
    List<String> unmodifiableList = Collections.unmodifiableList(list);
    boolean result = resolver.isReadOnly(context, unmodifiableList,
            new Integer(0));

    Assert.assertTrue(result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #23
Source File: TestArrayELResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that an exception will be thrown if the value is not from the
 * corresponding type.
 */
@Test(expected = ClassCastException.class)
public void testSetValue07() {
    ArrayELResolver resolver = new ArrayELResolver();
    ELContext context = new ELContextImpl();

    String[] base = new String[] { "element" };
    resolver.setValue(context, base, new Integer(0), new Integer(1));
}
 
Example #24
Source File: TestBeanELResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that an exception will be thrown when the property does not exist.
 */
@Test(expected = PropertyNotFoundException.class)
public void testGetType04() {
    BeanELResolver resolver = new BeanELResolver();
    ELContext context = new ELContextImpl();

    resolver.getType(context, new Bean(), PROPERTY02_NAME);
}
 
Example #25
Source File: TestBeanELResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a valid property is resolved.
 */
@Test
public void testGetType03() {
    BeanELResolver resolver = new BeanELResolver();
    ELContext context = new ELContextImpl();

    Class<?> result = resolver.getType(context, new Bean(), PROPERTY01_NAME);

    Assert.assertEquals(String.class, result);
    Assert.assertTrue(context.isPropertyResolved());
}
 
Example #26
Source File: TestBeanELResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug53421() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl();

    Bean bean = new Bean();

    ValueExpression varBean =
        factory.createValueExpression(bean, Bean.class);
    context.getVariableMapper().setVariable("bean", varBean);


    ValueExpression ve = factory.createValueExpression(
            context, "${bean.valueA}", String.class);
    Exception e = null;
    try {
        ve.getValue(context);
    } catch (PropertyNotFoundException pnfe) {
        e = pnfe;
    }
    Assert.assertTrue("Wrong exception type",
            e instanceof PropertyNotFoundException);
    String type = Bean.class.getName();
    String msg = e.getMessage();
    Assert.assertTrue("No reference to type [" + type +
            "] where property cannot be found in [" + msg + "]",
            msg.contains(type));
}
 
Example #27
Source File: TestListELResolver.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the key is out of bounds and exception will be thrown.
 */
@Test(expected = PropertyNotFoundException.class)
public void testSetValue07() {
    ListELResolver resolver = new ListELResolver();
    ELContext context = new ELContextImpl();

    List<String> list = new ArrayList<String>();
    list.add("key");
    resolver.setValue(context, list, new Integer(1), "value");
}
 
Example #28
Source File: TestListELResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the key is out of bounds and exception will be thrown.
 */
@Test(expected = PropertyNotFoundException.class)
public void testIsReadOnly05() {
    ListELResolver resolver = new ListELResolver();
    ELContext context = new ELContextImpl();

    List<String> list = new ArrayList<String>();
    list.add("key");
    resolver.isReadOnly(context, list, new Integer(1));
}
 
Example #29
Source File: TestBeanELResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that an exception will be thrown when the property is not readable.
 */
@Test(expected = PropertyNotFoundException.class)
public void testGetValue06() {
    BeanELResolver resolver = new BeanELResolver();
    ELContext context = new ELContextImpl();

    resolver.getValue(context, new Bean(), PROPERTY01_NAME);
}
 
Example #30
Source File: TestArrayELResolver.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that an exception is thrown when readOnly is true.
 */
@Test(expected = PropertyNotWritableException.class)
public void testSetValue03() {
    ArrayELResolver resolver = new ArrayELResolver(true);
    ELContext context = new ELContextImpl();

    resolver.setValue(context, new String[] {}, new Object(), new Object());
}