@Override protected void setUp() throws Exception { super.setUp(); converter = container.getInstance(XWorkConverter.class); ac = ActionContext.getContext(); ac.setLocale(Locale.US); context = ac.getContextMap(); }
public void testFindConversionErrorMessage() { ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction(); ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(action); stack.push(action.getModel()); String message = XWorkConverter.getConversionErrorMessage("birth", stack); assertNotNull(message); assertEquals("Invalid date for birth.", message); message = XWorkConverter.getConversionErrorMessage("foo", stack); assertNotNull(message); assertEquals("Invalid field value for field \"foo\".", message); }
// TODO: Fixme... This test does not work with GenericsObjectDeterminer! public void testStringToCollectionConversion() { ValueStack stack = ActionContext.getContext().getValueStack(); Map<String, Object> stackContext = stack.getContext(); stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE); stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE); stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE); AnnotationUser user = new AnnotationUser(); stack.push(user); stack.setValue("list", "asdf"); assertNotNull(user.getList()); assertEquals(1, user.getList().size()); assertEquals(String.class, user.getList().get(0).getClass()); assertEquals("asdf", user.getList().get(0)); }
public void testFindConversionMappingForInterface() { 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()); }
public void testFieldErrorMessageAddedWhenConversionFailsOnModelDriven() { ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction(); 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 = new String[] {"invalid date"}; assertEquals( "Conversion should have failed.", OgnlRuntime.NoConversionPossible, converter.convertValue(ognlStackContext, action, null, "birth", value, Date.class)); stack.pop(); stack.pop(); Map conversionErrors = (Map) ognlStackContext.get(ActionContext.CONVERSION_ERRORS); assertNotNull(conversionErrors); assertEquals(1, conversionErrors.size()); assertNotNull(conversionErrors.get("birth")); assertEquals(value, conversionErrors.get("birth")); }
public void testFieldErrorMessageAddedForComplexProperty() { SimpleAnnotationAction action = new SimpleAnnotationAction(); action.setBean(new AnnotatedTestBean()); ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(action); Map<String, Object> ognlStackContext = stack.getContext(); ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE); ognlStackContext.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, "bean.birth"); String[] value = new String[] {"invalid date"}; assertEquals( "Conversion should have failed.", OgnlRuntime.NoConversionPossible, converter.convertValue( ognlStackContext, action.getBean(), null, "birth", value, Date.class)); stack.pop(); Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS); assertNotNull(conversionErrors); assertTrue(conversionErrors.size() == 1); assertEquals(value, conversionErrors.get("bean.birth")); }
public void testValueStackWithTypeParameter() { ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(new Foo1()); Bar1 bar = (Bar1) stack.findValue("bar", Bar1.class); assertNotNull(bar); }