Search in sources :

Example 1 with Cat

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

the class FooBarConverter method convertValue.

@Override
public Object convertValue(Map<String, Object> context, Object value, Class toType) {
    if (toType == String.class) {
        Bar bar = (Bar) value;
        return bar.getTitle() + ":" + bar.getSomethingElse();
    } else if (toType == Bar.class) {
        String valueStr = (String) value;
        int loc = valueStr.indexOf(":");
        String title = valueStr.substring(0, loc);
        String rest = valueStr.substring(loc + 1);
        Bar bar = new Bar();
        bar.setTitle(title);
        bar.setSomethingElse(Integer.parseInt(rest));
        return bar;
    } else if (toType == Cat.class) {
        Cat cat = new Cat();
        cat.setName((String) value);
        return cat;
    } else if (toType == AnnotatedCat.class) {
        AnnotatedCat cat = new AnnotatedCat();
        cat.setName((String) value);
        return cat;
    } else {
        System.out.println("Don't know how to convert between " + value.getClass().getName() + " and " + toType.getName());
    }
    return null;
}
Also used : AnnotatedCat(com.opensymphony.xwork2.util.AnnotatedCat) Bar(com.opensymphony.xwork2.util.Bar) AnnotatedCat(com.opensymphony.xwork2.util.AnnotatedCat) Cat(com.opensymphony.xwork2.util.Cat)

Example 2 with Cat

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

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

the class OgnlValueStackTest method testPetSoarBug.

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

Example 4 with Cat

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

the class OgnlValueStackTest method testFindValueWithConversion.

public void testFindValueWithConversion() {
    // register converter
    TestBean2 tb2 = new TestBean2();
    OgnlValueStack stack = createValueStack();
    stack.push(tb2);
    Map myContext = stack.getContext();
    Map props = new HashMap();
    props.put("cat", "Kitty");
    ognlUtil.setProperties(props, tb2, myContext);
    // expect String to be converted into a Cat
    assertEquals("Kitty", tb2.getCat().getName());
    // findValue should be able to access the name
    Object value = stack.findValue("cat.name == 'Kitty'", Boolean.class);
    assertNotNull(value);
    assertEquals(Boolean.class, value.getClass());
    assertEquals(Boolean.TRUE, value);
    value = stack.findValue("cat == null", Boolean.class);
    assertNotNull(value);
    assertEquals(Boolean.class, value.getClass());
    assertEquals(Boolean.FALSE, value);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestBean2(com.opensymphony.xwork2.test.TestBean2) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with Cat

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

the class SetPropertiesTest method doTestAddingToMapsWithObjects.

public void doTestAddingToMapsWithObjects(boolean allowAdditions) throws Exception {
    loadButAdd(ObjectTypeDeterminer.class, new MockObjectTypeDeterminer(Long.class, Cat.class, null, allowAdditions));
    Foo foo = new Foo();
    foo.setAnotherCatMap(new HashMap());
    String spielname = "Spielen";
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    vs.push(foo);
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    vs.setValue("anotherCatMap[\"3\"].name", spielname);
    Object setCat = foo.getAnotherCatMap().get(new Long(3));
    if (allowAdditions) {
        assertNotNull(setCat);
        assertTrue(setCat instanceof Cat);
        assertTrue(((Cat) setCat).getName().equals(spielname));
    } else {
        assertNull(setCat);
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) Cat(com.opensymphony.xwork2.util.Cat) Foo(com.opensymphony.xwork2.util.Foo) MockObjectTypeDeterminer(com.opensymphony.xwork2.mock.MockObjectTypeDeterminer)

Aggregations

Foo (com.opensymphony.xwork2.util.Foo)6 Cat (com.opensymphony.xwork2.util.Cat)4 MockObjectTypeDeterminer (com.opensymphony.xwork2.mock.MockObjectTypeDeterminer)2 ValueStack (com.opensymphony.xwork2.util.ValueStack)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Action (com.opensymphony.xwork2.Action)1 ActionContext (com.opensymphony.xwork2.ActionContext)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 ObjectTypeDeterminer (com.opensymphony.xwork2.conversion.ObjectTypeDeterminer)1 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)1 Context (com.opensymphony.xwork2.inject.Context)1 Factory (com.opensymphony.xwork2.inject.Factory)1 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)1 TestBean2 (com.opensymphony.xwork2.test.TestBean2)1 AnnotatedCat (com.opensymphony.xwork2.util.AnnotatedCat)1 Bar (com.opensymphony.xwork2.util.Bar)1 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)1 ArrayList (java.util.ArrayList)1