formatter polishing
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.binding.convert.service;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -25,7 +26,9 @@ import org.springframework.binding.convert.ConversionExecutionException;
|
||||
import org.springframework.binding.convert.ConversionExecutor;
|
||||
import org.springframework.binding.convert.ConversionExecutorNotFoundException;
|
||||
import org.springframework.binding.convert.Converter;
|
||||
import org.springframework.binding.convert.converters.FormatterConverter;
|
||||
import org.springframework.binding.convert.converters.TextToBoolean;
|
||||
import org.springframework.binding.format.formatters.IntegerFormatter;
|
||||
|
||||
/**
|
||||
* Test case for the default conversion service.
|
||||
@@ -85,4 +88,20 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
Integer three = (Integer) executor.execute("3");
|
||||
assertEquals(3, three.intValue());
|
||||
}
|
||||
|
||||
public void testRegisterConverter() {
|
||||
GenericConversionService service = new GenericConversionService();
|
||||
IntegerFormatter formatter = new IntegerFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.US);
|
||||
FormatterConverter converter = new FormatterConverter(formatter);
|
||||
service.addConverter(converter);
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, Integer.class);
|
||||
Integer three = (Integer) executor.execute("3,000");
|
||||
assertEquals(3000, three.intValue());
|
||||
|
||||
ConversionExecutor executor2 = service.getConversionExecutor(Integer.class, String.class);
|
||||
String string = (String) executor2.execute(new Integer(3000));
|
||||
assertEquals("3,000", string);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public class NumberFormatterTests extends TestCase {
|
||||
|
||||
public void testFormatIntegerDefaultPattern() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.ENGLISH);
|
||||
formatter.setLocale(Locale.US);
|
||||
String value = formatter.format(new Integer(12345));
|
||||
assertEquals("12,345", value);
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class NumberFormatterTests extends TestCase {
|
||||
public void testFormatBigDecimalCustomPattern() {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
formatter.setPattern("000.00");
|
||||
formatter.setLocale(Locale.ENGLISH);
|
||||
formatter.setLocale(Locale.US);
|
||||
BigDecimal dec = new BigDecimal("123.45");
|
||||
String value = formatter.format(dec);
|
||||
assertEquals("123.45", value);
|
||||
@@ -34,7 +34,7 @@ public class NumberFormatterTests extends TestCase {
|
||||
|
||||
public void testParseIntegerDefaultPattern() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.ENGLISH);
|
||||
formatter.setLocale(Locale.US);
|
||||
Integer integer = (Integer) formatter.parse("123,450");
|
||||
assertEquals(Integer.valueOf(123450), integer);
|
||||
}
|
||||
@@ -42,7 +42,7 @@ public class NumberFormatterTests extends TestCase {
|
||||
public void testParseBigDecimalCustomPattern() {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
formatter.setPattern("000.00");
|
||||
formatter.setLocale(Locale.ENGLISH);
|
||||
formatter.setLocale(Locale.US);
|
||||
BigDecimal dec = (BigDecimal) formatter.parse("123.45");
|
||||
assertEquals(new BigDecimal("123.45"), dec);
|
||||
}
|
||||
@@ -50,12 +50,32 @@ public class NumberFormatterTests extends TestCase {
|
||||
public void testParseInvalidFormatPatternTruncation() {
|
||||
try {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.US);
|
||||
formatter.parse("123,450b");
|
||||
fail("Should have failed");
|
||||
} catch (InvalidFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseInvalidFormatPatternTruncationInteger() {
|
||||
try {
|
||||
formatter = new IntegerFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.US);
|
||||
formatter.parse("123,450.00");
|
||||
fail("Should have failed");
|
||||
} catch (InvalidFormatException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseInvalidFormatPatternLenient() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.US);
|
||||
formatter.setLenient(true);
|
||||
Integer integer = (Integer) formatter.parse("123,450b");
|
||||
assertEquals(Integer.valueOf(123450), integer);
|
||||
}
|
||||
|
||||
public void testParseInvalidFormatPattern() {
|
||||
try {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
|
||||
@@ -14,35 +14,34 @@ public class GenericFormatterRegistryTests extends TestCase {
|
||||
GenericFormatterRegistry registry = new GenericFormatterRegistry();
|
||||
|
||||
public void testRegisterAndGetFormatter() {
|
||||
registry.registerFormatter(Integer.class, new NumberFormatter(Integer.class));
|
||||
registry.registerFormatter(new NumberFormatter(Integer.class));
|
||||
Formatter formatter = registry.getFormatter(Integer.class);
|
||||
Integer value = (Integer) formatter.parse("3");
|
||||
assertEquals(new Integer(3), value);
|
||||
}
|
||||
|
||||
public void testRegisterAndGetFormatterAbstractClass() {
|
||||
registry.registerFormatter(Number.class, new NumberFormatter(Long.class));
|
||||
registry.registerFormatter(new NumberFormatter(Number.class));
|
||||
Formatter formatter = registry.getFormatter(Long.class);
|
||||
Number value = (Number) formatter.parse("3");
|
||||
assertEquals(3L, value.longValue());
|
||||
assertNull(formatter);
|
||||
}
|
||||
|
||||
public void testRegisterAndGetFormatterPrimitive() {
|
||||
registry.registerFormatter(Integer.class, new NumberFormatter(Integer.class));
|
||||
registry.registerFormatter(new NumberFormatter(Integer.class));
|
||||
Formatter formatter = registry.getFormatter(int.class);
|
||||
Integer value = (Integer) formatter.parse("3");
|
||||
assertEquals(new Integer(3), value);
|
||||
}
|
||||
|
||||
public void testRegisterAndGetFormatterInterface() {
|
||||
registry.registerFormatter(CustomType.class, new CustomTypeFormatter());
|
||||
Formatter formatter = registry.getFormatter(DefaultCustomType.class);
|
||||
registry.registerFormatter(new CustomTypeFormatter());
|
||||
Formatter formatter = registry.getFormatter(CustomType.class);
|
||||
assertEquals("12345", formatter.format(new DefaultCustomType("12345")));
|
||||
assertEquals(new DefaultCustomType("12345"), formatter.parse("12345"));
|
||||
}
|
||||
|
||||
public void testRegisterCustomFormatter() {
|
||||
registry.registerFormatter(Integer.class, new NumberFormatter(Integer.class));
|
||||
registry.registerFormatter(new NumberFormatter(Integer.class));
|
||||
NumberFormatter percentFormatter = new NumberFormatter(BigDecimal.class);
|
||||
percentFormatter.setPattern("00%");
|
||||
registry.registerFormatter("percent", percentFormatter);
|
||||
@@ -53,7 +52,7 @@ public class GenericFormatterRegistryTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testRegisterCustomFormatterBogusLookupId() {
|
||||
registry.registerFormatter(Integer.class, new NumberFormatter(Integer.class));
|
||||
registry.registerFormatter(new NumberFormatter(Integer.class));
|
||||
registry.registerFormatter("double", new NumberFormatter(Double.class));
|
||||
Formatter formatter = registry.getFormatter("bogusFormat");
|
||||
assertNull(formatter);
|
||||
@@ -85,6 +84,10 @@ public class GenericFormatterRegistryTests extends TestCase {
|
||||
|
||||
private class CustomTypeFormatter implements Formatter {
|
||||
|
||||
public Class getObjectType() {
|
||||
return CustomType.class;
|
||||
}
|
||||
|
||||
public String format(Object value) throws IllegalArgumentException {
|
||||
CustomType type = (CustomType) value;
|
||||
return type.getText();
|
||||
|
||||
Reference in New Issue
Block a user