This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 162593
Collapse All | Expand All

(-)a/gsf.testrunner/apichanges.xml (+132 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!--
3
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
5
Copyright 2013 Oracle and/or its affiliates. All rights reserved.
6
7
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
8
Other names may be trademarks of their respective owners.
9
10
The contents of this file are subject to the terms of either the GNU
11
General Public License Version 2 only ("GPL") or the Common
12
Development and Distribution License("CDDL") (collectively, the
13
"License"). You may not use this file except in compliance with the
14
License. You can obtain a copy of the License at
15
http://www.netbeans.org/cddl-gplv2.html
16
or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
17
specific language governing permissions and limitations under the
18
License.  When distributing the software, include this License Header
19
Notice in each file and include the License file at
20
nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
21
particular file as subject to the "Classpath" exception as provided
22
by Oracle in the GPL Version 2 section of the License file that
23
accompanied this code. If applicable, add the following below the
24
License Header, with the fields enclosed by brackets [] replaced by
25
your own identifying information:
26
"Portions Copyrighted [year] [name of copyright owner]"
27
28
If you wish your version of this file to be governed by only the CDDL
29
or only the GPL Version 2, indicate your decision by adding
30
"[Contributor] elects to include this software in this distribution
31
under the [CDDL or GPL Version 2] license." If you do not indicate a
32
single choice of license, a recipient has the option to distribute
33
your version of this file under either the CDDL, the GPL Version 2 or
34
to extend the choice of license to its licensees as provided above.
35
However, if you add GPL Version 2 code and therefore, elected the GPL
36
Version 2 license, then the option applies only if the new code is
37
made subject to such option by the copyright holder.
38
39
Contributor(s):
40
41
Portions Copyrighted 2013 Sun Microsystems, Inc.
42
-->
43
44
45
<?xml-stylesheet href="../nbbuild/javadoctools/apichanges.xsl" type="text/xsl"?>
46
<!DOCTYPE apichanges PUBLIC "-//NetBeans//DTD API changes list 1.0//EN" "../nbbuild/javadoctools/apichanges.dtd">
47
48
<!-- INFO FOR PEOPLE ADDING CHANGES:
49
[most of contents snipped - see openide's apichanges for how-to instructions]
50
<change>
51
    <api name="compiler"/>
52
    <summary>Some brief description here, can use <b>XHTML</b></summary>
53
    <version major="1" minor="99"/>
54
    <date day="13" month="6" year="2001"/>
55
    <author login="jrhacker"/>
56
    <compatibility addition="yes"/>
57
    <description>
58
        The main description of the change here.
59
        Again can use full <b>XHTML</b> as needed.
60
    </description>
61
    <class package="org.openide.compiler" name="DoWhatIWantCompiler"/>
62
    <issue number="14309"/>
63
</change>
64
-->
65
66
<apichanges>
67
68
<!-- First, a list of API names you may use: -->
69
<apidefs>
70
        <apidef name="CommonTestrunnerAPI">Common Test Runner API</apidef>
71
</apidefs>
72
73
<!-- ACTUAL CHANGES BEGIN HERE: -->
74
75
<changes>
76
    <change id="TestCreatorPanelDisplayer">
77
        <api name="CommonTestrunnerAPI"/>
78
        <summary>API to open the configuration dialog when creating tests</summary>
79
        <version major="1" minor="33"/>
80
        <date day="13" month="2" year="2013"/>
81
        <author login="theofanis"/>
82
        <compatibility addition="yes"/>
83
        <description>
84
            Added API to open the configuration dialog when creating tests, so that it
85
	    can be used for example by java.testrunner hints.
86
        </description>
87
        <class package="org.netbeans.modules.gsf.testrunner.api" name="TestCreatorPanelDisplayer"/>
88
        <issue number="162593"/>
89
    </change>
90
</changes>
91
92
  <!-- Now the surrounding HTML text and document structure: -->
93
94
  <htmlcontents>
95
<!--
96
97
                            NO NO NO NO NO!
98
99
         ==============>    DO NOT EDIT ME!  <======================
100
101
          AUTOMATICALLY GENERATED FROM APICHANGES.XML, DO NOT EDIT
102
103
                SEE xml/api/doc/changes/apichanges.xml
104
105
	-->
106
	<head>
107
	    <title>Options Dialog API changes by date</title>
108
	    <link rel="stylesheet" href="prose.css" type="text/css"/>
109
	</head>
110
	<body>
111
112
	    <p class="overviewlink">
113
		<a href="overview-summary.html">Overview</a>
114
	    </p>
115
116
	    <h1>Introduction</h1>
117
118
	    <p>This document lists changes made to the Common Test Runner APIs. Please ask on the
119
		<code>nbdev@netbeans.org</code>
120
		mailing list if you have any questions about the details of a
121
		change, or are wondering how to convert existing code to be compatible.
122
	    </p>
123
124
	    <hr/>
125
	    <standard-changelists module-code-name="org.netbeans.modules.gsf.testrunner.api/1"/>
126
127
	    <hr/>
128
	    <p>@FOOTER@</p>
129
130
	</body>
131
    </htmlcontents>
132
</apichanges>
(-)a/gsf.testrunner/manifest.mf (-1 / +1 lines)
Lines 3-7 Link Here
3
OpenIDE-Module: org.netbeans.modules.gsf.testrunner/1
3
OpenIDE-Module: org.netbeans.modules.gsf.testrunner/1
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/gsf/testrunner/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/gsf/testrunner/Bundle.properties
5
OpenIDE-Module-Layer: org/netbeans/modules/gsf/testrunner/layer.xml
5
OpenIDE-Module-Layer: org/netbeans/modules/gsf/testrunner/layer.xml
6
OpenIDE-Module-Specification-Version: 1.32
6
OpenIDE-Module-Specification-Version: 1.33
7
7
(-)a/gsf.testrunner/nbproject/project.properties (+1 lines)
Lines 1-3 Link Here
1
is.autoload=true
1
is.autoload=true
2
javac.source=1.6
2
javac.source=1.6
3
javac.compilerargs=-Xlint -Xlint:-serial
3
javac.compilerargs=-Xlint -Xlint:-serial
4
javadoc.apichanges=${basedir}/apichanges.xml
(-)a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/CommonTestsCfgOfCreate.java (+15 lines)
Lines 582-587 Link Here
582
        cboxFramework.setModel(new DefaultComboBoxModel(testingFrameworks.toArray()));
582
        cboxFramework.setModel(new DefaultComboBoxModel(testingFrameworks.toArray()));
583
        fireFrameworkChanged();
583
        fireFrameworkChanged();
584
    }
584
    }
