fix converter

This commit is contained in:
Looly
2022-06-07 09:23:27 +08:00
parent 1473d4625f
commit e3d5c2756c
21 changed files with 233 additions and 317 deletions

View File

@@ -39,8 +39,8 @@ public class ConvertToArrayTest {
public void toIntArrayTestIgnoreComponentErrorTest() {
final String[] b = { "a", "1" };
final ArrayConverter arrayConverter = new ArrayConverter(Integer[].class, true);
final Integer[] integerArray = (Integer[]) arrayConverter.convert(b, null);
final ArrayConverter arrayConverter = new ArrayConverter(true);
final Integer[] integerArray = arrayConverter.convert(Integer[].class, b, null);
Assert.assertArrayEquals(integerArray, new Integer[]{null, 1});
}
@@ -89,8 +89,8 @@ public class ConvertToArrayTest {
//字符串转数组
final String arrayStr = "1,2,3,4,5";
//获取Converter类的方法2自己实例化相应Converter对象
final ArrayConverter c3 = new ArrayConverter(int[].class);
final int[] result3 = (int[]) c3.convert(arrayStr, null);
final ArrayConverter c3 = new ArrayConverter();
final int[] result3 = c3.convert(int[].class, arrayStr, null);
Assert.assertArrayEquals(new int[]{1,2,3,4,5}, result3);
}

View File

@@ -3,6 +3,8 @@ package cn.hutool.core.convert;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Type;
/**
* ConverterRegistry 单元测试
* @author Looly
@@ -12,7 +14,7 @@ public class ConverterRegistryTest {
@Test
public void getConverterTest() {
final Converter<Object> converter = ConverterRegistry.getInstance().getConverter(CharSequence.class, false);
final Converter converter = ConverterRegistry.getInstance().getConverter(CharSequence.class, false);
Assert.assertNotNull(converter);
}
@@ -26,14 +28,14 @@ public class ConverterRegistryTest {
//此处做为示例自定义CharSequence转换因为Hutool中已经提供CharSequence转换请尽量不要替换
//替换可能引发关联转换异常例如覆盖CharSequence转换会影响全局
converterRegistry.putCustom(CharSequence.class, CustomConverter.class);
converterRegistry.putCustom(CharSequence.class, new CustomConverter());
result = converterRegistry.convert(CharSequence.class, a);
Assert.assertEquals("Custom: 454553", result);
}
public static class CustomConverter implements Converter<CharSequence>{
public static class CustomConverter implements Converter{
@Override
public CharSequence convert(final Object value, final CharSequence defaultValue) throws IllegalArgumentException {
public Object convert(Type targetType, Object value) throws ConvertException {
return "Custom: " + value.toString();
}
}

View File

@@ -8,15 +8,15 @@ public class NumberConverterTest {
@Test
public void toDoubleTest(){
final NumberConverter numberConverter = new NumberConverter(Double.class);
final Number convert = numberConverter.convert("1,234.55", null);
final NumberConverter numberConverter = new NumberConverter();
final Number convert = numberConverter.convert(Double.class, "1,234.55", null);
Assert.assertEquals(1234.55D, convert);
}
@Test
public void toIntegerTest(){
final NumberConverter numberConverter = new NumberConverter(Integer.class);
final Number convert = numberConverter.convert("1,234.55", null);
final NumberConverter numberConverter = new NumberConverter();
final Number convert = numberConverter.convert(Integer.class, "1,234.55", null);
Assert.assertEquals(1234, convert);
}
}