|
| 1 | +/* |
| 2 | + * Copyright 2002-2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.format.support; |
| 18 | + |
| 19 | +import java.lang.annotation.ElementType; |
| 20 | +import java.lang.annotation.Retention; |
| 21 | +import java.lang.annotation.RetentionPolicy; |
| 22 | +import java.lang.annotation.Target; |
| 23 | +import java.text.ParseException; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import org.springframework.core.convert.TypeDescriptor; |
| 31 | +import org.springframework.format.AnnotationFormatterFactory; |
| 32 | +import org.springframework.format.Formatter; |
| 33 | +import org.springframework.format.Parser; |
| 34 | +import org.springframework.format.Printer; |
| 35 | +import org.springframework.format.annotation.NumberFormat; |
| 36 | +import org.springframework.format.annotation.NumberFormat.Style; |
| 37 | + |
| 38 | +import static org.junit.Assert.*; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Rossen Stoyanchev |
| 42 | + */ |
| 43 | +public class FormattingConversionServiceFactoryBeanTests { |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testDefaultFormatters() throws Exception { |
| 47 | + FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); |
| 48 | + factory.afterPropertiesSet(); |
| 49 | + FormattingConversionService fcs = factory.getObject(); |
| 50 | + TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("percent")); |
| 51 | + Object value = fcs.convert("5%", TypeDescriptor.valueOf(String.class), descriptor); |
| 52 | + assertEquals(.05, value); |
| 53 | + value = fcs.convert(.05, descriptor, TypeDescriptor.valueOf(String.class)); |
| 54 | + assertEquals("5%", value); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testCustomFormatter() throws Exception { |
| 59 | + FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); |
| 60 | + Set<Object> formatters = new HashSet<Object>(); |
| 61 | + formatters.add(new TestBeanFormatter()); |
| 62 | + formatters.add(new SpecialIntAnnotationFormatterFactory()); |
| 63 | + factory.setFormatters(formatters); |
| 64 | + factory.afterPropertiesSet(); |
| 65 | + FormattingConversionService fcs = factory.getObject(); |
| 66 | + |
| 67 | + TestBean testBean = fcs.convert("5", TestBean.class); |
| 68 | + assertEquals(5, testBean.getSpecialInt()); |
| 69 | + assertEquals("5", fcs.convert(testBean, String.class)); |
| 70 | + |
| 71 | + TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("specialInt")); |
| 72 | + Object value = fcs.convert(":5", TypeDescriptor.valueOf(String.class), descriptor); |
| 73 | + assertEquals(5, value); |
| 74 | + value = fcs.convert(5, descriptor, TypeDescriptor.valueOf(String.class)); |
| 75 | + assertEquals(":5", value); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testInvalidFormatter() throws Exception { |
| 80 | + FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); |
| 81 | + Set<Object> formatters = new HashSet<Object>(); |
| 82 | + formatters.add(new Object()); |
| 83 | + factory.setFormatters(formatters); |
| 84 | + try { |
| 85 | + factory.afterPropertiesSet(); |
| 86 | + fail("Expected formatter to be rejected"); |
| 87 | + } |
| 88 | + catch (IllegalArgumentException ex) { |
| 89 | + // expected |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) |
| 95 | + @Retention(RetentionPolicy.RUNTIME) |
| 96 | + private @interface SpecialInt { |
| 97 | + } |
| 98 | + |
| 99 | + private static class TestBean { |
| 100 | + |
| 101 | + @SuppressWarnings("unused") |
| 102 | + @NumberFormat(style = Style.PERCENT) |
| 103 | + private double percent; |
| 104 | + |
| 105 | + @SpecialInt |
| 106 | + private int specialInt; |
| 107 | + |
| 108 | + public int getSpecialInt() { |
| 109 | + return specialInt; |
| 110 | + } |
| 111 | + |
| 112 | + public void setSpecialInt(int field) { |
| 113 | + this.specialInt = field; |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + private static class TestBeanFormatter implements Formatter<TestBean> { |
| 119 | + |
| 120 | + public String print(TestBean object, Locale locale) { |
| 121 | + return String.valueOf(object.getSpecialInt()); |
| 122 | + } |
| 123 | + |
| 124 | + public TestBean parse(String text, Locale locale) throws ParseException { |
| 125 | + TestBean object = new TestBean(); |
| 126 | + object.setSpecialInt(Integer.parseInt(text)); |
| 127 | + return object; |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + private static class SpecialIntAnnotationFormatterFactory implements AnnotationFormatterFactory<SpecialInt> { |
| 133 | + |
| 134 | + private final Set<Class<?>> fieldTypes = new HashSet<Class<?>>(1); |
| 135 | + |
| 136 | + public SpecialIntAnnotationFormatterFactory() { |
| 137 | + fieldTypes.add(Integer.class); |
| 138 | + } |
| 139 | + |
| 140 | + public Set<Class<?>> getFieldTypes() { |
| 141 | + return fieldTypes; |
| 142 | + } |
| 143 | + |
| 144 | + public Printer<?> getPrinter(SpecialInt annotation, Class<?> fieldType) { |
| 145 | + return new Printer<Integer>() { |
| 146 | + public String print(Integer object, Locale locale) { |
| 147 | + return ":" + object.toString(); |
| 148 | + } |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + public Parser<?> getParser(SpecialInt annotation, Class<?> fieldType) { |
| 153 | + return new Parser<Integer>() { |
| 154 | + public Integer parse(String text, Locale locale) throws ParseException { |
| 155 | + return Integer.parseInt(text.substring(1)); |
| 156 | + } |
| 157 | + }; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments