Skip to content

Support the meta annotation at @NumberFormat #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -341,6 +345,10 @@ public static class MetaValueBean {

@MyDateAnn
public Date date;

@MyNumberAnn
public Double number;

}


Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
Expand Down