From fe33feb0267096873b081dc7ce9718cd92afa2de Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Sun, 22 Feb 2015 23:58:10 +0900 Subject: [PATCH] Support the meta annotation at @NumberFormat Issue: SPR-12743 --- .../format/annotation/NumberFormat.java | 2 +- .../FormattingConversionServiceTests.java | 15 ++++++++++++- .../web/servlet/config/MvcNamespaceTests.java | 21 ++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java b/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java index 944a276cb0b7..6e158ece4878 100644 --- a/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java +++ b/spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java @@ -41,7 +41,7 @@ */ @Documented @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) public @interface NumberFormat { /** diff --git a/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java b/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java index a2930e3e7e93..d39326260138 100644 --- a/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java +++ b/spring-context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 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. @@ -48,6 +48,7 @@ import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.format.Formatter; import org.springframework.format.Printer; +import org.springframework.format.annotation.NumberFormat; import org.springframework.format.datetime.joda.DateTimeParser; import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory; import org.springframework.format.datetime.joda.ReadablePartialPrinter; @@ -120,12 +121,15 @@ public void testFormatFieldForValueInjectionUsingMetaAnnotations() { ac.registerBeanDefinition("ppc", new RootBeanDefinition(PropertyPlaceholderConfigurer.class)); ac.refresh(); System.setProperty("myDate", "10-31-09"); + System.setProperty("myNumber", "99.99%"); try { MetaValueBean valueBean = ac.getBean(MetaValueBean.class); assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date)); + assertEquals(Double.valueOf(0.9999), valueBean.number); } finally { System.clearProperty("myDate"); + System.clearProperty("myNumber"); } } @@ -341,6 +345,10 @@ public static class MetaValueBean { @MyDateAnn public Date date; + + @MyNumberAnn + public Double number; + } @@ -350,6 +358,11 @@ public static class MetaValueBean { public static @interface MyDateAnn { } + @Value("${myNumber}") + @org.springframework.format.annotation.NumberFormat(style = NumberFormat.Style.PERCENT) + @Retention(RetentionPolicy.RUNTIME) + public static @interface MyNumberAnn { + } public static class Model { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java index a7de588e6c7c..f9e5a6a3e6c0 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java @@ -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. @@ -37,6 +37,7 @@ 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 +53,7 @@ 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; @@ -165,7 +167,7 @@ public void setUp() throws Exception { 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 +213,7 @@ public void testDefaultConfig() throws Exception { // 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 +225,8 @@ public void testDefaultConfig() throws Exception { 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, @@ -841,6 +846,12 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) { 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 +861,14 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) { @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); } }