FormatterRegistry extends ConverterRegistry now; FormattingConversionService extends GenericConversionService
This commit is contained in:
@@ -1,6 +1,20 @@
|
||||
package org.springframework.format.datetime.joda;
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
@@ -12,15 +26,22 @@ import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.After;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.format.support.FormattingConversionService;
|
||||
import org.springframework.validation.DataBinder;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class JodaTimeFormattingTests {
|
||||
|
||||
private FormattingConversionService conversionService = new FormattingConversionService();
|
||||
@@ -29,6 +50,8 @@ public class JodaTimeFormattingTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ConversionServiceFactory.addDefaultConverters(conversionService);
|
||||
|
||||
JodaTimeFormattingConfigurer configurer = new JodaTimeFormattingConfigurer();
|
||||
configurer.installJodaTimeFormatting(conversionService);
|
||||
|
||||
@@ -81,6 +104,16 @@ public class JodaTimeFormattingTests {
|
||||
assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalDateAnnotatedWithDirectFieldAccess() {
|
||||
binder.initDirectFieldAccess();
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
|
||||
binder.bind(propertyValues);
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalTime() {
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
@@ -235,6 +268,7 @@ public class JodaTimeFormattingTests {
|
||||
assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime"));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class JodaTimeBean {
|
||||
|
||||
|
||||
@@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.format.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
@@ -27,11 +24,14 @@ import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.junit.After;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
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.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.format.datetime.joda.DateTimeFormatAnnotationFormatterFactory;
|
||||
import org.springframework.format.datetime.joda.DateTimeParser;
|
||||
import org.springframework.format.datetime.joda.ReadablePartialPrinter;
|
||||
@@ -48,6 +48,7 @@ public class FormattingConversionServiceTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
formattingService = new FormattingConversionService();
|
||||
ConversionServiceFactory.addDefaultConverters(formattingService);
|
||||
LocaleContextHolder.setLocale(Locale.US);
|
||||
}
|
||||
|
||||
@@ -59,15 +60,15 @@ public class FormattingConversionServiceTests {
|
||||
@Test
|
||||
public void testFormatFieldForTypeWithFormatter() throws ParseException {
|
||||
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
|
||||
String formatted = formattingService.convert(new Integer(3), String.class);
|
||||
String formatted = formattingService.convert(3, String.class);
|
||||
assertEquals("3", formatted);
|
||||
Integer i = (Integer) formattingService.convert("3", Integer.class);
|
||||
Integer i = formattingService.convert("3", Integer.class);
|
||||
assertEquals(new Integer(3), i);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatFieldForTypeWithPrinterParserWithCoersion() throws ParseException {
|
||||
formattingService.getConverterRegistry().addConverter(new Converter<DateTime, LocalDate>() {
|
||||
formattingService.addConverter(new Converter<DateTime, LocalDate>() {
|
||||
public LocalDate convert(DateTime source) {
|
||||
return source.toLocalDate();
|
||||
}
|
||||
@@ -76,18 +77,18 @@ public class FormattingConversionServiceTests {
|
||||
.shortDate()), new DateTimeParser(DateTimeFormat.shortDate()));
|
||||
String formatted = formattingService.convert(new LocalDate(2009, 10, 31), String.class);
|
||||
assertEquals("10/31/09", formatted);
|
||||
LocalDate date = (LocalDate) formattingService.convert("10/31/09", LocalDate.class);
|
||||
LocalDate date = formattingService.convert("10/31/09", LocalDate.class);
|
||||
assertEquals(new LocalDate(2009, 10, 31), date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatFieldForAnnotation() throws Exception {
|
||||
formattingService.getConverterRegistry().addConverter(new Converter<Date, Long>() {
|
||||
formattingService.addConverter(new Converter<Date, Long>() {
|
||||
public Long convert(Date source) {
|
||||
return source.getTime();
|
||||
}
|
||||
});
|
||||
formattingService.getConverterRegistry().addConverter(new Converter<DateTime, Date>() {
|
||||
formattingService.addConverter(new Converter<DateTime, Date>() {
|
||||
public Date convert(DateTime source) {
|
||||
return source.toDate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user