Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.java.testrunner.hints; |
43 |
|
44 |
import com.sun.source.tree.ClassTree; |
45 |
import com.sun.source.tree.MethodTree; |
46 |
import com.sun.source.tree.Tree; |
47 |
import com.sun.source.util.SourcePositions; |
48 |
import com.sun.source.util.TreePath; |
49 |
import java.io.File; |
50 |
import java.io.IOException; |
51 |
import java.util.ArrayList; |
52 |
import java.util.Collection; |
53 |
import java.util.HashMap; |
54 |
import java.util.Map.Entry; |
55 |
import javax.lang.model.element.Modifier; |
56 |
import javax.lang.model.element.TypeElement; |
57 |
import org.netbeans.api.java.classpath.ClassPath; |
58 |
import org.netbeans.api.java.source.ClasspathInfo; |
59 |
import org.netbeans.api.java.source.CompilationInfo; |
60 |
import org.netbeans.api.java.source.support.CaretAwareJavaSourceTaskFactory; |
61 |
import org.netbeans.api.project.FileOwnerQuery; |
62 |
import org.netbeans.api.project.Project; |
63 |
import org.netbeans.api.project.SourceGroup; |
64 |
import org.netbeans.api.project.SourceGroupModifier; |
65 |
import org.netbeans.modules.gsf.testrunner.api.TestCreatorPanelDisplayer; |
66 |
import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider; |
67 |
import org.netbeans.modules.gsf.testrunner.plugin.CommonTestUtilProvider; |
68 |
import org.netbeans.modules.gsf.testrunner.plugin.GuiUtilsProvider; |
69 |
import org.netbeans.modules.gsf.testrunner.plugin.RootsProvider; |
70 |
import org.netbeans.spi.editor.hints.ErrorDescription; |
71 |
import org.netbeans.spi.editor.hints.Fix; |
72 |
import org.netbeans.spi.editor.hints.Severity; |
73 |
import org.netbeans.spi.java.hints.ErrorDescriptionFactory; |
74 |
import org.netbeans.spi.java.hints.Hint; |
75 |
import org.netbeans.spi.java.hints.HintContext; |
76 |
import org.netbeans.spi.java.hints.JavaFix; |
77 |
import org.netbeans.spi.java.hints.TriggerTreeKind; |
78 |
import org.openide.filesystems.FileObject; |
79 |
import org.openide.filesystems.FileUtil; |
80 |
import org.openide.loaders.DataObject; |
81 |
import org.openide.loaders.DataObjectNotFoundException; |
82 |
import org.openide.nodes.Node; |
83 |
import org.openide.util.Exceptions; |
84 |
import org.openide.util.Lookup; |
85 |
import org.openide.util.NbBundle.Messages; |
86 |
|
87 |
@Hint(displayName = "#DN_CreateTestMethodsHint", description = "#DESC_CreateTestMethodsHint", category = "general" |
88 |
, hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) |
89 |
@Messages({ |
90 |
"DN_CreateTestMethodsHint=Generate All Test Methods", |
91 |
"DESC_CreateTestMethodsHint=Generate all test methods for the selected source class" |
92 |
}) |
93 |
public class CreateTestMethodsHint { |
94 |
|
95 |
private static HashMap<Object, ArrayList<String>> validCombinations; |
96 |
|
97 |
@TriggerTreeKind(Tree.Kind.METHOD) |
98 |
@Messages("ERR_CreateTestMethodsHint=Generate All Test Methods") |
99 |
public static ErrorDescription computeWarning(HintContext context) { |
100 |
final TreePath tp = context.getPath(); |
101 |
final MethodTree method = (MethodTree) tp.getLeaf(); |
102 |
if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) { |
103 |
return null; |
104 |
} |
105 |
String methodName = method.getName().toString(); |
106 |
|
107 |
CompilationInfo info = context.getInfo(); |
108 |
SourcePositions sourcePositions = info.getTrees().getSourcePositions(); |
109 |
int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), method); |
110 |
int caret = CaretAwareJavaSourceTaskFactory.getLastPosition(context.getInfo().getFileObject()); |
111 |
String code = context.getInfo().getText(); |
112 |
if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) { |
113 |
return null; |
114 |
} |
115 |
|
116 |
String headerText = code.substring(startPos, caret); |
117 |
int idx = headerText.indexOf('{'); //NOI18N |
118 |
if (idx >= 0) { |
119 |
return null; |
120 |
} |
121 |
|
122 |
ClassPath cp = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE); |
123 |
FileObject root = cp.findOwnerRoot(info.getFileObject()); |
124 |
if (root == null) { //File not part of any project |
125 |
return null; |
126 |
} |
127 |
|
128 |
Collection<? extends Lookup.Item<TestCreatorProvider>> providers = Lookup.getDefault().lookupResult(TestCreatorProvider.class).allItems(); |
129 |
boolean enable; |
130 |
DataObject dataObject; |
131 |
Node activeNode = null; |
132 |
try { |
133 |
dataObject = DataObject.find(info.getFileObject()); |
134 |
activeNode = dataObject.getNodeDelegate(); |
135 |
} catch (DataObjectNotFoundException ex) { |
136 |
Exceptions.printStackTrace(ex); |
137 |
} |
138 |
if (activeNode != null) { |
139 |
validCombinations = Utils.getValidCombinations(info, methodName); |
140 |
|
141 |
for (Lookup.Item<TestCreatorProvider> provider : providers) { |
142 |
enable = provider.getInstance().enable(new Node[]{activeNode}); |
143 |
if (enable && !validCombinations.isEmpty()) { |
144 |
ArrayList<Fix> fixes = new ArrayList<Fix>(); |
145 |
Fix fix; |
146 |
for(Entry<Object, ArrayList<String>> entrySet : validCombinations.entrySet()) { |
147 |
Object location = entrySet.getKey(); |
148 |
for(String testingFramework : entrySet.getValue()) { |
149 |
fix = new CreateTestMethodsFix(context.getInfo(), context.getPath(), new Node[]{activeNode}, |
150 |
location, testingFramework).toEditorFix(); |
151 |
fixes.add(fix); |
152 |
} |
153 |
} |
154 |
return ErrorDescriptionFactory.forTree(context, context.getPath(), Bundle.ERR_CreateTestMethodsHint(), |
155 |
fixes.toArray(new Fix[fixes.size()])); |
156 |
} |
157 |
} |
158 |
} |
159 |
return null; |
160 |
} |
161 |
|
162 |
private static final class CreateTestMethodsFix extends JavaFix { |
163 |
|
164 |
CompilationInfo info; |
165 |
Node[] activatedNodes; |
166 |
Object location; |
167 |
String testingFramework; |
168 |
|
169 |
public CreateTestMethodsFix(CompilationInfo info, TreePath tp, Node[] activatedNodes, Object location, String testingFramework) { |
170 |
super(info, tp); |
171 |
this.info = info; |
172 |
this.activatedNodes = activatedNodes; |
173 |
this.location = location; |
174 |
this.testingFramework = testingFramework; |
175 |
} |
176 |
|
177 |
@Override |
178 |
@Messages({ |
179 |
"# {0} - the testing framework to be used, e.g. JUnit, TestNG,...", |
180 |
"# {1} - the location where the test class is", |
181 |
"FIX_CreateTestMethodsHint=Generate All Test Methods [{0} in {1}]"}) |
182 |
protected String getText() { |
183 |
return Bundle.FIX_CreateTestMethodsHint(testingFramework, Utils.getLocationText(location)); |
184 |
} |
185 |
|
186 |
@Override |
187 |
protected void performRewrite(TransformationContext ctx) { |
188 |
TestCreatorPanelDisplayer.getDefault().displayPanel(activatedNodes, location, testingFramework); |
189 |
} |
190 |
} |
191 |
} |