585
586
    public void setPreselectedLocation(Object location) {
587
	if (location != null) {
588
	    cboxLocation.setSelectedItem(location);
589
	    cboxLocation.setEnabled(false);
590
	}
591
    }
592
593
    public void setPreselectedFramework(String testingFramework) {
594
	if (testingFramework != null) {
595
	    cboxFramework.setSelectedItem(testingFramework);
596
	    cboxFramework.setEnabled(false);
597
	    setSelectedTestingFramework();
598
	}
599
    }
585
    
600
    
586
    /**
601
    /**
587
     */
602
     */
(-)a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/TestCreatorAction.java (-47 / +2 lines)
Lines 44-63 Link Here
44
44
45
package org.netbeans.modules.gsf.testrunner;
45
package org.netbeans.modules.gsf.testrunner;
46
46
47
import java.io.IOException;
48
import java.util.ArrayList;
47
import java.util.ArrayList;
49
import java.util.Collection;
48
import java.util.Collection;
50
import org.netbeans.modules.gsf.testrunner.CommonTestsCfgOfCreate;
49
import org.netbeans.modules.gsf.testrunner.api.TestCreatorPanelDisplayer;
51
import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider;
50
import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider;
52
import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider.Registration;
53
import org.openide.awt.ActionID;
51
import org.openide.awt.ActionID;
54
import org.openide.awt.ActionReference;
52
import org.openide.awt.ActionReference;
55
import org.openide.awt.ActionReferences;
53
import org.openide.awt.ActionReferences;
56
import org.openide.awt.ActionRegistration;
54
import org.openide.awt.ActionRegistration;
57
import org.openide.cookies.SaveCookie;
58
import org.openide.loaders.DataObject;
59
import org.openide.nodes.Node;
55
import org.openide.nodes.Node;
60
import org.openide.util.Exceptions;
61
import org.openide.util.HelpCtx;
56
import org.openide.util.HelpCtx;
62
import org.openide.util.Lookup;
57
import org.openide.util.Lookup;
63
import org.openide.util.NbBundle;
58
import org.openide.util.NbBundle;
Lines 73-79 Link Here
73
    @ActionReference(path = "UI/ToolActions/Java")})
