FormattingConversionServiceTests.java 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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.
 */

package org.springframework.ui.format.support;

import static org.junit.Assert.assertEquals;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
28
import org.junit.After;
29 30
import org.junit.Before;
import org.junit.Test;
31
import org.springframework.context.i18n.LocaleContextHolder;
32 33 34 35 36 37 38 39 40 41 42 43
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
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.FormatStyle;
import org.springframework.ui.format.number.IntegerFormatter;

/**
 * @author Keith Donald
 * @author Juergen Hoeller
 */
K
polish  
Keith Donald 已提交
44
public class FormattingConversionServiceTests {
45

46
	private FormattingConversionService formattingService;
47 48 49

	@Before
	public void setUp() {
50 51 52 53 54 55 56
		formattingService = new FormattingConversionService();
		LocaleContextHolder.setLocale(Locale.US);
	}

	@After
	public void tearDown() {
		LocaleContextHolder.setLocale(null);
57 58 59 60 61
	}

	@Test
	public void testFormatFieldForTypeWithFormatter() throws ParseException {
		formattingService.addFormatterForFieldType(Number.class, new IntegerFormatter());
62
		String formatted = formattingService.convert(new Integer(3), String.class);
63
		assertEquals("3", formatted);
64
		Integer i = (Integer) formattingService.convert("3", Integer.class);
65 66 67 68 69 70 71 72 73 74 75 76
		assertEquals(new Integer(3), i);
	}

	@Test
	public void testFormatFieldForTypeWithPrinterParserWithCoersion() throws ParseException {
		formattingService.getConverterRegistry().addConverter(new Converter<DateTime, LocalDate>() {
			public LocalDate convert(DateTime source) {
				return source.toLocalDate();
			}
		});
		formattingService.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(DateTimeFormat
				.shortDate()), new DateTimeParser(DateTimeFormat.shortDate()));
77
		String formatted = formattingService.convert(new LocalDate(2009, 10, 31), String.class);
78
		assertEquals("10/31/09", formatted);
79
		LocalDate date = (LocalDate) formattingService.convert("10/31/09", LocalDate.class);
80 81 82 83 84 85 86 87 88 89 90 91 92 93
		assertEquals(new LocalDate(2009, 10, 31), date);
	}

	@Test
	public void testFormatFieldForAnnotation() throws Exception {
		formattingService.getConverterRegistry().addConverter(new Converter<Date, Long>() {
			public Long convert(Date source) {
				return source.getTime();
			}
		});
		formattingService.getConverterRegistry().addConverter(new Converter<DateTime, Date>() {
			public Date convert(DateTime source) {
				return source.toDate();
			}
94
		});
95
		formattingService.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
K
polish  
Keith Donald 已提交
96
		System.out.println(this.formattingService);
97 98
		String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
				.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.valueOf(String.class));
99
		assertEquals("10/31/09", formatted);
100 101
		LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.valueOf(String.class),
				new TypeDescriptor(Model.class.getField("date"))));
102 103
		assertEquals(new LocalDate(2009, 10, 31), date);
	}
104

105
	private static class Model {
106

107
		@SuppressWarnings("unused")
108
		@org.springframework.ui.format.jodatime.DateTimeFormat(dateStyle = FormatStyle.SHORT)
109 110
		public Date date;

111
	}
112 113

}