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

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

先记录一下。

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

时间: 2024-08-06 08:57:37

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

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

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

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

JUnit4.8.2源代码,最为yqj2065兴趣是org.junit.runner.Request,现在是几点意味着它? ①封装JUnit的输入 JUnit4作为信息处理单元,它的输入是单元測试类--布满各种JUnit4的RUNTIME标注的类,但因为使用反射机制,JUnit4的输入严格地说是一个或多个(组)单元測试类的Class对象.早期版本号的JUnit主要处理一个測试或測试构成的树,在增添了对过滤/ filtering和排序/ sorting支持后,JUnit4增加了这个概念.毕竟依照1

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

abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤.(虽然JUnit4.8.2源代码分析-6.1 排序和过滤中演示了客户使用排序和过滤的方式,也有些不明白其设计意图,但是,先读懂源代码为妙.说不定看着看着就明白了.) org.junit.runner.manipulation包 排序和过滤的相关类型,在org.junit.runner.manipulation包中. 1.例

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

abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤. (尽管JUnit4.8.2源码分析-6.1 排序和过滤中演示了客户使用排序和过滤的方式,也有些不明确其设计意图.可是.先读懂源码为妙.说不定看着看着就明确了. ) org.junit.runner.manipulation包 排序和过滤的相关类型.在org.junit.runner.manipulation包中.Sort

微生物来源分析

目录 微生物来源分析 写在前面 准备 微生物来源分析 rm(list = ls()) gc() 导入主函数 导入分组文件和OTU表格 Load OTU table 下面区分目标样品和来源样品. Extract the source environments and source/sink indices 对两组样品进行抽平 Estimate source proportions for each sink 就正常样品而言,我们都会测定重复,这里基于多个样品的sourceracker分析 导入主函

排序和过滤

在数据表格展示中,基本上会出现三个元素:排序,过滤(也就是搜索),分页.在这里,我想将这排序和过滤的逻辑处理关系描述一下. 在这里,不考虑在进行排序或过滤操作时,数据处于第几页,在操作完成后,默认回到首页.如果在进行该操作时,已选中一行数据,这又需要另外分析,操作后是否依旧调到该数据所在的页面. 排序: 1.无过滤排序:当前不存在过滤条件(过滤条件为空),排序后,重新按照新的排序规无过滤条件读取数据,展示首页 2.过滤排序:当前已有过滤条件,排序后,重新按照新的排序规则并加上过滤条件读取数据,展

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

转载请注明出处:http://blog.csdn.net/xiaanming/article/details/12684155 前段时间因为换工作的缘故又恰巧碰到国庆节,所以有段时间自己没有更新博客了,过完国庆到新公司报道,感觉还不错,就是现在住的地方离新公司有点远,地铁20站,伤不起啊,我每天早上7点多就要起床,然后屁颠屁颠的去挤地铁上班,晚上下班还要挤地铁,先不说路程远,车费一天就要10几块,我的银子啊,有坐龙华线去上班的深圳程序员不?听说那条线上班高峰期很挤?我没在上班高峰期坐过那趟车,我

MVC5+EF6--3 排序、过滤和分页

近期学习MVC5+EF6,找到了Microsoft的原文,一个非常棒的系列,Getting Started with Entity Framework 6 Code First using MVC 5,网址:http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-appli

跟我学SharePoint 2013视频培训课程——排序、过滤在列表、库中的使用(10)

课程简介 第10天,SharePoint 2013排序.过滤在列表.库中的使用. 视频 SharePoint 2013 交流群 41032413