1 12 13 package org.eclipse.jdt.internal.junit.runner; 14 15 import java.lang.reflect.Field ; 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.Enumeration ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Vector ; 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 fPriorities; 30 31 public FailuresFirstPrioritizer(String [] priorities) { 32 fPriorities= new HashSet (Arrays.asList(priorities)); 33 } 34 35 public Test prioritize(Test suite) { 36 doPrioritize(suite, new ArrayList ()); 37 return suite; 38 } 39 40 private void doPrioritize(Test suite, List 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 path, TestSuite aSuite) { 59 for (Enumeration e= aSuite.tests(); e.hasMoreElements();) { 60 doPrioritize((Test)e.nextElement(), path); 61 } 62 } 63 64 65 private void reorder(Test test, List path) { 66 doReorder(test, path, path.size()-1); 67 } 68 69 private void doReorder(Test test, List path, int top) { 70 if (top < 0) 71 return; 72 Test topTest= (Test) path.get(top); 73 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 tests= (Vector )getField(suite, "fTests"); 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 getField(Object object, String fieldName) { 97 return getFieldInClass(object, fieldName, object.getClass()); 98 } 99 100 private static Object getFieldInClass(Object object, String fieldName, Class clazz) { 101 Field 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 e) { 109 } 111 return getFieldInClass(object, fieldName, clazz.getSuperclass()); 112 } 113 } 114 | Popular Tags |