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 java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Collection; |
48 |
import java.util.HashMap; |
49 |
import java.util.List; |
50 |
import java.util.Map; |
51 |
import org.netbeans.api.java.classpath.ClassPath; |
52 |
import org.netbeans.api.java.source.CompilationInfo; |
53 |
import org.netbeans.api.project.FileOwnerQuery; |
54 |
import org.netbeans.api.project.Project; |
55 |
import org.netbeans.api.project.SourceGroup; |
56 |
import org.netbeans.api.project.SourceGroupModifier; |
57 |
import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider; |
58 |
import org.netbeans.modules.gsf.testrunner.plugin.CommonTestUtilProvider; |
59 |
import org.netbeans.modules.gsf.testrunner.plugin.GuiUtilsProvider; |
60 |
import org.netbeans.modules.gsf.testrunner.plugin.RootsProvider; |
61 |
import org.openide.filesystems.FileObject; |
62 |
import org.openide.filesystems.FileUtil; |
63 |
import org.openide.loaders.DataObject; |
64 |
import org.openide.util.Exceptions; |
65 |
import org.openide.util.Lookup; |
66 |
|
67 |
public class Utils { |
68 |
|
69 |
private static final String TEST_CLASS_SUFFIX = "Test"; //NOI18N |
70 |
private static List<String> testingFrameworks; |
71 |
private static Object[] locations; |
72 |
|
73 |
public static String getLocationText(Object location) { |
74 |
String text = location instanceof SourceGroup |
75 |
? ((SourceGroup) location).getDisplayName() |
76 |
: location instanceof FileObject |
77 |
? FileUtil.getFileDisplayName((FileObject) location) |
78 |
: location.toString(); |
79 |
return text; |
80 |
} |
81 |
|
82 |
public static Map<Object, List<String>> getValidCombinations(CompilationInfo info, String methodName) { |
83 |
populateTestingFrameworks(); |
84 |
if(testingFrameworks.isEmpty()) { |
85 |
return null; |
86 |
} |
87 |
populateLocations(info.getFileObject()); |
88 |
Map<Object, List<String>> validCombinations = new HashMap<Object, List<String>>(); |
89 |
for (Object location : locations) { |
90 |
FileObject targetFolder = Utils.getTargetFolder(location); |
91 |
List<String> framework2Add = new ArrayList<String>(); |
92 |
for (String framework : testingFrameworks) { |
93 |
String preffiledName = Utils.getPreffiledName(info, framework); |
94 |
preffiledName = preffiledName.replaceAll("\\.", "/").concat(".java"); //NOI18N |
95 |
String path = targetFolder.getPath().concat("/").concat(preffiledName); |
96 |
File f = new File(path); |
97 |
FileObject fo = FileUtil.toFileObject(f); |
98 |
if(methodName == null) { |
99 |
if (fo == null) { |
100 |
framework2Add.add(framework); |
101 |
} |
102 |
} else { |
103 |
try { |
104 |
String testMethodName = getTestMethodName(methodName); |
105 |
if (fo != null && !fo.asText().replaceAll("\n", "").trim().contains(testMethodName.concat("("))) { //NOI18N |
106 |
framework2Add.add(framework); |
107 |
} |
108 |
} catch (IOException ex) { |
109 |
Exceptions.printStackTrace(ex); |
110 |
} |
111 |
} |
112 |
} |
113 |
if (!framework2Add.isEmpty()) { |
114 |
validCombinations.put(location, framework2Add); |
115 |
} |
116 |
} |
117 |
testingFrameworks.clear(); |
118 |
locations = null; |
119 |
return validCombinations; |
120 |
} |
121 |
|
122 |
private static String getTestMethodName(String methodName) { |
123 |
return "test" + capitalizeFirstLetter(methodName); //NOI18N |
124 |
} |
125 |
|
126 |
private static String capitalizeFirstLetter(String str) { |
127 |
if (str == null || str.length() <= 0) { |
128 |
return str; |
129 |
} |
130 |
|
131 |
char chars[] = str.toCharArray(); |
132 |
chars[0] = Character.toUpperCase(chars[0]); |
133 |
return new String(chars); |
134 |
} |
135 |
|
136 |
private static void populateTestingFrameworks() { |
137 |
testingFrameworks = new ArrayList<String>(); |
138 |
Collection<? extends Lookup.Item<TestCreatorProvider>> testCreatorProviders = Lookup.getDefault().lookupResult(TestCreatorProvider.class).allItems(); |
139 |
for (Lookup.Item<TestCreatorProvider> provider : testCreatorProviders) { |
140 |
testingFrameworks.add(provider.getDisplayName()); |
141 |
} |
142 |
} |
143 |
|
144 |
private static void populateLocations(FileObject activeFO) { |
145 |
Collection<? extends CommonTestUtilProvider> testUtilProviders = Lookup.getDefault().lookupAll(CommonTestUtilProvider.class); |
146 |
for (CommonTestUtilProvider provider : testUtilProviders) { |
147 |
locations = provider.getTestTargets(activeFO); |
148 |
break; |
149 |
} |
150 |
if (locations != null && locations.length == 0) { |
151 |
Project owner = FileOwnerQuery.getOwner(activeFO); |
152 |
if (owner != null) { |
153 |
String type = ""; |
154 |
String hint = ""; |
155 |
Collection<? extends RootsProvider> rootProviders = Lookup.getDefault().lookupAll(RootsProvider.class); |
156 |
for (RootsProvider rootProvider : rootProviders) { |
157 |
type = rootProvider.getSourceRootType(); |
158 |
hint = rootProvider.getProjectTestsHint(); |
159 |
break; |
160 |
} |
161 |
if (SourceGroupModifier.createSourceGroup(owner, type, hint) != null) { |
162 |
testUtilProviders = Lookup.getDefault().lookupAll(CommonTestUtilProvider.class); |
163 |
for (CommonTestUtilProvider provider : testUtilProviders) { |
164 |
locations = provider.getTestTargets(activeFO); |
165 |
break; |
166 |
} |
167 |
} |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
private static String getPreffiledName(CompilationInfo info, String selectedFramework) { |
173 |
DataObject dataObj = info.getFileObject().getLookup().lookup(DataObject.class); |
174 |
FileObject fileObj = dataObj.getPrimaryFile(); |
175 |
ClassPath cp = ClassPath.getClassPath(fileObj, ClassPath.SOURCE); |
176 |
String className = cp.getResourceName(fileObj, '.', false); |
177 |
return className + getTestingFrameworkSuffix(selectedFramework) + TEST_CLASS_SUFFIX; |
178 |
} |
179 |
|
180 |
private static String getTestingFrameworkSuffix(String selectedFramework) { |
181 |
if (selectedFramework == null) { |
182 |
return ""; |
183 |
} |
184 |
String testngFramework = ""; |
185 |
Collection<? extends GuiUtilsProvider> providers = Lookup.getDefault().lookupAll(GuiUtilsProvider.class); |
186 |
for (GuiUtilsProvider provider : providers) { |
187 |
testngFramework = provider.getTestngFramework(); |
188 |
break; |
189 |
} |
190 |
return selectedFramework.equals(testngFramework) ? "NG" : ""; //NOI18N |
191 |
} |
192 |
|
193 |
private static FileObject getTargetFolder(Object selectedLocation) { |
194 |
if (selectedLocation == null) { |
195 |
return null; |
196 |
} |
197 |
|
198 |
if (selectedLocation instanceof SourceGroup) { |
199 |
return ((SourceGroup) selectedLocation).getRootFolder(); |
200 |
} |
201 |
assert selectedLocation instanceof FileObject; //root folder |
202 |
return (FileObject) selectedLocation; |
203 |
} |
204 |
|
205 |
} |