fix test and sm2 bug

This commit is contained in:
Looly 2019-09-27 15:55:02 +08:00
parent 5cde137517
commit 60f3970b04
9 changed files with 122 additions and 44 deletions

View File

@ -7,10 +7,12 @@
### 新特性
* 【core】 ArrayUtil.isEmpty可变长参数改为数组issue#555@Github
* 【core】 新增Convert.toMap方法issue#I12ISI@Gitee
### Bug修复
* 【extra】 修复Mail中sslEnable无效问题pr#74@Gitee
* 【extra】 修复CsvParser中最后一行双引号没有去除的问题pr#73@Gitee
* 【crypto】 修复SM2算法在自定义密钥时无效问题issue#I12P5I@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -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

View File

@ -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 //

View File

@ -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");

View File

@ -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

View File

@ -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() {

View File

@ -123,7 +123,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
if (KeyType.PublicKey != keyType) {
throw new IllegalArgumentException("Encrypt is only support by public key");
}
ckeckKey(keyType);
checkKey(keyType);
lock.lock();
final SM2Engine engine = getEngine();
@ -149,7 +149,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
if (KeyType.PrivateKey != keyType) {
throw new IllegalArgumentException("Decrypt is only support by private key");
}
ckeckKey(keyType);
checkKey(keyType);
lock.lock();
final SM2Engine engine = getEngine();
@ -234,6 +234,28 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
}
}
@Override
public SM2 setPrivateKey(PrivateKey privateKey) {
super.setPrivateKey(privateKey);
// 重新初始化密钥参数防止重新设置密钥时导致密钥无法更新
this.privateKeyParams = null;
initCipherParams();
return this;
}
@Override
public SM2 setPublicKey(PublicKey publicKey) {
super.setPublicKey(publicKey);
// 重新初始化密钥参数防止重新设置密钥时导致密钥无法更新
this.publicKeyParams = null;
initCipherParams();
return this;
}
/**
* 设置加密类型
*
@ -250,7 +272,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
// ------------------------------------------------------------------------------------------------------------------------- Private method start
/**
* 初始化加密解密参数
* 初始化加密解密参数包括私钥和公钥参数
*
* @return this
*/
@ -291,7 +313,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
*
* @param keyType key类型
*/
private void ckeckKey(KeyType keyType) {
private void checkKey(KeyType keyType) {
switch (keyType) {
case PublicKey:
if (null == this.publicKey) {

View File

@ -154,8 +154,8 @@ public class SM2Engine {
* @author looly
*
*/
public static enum SM2Mode {
C1C2C3, C1C3C2;
public enum SM2Mode {
C1C2C3, C1C3C2
}
protected ECMultiplier createBasePointMultiplier() {
@ -348,8 +348,7 @@ public class SM2Engine {
/**
* 增加字段节点
*
* @param digest
* @param v
* @param v 节点
*/
private void addFieldElement(ECFieldElement v) {
final byte[] p = BigIntegers.asUnsignedByteArray(this.curveLength, v.toBigInteger());

View File

@ -1,6 +1,7 @@
package cn.hutool.crypto.test;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import org.junit.Assert;
@ -87,17 +88,27 @@ public class SM2Test {
@Test
public void sm2Base64Test() {
String textBase = "我是一段特别长的测试";
String text = "";
StringBuilder text = new StringBuilder();
for (int i = 0; i < 100; i++) {
text += textBase;
text.append(textBase);
}
final SM2 sm2 = new SM2();
SM2 sm2 = new SM2();
// 公钥加密私钥解密
String encryptStr = sm2.encryptBase64(text, KeyType.PublicKey);
String encryptStr = sm2.encryptBase64(text.toString(), KeyType.PublicKey);
String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
Assert.assertEquals(text, decryptStr);
Assert.assertEquals(text.toString(), decryptStr);
// 测试自定义密钥后是否生效
PrivateKey privateKey = sm2.getPrivateKey();
PublicKey publicKey = sm2.getPublicKey();
sm2 = SmUtil.sm2();
sm2.setPrivateKey(privateKey);
sm2.setPublicKey(publicKey);
String decryptStr2 = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
Assert.assertEquals(text.toString(), decryptStr2);
}
@Test