68
    @ActionReference(path = "UI/ToolActions/Java")})
74
@NbBundle.Messages({"LBL_CreateCommonTestAction=Create Tests"})
69
@NbBundle.Messages({"LBL_CreateCommonTestAction=Create Tests"})
75
public class TestCreatorAction extends NodeAction {
70
public class TestCreatorAction extends NodeAction {
76
    private ArrayList<String> testingFrameworksToAdd = new ArrayList<String>();
77
    
71
    
78
    /** Creates a new instance of TestCreatorAction */
72
    /** Creates a new instance of TestCreatorAction */
79
    public TestCreatorAction() {
73
    public TestCreatorAction() {
Lines 113-158 Link Here
113
107
114
    @Override
108
    @Override
115
    protected void performAction(Node[] activatedNodes) {
109
    protected void performAction(Node[] activatedNodes) {
116
        final DataObject[] modified = DataObject.getRegistry().getModified();
110
	TestCreatorPanelDisplayer.getDefault().displayPanel(activatedNodes, null, null);
117
        CommonTestsCfgOfCreate cfg = new CommonTestsCfgOfCreate(activatedNodes);
118
        cfg.createCfgPanel(modified.length == 0 ? false : true);
119
        
120
        testingFrameworksToAdd.clear();
121
        Collection<? extends Lookup.Item<TestCreatorProvider>> providers = Lookup.getDefault().lookupResult(TestCreatorProvider.class).allItems();
122
        for (Lookup.Item<TestCreatorProvider> provider : providers) {
123
            testingFrameworksToAdd.add(provider.getDisplayName());
124
        }
125
        cfg.addTestingFrameworks(testingFrameworksToAdd);
126
        if (!cfg.configure()) {
127
            return;
128
        }
129
        saveAll(modified); // #149048
130
        String selected = cfg.getSelectedTestingFramework();
131
        
132
        for (Lookup.Item<TestCreatorProvider> provider : providers) {
133
            if (provider.getDisplayName().equals(selected)) {
134
                TestCreatorProvider.Context context = new TestCreatorProvider.Context(activatedNodes);
135
                context.setSingleClass(cfg.isSingleClass());
136
                context.setTargetFolder(cfg.getTargetFolder());
137
                context.setTestClassName(cfg.getTestClassName());
138
                provider.getInstance().createTests(context);
139
                cfg = null;
140
                break;
141
            }
142
        }
143
    }
144
145
    private void saveAll(DataObject[] dataObjects) {
146
        for(DataObject dataObject: dataObjects) {
147
            SaveCookie saveCookie = dataObject.getLookup().lookup(SaveCookie.class);
148
            if(saveCookie != null) {
149
                try {
150
                    saveCookie.save();
151
                } catch (IOException ex) {
152
                    Exceptions.printStackTrace(ex);
153
                }
154
            }
155
        }
156
    }
111
    }
157
    
112
    
158
}
113
}
(-)a/gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/api/TestCreatorPanelDisplayer.java (+111 lines)
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.gsf.testrunner.api;
43
44
import java.io.IOException;
45
import java.util.ArrayList;
46
import java.util.Collection;
47
import org.netbeans.modules.gsf.testrunner.CommonTestsCfgOfCreate;
48
import org.openide.cookies.SaveCookie;
49
import org.openide.loaders.DataObject;
50
import org.openide.nodes.Node;
51
import org.openide.util.Exceptions;
52
import org.openide.util.Lookup;
53
54
public final class TestCreatorPanelDisplayer {
55
56
    private static final TestCreatorPanelDisplayer INSTANCE = new TestCreatorPanelDisplayer();
57
58
    private TestCreatorPanelDisplayer() {}
59
    /**
60
     * Get the default <code>TestCreatorPanelDisplayer</code>
61
     * @return the default instance
62
     */
63
    public static TestCreatorPanelDisplayer getDefault() {
64
        return INSTANCE;
65
    }
66
67
    public void displayPanel(Node[] activatedNodes, Object location, String testingFramework) {
68
	final DataObject[] modified = DataObject.getRegistry().getModified();
69
	CommonTestsCfgOfCreate cfg = new CommonTestsCfgOfCreate(activatedNodes);
70
	cfg.createCfgPanel(modified.length == 0 ? false : true);
71
72
	ArrayList<String> testingFrameworks = new ArrayList<String>();
73
	Collection<? extends Lookup.Item<TestCreatorProvider>> providers = Lookup.getDefault().lookupResult(TestCreatorProvider.class).allItems();
74
	for (Lookup.Item<TestCreatorProvider> provider : providers) {
75
	    testingFrameworks.add(provider.getDisplayName());
76
	}
77
	cfg.addTestingFrameworks(testingFrameworks);
78
	cfg.setPreselectedLocation(location);
79
	cfg.setPreselectedFramework(testingFramework);
80
	if (!cfg.configure()) {
81
	    return;
82
	}
83
	saveAll(modified); // #149048
84
	String selected = cfg.getSelectedTestingFramework();
85
86
	for (Lookup.Item<TestCreatorProvider> provider : providers) {
87
	    if (provider.getDisplayName().equals(selected)) {
88
		TestCreatorProvider.Context context = new TestCreatorProvider.Context(activatedNodes);
89
		context.setSingleClass(cfg.isSingleClass());
90
		context.setTargetFolder(cfg.getTargetFolder());
91
		context.setTestClassName(cfg.getTestClassName());
92
		provider.getInstance().createTests(context);
93
		cfg = null;
94
		break;
95
	    }
96
	}
97
    }
98
99
    private void saveAll(DataObject[] dataObjects) {
100
	for (DataObject dataObject : dataObjects) {
101
	    SaveCookie saveCookie = dataObject.getLookup().lookup(SaveCookie.class);
102
	    if (saveCookie != null) {
103
		try {
104
		    saveCookie.save();
105
		} catch (IOException ex) {
106
		    Exceptions.printStackTrace(ex);
107
		}
108
	    }
109
	}
110
    }
111
}
(-)a/java.testrunner/manifest.mf (-1 / +1 lines)
Lines 2-6 Link Here
2
AutoUpdate-Show-In-Client: false
2
AutoUpdate-Show-In-Client: false
3
OpenIDE-Module: org.netbeans.modules.java.testrunner
3
OpenIDE-Module: org.netbeans.modules.java.testrunner
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/testrunner/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/testrunner/Bundle.properties
5
OpenIDE-Module-Specification-Version: 1.6
5
OpenIDE-Module-Specification-Version: 1.7
6
6
(-)a/java.testrunner/nbproject/project.xml (-1 / +51 lines)
Lines 15-26 Link Here
15
                    </run-dependency>
15
                    </run-dependency>
16
                </dependency>
16
                </dependency>
17
                <dependency>
17
                <dependency>
18
                    <code-name-base>org.netbeans.api.java.classpath</code-name-base>
19
                    <build-prerequisite/>
20
                    <compile-dependency/>
21
                    <run-dependency>
22
                        <release-version>1</release-version>
23
                        <specification-version>1.37</specification-version>
24
                    </run-dependency>
25
                </dependency>
26
                <dependency>
27
                    <code-name-base>org.netbeans.libs.javacapi</code-name-base>
28
                    <build-prerequisite/>
29
                    <compile-dependency/>
30
                    <run-dependency>
31
                        <specification-version>7.14</specification-version>
32
                    </run-dependency>
33
                </dependency>
34
                <dependency>
18
                    <code-name-base>org.netbeans.modules.gsf.testrunner</code-name-base>
35
                    <code-name-base>org.netbeans.modules.gsf.testrunner</code-name-base>
19
                    <build-prerequisite/>
36
                    <build-prerequisite/>
20
                    <compile-dependency/>
37
                    <compile-dependency/>
21
                    <run-dependency>
38
                    <run-dependency>
22
                        <release-version>1</release-version>
39
                        <release-version>1</release-version>
23
                        <specification-version>1.26</specification-version>
40
                        <specification-version>1.33</specification-version>
24
                    </run-dependency>
41
                    </run-dependency>
25
                </dependency>
42
                </dependency>
26
                <dependency>
43
                <dependency>
Lines 33-38 Link Here
33
                    </run-dependency>
50
                    </run-dependency>
34
                </dependency>
51
                </dependency>
35
                <dependency>
52
                <dependency>
53
                    <code-name-base>org.netbeans.modules.java.source</code-name-base>
54
                    <build-prerequisite/>
55
                    <compile-dependency/>
56
                    <run-dependency>
57
                        <specification-version>0.111</specification-version>
58
                    </run-dependency>
59
                </dependency>
60
                <dependency>
36
                    <code-name-base>org.netbeans.modules.projectapi</code-name-base>
61
                    <code-name-base>org.netbeans.modules.projectapi</code-name-base>
37
                    <build-prerequisite/>
62
                    <build-prerequisite/>
38
                    <compile-dependency/>
63
                    <compile-dependency/>
Lines 42-47 Link Here
42
                    </run-dependency>
67
                    </run-dependency>
43
                </dependency>
68
                </dependency>
44
                <dependency>
69
                <dependency>
70
                    <code-name-base>org.netbeans.spi.editor.hints</code-name-base>
71
                    <build-prerequisite/>
72
                    <compile-dependency/>
73
                    <run-dependency>
74
                        <release-version>0</release-version>
75
                        <specification-version>1.29</specification-version>
76
                    </run-dependency>
77
                </dependency>
78
                <dependency>
79
                    <code-name-base>org.netbeans.spi.java.hints</code-name-base>
80
                    <build-prerequisite/>
81
                    <compile-dependency/>
82
                    <run-dependency>
83
                        <specification-version>1.11</specification-version>
84
                    </run-dependency>
85
                </dependency>
86
                <dependency>
45
                    <code-name-base>org.openide.awt</code-name-base>
87
                    <code-name-base>org.openide.awt</code-name-base>
46
                    <build-prerequisite/>
88
                    <build-prerequisite/>
47
                    <compile-dependency/>
89
                    <compile-dependency/>
Lines 50-55 Link Here
50
                    </run-dependency>
92
                    </run-dependency>
51
                </dependency>
93
                </dependency>
52
                <dependency>
94
                <dependency>
95
                    <code-name-base>org.openide.dialogs</code-name-base>
96
                    <build-prerequisite/>
97
                    <compile-dependency/>
98
                    <run-dependency>
99
                        <specification-version>7.29</specification-version>
100
                    </run-dependency>
101
                </dependency>
102
                <dependency>
53
                    <code-name-base>org.openide.filesystems</code-name-base>
103
                    <code-name-base>org.openide.filesystems</code-name-base>
54
                    <build-prerequisite/>
104
                    <build-prerequisite/>
55
                    <compile-dependency/>
105
                    <compile-dependency/>
(-)a/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestClassHint.java (+167 lines)
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.Tree;
46
import com.sun.source.util.SourcePositions;
47
import com.sun.source.util.TreePath;
48
import java.util.ArrayList;
49
import java.util.Collection;
50
import java.util.List;
51
import java.util.Map;
52
import java.util.Map.Entry;
53
import org.netbeans.api.java.classpath.ClassPath;
54
import org.netbeans.api.java.source.ClasspathInfo;
55
import org.netbeans.api.java.source.CompilationInfo;
56
import org.netbeans.modules.gsf.testrunner.api.TestCreatorPanelDisplayer;
57
import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider;
58
import org.netbeans.spi.editor.hints.ChangeInfo;
59
import org.netbeans.spi.editor.hints.ErrorDescription;
60
import org.netbeans.spi.editor.hints.Fix;
61
import org.netbeans.spi.editor.hints.Severity;
62
import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
63
import org.netbeans.spi.java.hints.Hint;
64
import org.netbeans.spi.java.hints.HintContext;
65
import org.netbeans.spi.java.hints.TriggerTreeKind;
66
import org.openide.filesystems.FileObject;
67
import org.openide.loaders.DataObject;
68
import org.openide.loaders.DataObjectNotFoundException;
69
import org.openide.nodes.Node;
70
import org.openide.util.Exceptions;
71
import org.openide.util.Lookup;
72
import org.openide.util.NbBundle.Messages;
73
74
@Hint(displayName = "#DN_CreateTestClassHint", description = "#DESC_CreateTestClassHint", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT)
75
@Messages({
76
    "DN_CreateTestClassHint=Create Test Class",
77
    "DESC_CreateTestClassHint=Create a test class for the selected source class."
78
})
79
public class CreateTestClassHint {
80
81
    @TriggerTreeKind(Tree.Kind.CLASS)
82
    @Messages("ERR_CreateTestClassHint=Create Test Class")
83
    public static ErrorDescription computeWarning(HintContext context) {
84
	TreePath tp = context.getPath();
85
        ClassTree cls = (ClassTree) tp.getLeaf();
86
        CompilationInfo info = context.getInfo();
87
        SourcePositions sourcePositions = info.getTrees().getSourcePositions();
88
        int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), cls);
89
        int caret = context.getCaretLocation();
90
        String code = context.getInfo().getText();
91
	if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) {
92
            return null;
93
        }
94
95
        String headerText = code.substring(startPos, caret);
96
        int idx = headerText.indexOf('{'); //NOI18N
97
        if (idx >= 0) {
98
            return null;
99
        }
100
101
        ClassPath cp = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
102
        FileObject root = cp.findOwnerRoot(info.getFileObject());
103
        if (root == null) { //File not part of any project
104
            return null;
105
        }
106
107
	Collection<? extends TestCreatorProvider> providers = Lookup.getDefault().lookupAll(TestCreatorProvider.class);
108
        DataObject dataObject;
109
	Node activeNode = null;
110
	try {
111
	    dataObject = DataObject.find(info.getFileObject());
112
	    activeNode = dataObject.getNodeDelegate();
113
	} catch (DataObjectNotFoundException ex) {
114
	    Exceptions.printStackTrace(ex);
115
	}
116
	if (activeNode != null) {
117
	    Map<Object, List<String>> validCombinations = Utils.getValidCombinations(info, null);
118
	    if(validCombinations == null) { // no TestCreatorProvider found
119
		return null;
120
	    }
121
	    for (TestCreatorProvider provider : providers) {
122
		if (provider.enable(new Node[]{activeNode}) && !validCombinations.isEmpty()) {
123
		    List<Fix> fixes = new ArrayList<Fix>();
124
		    Fix fix;
125
		    for(Entry<Object, List<String>> entrySet : validCombinations.entrySet()) {
126
			Object location = entrySet.getKey();
127
			for(String testingFramework : entrySet.getValue()) {
128
			    fix = new CreateTestClassFix(new Node[]{activeNode}, location, testingFramework);
129
			    fixes.add(fix);
130
			}
131
		    }
132
		    validCombinations.clear();
133
		    return ErrorDescriptionFactory.forTree(context, context.getPath(), Bundle.ERR_CreateTestClassHint(), fixes.toArray(new Fix[fixes.size()]));
134
		}
135
	    }
136
	    validCombinations.clear();
137
	}
138
	return null;
139
    }
