Search in sources :

Example 21 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class OgnlValueStackTest method testSetNullList.

public void testSetNullList() {
    Foo foo = new Foo();
    OgnlValueStack vs = createValueStack();
    vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    vs.push(foo);
    vs.setValue("cats[0].name", "Cat One");
    vs.setValue("cats[1].name", "Cat Two");
    assertNotNull(foo.getCats());
    assertEquals(2, foo.getCats().size());
    assertEquals("Cat One", ((Cat) foo.getCats().get(0)).getName());
    assertEquals("Cat Two", ((Cat) foo.getCats().get(1)).getName());
    // test when both Key and Value types of Map are interfaces but concrete classes are defined in .properties file
    vs.setValue("animalMap[3].name", "Cat Three by interface");
    vs.setValue("animalMap[6].name", "Cat Six by interface");
    assertNotNull(foo.getAnimalMap());
    assertEquals(2, foo.getAnimalMap().size());
    assertEquals("Cat Three by interface", foo.getAnimalMap().get(3L).getName());
    assertEquals("Cat Six by interface", foo.getAnimalMap().get(6L).getName());
    vs.setValue("annotatedCats[0].name", "Cat One By Annotation");
    vs.setValue("annotatedCats[1].name", "Cat Two By Annotation");
    assertNotNull(foo.getAnnotatedCats());
    assertEquals(2, foo.getAnnotatedCats().size());
    assertEquals("Cat One By Annotation", ((Cat) foo.getAnnotatedCats().get(0)).getName());
    assertEquals("Cat Two By Annotation", ((Cat) foo.getAnnotatedCats().get(1)).getName());
    vs.setValue("cats[0].foo.cats[1].name", "Deep null cat");
    assertNotNull(((Cat) foo.getCats().get(0)).getFoo());
    assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats());
    assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats().get(1));
    assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCats().get(0)).getFoo().getCats().get(1)).getName());
    vs.setValue("annotatedCats[0].foo.annotatedCats[1].name", "Deep null cat by annotation");
    assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo());
    assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats());
    assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats().get(1));
    assertEquals("Deep null cat by annotation", ((Cat) ((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats().get(1)).getName());
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 22 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class OgnlValueStackTest method testNotFailOnTooLongExpressionWithDefaultProperties.

public void testNotFailOnTooLongExpressionWithDefaultProperties() {
    loadConfigurationProviders(new DefaultPropertiesProvider());
    Object defaultMaxLengthFromConfiguration = container.getInstance(String.class, StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH);
    if (defaultMaxLengthFromConfiguration != null) {
        assertTrue("non-null defaultMaxLengthFromConfiguration not a String ?", defaultMaxLengthFromConfiguration instanceof String);
        assertTrue("non-null defaultMaxLengthFromConfiguration not empty string by default ?", ((String) defaultMaxLengthFromConfiguration).length() == 0);
    } else {
        assertNull("defaultMaxLengthFromConfiguration not null ?", defaultMaxLengthFromConfiguration);
    }
    // Original test logic was to confirm failure of exceeding the default value.  Now the feature should be disabled by default,
    // so this test's expectations are now changed.
    // Since maxlength is disabled by default, just choose an arbitrary value for test
    Integer repeat = Integer.valueOf(256);
    OgnlValueStack vs = createValueStack();
    try {
        vs.findValue(StringUtils.repeat('.', repeat + 1), true);
        fail("findValue did not throw any exception (should either fail as invalid expression syntax or security exception) ?");
    } catch (Exception ex) {
        // If STRUTS_OGNL_EXPRESSION_MAX_LENGTH feature is disabled (default), the parse should fail due to a reason of invalid expression syntax
        // with ParseException.  Previously when it was enabled the reason for the failure would have been SecurityException.
        assertTrue(ex.getCause() instanceof OgnlException);
        assertTrue(((OgnlException) ex.getCause()).getReason() instanceof ParseException);
    }
}
Also used : OgnlException(ognl.OgnlException) ParseException(ognl.ParseException) DefaultPropertiesProvider(org.apache.struts2.config.DefaultPropertiesProvider) ParseException(ognl.ParseException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Example 23 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class OgnlValueStackTest method testSuccessFailOnErrorOnInheritedPropertiesWithMethods.

public void testSuccessFailOnErrorOnInheritedPropertiesWithMethods() {
    // this shuld not fail as the property is defined on a parent class
    OgnlValueStack vs = createValueStack();
    Foo foo = new Foo();
    BarJunior barjr = new BarJunior();
    foo.setBarJunior(barjr);
    vs.push(foo);
    assertNull(barjr.getTitle());
    vs.findValue("getBarJunior().title", true);
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 24 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class OgnlValueStackTest method testFailOnTooLongExpressionLongerThan192_ViaOverriddenProperty.

public void testFailOnTooLongExpressionLongerThan192_ViaOverriddenProperty() {
    try {
        loadConfigurationProviders(new StubConfigurationProvider() {

            @Override
            public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
                props.setProperty(StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH, "192");
            }
        });
        Integer repeat = Integer.parseInt(container.getInstance(String.class, StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH));
        OgnlValueStack vs = createValueStack();
        try {
            vs.findValue(StringUtils.repeat('.', repeat + 1), true);
            fail("Failed to throw exception on too long expression");
        } catch (Exception ex) {
            assertTrue(ex.getCause() instanceof OgnlException);
            assertTrue(((OgnlException) ex.getCause()).getReason() instanceof SecurityException);
        }
    } finally {
        // Reset expressionMaxLength value to default (disabled)
        ognlUtil.applyExpressionMaxLength(null);
    }
}
Also used : OgnlException(ognl.OgnlException) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ParseException(ognl.ParseException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Example 25 with OgnlValueStack

use of com.opensymphony.xwork2.ognl.OgnlValueStack in project struts by apache.

the class OgnlValueStackTest method testFailFailOnErrorOnInheritedPropertiesWithMethods.

public void testFailFailOnErrorOnInheritedPropertiesWithMethods() {
    OgnlValueStack vs = createValueStack();
    Foo foo = new Foo();
    BarJunior barjr = new BarJunior();
    foo.setBarJunior(barjr);
    vs.push(foo);
    assertNull(barjr.getTitle());
    try {
        vs.findValue("getBarJunior().title2", true);
        fail("should have failed on missing property");
    } catch (Exception e) {
    }
}
Also used : Foo(com.opensymphony.xwork2.util.Foo) ParseException(ognl.ParseException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Aggregations

Foo (com.opensymphony.xwork2.util.Foo)14 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)5 OgnlException (ognl.OgnlException)5 StrutsException (org.apache.struts2.StrutsException)5 OgnlValueStack (com.opensymphony.xwork2.ognl.OgnlValueStack)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 ParseException (ognl.ParseException)4 TextProvider (com.opensymphony.xwork2.TextProvider)2 XWorkConverter (com.opensymphony.xwork2.conversion.impl.XWorkConverter)2 WrapperTemplateModel (freemarker.ext.util.WrapperTemplateModel)2 AdapterTemplateModel (freemarker.template.AdapterTemplateModel)2 TemplateBooleanModel (freemarker.template.TemplateBooleanModel)2 TemplateCollectionModel (freemarker.template.TemplateCollectionModel)2 TemplateHashModel (freemarker.template.TemplateHashModel)2 TemplateModel (freemarker.template.TemplateModel)2 TemplateModelException (freemarker.template.TemplateModelException)2 TemplateModelIterator (freemarker.template.TemplateModelIterator)2 TemplateNumberModel (freemarker.template.TemplateNumberModel)2