JUnit4.8.2源代码分析-6.1 排序和过滤

[2015.10补充]Sorter.apply()、Request.sortWith()和Sortable.sort()三者的确做一件事情,从不同的起点出发,注入真正排序器。读源代码时比较难受——在若干类中跑来跑去,又不熟悉它们;看多了,还好。


Runner.sort、Request.sortWith和Sorter.apply

yqj2065都快被它们搞死了。快哭了

Sorter.apply()、Request.sortWith()和Sortable.sort()三者做一件事情?为什么呢?



java.util.Comparator接口是一个策略类,定义了int compare(T o1, T o2)方法。org.junit.runner.manipulation.Sorter implements Comparator<Description>,但是Sorter仅仅是形式上的实现类。它采用代理模式,底层有一个真正的、需要构造器注入的排序器Comparator<Description> fComparator。用户通常自定义一个排序器并注入。

而且@Override public intcompare(Description o1, Description o2)以实际排序器的返回作为返回。

真正不懂的是Sorter的方法apply,这个什么时候才有用?(注:SortingRequest中用了一下)

       publicvoid apply(Object object) {
              if(object instanceof Sortable) {
                     Sortablesortable = (Sortable) object;
                     sortable.sort(this);
              }
       }

接口Sortable,一个注入接口(依赖注入的一种),定义的唯一的方法public void sort(Sortersorter)。而Sortable是一些Runner的父接口,也就是说,Sortable对象是具有排序方式运行测试的Runner。按照我目前的直观感觉,Sortable.sort(Sorter)不如叫Sortable.setSorter(Sorter),某些Runner如ParentRunner

3. Request.sortWith()

Request有方法:

       publicRequest sortWith(Comparator<Description> comparator) {

              returnnew SortingRequest(this, comparator);

       }

将this和某个自定义排序器注入,当SortingRequest.getRunner()时,对于原Request对象获得的runner,应用了

new Sorter(comparator).apply(runner);

所以,Sorter.apply()、Request.sortWith()和Sortable.sort()三者做一件事情,从不同的起点出发,注入真正排序器。

现在有实际排序器:

package myTest.sort;
import java.util.Comparator;
import org.junit.runner.Description;
public class AlphabetComparator implements Comparator<Description> {
    @Override
    public int compare(Description d1, Description d2) {
        return d1.getMethodName().compareTo(d2.getMethodName());
    }
有测试目标

package myTest.sort;
import static tool.Print.*;
import org.junit.*;//Test/Ignore
public class Unit4{
    @Test public void a() {    pln("a() method executed.");    }
    @Test @Ignore public void b() {    pln("b() method executed.");    }
    @Test public void c() {     pln("c() method executed.");
        throw new RuntimeException("Throw delibrately");
    }
    @Test public void f() {     pln("f() method executed.");    }
}
然后呢?茴香豆的茴有3种写法?在不同场合使用。

package myTest.sort;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import org.junit.runner.notification.*;
import org.junit.runner.*;
import org.junit.runner.manipulation.Sorter;
public class SortDemo {
    public static void t1() {        
        BlockJUnit4ClassRunner runner=null ;
        try {
            runner= new BlockJUnit4ClassRunner(Unit4.class);
            //runner.filter(new MethodNameFilter("testFilteredOut"));
        } catch (InitializationError e) {   }
       runner.sort(new Sorter(new AlphabetComparator()));
       runner.run(new RunNotifier());
    }

    public static void t2() {
        Request request = Request.aClass( Unit4.class );
        request = request.sortWith(new AlphabetComparator());
        Runner runner = request.getRunner();
        runner.run(new RunNotifier());
    }
    public static void t3() {
        Request request = Request.aClass( Unit4.class );
        Runner runner = request.getRunner();
        Sorter sorter=new Sorter(new AlphabetComparator());
        sorter.apply(runner);
        runner.run(new RunNotifier());
    }
    public static void go(){t1() ;t2() ;t3(); }
} 

ok





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值