KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jdt.junit.model.ITestElement;
17 import org.eclipse.jdt.junit.model.ITestElementContainer;
18 import org.eclipse.jdt.junit.model.ITestRunSession;
19
20
21 public abstract class TestElement implements ITestElement {
22     public final static class Status {
23         public static final Status RUNNING_ERROR= new Status("RUNNING_ERROR", 5); //$NON-NLS-1$
24
public static final Status RUNNING_FAILURE= new Status("RUNNING_FAILURE", 6); //$NON-NLS-1$
25
public static final Status RUNNING= new Status("RUNNING", 3); //$NON-NLS-1$
26

27         public static final Status ERROR= new Status("ERROR", /*1*/ITestRunListener2.STATUS_ERROR); //$NON-NLS-1$
28
public static final Status FAILURE= new Status("FAILURE", /*2*/ITestRunListener2.STATUS_FAILURE); //$NON-NLS-1$
29
public static final Status OK= new Status("OK", /*0*/ITestRunListener2.STATUS_OK); //$NON-NLS-1$
30
public static final Status NOT_RUN= new Status("NOT_RUN", 4); //$NON-NLS-1$
31

32         private static final Status[] OLD_CODE= { OK, ERROR, FAILURE};
33         
34         private final String JavaDoc fName;
35         private final int fOldCode;
36         
37         private Status(String JavaDoc name, int oldCode) {
38             fName= name;
39             fOldCode= oldCode;
40         }
41         
42         public int getOldCode() {
43             return fOldCode;
44         }
45         
46         public String JavaDoc toString() {
47             return fName;
48         }
49
50         /* error state predicates */
51         
52         public boolean isOK() {
53             return this == OK || this == RUNNING || this == NOT_RUN;
54         }
55         
56         public boolean isFailure() {
57             return this == FAILURE || this == RUNNING_FAILURE;
58         }
59         
60         public boolean isError() {
61             return this == ERROR || this == RUNNING_ERROR;
62         }
63         
64         public boolean isErrorOrFailure() {
65             return isError() || isFailure();
66         }
67         
68         /* progress state predicates */
69         
70         public boolean isNotRun() {
71             return this == NOT_RUN;
72         }
73         
74         public boolean isRunning() {
75             return this == RUNNING || this == RUNNING_FAILURE || this == RUNNING_ERROR;
76         }
77         
78         public boolean isDone() {
79             return this == OK || this == FAILURE || this == ERROR;
80         }
81
82         public static Status combineStatus(Status one, Status two) {
83             Status progress= combineProgress(one, two);
84             Status error= combineError(one, two);
85             return combineProgressAndErrorStatus(progress, error);
86         }
87
88         private static Status combineProgress(Status one, Status two) {
89             if (one.isNotRun() && two.isNotRun())
90                 return NOT_RUN;
91             else if (one.isDone() && two.isDone())
92                 return OK;
93             else if (!one.isRunning() && !two.isRunning())
94                 return OK; // one done, one not-run -> a parent failed and its children are not run
95
else
96                 return RUNNING;
97         }
98         
99         private static Status combineError(Status one, Status two) {
100             if (one.isError() || two.isError())
101                 return ERROR;
102             else if (one.isFailure() || two.isFailure())
103                 return FAILURE;
104             else
105                 return OK;
106         }
107         
108         private static Status combineProgressAndErrorStatus(Status progress, Status error) {
109             if (progress.isDone()) {
110                 if (error.isError())
111                     return ERROR;
112                 if (error.isFailure())
113                     return FAILURE;
114                 return OK;
115             }
116             
117             if (progress.isNotRun()) {
118 // Assert.isTrue(!error.isErrorOrFailure());
119
return NOT_RUN;
120             }
121             
122 // Assert.isTrue(progress.isRunning());
123
if (error.isError())
124                 return RUNNING_ERROR;
125             if (error.isFailure())
126                 return RUNNING_FAILURE;
127 // Assert.isTrue(error.isOK());
128
return RUNNING;
129         }
130         
131         /**
132          * @param oldStatus one of {@link ITestRunListener2}'s STATUS_* constants
133          * @return the Status
134          */

135         public static Status convert(int oldStatus) {
136             return OLD_CODE[oldStatus];
137         }
138         
139         public Result convertToResult() {
140             if (isNotRun())
141                 return Result.UNDEFINED;
142             if (isError())
143                 return Result.ERROR;
144             if (isFailure())
145                 return Result.FAILURE;
146             if (isRunning()) {
147                 return Result.UNDEFINED;
148             }
149             return Result.OK;
150         }
151         
152         public ProgressState convertToProgressState() {
153             if (isRunning()) {
154                 return ProgressState.RUNNING;
155             }
156             if (isDone()) {
157                 return ProgressState.COMPLETED;
158             }
159             return ProgressState.NOT_STARTED;
160         }
161         
162     }
163     
164     private final TestSuiteElement fParent;
165     private final String JavaDoc fId;
166     private String JavaDoc fTestName;
167
168     private Status fStatus;
169     private String JavaDoc fTrace;
170     private String JavaDoc fExpected;
171     private String JavaDoc fActual;
172     
173     /**
174      * @param parent the parent, can be <code>null</code>
175      * @param id the test id
176      * @param testName the test name
177      */

178     public TestElement(TestSuiteElement parent, String JavaDoc id, String JavaDoc testName) {
179         Assert.isNotNull(id);
180         Assert.isNotNull(testName);
181         fParent= parent;
182         fId= id;
183         fTestName= testName;
184         fStatus= Status.NOT_RUN;
185         if (parent != null)
186             parent.addChild(this);
187     }
188     
189     /* (non-Javadoc)
190      * @see org.eclipse.jdt.junit.ITestElement#getProgressState()
191      */

192     public ProgressState getProgressState() {
193         return getStatus().convertToProgressState();
194     }
195     
196     /* (non-Javadoc)
197      * @see org.eclipse.jdt.junit.ITestElement#getTestResult()
198      */

199     public Result getTestResult(boolean includeChildren) {
200         return getStatus().convertToResult();
201     }
202     
203     /* (non-Javadoc)
204      * @see org.eclipse.jdt.junit.ITestElement#getTestRunSession()
205      */

206     public ITestRunSession getTestRunSession() {
207         return getRoot().getTestRunSession();
208     }
209     
210     /* (non-Javadoc)
211      * @see org.eclipse.jdt.junit.ITestElement#getParentContainer()
212      */

213     public ITestElementContainer getParentContainer() {
214         if (fParent instanceof TestRoot) {
215             return getTestRunSession();
216         }
217         return fParent;
218     }
219     
220     /* (non-Javadoc)
221      * @see org.eclipse.jdt.junit.model.ITestElement#getFailureTrace()
222      */

223     public FailureTrace getFailureTrace() {
224         Result testResult= getTestResult(false);
225         if (testResult == Result.ERROR || testResult == Result.FAILURE) {
226             return new FailureTrace(fTrace, fExpected, fActual);
227         }
228         return null;
229     }
230     
231     /**
232      * @return the parent suite, or <code>null</code> for the root
233      */

234     public TestSuiteElement getParent() {
235         return fParent;
236     }
237     
238     public String JavaDoc getId() {
239         return fId;
240     }
241     
242     public String JavaDoc getTestName() {
243         return fTestName;
244     }
245     
246     public void setName(String JavaDoc name) {
247         fTestName= name;
248     }
249     
250     public void setStatus(Status status) {
251         //TODO: notify about change?
252
//TODO: multiple errors/failures per test https://bugs.eclipse.org/bugs/show_bug.cgi?id=125296
253

254         fStatus= status;
255         TestSuiteElement parent= getParent();
256         if (parent != null)
257             parent.childChangedStatus(this, status);
258     }
259     
260     public void setStatus(Status status, String JavaDoc trace, String JavaDoc expected, String JavaDoc actual) {
261         //TODO: notify about change?
262
//TODO: multiple errors/failures per test https://bugs.eclipse.org/bugs/show_bug.cgi?id=125296
263
fTrace= trace;
264         fExpected= expected;
265         fActual= actual;
266         setStatus(status);
267     }
268
269     public Status getStatus() {
270         return fStatus;
271     }
272     
273     public String JavaDoc getTrace() {
274         return fTrace;
275     }
276     
277     public String JavaDoc getExpected() {
278         return fExpected;
279     }
280     
281     public String JavaDoc getActual() {
282         return fActual;
283     }
284     
285     public boolean isComparisonFailure() {
286         return fExpected != null && fActual != null;
287     }
288     
289     /**
290      * @see org.eclipse.jdt.internal.junit.runner.ITestIdentifier#getName()
291      * @see org.eclipse.jdt.internal.junit.runner.MessageIds#TEST_IDENTIFIER_MESSAGE_FORMAT
292      */

293     public String JavaDoc getClassName() {
294         return extractClassName(getTestName());
295     }
296     
297     private String JavaDoc extractClassName(String JavaDoc testNameString) {
298         int index= testNameString.indexOf('(');
299         if (index < 0)
300             return testNameString;
301         testNameString= testNameString.substring(index + 1);
302         testNameString= testNameString.replace('$', '.'); // see bug 178503
303
return testNameString.substring(0, testNameString.indexOf(')'));
304     }
305     
306     public TestRoot getRoot() {
307         return getParent().getRoot();
308     }
309     
310     public String JavaDoc toString() {
311         return getProgressState() + " - " + getTestResult(true); //$NON-NLS-1$
312
}
313 }
314
Popular Tags