revised matchable converter lookup algorithm; added conversion service bean container tests

This commit is contained in:
Keith Donald
2009-11-05 14:52:57 +00:00
parent c812cd370b
commit f0de1c3069
13 changed files with 147 additions and 39 deletions

View File

@@ -0,0 +1,14 @@
package org.springframework.context.conversionservice;
public class Bar {
private String value;
public Bar(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}

View File

@@ -0,0 +1,21 @@
package org.springframework.context.conversionservice;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConversionServiceContextConfigTests {
@Test
public void testConfigOk() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/context/conversionservice/conversionservice.xml");
TestClient client = context.getBean("testClient", TestClient.class);
assertEquals(2, client.getBars().size());
assertEquals("value1", client.getBars().get(0).getValue());
assertEquals("value2", client.getBars().get(1).getValue());
assertTrue(client.isBool());
}
}

View File

@@ -0,0 +1,11 @@
package org.springframework.context.conversionservice;
import org.springframework.core.convert.converter.Converter;
public class StringToBarConverter implements Converter<String, Bar> {
public Bar convert(String source) {
return new Bar(source);
}
}

View File

@@ -0,0 +1,30 @@
package org.springframework.context.conversionservice;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
public class TestClient {
private List<Bar> bars;
private boolean bool;
public List<Bar> getBars() {
return bars;
}
@Autowired
public void setBars(List<Bar> bars) {
this.bars = bars;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
}

View File

@@ -12,11 +12,11 @@ import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.format.jodatime.DateTimeFormat.Style;
import org.springframework.ui.format.annotation.DateTimeFormat;
import org.springframework.ui.format.annotation.DateTimeFormat.Style;
import org.springframework.ui.format.support.FormattingConversionService;
import org.springframework.validation.DataBinder;
@@ -30,7 +30,7 @@ public class JodaTimeFormattingTests {
public void setUp() {
JodaTimeFormattingConfigurer configurer = new JodaTimeFormattingConfigurer();
configurer.installJodaTimeFormatting(conversionService);
binder = new DataBinder(new JodaTimeBean());
binder.setConversionService(conversionService);
@@ -67,7 +67,7 @@ public class JodaTimeFormattingTests {
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
@@ -101,17 +101,15 @@ public class JodaTimeFormattingTests {
propertyValues.addPropertyValue("localDateTimeAnnotated", "Saturday, October 31, 2009 12:00:00 PM ");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Saturday, October 31, 2009 12:00:00 PM ", binder.getBindingResult().getFieldValue("localDateTimeAnnotated"));
assertEquals("Saturday, October 31, 2009 12:00:00 PM ", binder.getBindingResult().getFieldValue(
"localDateTimeAnnotated"));
}
@Test
@Ignore
public void testBindDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("dateTime", "10/31/09 12:00 PM");
// this doesn't work because the String->ReadableInstant converter doesn't match due to String->@DateTimeFormat DateTime Matchable taking precedence
binder.bind(propertyValues);
System.out.println(binder.getBindingResult());
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTime"));
}
@@ -125,7 +123,7 @@ public class JodaTimeFormattingTests {
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated"));
}
@Test
public void testBindDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();

View File

@@ -31,10 +31,10 @@ import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.ui.format.annotation.DateTimeFormat.Style;
import org.springframework.ui.format.jodatime.DateTimeFormatAnnotationFormatterFactory;
import org.springframework.ui.format.jodatime.DateTimeParser;
import org.springframework.ui.format.jodatime.ReadablePartialPrinter;
import org.springframework.ui.format.jodatime.DateTimeFormat.Style;
import org.springframework.ui.format.number.IntegerFormatter;
/**
@@ -105,7 +105,7 @@ public class FormattingConversionServiceTests {
private static class Model {
@SuppressWarnings("unused")
@org.springframework.ui.format.jodatime.DateTimeFormat(dateStyle = Style.SHORT)
@org.springframework.ui.format.annotation.DateTimeFormat(dateStyle = Style.SHORT)
public Date date;
}