140
141
    private static final class CreateTestClassFix implements Fix {
142
	Node[] activatedNodes;
143
	Object location;
144
	String testingFramework;
145
146
	public CreateTestClassFix(Node[] activatedNodes, Object location, String testingFramework) {
147
	    this.activatedNodes = activatedNodes;
148
	    this.location = location;
149
	    this.testingFramework = testingFramework;
150
	}
151
152
	@Override
153
	@Messages({
154
	    "# {0} - the testing framework to be used, e.g. JUnit, TestNG,...",
155
	    "# {1} - the location where the test class will be created",
156
	    "FIX_CreateTestClassHint=Create Test Class [{0} in {1}]"})
157
	public String getText() {
158
	    return Bundle.FIX_CreateTestClassHint(testingFramework, Utils.getLocationText(location));
159
	}
160
161
	@Override
162
	public ChangeInfo implement() throws Exception {
163
	    TestCreatorPanelDisplayer.getDefault().displayPanel(activatedNodes, location, testingFramework);
164
	    return null;
165
	}
166
    }
167
}
(-)a/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/CreateTestMethodsHint.java (+173 lines)
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.MethodTree;
45
import com.sun.source.tree.Tree;
46
import com.sun.source.util.SourcePositions;
47
import com.sun.source.util.TreePath;
48
import java.util.ArrayList;
49
import java.util.Collection;
50
import java.util.List;
51
import java.util.Map;
52
import java.util.Map.Entry;
53
import javax.lang.model.element.Modifier;
54
import org.netbeans.api.java.classpath.ClassPath;
55
import org.netbeans.api.java.source.ClasspathInfo;
56
import org.netbeans.api.java.source.CompilationInfo;
57
import org.netbeans.modules.gsf.testrunner.api.TestCreatorPanelDisplayer;
58
import org.netbeans.modules.gsf.testrunner.api.TestCreatorProvider;
59
import org.netbeans.spi.editor.hints.ChangeInfo;
60
import org.netbeans.spi.editor.hints.ErrorDescription;
61
import org.netbeans.spi.editor.hints.Fix;
62
import org.netbeans.spi.editor.hints.Severity;
63
import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
64
import org.netbeans.spi.java.hints.Hint;
65
import org.netbeans.spi.java.hints.HintContext;
66
import org.netbeans.spi.java.hints.TriggerTreeKind;
67
import org.openide.filesystems.FileObject;
68
import org.openide.loaders.DataObject;
69
import org.openide.loaders.DataObjectNotFoundException;
70
import org.openide.nodes.Node;
71
import org.openide.util.Exceptions;
72
import org.openide.util.Lookup;
73
import org.openide.util.NbBundle.Messages;
74
75
@Hint(displayName = "#DN_CreateTestMethodsHint", description = "#DESC_CreateTestMethodsHint", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT)
76
@Messages({
77
    "DN_CreateTestMethodsHint=Generate All Test Methods",
78
    "DESC_CreateTestMethodsHint=Generate all test methods for the selected source class."
79
})
80
public class CreateTestMethodsHint {
81
82
    @TriggerTreeKind(Tree.Kind.METHOD)
83
    @Messages("ERR_CreateTestMethodsHint=Generate All Test Methods")
84
    public static ErrorDescription computeWarning(HintContext context) {
85
        final TreePath tp = context.getPath();
86
        final MethodTree method = (MethodTree) tp.getLeaf();
87
	if (method.getModifiers().getFlags().contains(Modifier.PRIVATE)) {
88
	    return null;
89
	}
90
	String methodName = method.getName().toString();
91
92
        CompilationInfo info = context.getInfo();
93
        SourcePositions sourcePositions = info.getTrees().getSourcePositions();
94
        int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), method);
