001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2006-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.junit.plugin;
043:
044: //import java.util.Collections;
045: //import java.util.EnumSet;
046: import java.awt.EventQueue;
047: import java.util.Map; //import java.util.Set;
048: //import javax.lang.model.element.Element;
049: //import javax.lang.model.element.ElementKind;
050: //import org.netbeans.api.java.source.ElementHandle;
051: import org.netbeans.modules.junit.JUnitPluginTrampoline;
052: import org.openide.filesystems.FileObject;
053:
054: /**
055: * SPI for custom implementations of support for JUnit.
056: * It declares methods for:
057: * <ul>
058: * <li>navigation between source classes and corresponding test classes
059: * ({@link #getTestLocation getTestLocation},
060: * {@link #getTestedLocation getTestedLocation})</li>
061: * <li>creation of test class skeletons
062: * ({@link #createTests createTests})</li>
063: * </ul>
064: *
065: * @author Marian Petras
066: */
067: public abstract class JUnitPlugin {
068:
069: static {
070: JUnitPluginTrampoline.DEFAULT = new JUnitPluginTrampoline() {
071: public boolean createTestActionCalled(JUnitPlugin plugin,
072: FileObject[] filesToTest) {
073: return plugin.createTestActionCalled(filesToTest);
074: }
075:
076: public FileObject[] createTests(JUnitPlugin plugin,
077: FileObject[] filesToTest, FileObject targetRoot,
078: Map<CreateTestParam, Object> params) {
079: return plugin.createTests(filesToTest, targetRoot,
080: params);
081: }
082:
083: public Location getTestLocation(JUnitPlugin plugin,
084: Location sourceLocation) {
085: return plugin.getTestLocation(sourceLocation);
086: }
087:
088: public Location getTestedLocation(JUnitPlugin plugin,
089: Location testLocation) {
090: return plugin.getTestedLocation(testLocation);
091: }
092:
093: public boolean canCreateTests(JUnitPlugin plugin,
094: FileObject... fileObjects) {
095: return plugin.canCreateTests(fileObjects);
096: }
097: };
098: }
099:
100: /**
101: * Default constructor for use by subclasses.
102: */
103: protected JUnitPlugin() {
104: }
105:
106: /**
107: * Enumeration of test creation parameters.
108: */
109: public enum CreateTestParam {
110:
111: /**
112: * key for the map of test creation parameters
113: * - name of the test class
114: */
115: CLASS_NAME(99310),
116: /**
117: * key for the map of test creation parameters
118: * - include tests for public methods?
119: */
120: INC_PUBLIC(99311),
121: /**
122: * key for the map of test creation parameters
123: * - include tests for protected methods?
124: */
125: INC_PROTECTED(99312),
126: /**
127: * key for the map of test creation parameters
128: * - include tests for package-private methods?
129: */
130: INC_PKG_PRIVATE(99313),
131: /**
132: * key for the map of test creation parameters
133: * - generate test initializer method ({@code setup()}/{@code @Before})?
134: */
135: INC_SETUP(99314),
136: /**
137: * key for the map of test creation parameters
138: * - generate test finalizer method ({@code tearDown()}/{@code @After})?
139: */
140: INC_TEAR_DOWN(99315),
141: /**
142: * key for the map of test creation parameters
143: * - generate test class initializer method ({@code @BeforeClass})?
144: */
145: INC_CLASS_SETUP(99323),
146: /**
147: * key for the map of test creation parameters
148: * - generate test class finalizer method ({@code @AfterClass})?
149: */
150: INC_CLASS_TEAR_DOWN(99324),
151: /**
152: * key for the map of test creation parameters
153: * - generate default test method bodies?
154: */
155: INC_METHOD_BODIES(99316),
156: /**
157: * key for the map of test creation parameters
158: * - generate Javadoc comments for test methods?
159: */
160: INC_JAVADOC(99317),
161: /**
162: * key for the map of test creation parameters
163: * - generate source code hints?
164: */
165: INC_CODE_HINT(99318),
166: /**
167: * key for the map of test creation parameters
168: * - generate test classes for package-private classes?
169: */
170: INC_PKG_PRIVATE_CLASS(99319),
171: /**
172: * key for the map of test creation parameters
173: * - generate test classes for abstract classes?
174: */
175: INC_ABSTRACT_CLASS(99320),
176: /**
177: * key for the map of test creation parameters
178: * - generate test classes for exception classes?
179: */
180: INC_EXCEPTION_CLASS(99321),
181: /**
182: * key for the map of test creation parameters
183: * - generate test suites for packages?
184: */
185: INC_GENERATE_SUITE(99322);
186:
187: private final int idNumber;
188:
189: CreateTestParam(int idNumber) {
190: this .idNumber = idNumber;
191: }
192:
193: /**
194: * Return a unique number of this enum element.
195: *
196: * @return unique number of this enum element
197: */
198: public int getIdNumber() {
199: return idNumber;
200: }
201:
202: }
203:
204: /**
205: * Data structure for storage of specification of a Java element or
206: * a Java file.
207: */
208: public static final class Location {
209: //** */
210: //public static final Set<ElementKind> CLASS_LIKE_ELEM_TYPES;
211: //** */
212: //public static final Set<ElementKind> SUPPORTED_ELEM_TYPES;
213: /**
214: * holds specification of a Java file
215: */
216: private final FileObject fileObject;
217:
218: // /**
219: // */
220: // private final ElementHandle<Element> elementHandle;
221: //
222: // static {
223: // CLASS_LIKE_ELEM_TYPES = EnumSet.of(ElementKind.CLASS,
224: // ElementKind.INTERFACE,
225: // ElementKind.ENUM);
226: // EnumSet<ElementKind> elemTypes;
227: // elemTypes = EnumSet.copyOf(CLASS_LIKE_ELEM_TYPES);
228: // elemTypes.addAll(EnumSet.of(ElementKind.METHOD,
229: // ElementKind.CONSTRUCTOR,
230: // ElementKind.STATIC_INIT));
231: // SUPPORTED_ELEM_TYPES = Collections.unmodifiableSet(elemTypes);
232: // }
233:
234: /**
235: * Creates a new instance.
236: *
237: * @param fileObject the {@code FileObject}
238: *
239: *
240: *
241: */
242: public Location(FileObject fileObject/*,
243: Element element*/) {
244: if (fileObject == null) {
245: throw new IllegalArgumentException("fileObject is null");//NOI18N
246: }
247:
248: // while ((element != null)
249: // && !SUPPORTED_ELEM_TYPES.contains(element.getKind())) {
250: // element = element.getEnclosingElement();
251: // }
252:
253: this .fileObject = fileObject;
254: //this.elementHandle = (element != null)
255: // ? ElementHandle.create(element)
256: // : null;
257: }
258:
259: /**
260: * Returns the {@code FileObject}.
261: *
262: * @return the {@code FileObject} held in this instance
263: */
264: public FileObject getFileObject() {
265: return fileObject;
266: }
267:
268: // /**
269: // */
270: // public ElementHandle<Element> getElementHandle() {
271: // return elementHandle;
272: // }
273:
274: }
275:
276: /**
277: * Returns a specification of a Java element or file representing test
278: * for the given source Java element or file.
279: *
280: * @param sourceLocation specification of a Java element or file
281: * @return specification of a corresponding test Java element or file,
282: * or {@code null} if no corresponding test Java file is available
283: */
284: protected abstract Location getTestLocation(Location sourceLocation);
285:
286: /**
287: * Returns a specification of a Java element or file that is tested
288: * by the given test Java element or test file.
289: *
290: * @param testLocation specification of a Java element or file
291: * @return specification of a Java element or file that is tested
292: * by the given Java element or file.
293: */
294: protected abstract Location getTestedLocation(Location testLocation);
295:
296: /**
297: * Informs whether the plugin is capable of creating tests at the moment.
298: * The default implementation returns {@code true}.
299: *
300: * @return {@code true} if the plugin is able of creating tests
301: * for the given {@code FileObject}s, {@code false} otherwise
302: * @see #createTests
303: */
304: protected boolean canCreateTests(FileObject... fileObjects) {
305: return true;
306: }
307:
308: /**
309: * Creates test classes for given source classes.
310: * If the plugin does not support creating tests, implementation of this
311: * method should return {@code null}.
312: *
313: * @param filesToTest source files for which test classes should be
314: * created
315: * @param targetRoot root folder of the target source root
316: * @param params parameters of creating test class
317: * - each key is an {@code Integer} whose value is equal
318: * to some of the constants defined in the class;
319: * the value is either
320: * a {@code String} (for key with value {@code CLASS_NAME})
321: * or a {@code Boolean} (for other keys)
322: * @return created test files, or {@code null} if no test classes were
323: * created and/or updated
324: * @see #canCreateTests
325: */
326: protected abstract FileObject[] createTests(
327: FileObject[] filesToTest, FileObject targetRoot,
328: Map<CreateTestParam, Object> params);
329:
330: // /**
331: // * Determines whether the "create JUnit tests" functionality
332: // * should be enabled.
333: // * Before this method is called, other common pre-requisites are checked
334: // * (only Java classes or folders selected, all of them from the same source
335: // * of a Java project, all of them being valid {@code DataObject}s).
336: // * If some of the pre-requisites are not met, the functionality is disabled
337: // * and this method is not called.
338: // *
339: // * @return {@code true} if this action should be enabled,
340: // * {@code false} otherwise;
341: // * the default implementation returns always {@code true}
342: // */
343: // protected boolean canCreateTests() {
344: // return true;
345: // }
346:
347: /**
348: * Called immediately after the <em>Create Test</em> action was called.
349: * It can be used as a trigger for additional checks and/or for displaying
350: * user dialogs etc. It is always called from the event-dispatching thread.
351: *
352: * @param selectedFiles files and folders/packages that were selected
353: * when the action was called
354: * @return {@code true} if the action can continue,
355: * {@code false} if the action should not continue;
356: * the default implementation returns always {@code true}
357: */
358: protected boolean createTestActionCalled(FileObject[] selectedFiles) {
359: assert EventQueue.isDispatchThread();
360:
361: return true;
362: }
363:
364: }
|