formatter improvements
This commit is contained in:
@@ -38,7 +38,6 @@ public class CompositeConversionServiceTests extends TestCase {
|
||||
first.addConverter(new TextToBoolean("ja", "nee"));
|
||||
first.addDefaultAlias(Boolean.class);
|
||||
GenericConversionService second = new GenericConversionService();
|
||||
second.addConverter(new TextToNumber());
|
||||
second.addConverter(new TextToBoolean());
|
||||
second.addDefaultAlias(Integer.class);
|
||||
service = new CompositeConversionService(new ConversionService[] { first, second });
|
||||
@@ -48,7 +47,6 @@ public class CompositeConversionServiceTests extends TestCase {
|
||||
assertNotNull(service.getConversionExecutor(String.class, Class.class));
|
||||
assertNotNull(service.getConversionExecutor(String.class, Boolean.class));
|
||||
assertEquals(Boolean.TRUE, service.getConversionExecutor(String.class, Boolean.class).execute("ja"));
|
||||
assertNotNull(service.getConversionExecutor(String.class, Integer.class));
|
||||
try {
|
||||
service.getConversionExecutor(String.class, Date.class);
|
||||
fail();
|
||||
@@ -60,13 +58,11 @@ public class CompositeConversionServiceTests extends TestCase {
|
||||
public void testGetConversionExecutorByTargetAlias() {
|
||||
assertNotNull(service.getConversionExecutorByTargetAlias(String.class, "boolean"));
|
||||
assertEquals(Boolean.TRUE, service.getConversionExecutorByTargetAlias(String.class, "boolean").execute("ja"));
|
||||
assertNotNull(service.getConversionExecutorByTargetAlias(String.class, "integer"));
|
||||
assertNull(service.getConversionExecutorByTargetAlias(String.class, "class"));
|
||||
}
|
||||
|
||||
public void testGetConversionExecutorsForSource() {
|
||||
assertEquals(new TextToClass().getTargetClasses().length + new TextToBoolean().getTargetClasses().length
|
||||
+ new TextToNumber().getTargetClasses().length,
|
||||
assertEquals(new TextToClass().getTargetClasses().length + new TextToBoolean().getTargetClasses().length,
|
||||
service.getConversionExecutorsForSource(String.class).length);
|
||||
assertEquals(0, service.getConversionExecutorsForSource(Date.class).length);
|
||||
ConversionExecutor[] fromStringConversionExecutors = service.getConversionExecutorsForSource(String.class);
|
||||
@@ -81,7 +77,6 @@ public class CompositeConversionServiceTests extends TestCase {
|
||||
|
||||
public void testGetClassByAlias() {
|
||||
assertEquals(Boolean.class, service.getClassByAlias("boolean"));
|
||||
assertEquals(Integer.class, service.getClassByAlias("integer"));
|
||||
assertNull(service.getClassByAlias("class"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import junit.framework.TestCase;
|
||||
import org.springframework.binding.convert.ConversionException;
|
||||
import org.springframework.binding.convert.ConversionExecutor;
|
||||
import org.springframework.binding.convert.Converter;
|
||||
import org.springframework.core.enums.ShortCodedLabeledEnum;
|
||||
|
||||
/**
|
||||
* Test case for the default conversion service.
|
||||
@@ -86,31 +85,4 @@ public class DefaultConversionServiceTests extends TestCase {
|
||||
Integer three = (Integer) executor.execute("3");
|
||||
assertEquals(3, three.intValue());
|
||||
}
|
||||
|
||||
public void testLabeledEnumConversionNoSuchEnum() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, MyEnum.class);
|
||||
try {
|
||||
executor.execute("My Invalid Label");
|
||||
fail("Should have failed");
|
||||
} catch (ConversionException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testValidLabeledEnumConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, MyEnum.class);
|
||||
MyEnum myEnum = (MyEnum) executor.execute("My Label 1");
|
||||
assertEquals(MyEnum.ONE, myEnum);
|
||||
}
|
||||
|
||||
public static class MyEnum extends ShortCodedLabeledEnum {
|
||||
public static MyEnum ONE = new MyEnum(0, "My Label 1");
|
||||
|
||||
public static MyEnum TWO = new MyEnum(1, "My Label 2");
|
||||
|
||||
private MyEnum(int code, String label) {
|
||||
super(code, label);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2004-2007 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.format.support;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.binding.format.Formatter;
|
||||
import org.springframework.context.i18n.SimpleLocaleContext;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link NumberFormatterTests}.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
*/
|
||||
public class NumberFormatterTests extends TestCase {
|
||||
|
||||
private Locale systemDefaultLocale;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
systemDefaultLocale = Locale.getDefault();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
// restore default
|
||||
Locale.setDefault(systemDefaultLocale);
|
||||
}
|
||||
|
||||
public void testParseUsBigDecimalInUs() {
|
||||
Locale.setDefault(Locale.US);
|
||||
SimpleFormatterFactory formatterFactory = new SimpleFormatterFactory();
|
||||
Formatter formatter = formatterFactory.getNumberFormatter(BigDecimal.class);
|
||||
assertEquals(new BigDecimal("123.45"), formatter.parseValue("123.45", BigDecimal.class));
|
||||
}
|
||||
|
||||
public void testParseUsBigDecimalInGermany() {
|
||||
Locale.setDefault(Locale.GERMANY);
|
||||
SimpleFormatterFactory formatterFactory = new SimpleFormatterFactory();
|
||||
// we're expressing our numbers in US notation!
|
||||
formatterFactory.setLocaleContext(new SimpleLocaleContext(Locale.US));
|
||||
Formatter formatter = formatterFactory.getNumberFormatter(BigDecimal.class);
|
||||
assertEquals(new BigDecimal("123.45"), formatter.parseValue("123.45", BigDecimal.class));
|
||||
}
|
||||
|
||||
public void testParseGermanBigDecimalInGermany() {
|
||||
Locale.setDefault(Locale.GERMANY);
|
||||
SimpleFormatterFactory formatterFactory = new SimpleFormatterFactory();
|
||||
Formatter formatter = formatterFactory.getNumberFormatter(BigDecimal.class);
|
||||
assertEquals(new BigDecimal("123.45"), formatter.parseValue("123,45", BigDecimal.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user