Search in sources :

Example 51 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class OgnlUtilTest method testGetBeanMap.

public void testGetBeanMap() throws Exception {
    Bar bar = new Bar();
    bar.setTitle("I have beer");
    Foo foo = new Foo();
    foo.setALong(123);
    foo.setNumber(44);
    foo.setBar(bar);
    foo.setTitle("Hello Santa");
    foo.setUseful(true);
    // just do some of the 15 tests
    Map<String, Object> beans = ognlUtil.getBeanMap(foo);
    assertNotNull(beans);
    assertEquals(22, beans.size());
    assertEquals("Hello Santa", beans.get("title"));
    assertEquals(new Long("123"), beans.get("ALong"));
    assertEquals(new Integer("44"), beans.get("number"));
    assertEquals(bar, beans.get("bar"));
    assertEquals(Boolean.TRUE, beans.get("useful"));
}
Also used : Bar(com.opensymphony.xwork2.util.Bar) Foo(com.opensymphony.xwork2.util.Foo)

Example 52 with Foo

use of com.opensymphony.xwork2.util.Foo 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 53 with Foo

use of com.opensymphony.xwork2.util.Foo 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 54 with Foo

use of com.opensymphony.xwork2.util.Foo 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 55 with Foo

use of com.opensymphony.xwork2.util.Foo 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)53 HashMap (java.util.HashMap)32 ValueStack (com.opensymphony.xwork2.util.ValueStack)23 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)20 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)19 StrutsException (org.apache.struts2.StrutsException)19 OgnlException (ognl.OgnlException)18 IntrospectionException (java.beans.IntrospectionException)17 InappropriateExpressionException (ognl.InappropriateExpressionException)17 MethodFailedException (ognl.MethodFailedException)17 NoSuchPropertyException (ognl.NoSuchPropertyException)17 ActionProxy (com.opensymphony.xwork2.ActionProxy)16 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)9 ActionContext (com.opensymphony.xwork2.ActionContext)8 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)8 Bar (com.opensymphony.xwork2.util.Bar)8 Map (java.util.Map)8 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)7 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)7 StubValueStack (com.opensymphony.xwork2.StubValueStack)6