关闭

使用反射+注解实现类似JUnit的效果

发表于:2016-5-03 10:50

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:littlersmall    来源:51Testing软件测试网采编

  比较特殊的是rate,用于提供一种概率性的运行方式。
  3、框架核心代码
package com.sigh.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by sigh on 2015/6/10.
*/
public class SimulationFacade {
interface RunMethod {
void run();
double getRate();
}
interface ReportMethod {
void report();
}
private static List<Object> classes = null;
private static List<RunMethod> runMethods = null;
private static List<ReportMethod> reportMethods = null;
private final static int MAX_OPERATION_TIMES = 100;
static {
classes = new ArrayList<>();
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/spring-config.xml");
Map<String, Object> beanNames = applicationContext.getBeansWithAnnotation(Simulation.class);
for (Object o : beanNames.values()) {
classes.add(o);
}
System.out.println(beanNames);
runMethods = new ArrayList<RunMethod>();
reportMethods = new ArrayList<ReportMethod>();
for (final Object o : classes) {
Method[] methods = o.getClass().getDeclaredMethods();
for (final Method method : methods) {
if (method.isAnnotationPresent(Run.class)) {
runMethods.add(new RunMethod() {
@Override
public void run() {
try {
method.invoke(o);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public double getRate() {
return method.getAnnotation(Run.class).rate();
}
});
} else if (method.isAnnotationPresent(Report.class)) {
reportMethods.add(new ReportMethod() {
@Override
public void report() {
try {
method.invoke(o);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
});
}
}
}
}
public void run() {
double rate = Math.random();
for (RunMethod method : runMethods) {
if (rate <= method.getRate()) {
method.run();
break;
} else {
rate -= method.getRate();
}
}
}
public void report() {
for (ReportMethod method : reportMethods) {
method.report();
}
}
public static class MulTiThreadSimulation {
private final static int THREAD_NUM = 10;
SimulationFacade simulationFacade = new SimulationFacade();
static AtomicInteger operationTimes = new AtomicInteger(0);
public void run() {
List<Thread> threadList = new ArrayList<Thread>();
for (int i = 0; i < THREAD_NUM; i++) {
Thread thread = new Thread(new Runnable() {
@Override public void run() {
while (operationTimes.getAndIncrement() < SimulationFacade.MAX_OPERATION_TIMES) {
try {
//仿真测试
simulationFacade.run();
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
threadList.add(thread);
}
for (Thread thread : threadList) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void report() {
simulationFacade.report();
}
}
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, InstantiationException {
MulTiThreadSimulation mulTiThreadSimulation = new MulTiThreadSimulation();
mulTiThreadSimulation.run();
mulTiThreadSimulation.report();
}
}
  基本的思路也相对比较清晰,所以也没有太多需要解释的地方。
  java的内部类确实有很多很有意思的地方,许多地方现在想来还是有些复杂。估计还需要一段时间来慢慢理解java的内存模型了。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号