95
        int caret = context.getCaretLocation();
96
        String code = context.getInfo().getText();
97
	if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) {
98
            return null;
99
        }
100
101
        String headerText = code.substring(startPos, caret);
102
        int idx = headerText.indexOf('{'); //NOI18N
103
        if (idx >= 0) {
104
            return null;
105
        }
106
107
        ClassPath cp = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
108
        FileObject root = cp.findOwnerRoot(info.getFileObject());
109
        if (root == null) { //File not part of any project
110
            return null;
111
        }
112
113
	Collection<? extends TestCreatorProvider> providers = Lookup.getDefault().lookupAll(TestCreatorProvider.class);
114
        DataObject dataObject;
115
	Node activeNode = null;
116
	try {
117
	    dataObject = DataObject.find(info.getFileObject());
118
	    activeNode = dataObject.getNodeDelegate();
119
	} catch (DataObjectNotFoundException ex) {
120
	    Exceptions.printStackTrace(ex);
121
	}
122
	if (activeNode != null) {
123
	    Map<Object, List<String>> validCombinations = Utils.getValidCombinations(info, methodName);
124
	    if(validCombinations == null) { // no TestCreatorProvider found
125
		return null;
126
	    }
127
	    for (TestCreatorProvider provider : providers) {
128
		if (provider.enable(new Node[]{activeNode}) && !validCombinations.isEmpty()) {
129
		    List<Fix> fixes = new ArrayList<Fix>();
130
		    Fix fix;
131
		    for(Entry<Object, List<String>> entrySet : validCombinations.entrySet()) {
132
			Object location = entrySet.getKey();
133
			for(String testingFramework : entrySet.getValue()) {
134
			    fix = new CreateTestMethodsFix(new Node[]{activeNode}, location, testingFramework);
135
			    fixes.add(fix);
136
			}
137
		    }
138
		    validCombinations.clear();
139
		    return ErrorDescriptionFactory.forTree(context, context.getPath(), Bundle.ERR_CreateTestMethodsHint(), fixes.toArray(new Fix[fixes.size()]));
140
		}
141
	    }
142
	    validCombinations.clear();
143
	}
144
	return null;
145
    }
146
147
    private static final class CreateTestMethodsFix implements Fix {
148
	Node[] activatedNodes;
149
	Object location;
150
	String testingFramework;
151
152
	public CreateTestMethodsFix(Node[] activatedNodes, Object location, String testingFramework) {
153
	    this.activatedNodes = activatedNodes;
154
	    this.location = location;
155
	    this.testingFramework = testingFramework;
156
	}
157
158
	@Override
159
	@Messages({
160
	    "# {0} - the testing framework to be used, e.g. JUnit, TestNG,...",
161
	    "# {1} - the location where the test class is",
162
	    "FIX_CreateTestMethodsHint=Generate All Test Methods [{0} in {1}]"})
163
	public String getText() {
164
	    return Bundle.FIX_CreateTestMethodsHint(testingFramework, Utils.getLocationText(location));
165
	}
166
167
	@Override
168
	public ChangeInfo implement() throws Exception {
169
	    TestCreatorPanelDisplayer.getDefault().displayPanel(activatedNodes, location, testingFramework);
170
	    return null;
171
	}
172
    }
173
}
(-)a/java.testrunner/src/org/netbeans/modules/java/testrunner/hints/Utils.java (+205 lines)
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
}

Return to bug 162593