Skip to content

Commit 93304b5

Browse files
committed
removed ConversionService/TypeConverter convenience methods in order to restore 3.0's SPI (for backwards compatibility with implementers)
1 parent cd584af commit 93304b5

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.junit.Before;
3434
import org.junit.Test;
3535
import org.springframework.beans.BeanUtils;
36-
import org.springframework.beans.BeanWrapper;
36+
import org.springframework.beans.ConfigurablePropertyAccessor;
3737
import org.springframework.beans.PropertyAccessorFactory;
3838
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
3939
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -94,7 +94,13 @@ public LocalDate convert(DateTime source) {
9494
@Test
9595
public void testFormatFieldForAnnotation() throws Exception {
9696
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
97-
doTestFormatFieldForAnnotation(Model.class);
97+
doTestFormatFieldForAnnotation(Model.class, false);
98+
}
99+
100+
@Test
101+
public void testFormatFieldForAnnotationWithDirectFieldAccess() throws Exception {
102+
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
103+
doTestFormatFieldForAnnotation(Model.class, true);
98104
}
99105

100106
@Test
@@ -109,7 +115,7 @@ public void testFormatFieldForAnnotationWithPlaceholders() throws Exception {
109115
context.refresh();
110116
context.getBeanFactory().initializeBean(formattingService, "formattingService");
111117
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
112-
doTestFormatFieldForAnnotation(ModelWithPlaceholders.class);
118+
doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
113119
}
114120

115121
@Test
@@ -124,10 +130,10 @@ public void testFormatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws
124130
context.getBeanFactory().registerSingleton("ppc", ppc);
125131
context.refresh();
126132
formattingService = context.getBean("formattingService", FormattingConversionService.class);
127-
doTestFormatFieldForAnnotation(ModelWithPlaceholders.class);
133+
doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
128134
}
129135

130-
private void doTestFormatFieldForAnnotation(Class<?> modelClass) throws Exception {
136+
private void doTestFormatFieldForAnnotation(Class<?> modelClass, boolean directFieldAccess) throws Exception {
131137
formattingService.addConverter(new Converter<Date, Long>() {
132138
public Long convert(Date source) {
133139
return source.getTime();
@@ -160,20 +166,23 @@ public Date convert(DateTime source) {
160166
assertEquals(new LocalDate(2009, 11, 2), new LocalDate(dates.get(2)));
161167

162168
Object model = BeanUtils.instantiate(modelClass);
163-
BeanWrapper accessor = PropertyAccessorFactory.forBeanPropertyAccess(model);
169+
ConfigurablePropertyAccessor accessor = directFieldAccess ? PropertyAccessorFactory.forDirectFieldAccess(model) :
170+
PropertyAccessorFactory.forBeanPropertyAccess(model);
164171
accessor.setConversionService(formattingService);
165172
accessor.setPropertyValue("dates", "10-31-09,11-1-09,11-2-09");
166173
dates = (List<Date>) accessor.getPropertyValue("dates");
167174
assertEquals(new LocalDate(2009, 10, 31), new LocalDate(dates.get(0)));
168175
assertEquals(new LocalDate(2009, 11, 1), new LocalDate(dates.get(1)));
169176
assertEquals(new LocalDate(2009, 11, 2), new LocalDate(dates.get(2)));
170-
accessor.setPropertyValue("dates[0]", "10-30-09");
171-
accessor.setPropertyValue("dates[1]", "10-1-09");
172-
accessor.setPropertyValue("dates[2]", "10-2-09");
173-
dates = (List<Date>) accessor.getPropertyValue("dates");
174-
assertEquals(new LocalDate(2009, 10, 30), new LocalDate(dates.get(0)));
175-
assertEquals(new LocalDate(2009, 10, 1), new LocalDate(dates.get(1)));
176-
assertEquals(new LocalDate(2009, 10, 2), new LocalDate(dates.get(2)));
177+
if (!directFieldAccess) {
178+
accessor.setPropertyValue("dates[0]", "10-30-09");
179+
accessor.setPropertyValue("dates[1]", "10-1-09");
180+
accessor.setPropertyValue("dates[2]", "10-2-09");
181+
dates = (List<Date>) accessor.getPropertyValue("dates");
182+
assertEquals(new LocalDate(2009, 10, 30), new LocalDate(dates.get(0)));
183+
assertEquals(new LocalDate(2009, 10, 1), new LocalDate(dates.get(1)));
184+
assertEquals(new LocalDate(2009, 10, 2), new LocalDate(dates.get(2)));
185+
}
177186
}
178187

179188
@Test
@@ -191,7 +200,7 @@ public void testParseNull() throws ParseException {
191200
@Test
192201
public void testParseEmptyString() throws ParseException {
193202
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
194-
assertNull(formattingService.convert("", TypeDescriptor.valueOf(Integer.class)));
203+
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
195204
}
196205

197206
@Test
@@ -206,7 +215,7 @@ public void testParseNullDefault() throws ParseException {
206215

207216
@Test
208217
public void testParseEmptyStringDefault() throws ParseException {
209-
assertNull(formattingService.convert("", TypeDescriptor.valueOf(Integer.class)));
218+
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
210219
}
211220

212221

0 commit comments

Comments
 (0)