KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > TestKindRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  * David Saff (saff@mit.edu) - initial API and implementation
11  * (bug 102632: [JUnit] Support for JUnit 4.)
12  *******************************************************************************/

13
14 package org.eclipse.jdt.internal.junit.launcher;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Comparator JavaDoc;
19 import java.util.Iterator JavaDoc;
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 JavaDoc JUNIT3_TEST_KIND_ID= "org.eclipse.jdt.junit.loader.junit3"; //$NON-NLS-1$
34
public static final String JavaDoc JUNIT4_TEST_KIND_ID= "org.eclipse.jdt.junit.loader.junit4"; //$NON-NLS-1$
35

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 JavaDoc/*<TestKind>*/ fTestKinds;
48
49     private TestKindRegistry(IExtensionPoint point) {
50         fPoint = point;
51     }
52
53     public ArrayList JavaDoc/*<TestKind>*/ getAllKinds() {
54         loadKinds();
55         return fTestKinds;
56     }
57
58     private void loadKinds() {
59         if (fTestKinds != null)
60             return;
61         
62         ArrayList JavaDoc items= new ArrayList JavaDoc();
63         for (Iterator JavaDoc 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 JavaDoc() {
69             public int compare(Object JavaDoc arg0, Object JavaDoc 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 JavaDoc/*<String>*/ getDisplayNames() {
84         ArrayList JavaDoc result = new ArrayList JavaDoc();
85         ArrayList JavaDoc testTypes = getAllKinds();
86         for (Iterator JavaDoc iter = testTypes.iterator(); iter.hasNext();) {
87             ITestKind type = (ITestKind) iter.next();
88             result.add(type.getDisplayName());
89         }
90         return result;
91     }
92
93     /**
94      * @param testKindId an id, can be <code>null</code>
95      * @return a TestKind, ITestKind.NULL if not available
96      */

97     public ITestKind getKind(String JavaDoc testKindId) {
98         if (testKindId != null) {
99             for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc getConfigurationElements() {
117         ArrayList JavaDoc items= new ArrayList JavaDoc();
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 JavaDoc getAllKindIds() {
131         ArrayList JavaDoc allKinds= getAllKinds();
132         String JavaDoc returnThis= ""; //$NON-NLS-1$
133
for (Iterator JavaDoc iter= allKinds.iterator(); iter.hasNext();) {
134             ITestKind kind= (ITestKind) iter.next();
135             returnThis+= "(" + kind.getId() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
136
}
137         return returnThis;
138     }
139 }
140
Popular Tags