Skip to content

Commit 400d59d

Browse files
committed
读取excel元数转换器测试用例
1 parent 8630e32 commit 400d59d

File tree

9 files changed

+331
-0
lines changed

9 files changed

+331
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package online.dinghuiye.core.resolution.convert;
2+
3+
import online.dinghuiye.api.resolution.convert.Convertor;
4+
import online.dinghuiye.core.annotation.convert.BlankToNull;
5+
import online.dinghuiye.core.annotation.convert.DateFormat;
6+
import online.dinghuiye.core.annotation.convert.ValueMap;
7+
import online.dinghuiye.core.consts.Consts;
8+
import online.dinghuiye.core.resolution.convert.testcase.ListValueConvertor;
9+
import online.dinghuiye.core.resolution.convert.testcase.User;
10+
import online.dinghuiye.core.resolution.convert.testcase.Util;
11+
import org.junit.Assert;
12+
import org.junit.Test;
13+
14+
import java.lang.reflect.Field;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.text.ParseException;
17+
import java.text.SimpleDateFormat;
18+
import java.util.ArrayList;
19+
import java.util.Date;
20+
import java.util.List;
21+
import java.util.SimpleTimeZone;
22+
23+
/**
24+
* Created by Strangeen on 2017/7/2.
25+
*/
26+
public class TestConvertor {
27+
28+
@Test
29+
public void testConvert() throws NoSuchFieldException, ParseException {
30+
31+
Field field;
32+
List<Convertor> convertorList;
33+
Object obj;
34+
35+
// @ValueConver多重Convert注解
36+
// @BlankToNull
37+
field = User.class.getDeclaredField("name");
38+
convertorList = ConvertFactory.getConvertors(field);
39+
obj = " ";
40+
for (Convertor c : convertorList)
41+
obj = c.convert(obj, field, null);
42+
Assert.assertEquals(null, obj);
43+
44+
// @ValueMap
45+
field = User.class.getDeclaredField("sex");
46+
convertorList = ConvertFactory.getConvertors(field);
47+
obj = 0;
48+
for (Convertor c : convertorList)
49+
obj = c.convert(obj, field, null);
50+
Assert.assertEquals("female", obj);
51+
52+
// @DateFormat
53+
field = User.class.getDeclaredField("birthday");
54+
convertorList = ConvertFactory.getConvertors(field);
55+
obj = "1988-08-21 10:10:10";
56+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
57+
Date date = sdf.parse(obj.toString());
58+
for (Convertor c : convertorList)
59+
obj = c.convert(obj, field, null);
60+
Assert.assertEquals(date, obj);
61+
62+
// @ValueConver自定义Convertor
63+
field = User.class.getDeclaredField("scoreList");
64+
convertorList = ConvertFactory.getConvertors(field);
65+
obj = "15,16,17,18";
66+
for (Convertor c : convertorList)
67+
obj = c.convert(obj, field, null);
68+
List<Integer> list = new ArrayList<>();
69+
list.add(15);
70+
list.add(16);
71+
list.add(17);
72+
list.add(18);
73+
Assert.assertEquals(true, Util.listEquals(list, (List<?>) obj,
74+
new ListValueConvertor() {
75+
@Override
76+
public Object change(Object obj) {
77+
return obj;
78+
}
79+
})
80+
);
81+
}
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package online.dinghuiye.core.resolution.convert;
2+
3+
import online.dinghuiye.api.resolution.convert.Convertor;
4+
import online.dinghuiye.core.consts.Consts;
5+
import online.dinghuiye.core.resolution.convert.testcase.*;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.lang.reflect.Field;
10+
import java.util.*;
11+
12+
/**
13+
* Created by Strangeen on 2017/7/1.
14+
*/
15+
public class TestConvertorFactory {
16+
17+
18+
@Test
19+
public void testGetConvertors() throws NoSuchFieldException {
20+
21+
Field field = User.class.getDeclaredField("sex");
22+
List<Convertor> convertorList = ConvertFactory.getConvertors(field);
23+
24+
List<String> convertorClazzNameList = new ArrayList<>();
25+
convertorClazzNameList.add(Consts.CONVERTOR_PACKAGE + "ValueMapConvertor");
26+
convertorClazzNameList.add(Consts.CONVERTOR_PACKAGE + "BlankToNullConvertor");
27+
Assert.assertEquals(true, Util.listEquals(convertorList, convertorClazzNameList,
28+
new ListValueConvertor() {
29+
@Override
30+
public Object change(Object obj) {
31+
return ((Convertor) obj).getClass().getName();
32+
}
33+
})
34+
);
35+
}
36+
37+
@Test
38+
public void testConvertToType() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
39+
40+
// 基本类型
41+
Assert.assertEquals(Byte.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("b"), "1").getClass());
42+
Assert.assertEquals(Character.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("c"), "1").getClass());
43+
Assert.assertEquals(Short.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("s"), "1").getClass());
44+
Assert.assertEquals(Integer.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("i"), "1").getClass());
45+
Assert.assertEquals(Long.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("l"), "1").getClass());
46+
Assert.assertEquals(Float.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("f"), "1").getClass());
47+
Assert.assertEquals(Double.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("d"), "1").getClass());
48+
Assert.assertEquals(Boolean.class, ConvertFactory.convertToType(TestClazz0.class.getDeclaredField("bl"), "true").getClass());
49+
50+
// 包装类型
51+
Field[] fields = TestClazz1.class.getDeclaredFields();
52+
for (Field field : fields) {
53+
Assert.assertEquals(field.getType(), ConvertFactory.convertToType(field, "1").getClass());
54+
}
55+
56+
// 集合,Map类型,自定义类型
57+
Assert.assertEquals(int[].class, ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("is"), new int[0]).getClass());
58+
Assert.assertEquals(Integer[].class, ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("iObjs"), new Integer[0]).getClass());
59+
Assert.assertEquals(User[].class, ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("users"), new User[0]).getClass());
60+
61+
// 无法检测集合泛型,实际为List<Integer>
62+
Assert.assertEquals(true, List.class.isAssignableFrom(
63+
ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("iList"),new ArrayList<Integer>()).getClass()));
64+
Assert.assertEquals(true, List.class.isAssignableFrom(
65+
ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("iList"), new ArrayList<Long>()).getClass()));
66+
// 无法检测集合泛型,实际为Set<Integer>
67+
Assert.assertEquals(true, Set.class.isAssignableFrom(
68+
ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("iSet"), new HashSet<Integer>()).getClass()));
69+
Assert.assertEquals(true, Set.class.isAssignableFrom(
70+
ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("iSet"), new HashSet<Long>()).getClass()));
71+
// 无法检测集合泛型
72+
Assert.assertEquals(true, Map.class.isAssignableFrom(
73+
ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("map"), new HashMap<Integer, String>()).getClass()));
74+
Assert.assertEquals(true, User.class.isAssignableFrom(
75+
ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("user"), new User()).getClass()));
76+
// 无法检测集合泛型
77+
Assert.assertEquals(true, List.class.isAssignableFrom(
78+
ConvertFactory.convertToType(TestClazz2.class.getDeclaredField("userList"), new ArrayList<Integer>()).getClass()));
79+
80+
}
81+
}
82+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package online.dinghuiye.core.resolution.convert.testcase;
2+
3+
/**
4+
* Created by Strangeen on 2017/7/2.
5+
*/ // callback接口
6+
public interface ListValueConvertor {
7+
Object change(Object obj);
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package online.dinghuiye.core.resolution.convert.testcase;
2+
3+
import online.dinghuiye.api.resolution.convert.Convertor;
4+
5+
import java.lang.reflect.Field;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
/**
12+
* Created by Strangeen on 2017/7/2.
13+
*/
14+
public class ScoreConvertor implements Convertor {
15+
16+
@Override
17+
public Object convert(Object obj, Field field, Map<String, String> excelRecordMap) {
18+
if (obj instanceof String) {
19+
20+
String str = obj.toString();
21+
List<Integer> scoreList = new ArrayList<>();
22+
for (String s : str.split(",")) {
23+
scoreList.add(Integer.valueOf(s));
24+
}
25+
return scoreList;
26+
27+
} else {
28+
throw new RuntimeException("解析错误,field:" + field.getName());
29+
}
30+
}
31+
32+
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package online.dinghuiye.core.resolution.convert.testcase;
2+
3+
/**
4+
* Created by Strangeen on 2017/7/2.
5+
*/ // 测试类转换型类
6+
public class TestClazz0 {
7+
byte b;
8+
short s;
9+
char c;
10+
int i;
11+
long l;
12+
float f;
13+
double d;
14+
boolean bl;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package online.dinghuiye.core.resolution.convert.testcase;
2+
3+
/**
4+
* Created by Strangeen on 2017/7/2.
5+
*/ // 测试类转换型类
6+
public class TestClazz1 {
7+
Byte bObj;
8+
Short sObj;
9+
Character cObj;
10+
Integer iObj;
11+
Long lObj;
12+
Float fObj;
13+
Double dObj;
14+
Boolean blObj;
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package online.dinghuiye.core.resolution.convert.testcase;
2+
3+
import online.dinghuiye.core.resolution.convert.testcase.User;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
/**
10+
* Created by Strangeen on 2017/7/2.
11+
*/ // 测试类转换型类
12+
public class TestClazz2 {
13+
int[] is;
14+
Integer[] iObjs;
15+
User[] users;
16+
17+
List<Integer> iList;
18+
Set<Integer> iSet;
19+
Map<Integer, String> map;
20+
21+
User user;
22+
List<User> userList;
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package online.dinghuiye.core.resolution.convert.testcase;
2+
3+
import online.dinghuiye.core.annotation.convert.BlankToNull;
4+
import online.dinghuiye.core.annotation.convert.DateFormat;
5+
import online.dinghuiye.core.annotation.convert.ValueConvert;
6+
import online.dinghuiye.core.annotation.convert.ValueMap;
7+
import online.dinghuiye.core.resolution.convert.BlankToNullConvertor;
8+
9+
import java.util.Date;
10+
import java.util.List;
11+
12+
/**
13+
* Created by Strangeen on 2017/7/2.
14+
*/ // 测试类
15+
public class User {
16+
17+
@ValueConvert(value = BlankToNullConvertor.class)
18+
@BlankToNull
19+
private String name;
20+
@ValueMap(value = "{1:'male',0:'female'}")
21+
@BlankToNull
22+
private String sex;
23+
@DateFormat(value = "yyyy-MM-dd HH:mm:ss")
24+
// @DateFormat(value = "")
25+
private Date birthday;
26+
@ValueConvert(value = ScoreConvertor.class)
27+
List<Integer> scoreList;
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getSex() {
38+
return sex;
39+
}
40+
41+
public void setSex(String sex) {
42+
this.sex = sex;
43+
}
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package online.dinghuiye.core.resolution.convert.testcase;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by Strangeen on 2017/7/2.
7+
*/
8+
public class Util {
9+
10+
// list集合比较
11+
public static <T1,T2> boolean listEquals(List<T1> list1, List<T2> list2, ListValueConvertor convertor) {
12+
if (list1 == list2) return true;
13+
if (list1.size() != list2.size()) return false;
14+
boolean isEqual = true;
15+
for (int i = 0; i < list1.size(); i++) {
16+
T1 o1 = list1.get(i);
17+
T2 o2 = list2.get(i);
18+
if (o1 == o2) continue; // null
19+
if (o1 != null && o1.equals(o2)) continue;
20+
Object oc1 = convertor.change(o1);
21+
//Object oc2 = convertor.change(o2);
22+
if (oc1 == o2) continue; // null
23+
if (oc1 != null && oc1.equals(o2)) continue;
24+
isEqual = false;
25+
break;
26+
}
27+
return isEqual;
28+
}
29+
}

0 commit comments

Comments
 (0)