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. [置顶] Jsp中的table多表头导出excel文件

    首先引入两份JS:copyhtmltoexcel.js以及 tableToExcel.js /* * 默认转换实现函数,如果需要其他功能,需自行扩展 * 参数: * tableID : HTML中Ta ...

  2. POJ 2632 Crashing Robots (坑爹的模拟题)

    Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6599   Accepted: 2854 D ...

  3. 4.Swift教程翻译系列——Swift基本运算符

    英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 运算符是指一个特殊的符号,能够用来查看.更改值或者相加.比方说加法运算符+能够讲 ...

  4. expect 参数处理之一

    expect 参数处理之一 expect是很强悍的脚本,网上资源虽然很经典,但很少,我把自己的测试脚本贴之,仅作参考 #!/usr/bin/expect -f proc help {} { puts ...

  5. 运行Dos命令并得到dos的输出文本(使用管道函数CreatePipe和PeekNamedPipe)

    function RunDOS(const CommandLine: string): string;var  HRead, HWrite: THandle;  StartInfo: TStartup ...

  6. 新的方法 (New Approach)¶

    第一章:简介 - ANSI Common Lisp 中文版 新的方法 (New Approach)¶ 本书的目标之一是不仅是教授 Lisp 语言,而是教授一种新的编程方法,这种方法因为有了 Lisp ...

  7. 例3.1 猜猜数据结构 UVa11995

    1.标题叙述性说明:点击打开链接 2.解题思路:据来推測一种可能的数据结构,备选答案有"栈,队列.优先队列".结果也可能都不是或者不确定. STL中已经有这三种数据结构了,因此直接 ...

  8. 解决Andriod使用HttpURLConnection 失败问题

    在Android的Activity中使用HttpURLConnection连接到服务端时抛出异常,Access denied.第一个想到是权限问题.然后就尝试将INTERNET权限加上:在Manife ...

  9. Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET

    Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET Python -- 堆数据结构 heapq 分类: Python 2012-09 ...

  10. mvc action 有多种跳转

    在ASP.NET mvc下,action 有多种跳转方式: return RedirectToAction("Index");//一个参数时在本Controller下 如果Redi ...