Support @NumberFormat as a meta-annotation
This commit ensures that @NumberFormat can be used as a meta-annotation, as was already the case for @DateTimeFormat. In addition, this commit polishes FormattingConversionServiceTests and MvcNamespaceTests. Issue: SPR-12743
This commit is contained in:
committed by
Sam Brannen
parent
6861d35ea0
commit
c746b10da9
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -29,14 +29,14 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -52,6 +52,7 @@ import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.format.annotation.NumberFormat;
|
||||
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -132,6 +133,12 @@ import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -142,11 +149,13 @@ import static org.junit.Assert.*;
|
||||
* @author Jeremy Grelle
|
||||
* @author Brian Clozel
|
||||
* @author Sebastien Deleuze
|
||||
* @author Kazuki Shimizu
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class MvcNamespaceTests {
|
||||
|
||||
public static final String VIEWCONTROLLER_BEAN_NAME = "org.springframework.web.servlet.config.viewControllerHandlerMapping";
|
||||
|
||||
|
||||
private GenericWebApplicationContext appContext;
|
||||
|
||||
private TestController handler;
|
||||
@@ -165,7 +174,7 @@ public class MvcNamespaceTests {
|
||||
appContext.getServletContext().setAttribute(attributeName, appContext);
|
||||
|
||||
handler = new TestController();
|
||||
Method method = TestController.class.getMethod("testBind", Date.class, TestBean.class, BindingResult.class);
|
||||
Method method = TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class);
|
||||
handlerMethod = new InvocableHandlerMethod(handler, method);
|
||||
}
|
||||
|
||||
@@ -211,6 +220,7 @@ public class MvcNamespaceTests {
|
||||
// default web binding initializer behavior test
|
||||
request = new MockHttpServletRequest("GET", "/");
|
||||
request.addParameter("date", "2009-10-31");
|
||||
request.addParameter("percent", "99.99%");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
@@ -222,6 +232,8 @@ public class MvcNamespaceTests {
|
||||
|
||||
adapter.handle(request, response, handlerMethod);
|
||||
assertTrue(handler.recordedValidationError);
|
||||
assertEquals(LocalDate.parse("2009-10-31").toDate(), handler.date);
|
||||
assertEquals(Double.valueOf(0.9999),handler.percent);
|
||||
|
||||
CompositeUriComponentsContributor uriComponentsContributor = this.appContext.getBean(
|
||||
MvcUriComponentsBuilder.MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME,
|
||||
@@ -718,7 +730,7 @@ public class MvcNamespaceTests {
|
||||
assertEquals(TilesViewResolver.class, resolvers.get(2).getClass());
|
||||
|
||||
resolver = resolvers.get(3);
|
||||
FreeMarkerViewResolver freeMarkerViewResolver = (FreeMarkerViewResolver) resolver;
|
||||
assertThat(resolver, instanceOf(FreeMarkerViewResolver.class));
|
||||
accessor = new DirectFieldAccessor(resolver);
|
||||
assertEquals("freemarker-", accessor.getPropertyValue("prefix"));
|
||||
assertEquals(".freemarker", accessor.getPropertyValue("suffix"));
|
||||
@@ -726,14 +738,14 @@ public class MvcNamespaceTests {
|
||||
assertEquals(1024, accessor.getPropertyValue("cacheLimit"));
|
||||
|
||||
resolver = resolvers.get(4);
|
||||
VelocityViewResolver velocityViewResolver = (VelocityViewResolver) resolver;
|
||||
assertThat(resolver, instanceOf(VelocityViewResolver.class));
|
||||
accessor = new DirectFieldAccessor(resolver);
|
||||
assertEquals("", accessor.getPropertyValue("prefix"));
|
||||
assertEquals(".vm", accessor.getPropertyValue("suffix"));
|
||||
assertEquals(0, accessor.getPropertyValue("cacheLimit"));
|
||||
|
||||
resolver = resolvers.get(5);
|
||||
GroovyMarkupViewResolver groovyMarkupViewResolver = (GroovyMarkupViewResolver) resolver;
|
||||
assertThat(resolver, instanceOf(GroovyMarkupViewResolver.class));
|
||||
accessor = new DirectFieldAccessor(resolver);
|
||||
assertEquals("", accessor.getPropertyValue("prefix"));
|
||||
assertEquals(".tpl", accessor.getPropertyValue("suffix"));
|
||||
@@ -742,7 +754,6 @@ public class MvcNamespaceTests {
|
||||
assertEquals(InternalResourceViewResolver.class, resolvers.get(6).getClass());
|
||||
assertEquals(InternalResourceViewResolver.class, resolvers.get(7).getClass());
|
||||
|
||||
|
||||
TilesConfigurer tilesConfigurer = appContext.getBean(TilesConfigurer.class);
|
||||
assertNotNull(tilesConfigurer);
|
||||
String[] definitions = {
|
||||
@@ -841,6 +852,12 @@ public class MvcNamespaceTests {
|
||||
public @interface IsoDate {
|
||||
}
|
||||
|
||||
@NumberFormat(style = NumberFormat.Style.PERCENT)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface PercentNumber {
|
||||
}
|
||||
|
||||
@Validated(MyGroup.class)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@@ -850,10 +867,14 @@ public class MvcNamespaceTests {
|
||||
@Controller
|
||||
public static class TestController {
|
||||
|
||||
private Date date;
|
||||
private Double percent;
|
||||
private boolean recordedValidationError;
|
||||
|
||||
@RequestMapping
|
||||
public void testBind(@RequestParam @IsoDate Date date, @MyValid TestBean bean, BindingResult result) {
|
||||
public void testBind(@RequestParam @IsoDate Date date, @RequestParam(required = false) @PercentNumber Double percent, @MyValid TestBean bean, BindingResult result) {
|
||||
this.date = date;
|
||||
this.percent = percent;
|
||||
this.recordedValidationError = (result.getErrorCount() == 1);
|
||||
}
|
||||
}
|
||||
@@ -965,5 +986,4 @@ public class MvcNamespaceTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user