diff --git a/gsf.testrunner/apichanges.xml b/gsf.testrunner/apichanges.xml new file mode 100644 --- /dev/null +++ b/gsf.testrunner/apichanges.xml @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + Common Test Runner API + + + + + + + + API to open the configuration dialog when creating tests + + + + + + Added API to open the configuration dialog when creating tests, so that it + can be used for example by java.testrunner hints. + + + + + + + + + + + + Options Dialog API changes by date + + + + + + +

Introduction

+ +

This document lists changes made to the Common Test Runner APIs. Please ask on the + nbdev@netbeans.org + mailing list if you have any questions about the details of a + change, or are wondering how to convert existing code to be compatible. +

+ +
+ + +
+

@FOOTER@

+ + +
+
diff --git a/gsf.testrunner/manifest.mf b/gsf.testrunner/manifest.mf --- a/gsf.testrunner/manifest.mf +++ b/gsf.testrunner/manifest.mf @@ -3,5 +3,5 @@ OpenIDE-Module: org.netbeans.modules.gsf.testrunner/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/gsf/testrunner/Bundle.properties OpenIDE-Module-Layer: org/netbeans/modules/gsf/testrunner/layer.xml -OpenIDE-Module-Specification-Version: 1.32 +OpenIDE-Module-Specification-Version: 1.33 diff --git a/gsf.testrunner/nbproject/project.properties b/gsf.testrunner/nbproject/project.properties --- a/gsf.testrunner/nbproject/project.properties +++ b/gsf.testrunner/nbproject/project.properties @@ -1,3 +1,4 @@ is.autoload=true javac.source=1.6 javac.compilerargs=-Xlint -Xlint:-serial +javadoc.apichanges=${basedir}/apichanges.xml diff --git a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/CommonTestsCfgOfCreate.java b/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/CommonTestsCfgOfCreate.java --- a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/CommonTestsCfgOfCreate.java +++ b/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/CommonTestsCfgOfCreate.java @@ -582,6 +582,21 @@ cboxFramework.setModel(new DefaultComboBoxModel(testingFrameworks.toArray())); fireFrameworkChanged(); } + + public void setPreselectedLocation(Object location) { + if (location != null) { + cboxLocation.setSelectedItem(location); + cboxLocation.setEnabled(false); + } + } + + public void setPreselectedFramework(String testingFramework) { + if (testingFramework != null) { + cboxFramework.setSelectedItem(testingFramework); + cboxFramework.setEnabled(false); + setSelectedTestingFramework(); + } + } /** */ diff --git a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/TestCreatorAction.java b/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/TestCreatorAction.java --- a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/TestCreatorAction.java +++ b/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/TestCreatorAction.java @@ -44,20 +44,15 @@ package org.netbeans.modules.gsf.testrunner; -import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import org.netbeans.modules.gsf.testrunner.CommonTestsCfgOfCreate; +import org.netbeans.modules.gsf.testrunner.api.TestCreatorPanelDisplayer; import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider; -import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider.Registration; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.awt.ActionReferences; import org.openide.awt.ActionRegistration; -import org.openide.cookies.SaveCookie; -import org.openide.loaders.DataObject; import org.openide.nodes.Node; -import org.openide.util.Exceptions; import org.openide.util.HelpCtx; import org.openide.util.Lookup; import org.openide.util.NbBundle; @@ -73,7 +68,6 @@ @ActionReference(path = "UI/ToolActions/Java")}) @NbBundle.Messages({"LBL_CreateCommonTestAction=Create Tests"}) public class TestCreatorAction extends NodeAction { - private ArrayList testingFrameworksToAdd = new ArrayList(); /** Creates a new instance of TestCreatorAction */ public TestCreatorAction() { @@ -113,46 +107,7 @@ @Override protected void performAction(Node[] activatedNodes) { - final DataObject[] modified = DataObject.getRegistry().getModified(); - CommonTestsCfgOfCreate cfg = new CommonTestsCfgOfCreate(activatedNodes); - cfg.createCfgPanel(modified.length == 0 ? false : true); - - testingFrameworksToAdd.clear(); - Collection> providers = Lookup.getDefault().lookupResult(TestCreatorProvider.class).allItems(); - for (Lookup.Item provider : providers) { - testingFrameworksToAdd.add(provider.getDisplayName()); - } - cfg.addTestingFrameworks(testingFrameworksToAdd); - if (!cfg.configure()) { - return; - } - saveAll(modified); // #149048 - String selected = cfg.getSelectedTestingFramework(); - - for (Lookup.Item provider : providers) { - if (provider.getDisplayName().equals(selected)) { - TestCreatorProvider.Context context = new TestCreatorProvider.Context(activatedNodes); - context.setSingleClass(cfg.isSingleClass()); - context.setTargetFolder(cfg.getTargetFolder()); - context.setTestClassName(cfg.getTestClassName()); - provider.getInstance().createTests(context); - cfg = null; - break; - } - } - } - - private void saveAll(DataObject[] dataObjects) { - for(DataObject dataObject: dataObjects) { - SaveCookie saveCookie = dataObject.getLookup().lookup(SaveCookie.class); - if(saveCookie != null) { - try { - saveCookie.save(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - } - } + TestCreatorPanelDisplayer.getDefault().displayPanel(activatedNodes, null, null); } } diff --git a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/api/TestCreatorPanelDisplayer.java b/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/api/TestCreatorPanelDisplayer.java new file mode 100644 --- /dev/null +++ b/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/api/TestCreatorPanelDisplayer.java @@ -0,0 +1,111 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2013 Sun Microsystems, Inc. + */ +package org.netbeans.modules.gsf.testrunner.api; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import org.netbeans.modules.gsf.testrunner.CommonTestsCfgOfCreate; +import org.openide.cookies.SaveCookie; +import org.openide.loaders.DataObject; +import org.openide.nodes.Node; +import org.openide.util.Exceptions; +import org.openide.util.Lookup; + +public final class TestCreatorPanelDisplayer { + + private static final TestCreatorPanelDisplayer INSTANCE = new TestCreatorPanelDisplayer(); + + private TestCreatorPanelDisplayer() {} + /** + * Get the default TestCreatorPanelDisplayer + * @return the default instance + */ + public static TestCreatorPanelDisplayer getDefault() { + return INSTANCE; + } + + public void displayPanel(Node[] activatedNodes, Object location, String testingFramework) { + final DataObject[] modified = DataObject.getRegistry().getModified(); + CommonTestsCfgOfCreate cfg = new CommonTestsCfgOfCreate(activatedNodes); + cfg.createCfgPanel(modified.length == 0 ? false : true); + + ArrayList testingFrameworks = new ArrayList(); + Collection> providers = Lookup.getDefault().lookupResult(TestCreatorProvider.class).allItems(); + for (Lookup.Item provider : providers) { + testingFrameworks.add(provider.getDisplayName()); + } + cfg.addTestingFrameworks(testingFrameworks); + cfg.setPreselectedLocation(location); + cfg.setPreselectedFramework(testingFramework); + if (!cfg.configure()) { + return; + } + saveAll(modified); // #149048 + String selected = cfg.getSelectedTestingFramework(); + + for (Lookup.Item provider : providers) { + if (provider.getDisplayName().equals(selected)) { + TestCreatorProvider.Context context = new TestCreatorProvider.Context(activatedNodes); + context.setSingleClass(cfg.isSingleClass()); + context.setTargetFolder(cfg.getTargetFolder()); + context.setTestClassName(cfg.getTestClassName()); + provider.getInstance().createTests(context); + cfg = null; + break; + } + } + } + + private void saveAll(DataObject[] dataObjects) { + for (DataObject dataObject : dataObjects) { + SaveCookie saveCookie = dataObject.getLookup().lookup(SaveCookie.class); + if (saveCookie != null) { + try { + saveCookie.save(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + } +} diff --git a/java.testrunner/manifest.mf b/java.testrunner/manifest.mf --- a/java.testrunner/manifest.mf +++ b/java.testrunner/manifest.mf @@ -2,5 +2,5 @@ AutoUpdate-Show-In-Client: false OpenIDE-Module: org.netbeans.modules.java.testrunner OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/testrunner/Bundle.properties -OpenIDE-Module-Specification-Version: 1.6 +OpenIDE-Module-Specification-Version: 1.7 diff --git a/java.testrunner/nbproject/project.xml b/java.testrunner/nbproject/project.xml --- a/java.testrunner/nbproject/project.xml +++ b/java.testrunner/nbproject/project.xml @@ -15,12 +15,29 @@ + org.netbeans.api.java.classpath + + + + 1 + 1.37 + + + + org.netbeans.libs.javacapi + + + + 7.14 + + + org.netbeans.modules.gsf.testrunner 1 - 1.26 + 1.33 @@ -33,6 +50,14 @@ + org.netbeans.modules.java.source + + + + 0.111 + + + org.netbeans.modules.projectapi @@ -42,6 +67,23 @@ + org.netbeans.spi.editor.hints + + + + 0 + 1.29 + + + + org.netbeans.spi.java.hints + + + + 1.11 + + + org.openide.awt @@ -50,6 +92,14 @@ + org.openide.dialogs + + + + 7.29 + + + org.openide.filesystems diff --git a/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestClassHint.java b/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestClassHint.java new file mode 100644 --- /dev/null +++ b/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestClassHint.java @@ -0,0 +1,167 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2013 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.testrunner.hints; + +import com.sun.source.tree.ClassTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.SourcePositions; +import com.sun.source.util.TreePath; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.source.ClasspathInfo; +import org.netbeans.api.java.source.CompilationInfo; +import org.netbeans.modules.gsf.testrunner.api.TestCreatorPanelDisplayer; +import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider; +import org.netbeans.spi.editor.hints.ChangeInfo; +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ErrorDescriptionFactory; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import org.netbeans.spi.java.hints.TriggerTreeKind; +import org.openide.filesystems.FileObject; +import org.openide.loaders.DataObject; +import org.openide.loaders.DataObjectNotFoundException; +import org.openide.nodes.Node; +import org.openide.util.Exceptions; +import org.openide.util.Lookup; +import org.openide.util.NbBundle.Messages; + +@Hint(displayName = "#DN_CreateTestClassHint", description = "#DESC_CreateTestClassHint", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) +@Messages({ + "DN_CreateTestClassHint=Create Test Class", + "DESC_CreateTestClassHint=Create a test class for the selected source class." +}) +public class CreateTestClassHint { + + @TriggerTreeKind(Tree.Kind.CLASS) + @Messages("ERR_CreateTestClassHint=Create Test Class") + public static ErrorDescription computeWarning(HintContext context) { + TreePath tp = context.getPath(); + ClassTree cls = (ClassTree) tp.getLeaf(); + CompilationInfo info = context.getInfo(); + SourcePositions sourcePositions = info.getTrees().getSourcePositions(); + int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), cls); + int caret = context.getCaretLocation(); + String code = context.getInfo().getText(); + if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) { + return null; + } + + String headerText = code.substring(startPos, caret); + int idx = headerText.indexOf('{'); //NOI18N + if (idx >= 0) { + return null; + } + + ClassPath cp = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE); + FileObject root = cp.findOwnerRoot(info.getFileObject()); + if (root == null) { //File not part of any project + return null; + } + + Collection providers = Lookup.getDefault().lookupAll(TestCreatorProvider.class); + DataObject dataObject; + Node activeNode = null; + try { + dataObject = DataObject.find(info.getFileObject()); + activeNode = dataObject.getNodeDelegate(); + } catch (DataObjectNotFoundException ex) { + Exceptions.printStackTrace(ex); + } + if (activeNode != null) { + Map> validCombinations = Utils.getValidCombinations(info, null); + if(validCombinations == null) { // no TestCreatorProvider found + return null; + } + for (TestCreatorProvider provider : providers) { + if (provider.enable(new Node[]{activeNode}) && !validCombinations.isEmpty()) { + List fixes = new ArrayList(); + Fix fix; + for(Entry> entrySet : validCombinations.entrySet()) { + Object location = entrySet.getKey(); + for(String testingFramework : entrySet.getValue()) { + fix = new CreateTestClassFix(new Node[]{activeNode}, location, testingFramework); + fixes.add(fix); + } + } + validCombinations.clear(); + return ErrorDescriptionFactory.forTree(context, context.getPath(), Bundle.ERR_CreateTestClassHint(), fixes.toArray(new Fix[fixes.size()])); + } + } + validCombinations.clear(); + } + return null; + } + + private static final class CreateTestClassFix implements Fix { + Node[] activatedNodes; + Object location; + String testingFramework; + + public CreateTestClassFix(Node[] activatedNodes, Object location, String testingFramework) { + this.activatedNodes = activatedNodes; + this.location = location; + this.testingFramework = testingFramework; + } + + @Override + @Messages({ + "# {0} - the testing framework to be used, e.g. JUnit, TestNG,...", + "# {1} - the location where the test class will be created", + "FIX_CreateTestClassHint=Create Test Class [{0} in {1}]"}) + public String getText() { + return Bundle.FIX_CreateTestClassHint(testingFramework, Utils.getLocationText(location)); + } + + @Override + public ChangeInfo implement() throws Exception { + TestCreatorPanelDisplayer.getDefault().displayPanel(activatedNodes, location, testingFramework); + return null; + } + } +} diff --git a/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestMethodsHint.java b/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestMethodsHint.java new file mode 100644 --- /dev/null +++ b/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestMethodsHint.java @@ -0,0 +1,173 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2013 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.testrunner.hints; + +import com.sun.source.tree.MethodTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.SourcePositions; +import com.sun.source.util.TreePath; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import javax.lang.model.element.Modifier; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.source.ClasspathInfo; +import org.netbeans.api.java.source.CompilationInfo; +import org.netbeans.modules.gsf.testrunner.api.TestCreatorPanelDisplayer; +import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider; +import org.netbeans.spi.editor.hints.ChangeInfo; +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ErrorDescriptionFactory; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import org.netbeans.spi.java.hints.TriggerTreeKind; +import org.openide.filesystems.FileObject; +import org.openide.loaders.DataObject; +import org.openide.loaders.DataObjectNotFoundException; +import org.openide.nodes.Node; +import org.openide.util.Exceptions; +import org.openide.util.Lookup; +import org.openide.util.NbBundle.Messages; + +@Hint(displayName = "#DN_CreateTestMethodsHint", description = "#DESC_CreateTestMethodsHint", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) +@Messages({ + "DN_CreateTestMethodsHint=Generate All Test Methods", + "DESC_CreateTestMethodsHint=Generate all test methods for the selected source class." +}) +public class CreateTestMethodsHint { + + @TriggerTreeKind(Tree.Kind.METHOD) + @Messages("ERR_CreateTestMethodsHint=Generate All Test Methods") + public static ErrorDescription computeWarning(HintContext context) { + final TreePath tp = context.getPath(); + final MethodTree method = (MethodTree) tp.getLeaf(); + if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) { + return null; + } + String methodName = method.getName().toString(); + + CompilationInfo info = context.getInfo(); + SourcePositions sourcePositions = info.getTrees().getSourcePositions(); + int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), method); + int caret = context.getCaretLocation(); + String code = context.getInfo().getText(); + if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) { + return null; + } + + String headerText = code.substring(startPos, caret); + int idx = headerText.indexOf('{'); //NOI18N + if (idx >= 0) { + return null; + } + + ClassPath cp = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE); + FileObject root = cp.findOwnerRoot(info.getFileObject()); + if (root == null) { //File not part of any project + return null; + } + + Collection providers = Lookup.getDefault().lookupAll(TestCreatorProvider.class); + DataObject dataObject; + Node activeNode = null; + try { + dataObject = DataObject.find(info.getFileObject()); + activeNode = dataObject.getNodeDelegate(); + } catch (DataObjectNotFoundException ex) { + Exceptions.printStackTrace(ex); + } + if (activeNode != null) { + Map> validCombinations = Utils.getValidCombinations(info, methodName); + if(validCombinations == null) { // no TestCreatorProvider found + return null; + } + for (TestCreatorProvider provider : providers) { + if (provider.enable(new Node[]{activeNode}) && !validCombinations.isEmpty()) { + List fixes = new ArrayList(); + Fix fix; + for(Entry> entrySet : validCombinations.entrySet()) { + Object location = entrySet.getKey(); + for(String testingFramework : entrySet.getValue()) { + fix = new CreateTestMethodsFix(new Node[]{activeNode}, location, testingFramework); + fixes.add(fix); + } + } + validCombinations.clear(); + return ErrorDescriptionFactory.forTree(context, context.getPath(), Bundle.ERR_CreateTestMethodsHint(), fixes.toArray(new Fix[fixes.size()])); + } + } + validCombinations.clear(); + } + return null; + } + + private static final class CreateTestMethodsFix implements Fix { + Node[] activatedNodes; + Object location; + String testingFramework; + + public CreateTestMethodsFix(Node[] activatedNodes, Object location, String testingFramework) { + this.activatedNodes = activatedNodes; + this.location = location; + this.testingFramework = testingFramework; + } + + @Override + @Messages({ + "# {0} - the testing framework to be used, e.g. JUnit, TestNG,...", + "# {1} - the location where the test class is", + "FIX_CreateTestMethodsHint=Generate All Test Methods [{0} in {1}]"}) + public String getText() { + return Bundle.FIX_CreateTestMethodsHint(testingFramework, Utils.getLocationText(location)); + } + + @Override + public ChangeInfo implement() throws Exception { + TestCreatorPanelDisplayer.getDefault().displayPanel(activatedNodes, location, testingFramework); + return null; + } + } +} diff --git a/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/Utils.java b/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/Utils.java new file mode 100644 --- /dev/null +++ b/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/Utils.java @@ -0,0 +1,205 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2013 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.testrunner.hints; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.source.CompilationInfo; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.api.project.SourceGroup; +import org.netbeans.api.project.SourceGroupModifier; +import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider; +import org.netbeans.modules.gsf.testrunner.plugin.CommonTestUtilProvider; +import org.netbeans.modules.gsf.testrunner.plugin.GuiUtilsProvider; +import org.netbeans.modules.gsf.testrunner.plugin.RootsProvider; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.loaders.DataObject; +import org.openide.util.Exceptions; +import org.openide.util.Lookup; + +public class Utils { + + private static final String TEST_CLASS_SUFFIX = "Test"; //NOI18N + private static List testingFrameworks; + private static Object[] locations; + + public static String getLocationText(Object location) { + String text = location instanceof SourceGroup + ? ((SourceGroup) location).getDisplayName() + : location instanceof FileObject + ? FileUtil.getFileDisplayName((FileObject) location) + : location.toString(); + return text; + } + + public static Map> getValidCombinations(CompilationInfo info, String methodName) { + populateTestingFrameworks(); + if(testingFrameworks.isEmpty()) { + return null; + } + populateLocations(info.getFileObject()); + Map> validCombinations = new HashMap>(); + for (Object location : locations) { + FileObject targetFolder = Utils.getTargetFolder(location); + List framework2Add = new ArrayList(); + for (String framework : testingFrameworks) { + String preffiledName = Utils.getPreffiledName(info, framework); + preffiledName = preffiledName.replaceAll("\\.", "/").concat(".java"); //NOI18N + String path = targetFolder.getPath().concat("/").concat(preffiledName); + File f = new File(path); + FileObject fo = FileUtil.toFileObject(f); + if(methodName == null) { + if (fo == null) { + framework2Add.add(framework); + } + } else { + try { + String testMethodName = getTestMethodName(methodName); + if (fo != null && !fo.asText().replaceAll("\n", "").trim().contains(testMethodName.concat("("))) { //NOI18N + framework2Add.add(framework); + } + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + if (!framework2Add.isEmpty()) { + validCombinations.put(location, framework2Add); + } + } + testingFrameworks.clear(); + locations = null; + return validCombinations; + } + + private static String getTestMethodName(String methodName) { + return "test" + capitalizeFirstLetter(methodName); //NOI18N + } + + private static String capitalizeFirstLetter(String str) { + if (str == null || str.length() <= 0) { + return str; + } + + char chars[] = str.toCharArray(); + chars[0] = Character.toUpperCase(chars[0]); + return new String(chars); + } + + private static void populateTestingFrameworks() { + testingFrameworks = new ArrayList(); + Collection> testCreatorProviders = Lookup.getDefault().lookupResult(TestCreatorProvider.class).allItems(); + for (Lookup.Item provider : testCreatorProviders) { + testingFrameworks.add(provider.getDisplayName()); + } + } + + private static void populateLocations(FileObject activeFO) { + Collection testUtilProviders = Lookup.getDefault().lookupAll(CommonTestUtilProvider.class); + for (CommonTestUtilProvider provider : testUtilProviders) { + locations = provider.getTestTargets(activeFO); + break; + } + if (locations != null && locations.length == 0) { + Project owner = FileOwnerQuery.getOwner(activeFO); + if (owner != null) { + String type = ""; + String hint = ""; + Collection rootProviders = Lookup.getDefault().lookupAll(RootsProvider.class); + for (RootsProvider rootProvider : rootProviders) { + type = rootProvider.getSourceRootType(); + hint = rootProvider.getProjectTestsHint(); + break; + } + if (SourceGroupModifier.createSourceGroup(owner, type, hint) != null) { + testUtilProviders = Lookup.getDefault().lookupAll(CommonTestUtilProvider.class); + for (CommonTestUtilProvider provider : testUtilProviders) { + locations = provider.getTestTargets(activeFO); + break; + } + } + } + } + } + + private static String getPreffiledName(CompilationInfo info, String selectedFramework) { + DataObject dataObj = info.getFileObject().getLookup().lookup(DataObject.class); + FileObject fileObj = dataObj.getPrimaryFile(); + ClassPath cp = ClassPath.getClassPath(fileObj, ClassPath.SOURCE); + String className = cp.getResourceName(fileObj, '.', false); + return className + getTestingFrameworkSuffix(selectedFramework) + TEST_CLASS_SUFFIX; + } + + private static String getTestingFrameworkSuffix(String selectedFramework) { + if (selectedFramework == null) { + return ""; + } + String testngFramework = ""; + Collection providers = Lookup.getDefault().lookupAll(GuiUtilsProvider.class); + for (GuiUtilsProvider provider : providers) { + testngFramework = provider.getTestngFramework(); + break; + } + return selectedFramework.equals(testngFramework) ? "NG" : ""; //NOI18N + } + + private static FileObject getTargetFolder(Object selectedLocation) { + if (selectedLocation == null) { + return null; + } + + if (selectedLocation instanceof SourceGroup) { + return ((SourceGroup) selectedLocation).getRootFolder(); + } + assert selectedLocation instanceof FileObject; //root folder + return (FileObject) selectedLocation; + } + +}