使用反射+注解實現(xiàn)類似JUnit的效果

一直好奇JUnit里面@Test的實現(xiàn),搜了不少網(wǎng)頁,大部分都是講的使用方式,很少有人去講解實現(xiàn)原理。自己看源碼看了半天也沒有找到頭緒。
索性自己摸索著實現(xiàn)了一套類似的效果,下一步準備應用在已經(jīng)完成的仿真測試上,雖然很初級,但也算一個框架了。
1 定義注解

//Simulation.java
package com.sigh.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by sigh on 2015/6/10.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE})
public @interface Simulation {
}
//Run.java
package com.sigh.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Created by sigh on 2015/6/10.
 */
//類似junit的@Test效果
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Run {
    //該操作發(fā)生的概率
    double rate() default 0;
}
//Report.java
package com.sigh.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Created by sigh on 2015/6/10.
 */
//在所有@Run運行完之后報告結(jié)果
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD})
public @interface Report {
}

這三個注解就是對外提供的全部接口了。

2 測試類

//First.java
package com.sigh.test;
import org.springframework.stereotype.Service;
/**
* Created by sigh on 2015/6/9.
*/
@Simulation
@Service
public class First {
   @Run(rate = 0.5)
   boolean run() {
       System.out.println("first");

       return true;
   }
   @Report
   void report() {
       System.out.println("report first");
   }
}
//Second.java
package com.sigh.test;
import org.springframework.stereotype.Service;
/**
 * Created by sigh on 2015/6/9.
 */
@Simulation
@Service
public class Second {
    @Run(rate = 0.5)
    long doWork() {
        System.out.println("second");

        return 3;
    }
    @Report
    void display() {
        System.out.println("display second");
    }
}

比較特殊的是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的內(nèi)部類確實有很多很有意思的地方,許多地方現(xiàn)在想來還是有些復雜。估計還需要一段時間來慢慢理解java的內(nèi)存模型了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容