1.实体类get、set方法ut覆盖率工具:
package com.test;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class TestUtils {
private static Map<String, Object> baseMap = new HashMap<String, Object>();
static {
baseMap.put("int", 1);
baseMap.put("boolean", true);
baseMap.put("byte", (byte) 1);
baseMap.put("double", (double) 1);
baseMap.put("float", 1.0F);
baseMap.put("long", 1L);
baseMap.put("short", (short) 1);
}
public static void TestPojo(Class<?> cls) throws IllegalAccessException, InstantiationException {
Field[] fields = cls.getDeclaredFields();
Method[] methods = cls.getDeclaredMethods();
Object obj = cls.newInstance();
Arrays.stream(methods).forEach(method -> {
if ("readObject".equals(method.getName())) {
doReadObjcetTest(obj);
return;
}
Arrays.stream(fields).forEach(field -> {
if (method.getName().toLowerCase().contains(field.getName().toLowerCase())) {
if (method.getName().startsWith("get")) {
doGetTest(obj, method);
} else if (method.getName().startsWith("set")) {
doSetTest(obj, method);
}
}
}
);
});
}
private static void doGetTest(Object obj, Method method) {
try {
method.invoke(obj);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
private static void doSetTest(Object obj, Method method) {
Class<?>[] paramClss = method.getParameterTypes();
if (paramClss != null && paramClss.length > 0) {
Object param = getParamByClass(paramClss[0]);
try {
method.invoke(obj, param);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
}
private static void doReadObjcetTest(Object obj) {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
oos = new ObjectOutputStream(byteArrayOutputStream);
oos.writeObject(obj);
byte[] data = byteArrayOutputStream.toByteArray();
ois = new ObjectInputStream(new ByteArrayInputStream(data));
obj = ois.readObject();
data = null;
} catch (Exception e) {
} finally {
closeStream(oos);
closeStream(ois);
}
}
private static Object getParamByClass(Class<?> paramClss) {
return (baseMap.containsKey(paramClss.getName())) ? baseMap.get(paramClss.getName()) : null;
}
private static <T extends Closeable> void closeStream(T t) {
try {
if (t != null) {
t.close();
}
} catch (Exception e) {
}
}
}
package com.test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class ClassUtil {
public static List<Class<?>> getClasses(String packageName)
throws ClassNotFoundException, IOException {
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
String path = packageName.replace(".", "/");
Enumeration<URL> resources = classLoader.getResources(path);
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
File file = new File(resource.getFile());
classes.addAll(findClasses(file, packageName));
}
return classes;
}
private static List<Class<?>> findClasses(File directory, String packageName)
throws ClassNotFoundException {
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
if (!directory.exists()) {
return classes;
}
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
assert !file.getName().contains(".");
classes.addAll(findClasses(file,
packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(packageName
+ "."
+ file.getName().substring(0,
file.getName().length() - 6)));
}
}
return classes;
}
}
package com.test;
import java.io.IOException;
import java.util.List;
public class MainApp {
public static void main(String[] args) {
try {
List<Class<?>> classes = ClassUtil.getClasses("com.test.entity");
classes.forEach(cls->{
try {
TestUtils.TestPojo(cls);
} catch (IllegalAccessException e) {
} catch (InstantiationException e) {
}
});
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}