利用反射制作自动化测试工具

该篇文件利用反射模仿spring框架读取xml文件构造对象,后续有时间会给代码加上注释。

student.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<student class="com.gxnu.bigdata.study.bean.Student">
<name value="zhangsan" type="java.lang.String"></name>
<height>
<type>double</type>
<value>168</value>
</height>
<sex>
<type>boolean</type>
<value>true</value>
</sex>
</student>

Student.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class  {
private String name;
private double height;
private boolean sex;
public () {
super();
}
public (String name, double height, boolean sex) {
super();
this.name = name;
this.height = height;
this.sex = sex;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {

return height;
}
public void setHeight(double height) {
System.out.println("执行setHeight");
this.height = height;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Student [name=").append(name).append(", height=").append(height).append(", sex=").append(sex)
.append("]");
return builder.toString();
}

}

BeanProperty.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class BeanProperty {

private Class clazz;

private Map<String, Map.Entry<String, String>> map;

{
map = new HashMap<String, Map.Entry<String,String>>();
}

public BeanProperty() {
super();
}

public BeanProperty(Class clazz, Map<String, Entry<String, String>> map) {
super();
this.clazz = clazz;
this.map = map;
}

public Class getClazz() {
return clazz;
}

public void setClazz(Class clazz) {
this.clazz = clazz;
}

public Map<String, Map.Entry<String, String>> getMap() {
return map;
}

public void setMap(Map<String, Map.Entry<String, String>> map) {
this.map = map;
}


public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("BeanProperty [clazz=").append(clazz).append(", map=").append(map).append("]");
return builder.toString();
}

}

XmlEx.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
public class XmlEx {

public static Document getDocument(String xml) throws Exception{

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder ndb = dbf.newDocumentBuilder();

InputStream ras = XmlEx.class.getResourceAsStream(xml);


Document document = ndb.parse(ras);

return document;
}

public static BeanProperty initBeanProperty() throws Exception {

BeanProperty beanProperty = new BeanProperty();

Document document = getDocument("/student.xml");

Element item = (Element) document.getChildNodes().item(0);
String classValue = item.getAttribute("class");
Class clazz = Class.forName(classValue);
beanProperty.setClazz(clazz);

Map<String, Entry<String, String>> map = initMap(item);
beanProperty.setMap(map);

return beanProperty;
}


public static Map<String, Map.Entry<String, String>> initMap(Element item){
Map<String, Map.Entry<String, String>> map = new HashMap<>();
NodeList nodeList = item.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node temp = nodeList.item(i);
if(temp.getNodeType()==Node.ELEMENT_NODE){
String startName = temp.getNodeName();
Element temp2 = (Element) temp;
String type = getTagValue(temp2, "type");
String value = getTagValue(temp2,"value");
map.put(startName, new SimpleEntry(type,value));
}
}
return map;
}

public static String getTagValue(Element temp2,String str) {
String valueOrType = temp2.getAttribute(str);
if (valueOrType==null||"".equals(valueOrType.trim())) {
Node itemValue = temp2.getElementsByTagName(str).item(0);
valueOrType = itemValue.getTextContent();

}
return valueOrType;
}

public static Student run() throws Exception{
BeanProperty beanProperty = initBeanProperty();
//BeanProperty beanProperty = new BeanProperty();
Class clazz = beanProperty.getClazz();
Object object = clazz.newInstance();

execute(clazz,object,beanProperty.getMap());

return (Student) object;
}

public static void execute(Class clazz,Object object,Map<String, Entry<String, String>> map){
map.forEach((name,str)->{
String value = str.getValue();
String type = str.getKey();

Class ptyClass = stringToClass(type);
Object val = stringToObject(ptyClass,value);

String methodName = getMethodName(name);

try {
Method method = clazz.getMethod(methodName, ptyClass);
method.invoke(object, val);
} catch (Exception e) {
e.printStackTrace();
}

});
}

public static Object stringToObject(Class cladd,String value){
Object obj=null;
if(cladd==String.class){
obj=value;
}else if(cladd==boolean.class || cladd==Boolean.class){
obj=Boolean.valueOf(value);
}else if(cladd==char.class){
obj=value.charAt(0);
}else if(cladd==double.class){
obj=Double.valueOf(value);
}else if(cladd==float.class){
obj=Float.valueOf(value);
}else if(cladd==byte.class){
obj=Byte.valueOf(value);
}else if(cladd==short.class){
obj=Short.valueOf(value);
}else if(cladd==int.class){
obj=Integer.valueOf(value);
}else if(cladd==long.class){
obj=Long.valueOf(value);
}

return obj;
}

public static Class stringToClass(String str){
Class c=null;
switch(str){
case "char":
c=char.class;
break;
case "boolean":
c=boolean.class;
break;
case "double":
c=double.class;
break;

case "float":
c=float.class;
break;
case "int":
c=int.class;
break;

case "byte":
c=byte.class;
break;
case "short":
c=short.class;
break;
case "long":
c=long.class;
break;
default:
try {
c=Class.forName(str);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
}
return c;
}

public static String getMethodName(String startName){
return "set"+StringUtils.capitalize(startName);
}

@Test
public void test() throws Exception{
//getDocument("/student.xml");
//initBeanProperty();
Student student = run();
System.out.println(student.toString());
}
}

limaodeng

scribble