Search in sources :

Example 1 with FooBarConverter

use of com.opensymphony.xwork2.conversion.impl.FooBarConverter in project struts by apache.

the class ListAction method testStringToCustomTypeUsingCustomConverter.

public void testStringToCustomTypeUsingCustomConverter() {
    // the converter needs to be registered as the Bar.class converter
    // it won't be detected from the Foo-conversion.properties
    // because the Foo-conversion.properties file is only used when converting a property of Foo
    converter.registerConverter(Bar.class.getName(), new FooBarConverter());
    Bar bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
    assertNotNull("conversion failed", bar);
    assertEquals(123, bar.getSomethingElse());
    assertEquals("blah", bar.getTitle());
}
Also used : Bar(com.opensymphony.xwork2.util.Bar)

Example 2 with FooBarConverter

use of com.opensymphony.xwork2.conversion.impl.FooBarConverter in project struts by apache.

the class AnnotationXWorkConverterTest method testStringToCustomTypeUsingCustomConverter.

public void testStringToCustomTypeUsingCustomConverter() {
    // the converter needs to be registered as the Bar.class converter
    // it won't be detected from the Foo-conversion.properties
    // because the Foo-conversion.properties file is only used when converting a property of Foo
    converter.registerConverter(Bar.class.getName(), new FooBarConverter());
    Bar bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
    assertNotNull("conversion failed", bar);
    assertEquals(123, bar.getSomethingElse());
    assertEquals("blah", bar.getTitle());
}
Also used : Bar(com.opensymphony.xwork2.util.Bar)

Example 3 with FooBarConverter

use of com.opensymphony.xwork2.conversion.impl.FooBarConverter in project struts by apache.

the class SetPropertiesTest method testSetCollectionByConverterFromArray.

public void testSetCollectionByConverterFromArray() {
    Foo foo = new Foo();
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    XWorkConverter c = (XWorkConverter) ((OgnlTypeConverterWrapper) Ognl.getTypeConverter(vs.getContext())).getTarget();
    c.registerConverter(Cat.class.getName(), new FooBarConverter());
    vs.push(foo);
    vs.setValue("cats", new String[] { "1", "2" });
    assertNotNull(foo.getCats());
    assertEquals(2, foo.getCats().size());
    assertEquals(Cat.class, foo.getCats().get(0).getClass());
    assertEquals(Cat.class, foo.getCats().get(1).getClass());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) Foo(com.opensymphony.xwork2.util.Foo) Cat(com.opensymphony.xwork2.util.Cat) FooBarConverter(com.opensymphony.xwork2.conversion.impl.FooBarConverter) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter)

Example 4 with FooBarConverter

use of com.opensymphony.xwork2.conversion.impl.FooBarConverter in project struts by apache.

the class SetPropertiesTest method testSetCollectionByConverterFromCollection.

public void testSetCollectionByConverterFromCollection() {
    Foo foo = new Foo();
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    XWorkConverter c = (XWorkConverter) ((OgnlTypeConverterWrapper) Ognl.getTypeConverter(vs.getContext())).getTarget();
    c.registerConverter(Cat.class.getName(), new FooBarConverter());
    vs.push(foo);
    HashSet s = new HashSet();
    s.add("1");
    s.add("2");
    vs.setValue("cats", s);
    assertNotNull(foo.getCats());
    assertEquals(2, foo.getCats().size());
    assertEquals(Cat.class, foo.getCats().get(0).getClass());
    assertEquals(Cat.class, foo.getCats().get(1).getClass());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) Foo(com.opensymphony.xwork2.util.Foo) Cat(com.opensymphony.xwork2.util.Cat) FooBarConverter(com.opensymphony.xwork2.conversion.impl.FooBarConverter) XWorkConverter(com.opensymphony.xwork2.conversion.impl.XWorkConverter)

Example 5 with FooBarConverter

use of com.opensymphony.xwork2.conversion.impl.FooBarConverter in project struts by apache.

the class PropertyTest method testTypeConverterShouldBeUsed.

public void testTypeConverterShouldBeUsed() {
    final ValueStack stack = ActionContext.getContext().getValueStack();
    converter.registerConverter("org.apache.struts2.components.PropertyTest$FooBar", new FooBarConverter());
    stack.push(new FooBar("foo-value", "bar-value"));
    final Property property = new Property(stack);
    property.setDefault("default");
    property.setValue("top");
    assertPropertyOutput("*foo-value + bar-value*", property);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)4 FooBarConverter (com.opensymphony.xwork2.conversion.impl.FooBarConverter)2 XWorkConverter (com.opensymphony.xwork2.conversion.impl.XWorkConverter)2 Bar (com.opensymphony.xwork2.util.Bar)2 Cat (com.opensymphony.xwork2.util.Cat)2 Foo (com.opensymphony.xwork2.util.Foo)2