1 13 14 package org.eclipse.jdt.internal.junit.launcher; 15 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.Comparator ; 19 import java.util.Iterator ; 20 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtension; 23 import org.eclipse.core.runtime.IExtensionPoint; 24 import org.eclipse.core.runtime.Platform; 25 26 import org.eclipse.jdt.core.IJavaElement; 27 28 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 29 import org.eclipse.jdt.internal.junit.util.TestSearchEngine; 30 31 public class TestKindRegistry { 32 33 public static final String JUNIT3_TEST_KIND_ID= "org.eclipse.jdt.junit.loader.junit3"; public static final String JUNIT4_TEST_KIND_ID= "org.eclipse.jdt.junit.loader.junit4"; 36 public static TestKindRegistry getDefault() { 37 if (fgRegistry != null) 38 return fgRegistry; 39 40 fgRegistry= new TestKindRegistry(Platform.getExtensionRegistry().getExtensionPoint(JUnitPlugin.ID_EXTENSION_POINT_TEST_KINDS)); 41 return fgRegistry; 42 } 43 44 private static TestKindRegistry fgRegistry; 45 46 private final IExtensionPoint fPoint; 47 private ArrayList fTestKinds; 48 49 private TestKindRegistry(IExtensionPoint point) { 50 fPoint = point; 51 } 52 53 public ArrayList getAllKinds() { 54 loadKinds(); 55 return fTestKinds; 56 } 57 58 private void loadKinds() { 59 if (fTestKinds != null) 60 return; 61 62 ArrayList items= new ArrayList (); 63 for (Iterator iter= getConfigurationElements().iterator(); iter.hasNext();) { 64 IConfigurationElement element= (IConfigurationElement) iter.next(); 65 items.add(new TestKind(element)); 66 } 67 68 Collections.sort(items, new Comparator () { 69 public int compare(Object arg0, Object arg1) { 70 TestKind kind0 = (TestKind) arg0; 71 TestKind kind1 = (TestKind) arg1; 72 73 if (kind0.precedes(kind1)) 74 return -1; 75 if (kind1.precedes(kind0)) 76 return 1; 77 return 0; 78 } 79 }); 80 fTestKinds= items; 81 } 82 83 public ArrayList getDisplayNames() { 84 ArrayList result = new ArrayList (); 85 ArrayList testTypes = getAllKinds(); 86 for (Iterator iter = testTypes.iterator(); iter.hasNext();) { 87 ITestKind type = (ITestKind) iter.next(); 88 result.add(type.getDisplayName()); 89 } 90 return result; 91 } 92 93 97 public ITestKind getKind(String testKindId) { 98 if (testKindId != null) { 99 for (Iterator iter= getAllKinds().iterator(); iter.hasNext();) { 100 TestKind kind= (TestKind) iter.next(); 101 if (testKindId.equals(kind.getId())) 102 return kind; 103 } 104 } 105 return ITestKind.NULL; 106 } 107 108 public static String getContainerTestKindId(IJavaElement element) { 109 return element != null && TestSearchEngine.hasTestAnnotation(element.getJavaProject()) ? JUNIT4_TEST_KIND_ID : JUNIT3_TEST_KIND_ID; 110 } 111 112 public static ITestKind getContainerTestKind(IJavaElement element) { 113 return getDefault().getKind(getContainerTestKindId(element)); 114 } 115 116 private ArrayList getConfigurationElements() { 117 ArrayList items= new ArrayList (); 118 IExtension[] extensions= fPoint.getExtensions(); 119 for (int i= 0; i < extensions.length; i++) { 120 IExtension extension= extensions[i]; 121 IConfigurationElement[] elements= extension.getConfigurationElements(); 122 for (int j= 0; j < elements.length; j++) { 123 IConfigurationElement element= elements[j]; 124 items.add(element); 125 } 126 } 127 return items; 128 } 129 130 public String getAllKindIds() { 131 ArrayList allKinds= getAllKinds(); 132 String returnThis= ""; for (Iterator iter= allKinds.iterator(); iter.hasNext();) { 134 ITestKind kind= (ITestKind) iter.next(); 135 returnThis+= "(" + kind.getId() + ")"; } 137 return returnThis; 138 } 139 } 140 | Popular Tags |