fixes for known 2.0.0 bugs reported since release - see changelog
This commit is contained in:
@@ -21,12 +21,11 @@ import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.convert.ConversionException;
|
||||
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.TextToBoolean;
|
||||
import org.springframework.binding.convert.service.DefaultConversionService;
|
||||
import org.springframework.binding.convert.service.StaticConversionExecutor;
|
||||
|
||||
/**
|
||||
* Test case for the default conversion service.
|
||||
@@ -44,7 +43,7 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
try {
|
||||
service.getConversionExecutor(List.class, ArrayList.class);
|
||||
fail();
|
||||
} catch (ConversionException e) {
|
||||
} catch (ConversionExecutorNotFoundException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
@@ -60,7 +59,7 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
try {
|
||||
executor.execute("ja");
|
||||
fail();
|
||||
} catch (ConversionException e) {
|
||||
} catch (ConversionExecutionException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
@@ -76,7 +75,7 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
try {
|
||||
service.getConversionExecutor(String.class, HashMap.class);
|
||||
fail("Should have thrown an exception");
|
||||
} catch (ConversionException e) {
|
||||
} catch (ConversionExecutorNotFoundException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ import java.util.Date;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.convert.ConversionException;
|
||||
import org.springframework.binding.convert.converters.AbstractConverter;
|
||||
import org.springframework.binding.convert.ConversionExecutionException;
|
||||
import org.springframework.binding.convert.Converter;
|
||||
|
||||
public class StaticConversionExecutorImplTests extends TestCase {
|
||||
|
||||
@@ -47,12 +47,12 @@ public class StaticConversionExecutorImplTests extends TestCase {
|
||||
try {
|
||||
conversionExecutor.execute(new StringBuffer());
|
||||
fail();
|
||||
} catch (ConversionException e) {
|
||||
} catch (ConversionExecutionException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private class TestTextToDate extends AbstractConverter {
|
||||
private class TestTextToDate implements Converter {
|
||||
|
||||
public Class[] getSourceClasses() {
|
||||
return new Class[] { String.class };
|
||||
@@ -62,7 +62,7 @@ public class StaticConversionExecutorImplTests extends TestCase {
|
||||
return new Class[] { Date.class };
|
||||
}
|
||||
|
||||
protected Object doConvert(Object source, Class targetClass, Object context) throws Exception {
|
||||
public Object convert(Object source, Class targetClass, Object context) throws Exception {
|
||||
return source == null ? null : new Date();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
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(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import java.util.GregorianCalendar;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.format.InvalidFormatException;
|
||||
|
||||
public class DateFormatterTests extends TestCase {
|
||||
|
||||
public void testFormatDefaultPattern() {
|
||||
@@ -14,6 +16,18 @@ public class DateFormatterTests extends TestCase {
|
||||
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();
|
||||
@@ -23,4 +37,24 @@ public class DateFormatterTests extends TestCase {
|
||||
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(""));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.springframework.binding.format.formatters;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
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);
|
||||
String value = formatter.format(new Integer(12345));
|
||||
assertEquals("12345", value);
|
||||
}
|
||||
|
||||
public void testFormatBigDecimalCustomPattern() {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
formatter.setPattern("000.00");
|
||||
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);
|
||||
Integer integer = (Integer) formatter.parse("12345");
|
||||
assertEquals(Integer.valueOf(12345), integer);
|
||||
}
|
||||
|
||||
public void testParseBigDecimalCustomPattern() {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
formatter.setPattern("000.00");
|
||||
BigDecimal dec = (BigDecimal) formatter.parse("123.45");
|
||||
assertEquals(new BigDecimal("123.45"), dec);
|
||||
}
|
||||
|
||||
public void testParseInvalidFormatNoPattern() {
|
||||
try {
|
||||
formatter = new NumberFormatter(Integer.class);
|
||||
formatter.parse("12345b");
|
||||
fail("Should have failed");
|
||||
} catch (InvalidFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseInvalidFormatPattern() {
|
||||
try {
|
||||
formatter = new NumberFormatter(BigDecimal.class);
|
||||
formatter.setPattern("000.00");
|
||||
formatter.parse("bogus");
|
||||
} 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(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import org.springframework.binding.format.InvalidFormatException;
|
||||
import org.springframework.binding.format.formatters.NumberFormatter;
|
||||
import org.springframework.binding.format.registry.GenericFormatterRegistry;
|
||||
|
||||
public class FormatterRegistryImplTests extends TestCase {
|
||||
public class GenericFormatterRegistryTests extends TestCase {
|
||||
|
||||
GenericFormatterRegistry registry = new GenericFormatterRegistry();
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
package org.springframework.binding.mapping;
|
||||
|
||||
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;
|
||||
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();
|
||||
private ExpressionParser parser = new ELExpressionParser(DefaultExpressionFactoryUtils.createExpressionFactory());
|
||||
|
||||
public void testMapping() {
|
||||
DefaultMapping mapping1 = new DefaultMapping(parser.parseExpression("foo", null), parser.parseExpression("bar",
|
||||
null));
|
||||
DefaultMapping mapping2 = new DefaultMapping(parser.parseExpression("foo", null), parser.parseExpression("baz",
|
||||
null));
|
||||
mapper.addMapping(mapping1);
|
||||
mapper.addMapping(mapping2);
|
||||
assertEquals(2, mapper.getMappings().length);
|
||||
TestBean bean1 = new TestBean();
|
||||
bean1.foo = "a";
|
||||
TestBean2 bean2 = new TestBean2();
|
||||
MappingResults results = mapper.map(bean1, bean2);
|
||||
assertSame(bean1, results.getSource());
|
||||
assertSame(bean2, results.getTarget());
|
||||
assertEquals(2, results.getAllResults().size());
|
||||
assertEquals(0, results.getErrorResults().size());
|
||||
assertEquals("a", bean2.bar);
|
||||
assertEquals("a", bean2.baz);
|
||||
assertEquals(1, results.getResults(new MappingResultsCriteria() {
|
||||
public boolean test(MappingResult result) {
|
||||
if (result.getMapping().getTargetExpression().getExpressionString().equals("baz")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}).size());
|
||||
}
|
||||
|
||||
public void testMappingWithAutomaticConversion() {
|
||||
DefaultMapping mapping1 = new DefaultMapping(parser.parseExpression("foo", new FluentParserContext()
|
||||
.expectResult(Integer.class)), parser.parseExpression("boop", null));
|
||||
mapper.addMapping(mapping1);
|
||||
TestBean bean1 = new TestBean();
|
||||
bean1.foo = "12345";
|
||||
TestBean2 bean2 = new TestBean2();
|
||||
MappingResults results = mapper.map(bean1, bean2);
|
||||
assertSame(bean1, results.getSource());
|
||||
assertSame(bean2, results.getTarget());
|
||||
assertEquals(1, results.getAllResults().size());
|
||||
assertEquals(0, results.getErrorResults().size());
|
||||
assertEquals(new Integer(12345), bean2.boop);
|
||||
}
|
||||
|
||||
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));
|
||||
mapper.addMapping(mapping1);
|
||||
TestBean bean1 = new TestBean();
|
||||
bean1.foo = "en";
|
||||
TestBean2 bean2 = new TestBean2();
|
||||
MappingResults results = mapper.map(bean1, bean2);
|
||||
assertSame(bean1, results.getSource());
|
||||
assertSame(bean2, results.getTarget());
|
||||
assertEquals(1, results.getAllResults().size());
|
||||
System.out.println(results.getAllResults());
|
||||
assertEquals(0, results.getErrorResults().size());
|
||||
assertEquals(Locale.ENGLISH, bean2.beep);
|
||||
}
|
||||
|
||||
public static class TestBean {
|
||||
private String foo;
|
||||
|
||||
public String getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(String foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class TestBean2 {
|
||||
private String bar;
|
||||
private String baz;
|
||||
private Integer boop;
|
||||
private Locale beep;
|
||||
|
||||
public String getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public String getBaz() {
|
||||
return baz;
|
||||
}
|
||||
|
||||
public void setBaz(String baz) {
|
||||
this.baz = baz;
|
||||
}
|
||||
|
||||
public void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public Integer getBoop() {
|
||||
return boop;
|
||||
}
|
||||
|
||||
public void setBoop(Integer boop) {
|
||||
this.boop = boop;
|
||||
}
|
||||
|
||||
public Locale getBeep() {
|
||||
return beep;
|
||||
}
|
||||
|
||||
public void setBeep(Locale beep) {
|
||||
this.beep = beep;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user