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;
}
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());
}
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));
}
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);
}
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);
}
}
Aggregations