KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > runner > FailuresFirstPrioritizer


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  * David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.junit.runner;
14
15 import java.lang.reflect.Field JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import junit.extensions.TestDecorator;
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 public class FailuresFirstPrioritizer implements ITestPrioritizer {
29     private HashSet JavaDoc fPriorities;
30     
31     public FailuresFirstPrioritizer(String JavaDoc[] priorities) {
32         fPriorities= new HashSet JavaDoc(Arrays.asList(priorities));
33     }
34     
35     public Test prioritize(Test suite) {
36         doPrioritize(suite, new ArrayList JavaDoc());
37         return suite;
38     }
39     
40     private void doPrioritize(Test suite, List JavaDoc path) {
41         if (suite instanceof TestCase) {
42             TestCase testCase= (TestCase) suite;
43             if (hasPriority(testCase))
44                 reorder(testCase, path);
45         } else if (suite instanceof TestSuite) {
46             TestSuite aSuite= (TestSuite)suite;
47             path.add(suite);
48             loopTests(path, aSuite);
49             path.remove(path.size()-1);
50         } else if (suite instanceof TestDecorator) {
51             TestDecorator aDecorator= (TestDecorator)suite;
52             path.add(aDecorator);
53             doPrioritize(aDecorator.getTest(), path);
54             path.remove(path.size()-1);
55         }
56     }
57
58     private void loopTests(List JavaDoc path, TestSuite aSuite) {
59         for (Enumeration JavaDoc e= aSuite.tests(); e.hasMoreElements();) {
60             doPrioritize((Test)e.nextElement(), path);
61         }
62     }
63
64     
65     private void reorder(Test test, List JavaDoc path) {
66         doReorder(test, path, path.size()-1);
67     }
68
69     private void doReorder(Test test, List JavaDoc path, int top) {
70         if (top < 0)
71             return;
72         Test topTest= (Test) path.get(top);
73         // only reorder TestSuites
74
if (topTest instanceof TestSuite) {
75             TestSuite suite= (TestSuite) topTest;
76             moveTestToFront(suite, test);
77         }
78         doReorder(topTest, path, top-1);
79     }
80
81     void moveTestToFront(TestSuite suite, Test test) {
82         Vector JavaDoc tests= (Vector JavaDoc)getField(suite, "fTests"); //$NON-NLS-1$
83
for(int i= 0; i < tests.size(); i++) {
84             if (tests.get(i) == test) {
85                 tests.remove(i);
86                 tests.insertElementAt(test, 0);
87             }
88         }
89     }
90
91     
92     private boolean hasPriority(TestCase testCase) {
93         return fPriorities.contains(testCase.toString());
94     }
95     
96     public static Object JavaDoc getField(Object JavaDoc object, String JavaDoc fieldName) {
97         return getFieldInClass(object, fieldName, object.getClass());
98     }
99
100     private static Object JavaDoc getFieldInClass(Object JavaDoc object, String JavaDoc fieldName, Class JavaDoc clazz) {
101         Field JavaDoc field= null;
102         if (clazz == null)
103             return null;
104         try {
105             field= clazz.getDeclaredField(fieldName);
106             field.setAccessible(true);
107             return field.get(object);
108         } catch (Exception JavaDoc e) {
109             // fall through
110
}
111         return getFieldInClass(object, fieldName, clazz.getSuperclass());
112     }
113 }
114
Popular Tags