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不过形式上的实现类。通过构造器注入的方式Sorter(Comparator<Description>comparator),初始化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。应该有两个域SortStyle和FilterStyle。而不是implements它们。这样更easy理解?

如今有实际排序器:

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(); }
}

先记录一下。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

JUnit4.8.2来源分析-6.1 排序和过滤的更多相关文章

  1. JUnit4.8.2来源分析-2 org.junit.runner.Request

    JUnit4.8.2源代码,最为yqj2065兴趣是org.junit.runner.Request,现在是几点意味着它? ①封装JUnit的输入 JUnit4作为信息处理单元,它的输入是单元測试类- ...

  2. 【JUnit4.10源码分析】6.1 排序和过滤

    abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤. (尽管JUn ...

  3. 【JUnit4.10来源分析】6 Runner

    org.junit.runner.Runner它是JUnit作业引擎.它在许多类型的支持下的.处理试验和生产(Description).Failure和Result和其它输出. Runner参见图主类 ...

  4. 【JUnit4.10来源分析】0导航

    JUnit靠GOF 中的一个Erich Gamma和 Kent Beck 单元测试框架编写一个开源,yqj2065分析JUnit的主要目的是源 中学习对设计模式的运用. JUnit也是一个学习Java ...

  5. 常见排序算法总结分析之选择排序与归并排序-C#实现

    本篇文章对选择排序中的简单选择排序与堆排序,以及常用的归并排序做一个总结分析. 常见排序算法总结分析之交换排序与插入排序-C#实现是排序算法总结系列的首篇文章,包含了一些概念的介绍以及交换排序(冒泡与 ...

  6. [Android分享] 【转帖】Android ListView的A-Z字母排序和过滤搜索功能

      感谢eoe社区的分享   最近看关于Android实现ListView的功能问题,一直都是小伙伴们关心探讨的Android开发问题之一,今天看到有关ListView实现A-Z字母排序和过滤搜索功能 ...

  7. 让Asp.Net WebAPI支持OData查询,排序,过滤。

    让Asp.Net WebAPI支持OData后,就能支持在url中直接输入排序,过滤条件了. 一.创建Asp.Net WebAPI项目: 二.使用NuGet安装Asp.Net WebAPI 2.2和O ...

  8. Contoso 大学 - 3 - 排序、过滤及分页

    原文 Contoso 大学 - 3 - 排序.过滤及分页 目录 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 原文地址:http://www.asp.net/mvc/ ...

  9. Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音

    转载:http://blog.csdn.net/xiaanming/article/details/12684155 转载请注明出处:http://blog.csdn.net/xiaanming/ar ...

随机推荐

  1. 2007LA 3902 网络(树+贪心)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=283&am ...

  2. 让Delphi XE2程序支持UAC

    在win7下,开发的程序有的时候莫名其妙就不能正常工作了,其实都是因为权限不够,要想能够正常运行,就需要获得管理员权限,这就需要处理UAC.具体方法如下: 一,制作“uac.manifest”文件.新 ...

  3. Oracle Autonomous Transactions(自治事务)

    Oracle Autonomous Transactions Autonomous transactions allow you to leave the context of the calling ...

  4. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  5. BUG: scheduling while atomic: events/0/4/总结

    对于Linux内核来说,Oops就意外着内核出了异常,此时会将产生异常时CPU的状态,出错的指令地址.数据地址及其他寄存器,函数调用的顺序甚至是栈里面的内容都打印出来,然后根据异常的严重程度来决定下一 ...

  6. Net线程安全集合

    在看Supersocket源码的时候发现很多地方都用到了我们不是很常用的线程安全集合,这些都是由net优化后的线程安全集合因此 应该比我们常规lock来效率好一些 比如说: 1 CurrentStac ...

  7. 在VC资源文件中加入声音资源

    本文介绍如何在VC资源文件中加入自己的声音资源,使自己的应用程序可以播放声音. 1.首先用文本编辑器(如记事本)打开资源文件(.rc文件) 在最后加入自己的声音资源,如下IDW WAVE " ...

  8. Java核心技术-高级特性(2)- SoftReference, WeakReference and PhantomReference

    Java.lang.ref 是 Java 类库中比较特殊的一个包,它提供了与 Java 垃圾回收器密切相关的引用类.这些引用类对象可以指向其它对象,但它们不同于一般的引用,因为它们的存在并不防碍 Ja ...

  9. C++内存管理学习笔记(6)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...

  10. javascript中使用Map

    mis.comm.js.Map = function() { this.elements = new Array(); //获取MAP元素个数 this.size = function() { ret ...