Search in sources :

Example 6 with Bar

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

the class OgnlUtilTest method testIncudeExcludes.

public void testIncudeExcludes() {
    Foo foo1 = new Foo();
    Foo foo2 = new Foo();
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);
    foo1.setPoints(new long[] { 1, 2, 3 });
    foo1.setBirthday(cal.getTime());
    foo1.setUseful(false);
    foo1.setTitle("foo1 title");
    foo1.setNumber(1);
    foo2.setTitle("foo2 title");
    foo2.setNumber(2);
    Map<String, Object> context = ognlUtil.createDefaultContext(foo1);
    List<String> excludes = new ArrayList<>();
    excludes.add("title");
    excludes.add("number");
    ognlUtil.copy(foo1, foo2, context, excludes, null);
    // these values should remain unchanged in foo2
    assertEquals(foo2.getTitle(), "foo2 title");
    assertEquals(foo2.getNumber(), 2);
    // these values should be changed/copied
    assertEquals(foo1.getPoints(), foo2.getPoints());
    assertEquals(foo1.getBirthday(), foo2.getBirthday());
    assertEquals(foo1.isUseful(), foo2.isUseful());
    Bar b1 = new Bar();
    Bar b2 = new Bar();
    b1.setTitle("bar1 title");
    b1.setSomethingElse(10);
    b1.setId(1L);
    b2.setTitle("");
    b2.setId(2L);
    context = ognlUtil.createDefaultContext(b1);
    List<String> includes = new ArrayList<>();
    includes.add("title");
    includes.add("somethingElse");
    ognlUtil.copy(b1, b2, context, null, includes);
    // includes properties got copied
    assertEquals(b1.getTitle(), b2.getTitle());
    assertEquals(b1.getSomethingElse(), b2.getSomethingElse());
    // id properties did not
    assertEquals(b2.getId(), new Long(2));
}
Also used : Bar(com.opensymphony.xwork2.util.Bar) Foo(com.opensymphony.xwork2.util.Foo) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList)

Example 7 with Bar

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

the class DomHelperTest method testGetLocationObject.

public void testGetLocationObject() throws Exception {
    InputSource in = new InputSource(new StringReader(xml));
    in.setSystemId("foo://bar");
    Document doc = DomHelper.parse(in);
    NodeList nl = doc.getElementsByTagName("bar");
    Location loc = DomHelper.getLocationObject((Element) nl.item(0));
    assertNotNull(loc);
    assertTrue("Should be line 6, was " + loc.getLineNumber(), 6 == loc.getLineNumber());
}
Also used : InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) Location(com.opensymphony.xwork2.util.location.Location)

Example 8 with Bar

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

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

the class AnnotationXWorkConverterTest method testFindConversionMappingForInterfaceAndSuperclass.

public void testFindConversionMappingForInterfaceAndSuperclass() {
    ModelDrivenAnnotationAction2 action = new ModelDrivenAnnotationAction2();
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);
    stack.push(action.getModel());
    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    String value = "asdf:123";
    Object o = converter.convertValue(ognlStackContext, action.getModel(), null, "barObj", value, Bar.class);
    assertNotNull(o);
    assertTrue("class is: " + o.getClass(), o instanceof Bar);
    Bar b = (Bar) o;
    assertEquals(value, b.getTitle() + ":" + b.getSomethingElse());
    String value2 = "qwer:456";
    Object o2 = converter.convertValue(ognlStackContext, action.getModel(), null, "supperBarObj", value2, Bar.class);
    assertNotNull(o2);
    assertTrue("class is: " + o.getClass(), o2 instanceof Bar);
    Bar b2 = (Bar) o2;
    assertEquals(value2, b2.getTitle() + ":" + b2.getSomethingElse());
}
Also used : Bar(com.opensymphony.xwork2.util.Bar) ValueStack(com.opensymphony.xwork2.util.ValueStack) ModelDrivenAnnotationAction2(com.opensymphony.xwork2.test.ModelDrivenAnnotationAction2)

Example 10 with Bar

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

the class SimpleActionValidationTest method testParamterizedMessage.

public void testParamterizedMessage() {
    HashMap<String, Object> params = new HashMap<>();
    params.put("bar", "42");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
        proxy.execute();
        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
        Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List<String> barErrors = errors.get("bar");
        assertEquals(1, barErrors.size());
        String errorMessage = barErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("bar must be between 6 and 10, current value is 42.", errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Aggregations

HashMap (java.util.HashMap)17 Bar (com.opensymphony.xwork2.util.Bar)14 Foo (com.opensymphony.xwork2.util.Foo)13 ValueStack (com.opensymphony.xwork2.util.ValueStack)12 ActionProxy (com.opensymphony.xwork2.ActionProxy)9 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)9 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)7 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)7 List (java.util.List)6 Map (java.util.Map)6 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)5 Mock (com.mockobjects.dynamic.Mock)4 ActionContext (com.opensymphony.xwork2.ActionContext)4 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)4 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)3 MockResult (com.opensymphony.xwork2.mock.MockResult)3 ModelDrivenAction2 (com.opensymphony.xwork2.test.ModelDrivenAction2)3 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)3 ActionSupport (com.opensymphony.xwork2.ActionSupport)2