package com.cfpamf.ms.insur.biz;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
@RunWith(SpringRunner.class)
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BeanAutoTest {
@Test
public void makeSonarHappy() throws ClassNotFoundException, SecurityException, IllegalArgumentException, URISyntaxException {
Class<?> clz = com.cfpamf.ms.insur.biz.BootstrapApplication.class;
URL url = clz.getResource("/");
String rootPath = url.getPath();
rootPath = rootPath.substring(0, rootPath.indexOf("target") + 6) + "/classes/com/cfpamf/ms/insur/biz/pojo";
File file = new File(rootPath);
Stack<File> stack = new Stack<>();
stack.addAll(Arrays.asList(file));
List<File> clazzs = new ArrayList<>();
while (!stack.isEmpty()) {
File f = stack.pop();
if (f.isDirectory()) {
File[] fs = f.listFiles();
stack.addAll(Arrays.asList(fs));
} else {
clazzs.add(f);
}
}
int success = 0;
for (int i = 0, len = clazzs.size(); i < len; i++) {
File cl = clazzs.get(i);
String path = cl.getPath();
path = path.substring(path.indexOf("com")).replaceAll("\\\\", ".");
path = path.substring(path.indexOf("com")).replaceAll("/", ".");
String className = path.substring(0, path.length() - 6);
try {
Class clazz = Class.forName(className);
testClazz(clazz);
success++;
} catch (Exception e) {
System.out.println("fail auto test className=" + className);
}
}
System.out.println("Auto success/complete=" + success + "/" + clazzs.size());
}
private void testClazz(Class clz) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Object object1 = clz.newInstance();
Object object2 = clz.newInstance();
String name = "";
String type = "";
for (Field field : clz.getDeclaredFields()) {
try {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
field.setAccessible(true);
name = StringUtils.capitalize(field.getName());
type = field.getGenericType().toString();
if (name.equals("$jacocoData") || type.equals("interface org.slf4j.Logger")) {
continue;
}
Class<?> clz2 = field.getType();
Method setMethod = clz.getDeclaredMethod("set" + name, clz2);
String getMethodName = "get" + name;
if (type.equals("boolean")) {
getMethodName = "is" + name;
}
Method getMethod = clz.getDeclaredMethod(getMethodName);
setMethod.invoke(object1, getMethod.invoke(object1));
if (!clz2.isPrimitive()) {
setMethod.invoke(object1, clz2.newInstance());
getMethod.invoke(object1);
setMethod.invoke(object2, clz2.newInstance());
getMethod.invoke(object2);
}
} catch (Exception e) {
}
}
try {
Object object3 = clz.newInstance();
Object object4 = clz.newInstance();
Method equals = clz.getDeclaredMethod("equals", Object.class);
if (equals != null) {
equals.invoke(object1, object1);
equals.invoke(object1, new Object());
equals.invoke(object1, object2);
equals.invoke(object1, object3);
equals.invoke(object3, object4);
}
Method hashCode = clz.getDeclaredMethod("hashCode");
if (hashCode != null) {
hashCode.invoke(object1);
hashCode.invoke(object3);
}
Method toString = clz.getDeclaredMethod("toString");
if (toString != null) {
toString.invoke(object1);
}
Method canEqual = clz.getDeclaredMethod("canEqual", Object.class);
if (canEqual != null) {
canEqual.setAccessible(true);
canEqual.invoke(object1, clz.newInstance());
}
} catch (Exception ex) {
}
}
}