Search in sources :

Example 6 with OgnlValueStack

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

the class OgnlValueStackTest method testGetBarAsString.

public void testGetBarAsString() {
    Foo foo = new Foo();
    Bar bar = new Bar();
    bar.setTitle("bar");
    bar.setSomethingElse(123);
    foo.setBar(bar);
    OgnlValueStack vs = createValueStack();
    vs.push(foo);
    String output = (String) vs.findValue("bar", String.class);
    assertEquals("bar:123", output);
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 7 with OgnlValueStack

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

the class OgnlValueStackTest method testSetBarAsString.

public void testSetBarAsString() {
    Foo foo = new Foo();
    OgnlValueStack vs = createValueStack();
    vs.push(foo);
    vs.setValue("bar", "bar:123");
    assertEquals("bar", foo.getBar().getTitle());
    assertEquals(123, foo.getBar().getSomethingElse());
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 8 with OgnlValueStack

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

the class OgnlValueStackTest method testNotFailOnTooLongValueWithDefaultProperties.

public void testNotFailOnTooLongValueWithDefaultProperties() {
    try {
        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 is unchanged (testing that values can be larger than maximum expression length), but since the feature is disabled by
        // default we will now have to enable it with an arbitrary value, test, and reset it to disabled.
        // Since maxlength is disabled by default, just choose an arbitrary value for test
        Integer repeat = Integer.valueOf(256);
        // Apply a non-default value for expressionMaxLength (as it should be disabled by default)
        try {
            ognlUtil.applyExpressionMaxLength(repeat.toString());
        } catch (Exception ex) {
            fail("applyExpressionMaxLength did not accept maxlength string " + repeat.toString() + " ?");
        }
        OgnlValueStack vs = createValueStack();
        Dog dog = new Dog();
        vs.push(dog);
        String value = StringUtils.repeat('.', repeat + 1);
        vs.setValue("name", value);
        assertEquals(value, dog.getName());
    } finally {
        // Reset expressionMaxLength value to default (disabled)
        ognlUtil.applyExpressionMaxLength(null);
    }
}
Also used : DefaultPropertiesProvider(org.apache.struts2.config.DefaultPropertiesProvider) ParseException(ognl.ParseException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Example 9 with OgnlValueStack

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

the class OgnlValueStackTest method testSetNullMap.

public void testSetNullMap() {
    Foo foo = new Foo();
    OgnlValueStack vs = createValueStack();
    vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    vs.push(foo);
    vs.setValue("catMap['One'].name", "Cat One");
    vs.setValue("catMap['Two'].name", "Cat Two");
    assertNotNull(foo.getCatMap());
    assertEquals(2, foo.getCatMap().size());
    assertEquals("Cat One", ((Cat) foo.getCatMap().get("One")).getName());
    assertEquals("Cat Two", ((Cat) foo.getCatMap().get("Two")).getName());
    vs.setValue("catMap['One'].foo.catMap['Two'].name", "Deep null cat");
    assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo());
    assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo().getCatMap());
    assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two"));
    assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two")).getName());
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 10 with OgnlValueStack

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

the class OgnlValueStackTest method testGetComplexBarAsString.

public void testGetComplexBarAsString() {
    // children foo->foo->foo
    Foo foo = new Foo();
    Foo foo2 = new Foo();
    foo.setChild(foo2);
    Foo foo3 = new Foo();
    foo2.setChild(foo3);
    // relatives
    Foo fooA = new Foo();
    foo.setRelatives(new Foo[] { fooA });
    Foo fooB = new Foo();
    foo2.setRelatives(new Foo[] { fooB });
    Foo fooC = new Foo();
    foo3.setRelatives(new Foo[] { fooC });
    // the bar
    Bar bar = new Bar();
    bar.setTitle("bar");
    bar.setSomethingElse(123);
    // now place the bar all over
    foo.setBar(bar);
    foo2.setBar(bar);
    foo3.setBar(bar);
    fooA.setBar(bar);
    fooB.setBar(bar);
    fooC.setBar(bar);
    OgnlValueStack vs = createValueStack();
    vs.push(foo);
    vs.getContext().put("foo", foo);
    assertEquals("bar:123", vs.findValue("#foo.bar", String.class));
    assertEquals("bar:123", vs.findValue("bar", String.class));
    assertEquals("bar:123", vs.findValue("child.bar", String.class));
    assertEquals("bar:123", vs.findValue("child.child.bar", String.class));
    assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));
    assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));
    assertEquals("bar:123", vs.findValue("child.child.relatives[0].bar", String.class));
    vs.push(vs.findValue("child"));
    assertEquals("bar:123", vs.findValue("bar", String.class));
    assertEquals("bar:123", vs.findValue("child.bar", String.class));
    assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));
    assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

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