KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > model > TestSuiteElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.junit.model;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jdt.junit.model.ITestElement;
18 import org.eclipse.jdt.junit.model.ITestSuiteElement;
19
20
21 public class TestSuiteElement extends TestElement implements ITestSuiteElement {
22     
23     private List JavaDoc/*<TestElement>*/ fChildren;
24     private Status fChildrenStatus;
25     
26     public TestSuiteElement(TestSuiteElement parent, String JavaDoc id, String JavaDoc testName, int childrenCount) {
27         super(parent, id, testName);
28         fChildren= new ArrayList JavaDoc(childrenCount);
29     }
30     
31     /* (non-Javadoc)
32      * @see org.eclipse.jdt.junit.ITestElement#getTestResult()
33      */

34     public Result getTestResult(boolean includeChildren) {
35         if (includeChildren) {
36             return getStatus().convertToResult();
37         } else {
38             return super.getStatus().convertToResult();
39         }
40     }
41     
42     /* (non-Javadoc)
43      * @see org.eclipse.jdt.junit.ITestSuiteElement#getSuiteTypeName()
44      */

45     public String JavaDoc getSuiteTypeName() {
46         return getClassName();
47     }
48         
49     /* (non-Javadoc)
50      * @see org.eclipse.jdt.junit.model.ITestSuiteElement#getChildren()
51      */

52     public ITestElement[] getChildren() {
53         return (ITestElement[]) fChildren.toArray(new ITestElement[fChildren.size()]);
54     }
55     
56     public void addChild(TestElement child) {
57         fChildren.add(child);
58     }
59     
60     public Status getStatus() {
61         Status suiteStatus= getSuiteStatus();
62         if (fChildrenStatus != null) {
63             // must combine children and suite status here, since failures can occur e.g. in @AfterClass
64
return Status.combineStatus(fChildrenStatus, suiteStatus);
65         } else {
66             return suiteStatus;
67         }
68     }
69     
70     private Status getCumulatedStatus() {
71         TestElement[] children= (TestElement[]) fChildren.toArray(new TestElement[fChildren.size()]); // copy list to avoid concurreny problems
72
if (children.length == 0)
73             return getSuiteStatus();
74         
75         Status cumulated= children[0].getStatus();
76         
77         for (int i= 1; i < children.length; i++) {
78             Status childStatus= children[i].getStatus();
79             cumulated= Status.combineStatus(cumulated, childStatus);
80         }
81         // not necessary, see special code in Status.combineProgress()
82
// if (suiteStatus.isErrorOrFailure() && cumulated.isNotRun())
83
// return suiteStatus; //progress is Done if error in Suite and no children run
84
return cumulated;
85     }
86
87     public Status getSuiteStatus() {
88         return super.getStatus();
89     }
90     
91     public void childChangedStatus(TestElement child, Status childStatus) {
92         int childCount= fChildren.size();
93         if (child == fChildren.get(0) && childStatus.isRunning()) {
94             // is first child, and is running -> copy status
95
internalSetChildrenStatus(childStatus);
96             return;
97         }
98         TestElement lastChild= (TestElement) fChildren.get(childCount - 1);
99         if (child == lastChild) {
100             if (childStatus.isDone()) {
101                 // all children done, collect cumulative status
102
internalSetChildrenStatus(getCumulatedStatus());
103                 return;
104             }
105             // go on (child could e.g. be a TestSuiteElement with RUNNING_FAILURE)
106

107         } else if (! lastChild.getStatus().isNotRun()) {
108             // child is not last, but last child has been run -> child has been rerun or is rerunning
109
internalSetChildrenStatus(getCumulatedStatus());
110             return;
111         }
112         
113         // finally, set RUNNING_FAILURE/ERROR if child has failed but suite has not failed:
114
if (childStatus.isFailure()) {
115             if (fChildrenStatus == null || ! fChildrenStatus.isErrorOrFailure()) {
116                 internalSetChildrenStatus(Status.RUNNING_FAILURE);
117                 return;
118             }
119         } else if (childStatus.isError()) {
120             if (fChildrenStatus == null || ! fChildrenStatus.isError()) {
121                 internalSetChildrenStatus(Status.RUNNING_ERROR);
122                 return;
123             }
124         }
125     }
126
127     private void internalSetChildrenStatus(Status status) {
128         if (fChildrenStatus == status)
129             return;
130         fChildrenStatus= status;
131         TestSuiteElement parent= getParent();
132         if (parent != null)
133             parent.childChangedStatus(this, getStatus());
134     }
135
136     public String JavaDoc toString() {
137         return "TestSuite: " + getSuiteTypeName() + " : " + super.toString() + " (" + fChildren.size() + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
138
}
139     
140 }
141
Popular Tags