binding refactoring - in progress
This commit is contained in:
@@ -15,8 +15,11 @@
|
||||
*/
|
||||
package org.springframework.binding.convert.service;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -25,10 +28,10 @@ import junit.framework.TestCase;
|
||||
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;
|
||||
import org.springframework.binding.convert.converters.Converter;
|
||||
import org.springframework.binding.convert.converters.FormattedStringToNumber;
|
||||
import org.springframework.binding.convert.converters.StringToBoolean;
|
||||
import org.springframework.binding.format.DefaultNumberFormatFactory;
|
||||
|
||||
/**
|
||||
* Test case for the default conversion service.
|
||||
@@ -39,10 +42,8 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
|
||||
public void testConvertCompatibleTypes() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
|
||||
List lst = new ArrayList();
|
||||
assertSame(lst, service.getConversionExecutor(ArrayList.class, List.class).execute(lst));
|
||||
|
||||
try {
|
||||
service.getConversionExecutor(List.class, ArrayList.class);
|
||||
fail();
|
||||
@@ -52,10 +53,8 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testOverrideConverter() {
|
||||
Converter customConverter = new TextToBoolean("ja", "nee");
|
||||
|
||||
Converter customConverter = new StringToBoolean("ja", "nee");
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
|
||||
StaticConversionExecutor executor = (StaticConversionExecutor) service.getConversionExecutor(String.class,
|
||||
Boolean.class);
|
||||
assertNotSame(customConverter, executor.getConverter());
|
||||
@@ -65,9 +64,7 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
} catch (ConversionExecutionException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
service.addConverter(customConverter);
|
||||
|
||||
executor = (StaticConversionExecutor) service.getConversionExecutor(String.class, Boolean.class);
|
||||
assertSame(customConverter, executor.getConverter());
|
||||
assertTrue(((Boolean) executor.execute("ja")).booleanValue());
|
||||
@@ -87,21 +84,204 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, Integer.class);
|
||||
Integer three = (Integer) executor.execute("3");
|
||||
assertEquals(3, three.intValue());
|
||||
|
||||
ConversionExecutor executor2 = service.getConversionExecutor(Integer.class, String.class);
|
||||
String threeString = (String) executor2.execute(new Integer(3));
|
||||
assertEquals("3", threeString);
|
||||
}
|
||||
|
||||
public void testRegisterConverter() {
|
||||
GenericConversionService service = new GenericConversionService();
|
||||
IntegerFormatter formatter = new IntegerFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.US);
|
||||
FormatterConverter converter = new FormatterConverter(formatter);
|
||||
FormattedStringToNumber converter = new FormattedStringToNumber(Integer.class);
|
||||
DefaultNumberFormatFactory numberFormatFactory = new DefaultNumberFormatFactory();
|
||||
numberFormatFactory.setLocale(Locale.US);
|
||||
converter.setNumberFormatFactory(numberFormatFactory);
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
public void testConversionPrimitive() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, int.class);
|
||||
Integer three = (Integer) executor.execute("3");
|
||||
assertEquals(3, three.intValue());
|
||||
}
|
||||
|
||||
public void testArrayConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, Integer[].class);
|
||||
Integer[] result = (Integer[]) executor.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
public void testPrimitiveArrayConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, int[].class);
|
||||
int[] result = (int[]) executor.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
public void testArrayListConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, List.class);
|
||||
List result = (List) executor.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
assertEquals("3", result.get(2));
|
||||
}
|
||||
|
||||
public void testListArrayConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(Collection.class, String[].class);
|
||||
List list = new ArrayList();
|
||||
list.add("1");
|
||||
list.add("2");
|
||||
list.add("3");
|
||||
String[] result = (String[]) executor.execute(list);
|
||||
assertEquals("1", result[0]);
|
||||
assertEquals("2", result[1]);
|
||||
assertEquals("3", result[2]);
|
||||
}
|
||||
|
||||
public void testListArrayConversionWithComponentConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(Collection.class, Integer[].class);
|
||||
List list = new ArrayList();
|
||||
list.add("1");
|
||||
list.add("2");
|
||||
list.add("3");
|
||||
Integer[] result = (Integer[]) executor.execute(list);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
public void testArrayLinkedListConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, LinkedList.class);
|
||||
LinkedList result = (LinkedList) executor.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
assertEquals("3", result.get(2));
|
||||
}
|
||||
|
||||
public void testArrayAbstractListConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
try {
|
||||
service.getConversionExecutor(String[].class, AbstractList.class);
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void testToArrayConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, String[].class);
|
||||
String[] result = (String[]) executor.execute("1,2,3");
|
||||
assertEquals("1", result[0]);
|
||||
assertEquals("2", result[1]);
|
||||
assertEquals("3", result[2]);
|
||||
}
|
||||
|
||||
public void testToArrayConversionWithElementConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, Integer[].class);
|
||||
Integer[] result = (Integer[]) executor.execute("1,2,3");
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* public void testArrayListConversionWithElementConversion() throws Exception { DefaultConversionService service =
|
||||
* new DefaultConversionService(); ConversionExecutor executor = service.getConversionExecutor(String[].class,
|
||||
* IntegerArrayList.class); List result = (List) executor.execute(new String[] { "1", "2", "3" }); assertEquals(new
|
||||
* Integer(1), result.get(0)); assertEquals(new Integer(2), result.get(1)); assertEquals(new Integer(3),
|
||||
* result.get(2)); }
|
||||
*
|
||||
*
|
||||
* public static class IntegerArrayList implements List<Integer> {
|
||||
*
|
||||
* private ArrayList realList = new ArrayList();
|
||||
*
|
||||
* public IntegerArrayList() { }
|
||||
*
|
||||
* public void add(int index, Integer element) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public boolean add(Integer o) { return realList.add(o); }
|
||||
*
|
||||
* public boolean addAll(Collection<? extends Integer> c) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public boolean addAll(int index, Collection<? extends Integer> c) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public void clear() { // TODO Auto-generated method stub throw new UnsupportedOperationException("Auto-generated
|
||||
* method stub"); }
|
||||
*
|
||||
* public boolean contains(Object o) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public boolean containsAll(Collection<?> c) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public Integer get(int index) { return (Integer) realList.get(index); }
|
||||
*
|
||||
* public int indexOf(Object o) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public boolean isEmpty() { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public Iterator<Integer> iterator() { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public int lastIndexOf(Object o) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public ListIterator<Integer> listIterator() { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public ListIterator<Integer> listIterator(int index) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public Integer remove(int index) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public boolean remove(Object o) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public boolean removeAll(Collection<?> c) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public boolean retainAll(Collection<?> c) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public Integer set(int index, Integer element) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public int size() { // TODO Auto-generated method stub throw new UnsupportedOperationException("Auto-generated
|
||||
* method stub"); }
|
||||
*
|
||||
* public List<Integer> subList(int fromIndex, int toIndex) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public Object[] toArray() { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); }
|
||||
*
|
||||
* public <T> T[] toArray(T[] a) { // TODO Auto-generated method stub throw new
|
||||
* UnsupportedOperationException("Auto-generated method stub"); } }
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -20,14 +20,14 @@ import java.util.Date;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.convert.ConversionExecutionException;
|
||||
import org.springframework.binding.convert.Converter;
|
||||
import org.springframework.binding.convert.converters.StringToDate;
|
||||
|
||||
public class StaticConversionExecutorImplTests extends TestCase {
|
||||
|
||||
private StaticConversionExecutor conversionExecutor;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
conversionExecutor = new StaticConversionExecutor(String.class, Date.class, new TestTextToDate());
|
||||
conversionExecutor = new StaticConversionExecutor(String.class, Date.class, new StringToDate());
|
||||
}
|
||||
|
||||
public void testTypeConversion() {
|
||||
@@ -51,20 +51,4 @@ public class StaticConversionExecutorImplTests extends TestCase {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private class TestTextToDate implements Converter {
|
||||
|
||||
public Class[] getSourceClasses() {
|
||||
return new Class[] { String.class };
|
||||
}
|
||||
|
||||
public Class[] getTargetClasses() {
|
||||
return new Class[] { Date.class };
|
||||
}
|
||||
|
||||
public Object convert(Object source, Class targetClass, Object context) throws Exception {
|
||||
return source == null ? null : new Date();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
package org.springframework.binding.format.formatters;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.format.InvalidFormatException;
|
||||
|
||||
public class BooleanFormatterTests extends TestCase {
|
||||
|
||||
private BooleanFormatter formatter = new BooleanFormatter();
|
||||
|
||||
public void testFormatTrue() {
|
||||
assertEquals("true", formatter.format(Boolean.TRUE));
|
||||
}
|
||||
|
||||
public void testFormatFalse() {
|
||||
assertEquals("false", formatter.format(Boolean.FALSE));
|
||||
}
|
||||
|
||||
public void testFormatNull() {
|
||||
assertEquals("", formatter.format(null));
|
||||
}
|
||||
|
||||
public void testParseTrue() {
|
||||
assertEquals(Boolean.TRUE, formatter.parse("true"));
|
||||
}
|
||||
|
||||
public void testParseFalse() {
|
||||
assertEquals(Boolean.FALSE, formatter.parse("false"));
|
||||
}
|
||||
|
||||
public void testParseInvalid() {
|
||||
try {
|
||||
formatter.parse("bogus");
|
||||
fail("Should have failed");
|
||||
} catch (InvalidFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseNull() {
|
||||
assertNull(formatter.parse(null));
|
||||
}
|
||||
|
||||
public void testParseEmptyString() {
|
||||
assertNull(formatter.parse(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.springframework.binding.format.formatters;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.format.InvalidFormatException;
|
||||
|
||||
public class DateFormatterTests extends TestCase {
|
||||
|
||||
public void testFormatDefaultPattern() {
|
||||
DateFormatter dateFormatter = new DateFormatter();
|
||||
dateFormatter.setLocale(new Locale("nl"));
|
||||
Calendar calendar = new GregorianCalendar(2008, 3, 1);
|
||||
assertEquals("2008-04-01", dateFormatter.format(calendar.getTime()));
|
||||
}
|
||||
|
||||
public void testFormatCustomPattern() {
|
||||
DateFormatter dateFormatter = new DateFormatter();
|
||||
dateFormatter.setPattern("MM-dd-yyyy");
|
||||
Calendar calendar = new GregorianCalendar(2008, 3, 1);
|
||||
assertEquals("04-01-2008", dateFormatter.format(calendar.getTime()));
|
||||
}
|
||||
|
||||
public void testFormatNull() {
|
||||
DateFormatter dateFormatter = new DateFormatter();
|
||||
assertEquals("", dateFormatter.format(null));
|
||||
}
|
||||
|
||||
public void testParseDefaultPattern() {
|
||||
DateFormatter dateFormatter = new DateFormatter();
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.setTime((Date) dateFormatter.parse("2008-04-01"));
|
||||
assertEquals(2008, calendar.get(Calendar.YEAR));
|
||||
assertEquals(3, calendar.get(Calendar.MONTH));
|
||||
assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
public void testParseInvalidFormat() {
|
||||
DateFormatter dateFormatter = new DateFormatter();
|
||||
try {
|
||||
dateFormatter.parse("01/04/08");
|
||||
fail("Should have failed");
|
||||
} catch (InvalidFormatException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseNull() {
|
||||
DateFormatter dateFormatter = new DateFormatter();
|
||||
assertNull(dateFormatter.parse(null));
|
||||
}
|
||||
|
||||
public void testParseEmptyString() {
|
||||
DateFormatter dateFormatter = new DateFormatter();
|
||||
assertNull(dateFormatter.parse(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package org.springframework.binding.format.formatters;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Locale;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.format.InvalidFormatException;
|
||||
|
||||
public class NumberFormatterTests extends TestCase {
|
||||
|
||||
private NumberFormatter formatter;
|
||||
|
||||
public void testFormatIntegerDefaultPattern() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.US);
|
||||
String value = formatter.format(new Integer(12345));
|
||||
assertEquals("12,345", value);
|
||||
}
|
||||
|
||||
public void testFormatBigDecimalCustomPattern() {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
formatter.setPattern("000.00");
|
||||
formatter.setLocale(Locale.US);
|
||||
BigDecimal dec = new BigDecimal("123.45");
|
||||
String value = formatter.format(dec);
|
||||
assertEquals("123.45", value);
|
||||
}
|
||||
|
||||
public void testFormatNull() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
assertEquals("", formatter.format(null));
|
||||
}
|
||||
|
||||
public void testParseIntegerDefaultPattern() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
formatter.setLocale(Locale.US);
|
||||
Integer integer = (Integer) formatter.parse("123,450");
|
||||
assertEquals(Integer.valueOf(123450), integer);
|
||||
}
|
||||
|
||||
public void testParseBigDecimalCustomPattern() {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
formatter.setPattern("000.00");
|
||||
formatter.setLocale(Locale.US);
|
||||
BigDecimal dec = (BigDecimal) formatter.parse("123.45");
|
||||
assertEquals(new BigDecimal("123.45"), dec);
|
||||
}
|
||||
|
||||
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);
|
||||
formatter.setPattern("000.00");
|
||||
formatter.parse("bogus");
|
||||
fail("Should have failed");
|
||||
} catch (InvalidFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseNull() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
assertNull(formatter.parse(null));
|
||||
}
|
||||
|
||||
public void testParseEmptyString() {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
assertNull(formatter.parse(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package org.springframework.binding.format.impl;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.format.Formatter;
|
||||
import org.springframework.binding.format.InvalidFormatException;
|
||||
import org.springframework.binding.format.formatters.NumberFormatter;
|
||||
import org.springframework.binding.format.registry.GenericFormatterRegistry;
|
||||
|
||||
public class GenericFormatterRegistryTests extends TestCase {
|
||||
|
||||
GenericFormatterRegistry registry = new GenericFormatterRegistry();
|
||||
|
||||
public void testRegisterAndGetFormatter() {
|
||||
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(new NumberFormatter(Number.class));
|
||||
Formatter formatter = registry.getFormatter(Long.class);
|
||||
assertNull(formatter);
|
||||
}
|
||||
|
||||
public void testRegisterAndGetFormatterPrimitive() {
|
||||
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(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(new NumberFormatter(Integer.class));
|
||||
NumberFormatter percentFormatter = new NumberFormatter(BigDecimal.class);
|
||||
percentFormatter.setPattern("00%");
|
||||
registry.registerFormatter("percent", percentFormatter);
|
||||
Formatter formatter = registry.getFormatter(BigDecimal.class, "percent");
|
||||
assertEquals("35%", formatter.format(new BigDecimal(".35")));
|
||||
BigDecimal value = (BigDecimal) formatter.parse("35%");
|
||||
assertEquals(new BigDecimal(".35"), value);
|
||||
}
|
||||
|
||||
public void testRegisterCustomFormatterBogusLookupId() {
|
||||
registry.registerFormatter(new NumberFormatter(Integer.class));
|
||||
registry.registerFormatter("double", new NumberFormatter(Double.class));
|
||||
Formatter formatter = registry.getFormatter(Integer.class, "bogusFormat");
|
||||
assertNull(formatter);
|
||||
formatter = registry.getFormatter(Double.class, "double");
|
||||
assertNotNull(formatter);
|
||||
}
|
||||
|
||||
public void testRegisterCustomFormatterBogusClass() {
|
||||
registry.registerFormatter("double", new NumberFormatter(Double.class));
|
||||
try {
|
||||
registry.getFormatter(Integer.class, "double");
|
||||
fail("Should have failed");
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public interface CustomType {
|
||||
public String getText();
|
||||
}
|
||||
|
||||
public class DefaultCustomType implements CustomType {
|
||||
|
||||
private String text;
|
||||
|
||||
public DefaultCustomType(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
DefaultCustomType other = (DefaultCustomType) o;
|
||||
return text.equals(other.text);
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public Object parse(String formattedString) throws InvalidFormatException {
|
||||
return new DefaultCustomType(formattedString);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import java.util.Locale;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.convert.Converter;
|
||||
import org.springframework.binding.convert.service.DefaultConversionService;
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
import org.springframework.binding.expression.el.DefaultExpressionFactoryUtils;
|
||||
@@ -12,7 +11,6 @@ import org.springframework.binding.expression.el.ELExpressionParser;
|
||||
import org.springframework.binding.expression.support.FluentParserContext;
|
||||
import org.springframework.binding.mapping.impl.DefaultMapper;
|
||||
import org.springframework.binding.mapping.impl.DefaultMapping;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class DefaultMapperTests extends TestCase {
|
||||
private DefaultMapper mapper = new DefaultMapper();
|
||||
@@ -64,19 +62,6 @@ public class DefaultMapperTests extends TestCase {
|
||||
|
||||
public void testMappingWithCustomConversionService() {
|
||||
DefaultConversionService conversionService = new DefaultConversionService();
|
||||
conversionService.addConverter(new Converter() {
|
||||
public Class[] getSourceClasses() {
|
||||
return new Class[] { String.class };
|
||||
}
|
||||
|
||||
public Class[] getTargetClasses() {
|
||||
return new Class[] { Locale.class };
|
||||
}
|
||||
|
||||
public Object convert(Object source, Class targetClass, Object context) throws Exception {
|
||||
return StringUtils.parseLocaleString((String) source);
|
||||
}
|
||||
});
|
||||
mapper.setConversionService(conversionService);
|
||||
DefaultMapping mapping1 = new DefaultMapping(parser.parseExpression("foo", null), parser.parseExpression(
|
||||
"beep", null));
|
||||
|
||||
Reference in New Issue
Block a user