JUnit4.8.2 Source Code Analysis-6.1 Sorting and filtering

Source: Internet
Author: User
Tags object object

Runner.sort, Request.sortwith and sorter.apply

Yqj2065 are almost killed by them.

Sorter.apply (), Request.sortwith () and Sortable.sort () do one thing? Why is it?



The Java.util.Comparator interface is a policy class that defines the int compare (t O1, T O2) method. Org.junit.runner.manipulation. Sorter implements Comparator<description>, but Sorter is only the formal implementation class. Sorter (Comparator<description>comparator) by constructor injection, initializes an actual sequencer in the sorter package comparator<description> Fcomparator.

Also @Override public intcompare (Description O1, Description O2) returns as the actual sequencer returns.

Well, what's this design pattern? I'm not sure, but there's a point. really do not understand is Sorter method apply, when is this useful?

       Publicvoid apply (Object object) {              if (object instanceof sortable) {                     sortablesortable = (sortable) Object;                     Sortable.sort (this);              }       }
The only method that interface sortable defines is public void sort (sortersorter). And sortable is the parent interface of some runner, that is, the sortable object is runner that run the test with a sort method。 According to my present intuitive feeling, Sortable.sort (Sorter) is better called Sortable.setsorter (Sorter), some runner such as Parentrunner, Should there be two domains Sortstyle and Filterstyle instead of implements them, so it's easier to understand?

Now there are actual sequencer:

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 ());    }
Have a test target

Package Mytest.sort;import static tool. Print.*;import Org.junit.*;//test/ignorepublic 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.");}    }
And then what? Fennel bean anise has 3 kinds of writing? Unknown.

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 () {Blockjunit        4ClassRunner 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 ();}}

Record it first.





JUnit4.8.2 Source Code Analysis-6.1 Sorting and filtering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.