KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > plugin > JUnitPlugin


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2006-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.plugin;
21
22 //import java.util.Collections;
23
//import java.util.EnumSet;
24
import java.util.Map JavaDoc;
25 //import java.util.Set;
26
//import javax.lang.model.element.Element;
27
//import javax.lang.model.element.ElementKind;
28
//import org.netbeans.api.java.source.ElementHandle;
29
import org.netbeans.modules.junit.JUnitPluginTrampoline;
30 import org.openide.filesystems.FileObject;
31
32 /**
33  * SPI for custom implementations of support for JUnit.
34  * It declares methods for:
35  * <ul>
36  * <li>navigation between source classes and corresponding test classes
37  * ({@link #getTestLocation getTestLocation},
38  * {@link #getTestedLocation getTestedLocation})</li>
39  * <li>creation of test class skeletons
40  * ({@link #createTests createTests})</li>
41  * </ul>
42  *
43  * @author Marian Petras
44  */

45 public abstract class JUnitPlugin {
46     
47     static {
48         JUnitPluginTrampoline.DEFAULT = new JUnitPluginTrampoline() {
49             public FileObject[] createTests(
50                     JUnitPlugin plugin,
51                     FileObject[] filesToTest,
52                     FileObject targetRoot,
53                     Map JavaDoc<CreateTestParam,Object JavaDoc> params) {
54                 return plugin.createTests(filesToTest, targetRoot, params);
55             }
56             public Location getTestLocation(
57                     JUnitPlugin plugin,
58                     Location sourceLocation) {
59                 return plugin.getTestLocation(sourceLocation);
60             }
61             public Location getTestedLocation(
62                     JUnitPlugin plugin,
63                     Location testLocation) {
64                 return plugin.getTestedLocation(testLocation);
65             }
66         };
67     }
68     
69     /**
70      * Default constructor for use by subclasses.
71      */

72     protected JUnitPlugin() {}
73
74     /**
75      * Enumeration of test creation parameters.
76      */

77     public enum CreateTestParam {
78         
79         /**
80          * key for the map of test creation parameters
81          * - name of the test class
82          */

83         CLASS_NAME(99310),
84         /**
85          * key for the map of test creation parameters
86          * - include tests for public methods?
87          */

88         INC_PUBLIC(99311),
89         /**
90          * key for the map of test creation parameters
91          * - include tests for protected methods?
92          */

93         INC_PROTECTED(99312),
94         /**
95          * key for the map of test creation parameters
96          * - include tests for package-private methods?
97          */

98         INC_PKG_PRIVATE(99313),
99         /**
100          * key for the map of test creation parameters
101          * - generate method {@code setup()}?
102          */

103         INC_SETUP(99314),
104         /**
105          * key for the map of test creation parameters
106          * - generate method {@code tearDown()}?
107          */

108         INC_TEAR_DOWN(99315),
109         /**
110          * key for the map of test creation parameters
111          * - generate default test method bodies?
112          */

113         INC_METHOD_BODIES(99316),
114         /**
115          * key for the map of test creation parameters
116          * - generate Javadoc comments for test methods?
117          */

118         INC_JAVADOC(99317),
119         /**
120          * key for the map of test creation parameters
121          * - generate source code hints?
122          */

123         INC_CODE_HINT(99318),
124         /**
125          * key for the map of test creation parameters
126          * - generate test classes for package-private classes?
127          */

128         INC_PKG_PRIVATE_CLASS(99319),
129         /**
130          * key for the map of test creation parameters
131          * - generate test classes for abstract classes?
132          */

133         INC_ABSTRACT_CLASS(99320),
134         /**
135          * key for the map of test creation parameters
136          * - generate test classes for exception classes?
137          */

138         INC_EXCEPTION_CLASS(99321),
139         /**
140          * key for the map of test creation parameters
141          * - generate test suites for packages?
142          */

143         INC_GENERATE_SUITE(99322);
144         
145         private final int idNumber;
146         
147         CreateTestParam(int idNumber) {
148             this.idNumber = idNumber;
149         }
150         
151         /**
152          * Return a unique number of this enum element.
153          *
154          * @return unique number of this enum element
155          */

156         public int getIdNumber() {
157             return idNumber;
158         }
159         
160     }
161     
162     /**
163      * Data structure for storage of specification of a Java element or
164      * a Java file.
165      */

166     public static final class Location {
167         //** */
168
//public static final Set<ElementKind> CLASS_LIKE_ELEM_TYPES;
169
//** */
170
//public static final Set<ElementKind> SUPPORTED_ELEM_TYPES;
171
/**
172          * holds specification of a Java file
173          */

174         private final FileObject fileObject;
175 // /**
176
// */
177
// private final ElementHandle<Element> elementHandle;
178
//
179
// static {
180
// CLASS_LIKE_ELEM_TYPES = EnumSet.of(ElementKind.CLASS,
181
// ElementKind.INTERFACE,
182
// ElementKind.ENUM);
183
// EnumSet<ElementKind> elemTypes;
184
// elemTypes = EnumSet.copyOf(CLASS_LIKE_ELEM_TYPES);
185
// elemTypes.addAll(EnumSet.of(ElementKind.METHOD,
186
// ElementKind.CONSTRUCTOR,
187
// ElementKind.STATIC_INIT));
188
// SUPPORTED_ELEM_TYPES = Collections.unmodifiableSet(elemTypes);
189
// }
190

191         /**
192          * Creates a new instance.
193          *
194          * @param fileObject the {@code FileObject}
195          *
196          *
197          *
198          */

199         public Location(FileObject fileObject/*,
200                         Element element*/
) {
201             if (fileObject == null) {
202                throw new IllegalArgumentException JavaDoc("fileObject is null");//NOI18N
203
}
204             
205 // while ((element != null)
206
// && !SUPPORTED_ELEM_TYPES.contains(element.getKind())) {
207
// element = element.getEnclosingElement();
208
// }
209

210             this.fileObject = fileObject;
211             //this.elementHandle = (element != null)
212
// ? ElementHandle.create(element)
213
// : null;
214
}
215         
216         /**
217          * Returns the {@code FileObject}.
218          *
219          * @return the {@code FileObject} held in this instance
220          */

221         public FileObject getFileObject() {
222             return fileObject;
223         }
224         
225 // /**
226
// */
227
// public ElementHandle<Element> getElementHandle() {
228
// return elementHandle;
229
// }
230

231     }
232     
233     /**
234      * Returns a specification of a Java element or file representing test
235      * for the given source Java element or file.
236      *
237      * @param sourceLocation specification of a Java element or file
238      * @return specification of a corresponding test Java element or file,
239      * or {@code null} if no corresponding test Java file is available
240      */

241     protected abstract Location getTestLocation(Location sourceLocation);
242     
243     /**
244      * Returns a specification of a Java element or file that is tested
245      * by the given test Java element or test file.
246      *
247      * @param testLocation specification of a Java element or file
248      * @return specification of a Java element or file that is tested
249      * by the given Java element or file.
250      */

251     protected abstract Location getTestedLocation(Location testLocation);
252     
253 // /**
254
// * Informs whether the plugin is capable of creating tests at the moment.
255
// * The default implementation returns {@code true}.
256
// *
257
// * @return {@code true} if the plugin is able of creating tests,
258
// * {@code false} otherwise
259
// * @see #createTests
260
// */
261
// protected boolean canCreateTests() {
262
// return true;
263
// }
264

265     /**
266      * Creates test classes for given source classes.
267      * If the plugin does not support creating tests, implementation of this
268      * method should return {@code null}.
269      *
270      * @param filesToTest source files for which test classes should be
271      * created
272      * @param targetRoot root folder of the target source root
273      * @param params parameters of creating test class
274      * - each key is an {@code Integer} whose value is equal
275      * to some of the constants defined in the class;
276      * the value is either
277      * a {@code String} (for key with value {@code CLASS_NAME})
278      * or a {@code Boolean} (for other keys)
279      * @return created test files, or {@code null} if no test classes were
280      * created and/or updated
281      * @see #canCreateTests
282      */

283     protected abstract FileObject[] createTests(
284             FileObject[] filesToTest,
285             FileObject targetRoot,
286             Map JavaDoc<CreateTestParam, Object JavaDoc> params);
287
288 }
289
Popular Tags