mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix test and sm2 bug
This commit is contained in:
@@ -4,15 +4,12 @@ import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import cn.hutool.core.convert.impl.CollectionConverter;
|
||||
import cn.hutool.core.convert.impl.GenericEnumConverter;
|
||||
import cn.hutool.core.convert.impl.MapConverter;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.text.UnicodeUtil;
|
||||
@@ -525,6 +522,20 @@ public class Convert {
|
||||
public static <T> List<T> toList(Class<T> elementType, Object value) {
|
||||
return (List<T>) toCollection(ArrayList.class, elementType, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Map
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @param value 被转换的值
|
||||
* @return {@link Map}
|
||||
* @since 4.6.8
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> valueType, Object value) {
|
||||
return (Map<K, V>) new MapConverter(HashMap.class, keyType, valueType).convert(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换值为指定类型,类型采用字符串表示
|
||||
@@ -662,7 +673,7 @@ public class Convert {
|
||||
* @return 全角字符串.
|
||||
*/
|
||||
public static String toSBC(String input, Set<Character> notConvertSet) {
|
||||
char c[] = input.toCharArray();
|
||||
final char[] c = input.toCharArray();
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (null != notConvertSet && notConvertSet.contains(c[i])) {
|
||||
// 跳过不替换的字符
|
||||
@@ -700,7 +711,7 @@ public class Convert {
|
||||
if(StrUtil.isBlank(text)) {
|
||||
return text;
|
||||
}
|
||||
char c[] = text.toCharArray();
|
||||
final char[] c = text.toCharArray();
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (null != notConvertSet && notConvertSet.contains(c[i])) {
|
||||
// 跳过不替换的字符
|
||||
@@ -714,9 +725,8 @@ public class Convert {
|
||||
c[i] = (char) (c[i] - 65248);
|
||||
}
|
||||
}
|
||||
String returnString = new String(c);
|
||||
|
||||
return returnString;
|
||||
return new String(c);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------- hex
|
||||
|
@@ -52,7 +52,7 @@ public class MapConverter extends AbstractConverter<Map<?, ?>> {
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
protected Map<?, ?> convertInternal(Object value) {
|
||||
Map map = null;
|
||||
final Map map;
|
||||
if (value instanceof Map) {
|
||||
final Type[] typeArguments = TypeUtil.getTypeArguments(value.getClass());
|
||||
if (null != typeArguments //
|
||||
|
@@ -1,5 +1,12 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.bean.copier.ValueProvider;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
@@ -7,15 +14,6 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.bean.copier.ValueProvider;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
|
||||
/**
|
||||
* Bean工具单元测试
|
||||
*
|
||||
@@ -61,7 +59,7 @@ public class BeanUtilTest {
|
||||
|
||||
@Test
|
||||
public void fillBeanWithMapIgnoreCaseTest() {
|
||||
HashMap<String, Object> map = CollectionUtil.newHashMap();
|
||||
HashMap<String, Object> map = CollUtil.newHashMap();
|
||||
map.put("Name", "Joe");
|
||||
map.put("aGe", 12);
|
||||
map.put("openId", "DFDFSDFWERWER");
|
||||
@@ -73,7 +71,7 @@ public class BeanUtilTest {
|
||||
|
||||
@Test
|
||||
public void mapToBeanIgnoreCaseTest() {
|
||||
HashMap<String, Object> map = CollectionUtil.newHashMap();
|
||||
HashMap<String, Object> map = CollUtil.newHashMap();
|
||||
map.put("Name", "Joe");
|
||||
map.put("aGe", 12);
|
||||
|
||||
@@ -84,11 +82,11 @@ public class BeanUtilTest {
|
||||
|
||||
@Test
|
||||
public void mapToBeanTest() {
|
||||
HashMap<String, Object> map = CollectionUtil.newHashMap();
|
||||
HashMap<String, Object> map = CollUtil.newHashMap();
|
||||
map.put("a_name", "Joe");
|
||||
map.put("b_age", 12);
|
||||
|
||||
// 别名
|
||||
// 别名,用于对应bean的字段名
|
||||
HashMap<String, String> mapping = CollUtil.newHashMap();
|
||||
mapping.put("a_name", "name");
|
||||
mapping.put("b_age", "age");
|
||||
|
@@ -2,7 +2,10 @@ package cn.hutool.core.convert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -54,28 +57,28 @@ public class ConvertTest {
|
||||
Integer aInteger = Convert.toInt(a);
|
||||
Assert.assertEquals(Integer.valueOf(34232), aInteger);
|
||||
int aInt = ConverterRegistry.getInstance().convert(int.class, a);
|
||||
Assert.assertTrue(34232 == aInt);
|
||||
Assert.assertEquals(34232, aInt);
|
||||
|
||||
// 带小数测试
|
||||
String b = " 34232.00";
|
||||
Integer bInteger = Convert.toInt(b);
|
||||
Assert.assertEquals(Integer.valueOf(34232), bInteger);
|
||||
int bInt = ConverterRegistry.getInstance().convert(int.class, b);
|
||||
Assert.assertTrue(34232 == bInt);
|
||||
Assert.assertEquals(34232, bInt);
|
||||
|
||||
// boolean测试
|
||||
boolean c = true;
|
||||
Integer cInteger = Convert.toInt(c);
|
||||
Assert.assertEquals(Integer.valueOf(1), cInteger);
|
||||
int cInt = ConverterRegistry.getInstance().convert(int.class, c);
|
||||
Assert.assertTrue(1 == cInt);
|
||||
Assert.assertEquals(1, cInt);
|
||||
|
||||
// boolean测试
|
||||
String d = "08";
|
||||
Integer dInteger = Convert.toInt(d);
|
||||
Assert.assertEquals(Integer.valueOf(8), dInteger);
|
||||
int dInt = ConverterRegistry.getInstance().convert(int.class, d);
|
||||
Assert.assertTrue(8 == dInt);
|
||||
Assert.assertEquals(8, dInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,28 +94,28 @@ public class ConvertTest {
|
||||
Long aLong = Convert.toLong(a);
|
||||
Assert.assertEquals(Long.valueOf(342324545435435L), aLong);
|
||||
long aLong2 = ConverterRegistry.getInstance().convert(long.class, a);
|
||||
Assert.assertTrue(342324545435435L == aLong2);
|
||||
Assert.assertEquals(342324545435435L, aLong2);
|
||||
|
||||
// 带小数测试
|
||||
String b = " 342324545435435.245435435";
|
||||
Long bLong = Convert.toLong(b);
|
||||
Assert.assertEquals(Long.valueOf(342324545435435L), bLong);
|
||||
long bLong2 = ConverterRegistry.getInstance().convert(long.class, b);
|
||||
Assert.assertTrue(342324545435435L == bLong2);
|
||||
Assert.assertEquals(342324545435435L, bLong2);
|
||||
|
||||
// boolean测试
|
||||
boolean c = true;
|
||||
Long cLong = Convert.toLong(c);
|
||||
Assert.assertEquals(Long.valueOf(1), cLong);
|
||||
long cLong2 = ConverterRegistry.getInstance().convert(long.class, c);
|
||||
Assert.assertTrue(1 == cLong2);
|
||||
Assert.assertEquals(1, cLong2);
|
||||
|
||||
// boolean测试
|
||||
String d = "08";
|
||||
Long dLong = Convert.toLong(d);
|
||||
Assert.assertEquals(Long.valueOf(8), dLong);
|
||||
long dLong2 = ConverterRegistry.getInstance().convert(long.class, d);
|
||||
Assert.assertTrue(8 == dLong2);
|
||||
Assert.assertEquals(8, dLong2);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -1,8 +1,10 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -30,6 +32,37 @@ public class ConvertToBeanTest {
|
||||
Assert.assertEquals(map.get("age"), 14);
|
||||
Assert.assertEquals("11213232", map.get("openid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanToMapTest2() {
|
||||
SubPerson person = new SubPerson();
|
||||
person.setAge(14);
|
||||
person.setOpenid("11213232");
|
||||
person.setName("测试A11");
|
||||
person.setSubName("sub名字");
|
||||
|
||||
Map<String, String> map = Convert.toMap(String.class, String.class, person);
|
||||
Assert.assertEquals(map.get("name"), "测试A11");
|
||||
Assert.assertEquals(map.get("age"), 14);
|
||||
Assert.assertEquals("11213232", map.get("openid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapToMapTest() {
|
||||
LinkedHashMap<String, Integer> map1 = new LinkedHashMap<>();
|
||||
map1.put("key1", 1);
|
||||
map1.put("key2", 2);
|
||||
map1.put("key3", 3);
|
||||
map1.put("key4", 4);
|
||||
|
||||
Map<String, String> map2 = Convert.toMap(String.class, String.class, map1);
|
||||
Console.log(map2);
|
||||
|
||||
Assert.assertEquals("1", map2.get("key1"));
|
||||
Assert.assertEquals("2", map2.get("key2"));
|
||||
Assert.assertEquals("3", map2.get("key3"));
|
||||
Assert.assertEquals("4", map2.get("key4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapToBeanTest() {
|
||||
|
Reference in New Issue
Block a user