JUnit Combination Mode Application

Source: Internet
Author: User

Combination Mode

Definition:

Combine objects into a tree structure to represent the "part-whole" hierarchy. The composite mode ensures consistency between the use of a single object and a composite object.

Composition:

Component: This is an abstract role that specifies an interface for the objects in the combination. This role provides a common interface and default behavior. In fact, our test interface defines the run method.
Composite: implements a common interface and maintains a set of test cases. It is a composite test case testsuite.
Leaf: indicates the object that participates in the combination. It has no sub-object and only defines the behavior of the original object that participates in the combination. In fact, it is a single test case testcase. It only implements the test interface method.

 

Category:

Define methods for managing child elements in the composite class;

Define the method for managing child elements in the component interface;

Java code implementation:

Define the method for managing child elements in the composite class

public interface Component {    public void doSomething();  }
public class Composite implements Component {    private List<Component> list = new ArrayList<Component>();    public void add(Component component) {        list.add(component);    }    public void remove(Component component) {        list.remove(component);    }    public List<Component> getAll() {        return list;    }    @Override    public void doSomething() {        for (Component component : list) {            component.doSomething();        }    }}
public class Leaf implements Component {    public void doSomething() {        System.out.println("dosomething");    }}
public class Client {    public static void main(String[] args) {        Component leaf1 = new Leaf();        Component leaf2 = new Leaf();        Composite composite = new Composite();        composite.add(leaf1);        composite.add(leaf2);                Composite composite2 = new Composite();        Component leaf3 = new Leaf();        composite2.add(composite);        composite2.add(leaf3);        composite2.doSomething();    }}

Define the method for managing child elements in the Component Interface

public interface Component {    public void doSomething();      public void add(Component component);      public void remove(Component component) ;      public List<Component> getAll() ;  }
public class Composite implements Component {    private List<Component> list = new ArrayList<Component>();    public void add(Component component) {        list.add(component);    }    public void remove(Component component) {        list.remove(component);    }    public List<Component> getAll() {        return list;    }    @Override    public void doSomething() {        for (Component component : list) {            component.doSomething();        }    }}
public class Leaf implements Component {    public void doSomething() {        System.out.println("dosomething");    }    public void add(Component component) {    }    public List<Component> getAll() {        return null;    }    public void remove(Component component) {    }}
public class Client {    public static void main(String[] args) {        Component leaf1 = new Leaf();        Component leaf2 = new Leaf();        Composite composite = new Composite();        composite.add(leaf1);        composite.add(leaf2);                Composite composite2 = new Composite();        Component leaf3 = new Leaf();        composite2.add(composite);        composite2.add(leaf3);        composite2.doSomething();    }}

 

Application of Combined Mode in junit3 framework

public interface Test {         public abstract void run(TestResult result);  }    public abstract class TestCase extends Assert implements Test {      public void run(TestResult result) {          result.run(this);    }  }  public class TestSuite implements Test {      private Vector fTests= new Vector(10);      public void addTest(Test test) {          fTests.addElement(test);      }      public void addTestSuite(Class testClass) {          addTest(new TestSuite(testClass));      }          public void run(TestResult result) {          for (Enumeration e= tests(); e.hasMoreElements(); ) {              if (result.shouldStop() )                  break;              Test test= (Test)e.nextElement();              runTest(test, result);          }      }      public Enumeration tests() {          return fTests.elements();      }}  

The testsuit class has a property ftests (vector type) that stores its subtest cases, provides the addtest method to add the Sub-object testcase, and also provides methods such as testcount and tests to operate on sub-objects. Finally, the run () method is used to delegate its sub-objects. Finally, the addtestsuite method is provided to implement recursion and construct a tree shape;

Both testcase and testsuite implement the test interface. Because both testcase and testsuit comply with the test interface, we can recursively combine testsuite into testsuite through addtestsuite, which will form a tree structure. All developers can create their own testsuit. A tester can create a testsuit that combines these test cases to run all their testcase, such:

Public Static Test Suite () {testsuite suite1 = new testsuite ("My tests testsuit1"); testsuite suite2 = new testsuite ("My tests testsuit2"); suite1.addtestsuite (XXX. class); suite2.addtestsuite (XXX. class); suite1.addtest (suite2); Return suite1 ;}

 

Benefits of introducing the combination mode in junit3

1) Simplified JUnit code. JUnit can uniformly process the combined structure testsuite and a single object testcase. It makes JUnit development simple and easy, because there is no need to distinguish between parts and the whole, and there is no need to write some selection statements filled with if else;

2) the basic object testcase, which defines the testcase object and testsuite's class hierarchy, can be combined into more complex composite objects testsuite, and these composite objects can be combined. In the preceding example, this keeps recursion. In the code of a program, you can easily use composite objects wherever you use basic objects, greatly simplifying system maintenance and development;

3) makes it easier to add new types of testcase;

 

JUnit Combination Mode Application

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.