# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /home/theofanis/repositories/core-main # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: gsf.testrunner/apichanges.xml --- gsf.testrunner/apichanges.xml Base (BASE) +++ gsf.testrunner/apichanges.xml Locally Modified (Based On LOCAL) @@ -73,6 +73,20 @@ + + + API to log unit test's library usage. + + + + + + Added API to log unit test's library usage so that it can be used in + statistics visualization. + + + + Method to determine if a node is a test class. Index: gsf.testrunner/manifest.mf --- gsf.testrunner/manifest.mf Base (BASE) +++ gsf.testrunner/manifest.mf Locally Modified (Based On LOCAL) @@ -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.36 +OpenIDE-Module-Specification-Version: 1.37 Index: gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/api/UnitTestsUsage.java --- gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/api/UnitTestsUsage.java Base (BASE) +++ gsf.testrunner/src/org/netbeans/modules/gsf/testrunner/api/UnitTestsUsage.java Locally New @@ -0,0 +1,101 @@ +/* + * 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.net.URI; +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +/** + * Class that helps to log usage of unit test library used in a project to create or run tests. + * + * @author theofanis + */ +public final class UnitTestsUsage { + + private static UnitTestsUsage INSTANCE; + private HashMap projectsAlreadyLogged; + + private UnitTestsUsage() { + projectsAlreadyLogged = new HashMap(); + } + + public static UnitTestsUsage getInstance() { + if(INSTANCE != null) { + return INSTANCE; + } + return new UnitTestsUsage(); + } + + /** + * Logs usage of unit test library used in a project to create or run tests. + * + * @param projectURI the project's URI + * @param unitTestLibrary the unit test's library used, e.g. JUNIT3 or JUNIT4 + */ + public void logUnitTestUsage(URI projectURI, String unitTestLibrary) { + assert projectURI != null : "Project's URI cannot be null"; + assert unitTestLibrary != null : "Unit test's library cannot be null"; + Logger logger = Logger.getLogger("org.netbeans.ui.metrics.unittestslibrary"); // NOI18N + LogRecord rec = new LogRecord(Level.INFO, "USG_UNIT_TESTS_LIBRARY"); //NOI18N + if (unitTestLibrary.isEmpty()) { + return; + } + + if (!projectsAlreadyLogged.containsKey(projectURI)) { + projectsAlreadyLogged.put(projectURI, unitTestLibrary.toString()); + } else { + String unitTestLibrariesUsed = projectsAlreadyLogged.get(projectURI); + if (unitTestLibrariesUsed.equals(unitTestLibrary)) { + return; + } else { + projectsAlreadyLogged.put(projectURI, unitTestLibrary); + } + } + rec.setParameters(new Object[]{unitTestLibrary.toString()}); + rec.setLoggerName(logger.getName()); + logger.log(rec); + } + +} Index: junit/src/org/netbeans/modules/junit/DefaultPlugin.java --- junit/src/org/netbeans/modules/junit/DefaultPlugin.java Base (BASE) +++ junit/src/org/netbeans/modules/junit/DefaultPlugin.java Locally Modified (Based On LOCAL) @@ -48,7 +48,9 @@ import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionListener; +import java.io.File; import java.io.IOException; +import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -119,11 +121,13 @@ import static org.netbeans.api.java.classpath.ClassPath.SOURCE; import static org.netbeans.api.java.classpath.ClassPath.COMPILE; import static org.netbeans.api.java.project.JavaProjectConstants.SOURCES_TYPE_JAVA; +import org.netbeans.modules.gsf.testrunner.api.UnitTestsUsage; import org.netbeans.modules.java.testrunner.GuiUtils; import static org.openide.ErrorManager.ERROR; import static org.openide.ErrorManager.WARNING; import static org.openide.NotifyDescriptor.CANCEL_OPTION; import static org.openide.NotifyDescriptor.WARNING_MESSAGE; +import org.openide.util.Utilities; /** * Default JUnit plugin. @@ -145,7 +149,7 @@ = "org/junit/Test.class"; //NOI18N /** */ - private JUnitVersion junitVer; + private static JUnitVersion junitVer; /** name of FreeMarker template property - generate {@code @BeforeClass} method? */ private static final String templatePropBeforeClass = "classSetUp"; //NOI18N @@ -173,6 +177,24 @@ private static java.util.ResourceBundle bundle = org.openide.util.NbBundle.getBundle( DefaultPlugin.class); + public static void logJUnitUsage(URI projectURI) { + String version = ""; + if (junitVer == null) { + Project project = FileOwnerQuery.getOwner(projectURI); + final ClassPath classPath = getTestClassPath(project); + if (classPath != null) { + if (classPath.findResource(JUNIT4_SPECIFIC) != null) { + version = JUnitVersion.JUNIT4.toString(); + } else if (classPath.findResource(JUNIT3_SPECIFIC) != null) { + version = JUnitVersion.JUNIT3.toString(); + } + } + } else { + version = junitVer.toString(); + } + UnitTestsUsage.getInstance().logUnitTestUsage(projectURI, version); + } + /** * */ @@ -813,7 +835,13 @@ final Map params) { //XXX: not documented that in case that if filesToTest is , //the target root param works as a target folder - + Project project = FileOwnerQuery.getOwner(targetRoot); + if (project != null) { + File projectFile = FileUtil.toFile(project.getProjectDirectory()); + if (projectFile != null) { + logJUnitUsage(Utilities.toURI(projectFile)); + } + } ProgressIndicator progress = new ProgressIndicator(); progress.show(); Index: junit/src/org/netbeans/modules/junit/output/JUnitOutputReader.java --- junit/src/org/netbeans/modules/junit/output/JUnitOutputReader.java Base (BASE) +++ junit/src/org/netbeans/modules/junit/output/JUnitOutputReader.java Locally Modified (Based On LOCAL) @@ -84,6 +84,7 @@ import org.openide.util.NbBundle; import org.xml.sax.SAXException; import static java.util.logging.Level.FINER; +import org.netbeans.modules.junit.DefaultPlugin; import static org.netbeans.modules.junit.output.RegexpUtils.ADD_ERROR_PREFIX; import static org.netbeans.modules.junit.output.RegexpUtils.ADD_FAILURE_PREFIX; import static org.netbeans.modules.junit.output.RegexpUtils.END_OF_TEST_PREFIX; @@ -94,6 +95,7 @@ import static org.netbeans.modules.junit.output.RegexpUtils.TESTSUITE_PREFIX; import static org.netbeans.modules.junit.output.RegexpUtils.TESTSUITE_STATS_PREFIX; import org.openide.util.Lookup; +import org.openide.util.Utilities; /** * Obtains events from a single session of an Ant junit task @@ -183,7 +185,11 @@ this.testSession = new JUnitTestSession(sName, project, sessionType, new JUnitTestRunnerNodeFactory()); //NOI18N testSession.setRerunHandler(new JUnitExecutionManager(session, testSession, props)); + File projectFile = FileUtil.toFile(project.getProjectDirectory()); + if(projectFile != null) { + DefaultPlugin.logJUnitUsage(Utilities.toURI(projectFile)); } + } TestSession getTestSession() { return testSession; Index: maven.junit/src/org/netbeans/modules/maven/junit/JUnitOutputListenerProvider.java --- maven.junit/src/org/netbeans/modules/maven/junit/JUnitOutputListenerProvider.java Base (BASE) +++ maven.junit/src/org/netbeans/modules/maven/junit/JUnitOutputListenerProvider.java Locally Modified (Based On LOCAL) @@ -78,6 +78,7 @@ import org.netbeans.modules.gsf.testrunner.api.TestSuite; import org.netbeans.modules.gsf.testrunner.api.Testcase; import org.netbeans.modules.gsf.testrunner.api.Trouble; +import org.netbeans.modules.gsf.testrunner.api.UnitTestsUsage; import org.netbeans.modules.maven.api.Constants; import org.netbeans.modules.maven.api.NbMavenProject; import org.netbeans.modules.maven.api.PluginPropertyUtils; @@ -175,6 +176,10 @@ if (prj != null) { NbMavenProject mvnprj = prj.getLookup().lookup(NbMavenProject.class); if (mvnprj != null) { + File projectFile = FileUtil.toFile(prj.getProjectDirectory()); + if (projectFile != null) { + UnitTestsUsage.getInstance().logUnitTestUsage(Utilities.toURI(projectFile), getJUnitVersion(config.getMavenProject())); + } TestSession.SessionType type = TestSession.SessionType.TEST; String action = config.getActionName(); if (action != null) { //custom @@ -295,6 +300,22 @@ return false; } + private String getJUnitVersion(MavenProject prj) { + String juVersion = ""; + for (Artifact a : prj.getArtifacts()) { + if ("junit".equals(a.getGroupId()) && ("junit".equals(a.getArtifactId()) || "junit-dep".equals(a.getArtifactId()))) { //junit-dep see #214238 + String version = a.getVersion(); + if (version != null && new ComparableVersion(version).compareTo(new ComparableVersion("4.8")) >= 0) { + return "JUNIT4"; //NOI18N + } + if (version != null && new ComparableVersion(version).compareTo(new ComparableVersion("3.8")) >= 0) { + return "JUNIT3"; //NOI18N + } + } + } + return juVersion; + } + private boolean usingJUnit4(MavenProject prj) { // SUREFIRE-724 for (Artifact a : prj.getArtifacts()) { if ("junit".equals(a.getGroupId()) && ("junit".equals(a.getArtifactId()) || "junit-dep".equals(a.getArtifactId()))) { //junit-dep see #214238