SWF-690 - sf:clientTextValidator doesn't respect custom faces converter
This commit is contained in:
@@ -23,6 +23,7 @@ import javax.faces.component.UIComponent;
|
||||
import javax.faces.component.ValueHolder;
|
||||
import javax.faces.context.FacesContext;
|
||||
import javax.faces.context.ResponseWriter;
|
||||
import javax.faces.convert.Converter;
|
||||
|
||||
import org.springframework.faces.ui.resource.ResourceHelper;
|
||||
import org.springframework.faces.webflow.JsfUtils;
|
||||
@@ -106,16 +107,9 @@ public class DojoDecorationRenderer extends BaseSpringJavascriptDecorationRender
|
||||
|
||||
if (valueHolder.getValue() != null) {
|
||||
attrs.append(", value : ");
|
||||
String strValue;
|
||||
if (valueHolder.getValue() instanceof String) {
|
||||
strValue = "'" + (String) valueHolder.getValue() + "'";
|
||||
} else {
|
||||
strValue = "'" + valueHolder.getConverter().getAsString(context, component, valueHolder.getValue())
|
||||
+ "'";
|
||||
if (valueHolder.getValue() instanceof Date) {
|
||||
strValue = "dojo.date.locale.parse(" + strValue
|
||||
+ ", {selector : 'date', datePattern : 'yyyy-MM-dd'})";
|
||||
}
|
||||
String strValue = "'" + getValueAsString(context, component) + "'";
|
||||
if (valueHolder.getValue() instanceof Date) {
|
||||
strValue = "dojo.date.locale.parse(" + strValue + ", {selector : 'date', datePattern : 'yyyy-MM-dd'})";
|
||||
}
|
||||
attrs.append(strValue);
|
||||
}
|
||||
@@ -123,6 +117,30 @@ public class DojoDecorationRenderer extends BaseSpringJavascriptDecorationRender
|
||||
return attrs.toString();
|
||||
}
|
||||
|
||||
protected String getValueAsString(FacesContext context, UIComponent component) {
|
||||
|
||||
ValueHolder valueHolder = (ValueHolder) component;
|
||||
|
||||
if (valueHolder.getValue() instanceof String) {
|
||||
return valueHolder.getValue().toString();
|
||||
}
|
||||
|
||||
Converter converter;
|
||||
if (valueHolder.getConverter() != null) {
|
||||
converter = valueHolder.getConverter();
|
||||
} else {
|
||||
converter = context.getApplication().createConverter(valueHolder.getValue().getClass());
|
||||
}
|
||||
|
||||
if (converter == null) {
|
||||
throw new FacesException("A converter could not be found to convert the value of " + component
|
||||
+ " to a String.");
|
||||
}
|
||||
|
||||
return converter.getAsString(context, component, valueHolder.getValue());
|
||||
|
||||
}
|
||||
|
||||
protected String getDojoAttributesAsString(FacesContext context, UIComponent component) {
|
||||
|
||||
DojoDecoration advisor = (DojoDecoration) component;
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.springframework.faces.ui;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.faces.FacesException;
|
||||
import javax.faces.component.UIInput;
|
||||
import javax.faces.convert.DateTimeConverter;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.faces.webflow.JSFMockHelper;
|
||||
|
||||
public class DojoDecorationRendererTests extends TestCase {
|
||||
|
||||
JSFMockHelper jsf = new JSFMockHelper();
|
||||
|
||||
public void setUp() throws Exception {
|
||||
jsf.setUp();
|
||||
}
|
||||
|
||||
public void tearDown() throws Exception {
|
||||
jsf.tearDown();
|
||||
}
|
||||
|
||||
public void testGetValueAsString() {
|
||||
UIInput childComponent = new UIInput();
|
||||
childComponent.setValue("foo");
|
||||
DojoDecorationRenderer renderer = new DojoDecorationRenderer();
|
||||
String convertedValue = renderer.getValueAsString(jsf.facesContext(), childComponent);
|
||||
assertEquals("foo", convertedValue);
|
||||
}
|
||||
|
||||
public void testGetValueAsString_LocalConverter() {
|
||||
UIInput childComponent = new UIInput();
|
||||
childComponent.setValue(new TestValue());
|
||||
childComponent.setConverter(new TestConverter());
|
||||
DojoDecorationRenderer renderer = new DojoDecorationRenderer();
|
||||
String convertedValue = renderer.getValueAsString(jsf.facesContext(), childComponent);
|
||||
assertEquals("foo", convertedValue);
|
||||
}
|
||||
|
||||
public void testGetValueAsString_NoConverter() {
|
||||
UIInput childComponent = new UIInput();
|
||||
childComponent.setValue(new TestValue());
|
||||
DojoDecorationRenderer renderer = new DojoDecorationRenderer();
|
||||
|
||||
try {
|
||||
renderer.getValueAsString(jsf.facesContext(), childComponent);
|
||||
fail("getValueAsString should throw exception if no converter is found");
|
||||
} catch (FacesException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetValueAsString_GlobalConverter() throws Exception {
|
||||
UIInput childComponent = new UIInput();
|
||||
childComponent.setValue(new TestValue());
|
||||
jsf.facesContext().getApplication().addConverter(TestValue.class, TestConverter.class.getName());
|
||||
DojoDecorationRenderer renderer = new DojoDecorationRenderer();
|
||||
String convertedValue = renderer.getValueAsString(jsf.facesContext(), childComponent);
|
||||
assertEquals("foo", convertedValue);
|
||||
}
|
||||
|
||||
public void testGetNodeAttributesAsString() {
|
||||
String expectedAttributes = "name : 'foo', value : 'foo'";
|
||||
UIInput childComponent = new UIInput();
|
||||
childComponent.setId("foo");
|
||||
childComponent.setValue("foo");
|
||||
DojoDecorationRenderer renderer = new DojoDecorationRenderer();
|
||||
String nodeAttributes = renderer.getNodeAttributesAsString(jsf.facesContext(), childComponent);
|
||||
assertEquals(expectedAttributes, nodeAttributes);
|
||||
}
|
||||
|
||||
public void testGetNodeAttributesAsString_DateValue() {
|
||||
String expectedAttributes = "name : 'foo', value : dojo.date.locale.parse('Nov 21, 1977', "
|
||||
+ "{selector : 'date', datePattern : 'yyyy-MM-dd'})";
|
||||
UIInput childComponent = new UIInput();
|
||||
childComponent.setConverter(new DateTimeConverter());
|
||||
childComponent.setId("foo");
|
||||
childComponent.setValue(new Date("11/21/1977"));
|
||||
DojoDecorationRenderer renderer = new DojoDecorationRenderer();
|
||||
String nodeAttributes = renderer.getNodeAttributesAsString(jsf.facesContext(), childComponent);
|
||||
assertEquals(expectedAttributes, nodeAttributes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.springframework.faces.ui;
|
||||
|
||||
import javax.faces.component.UIComponent;
|
||||
import javax.faces.context.FacesContext;
|
||||
import javax.faces.convert.Converter;
|
||||
|
||||
public class TestConverter implements Converter {
|
||||
|
||||
public TestConverter() {
|
||||
}
|
||||
|
||||
public Object getAsObject(FacesContext context, UIComponent component, String value) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Auto-generated method stub");
|
||||
}
|
||||
|
||||
public String getAsString(FacesContext context, UIComponent component, Object value) {
|
||||
return ((TestValue) value).getStringValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.springframework.faces.ui;
|
||||
|
||||
public class TestValue {
|
||||
|
||||
public String getStringValue() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user