private static List<SourceGroup> getTestTargets(SourceGroup sourceGroup, Map foldersToSourceGroupsMap) { final URL[] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); if (rootURLs.length == 0) { return new ArrayList<SourceGroup>(); } List<SourceGroup> result = new ArrayList<SourceGroup>(); List<FileObject> sourceRoots = getFileObjects(rootURLs); for (FileObject sourceRoot : sourceRoots) { SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap.get(sourceRoot); if (srcGroup != null) { result.add(srcGroup); } } return result; }
private Iterable<? extends FileObject> getRoots() { Sources _src = null; synchronized (this) { if (this.roots != null) { return this.roots; } if (this.src == null) { this.src = this.project.getLookup().lookup(Sources.class); this.src.addChangeListener(sourcesChangeListener); } _src = this.src; } assert _src != null; final SourceGroup[] sgs = _src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); final List<FileObject> _roots = new ArrayList<FileObject>(sgs.length); for (SourceGroup sg : sgs) { final FileObject root = sg.getRootFolder(); if (UnitTestForSourceQuery.findSources(root).length == 0) { _roots.add(root); } } synchronized (this) { if (this.roots == null) { this.roots = _roots; } return this.roots; } }
public static boolean isTestClass(Node activatedNode) { FileObject fileObject = org.netbeans.modules.gsf.testrunner.ui.api.UICommonUtils.getFileObjectFromNode(activatedNode); if (fileObject != null && CommonTestUtil.isJavaFile(fileObject)) { Project project = FileOwnerQuery.getOwner(fileObject); if (project != null) { SourceGroup[] javaSGs = new JavaUtils(project).getJavaSourceGroups(); for (int i = 0; i < javaSGs.length; i++) { SourceGroup javaSG = javaSGs[i]; FileObject rootFolder = javaSG.getRootFolder(); URL[] testRoots = UnitTestForSourceQuery.findUnitTests(rootFolder); URL[] sourceRoots = UnitTestForSourceQuery.findSources(rootFolder); if (((fileObject == rootFolder) || FileUtil.isParentOf(rootFolder, fileObject)) && javaSG.contains(fileObject)) { // activated FO is contained in the javaSG source group if (testRoots.length == 0 && sourceRoots.length > 0) { // javaSG has corresponding source root but no corresponding test root, // thus the activated FO is a test class, so activate action return true; } } } } } return false; }
public static boolean isTestClass(Node activatedNode) { FileObject fileObject = org.netbeans.modules.gsf.testrunner.ui.api.UICommonUtils.getFileObjectFromNode(activatedNode); if (fileObject != null && CommonTestUtil.isJavaFile(fileObject)) { Project project = FileOwnerQuery.getOwner(fileObject); if (project != null) { SourceGroup[] javaSGs = new JavaUtils(project).getJavaSourceGroups(); for (int i = 0; i < javaSGs.length; i++) { SourceGroup javaSG = javaSGs[i]; FileObject rootFolder = javaSG.getRootFolder(); URL[] testRoots = UnitTestForSourceQuery.findUnitTests(rootFolder); URL[] sourceRoots = UnitTestForSourceQuery.findSources(rootFolder); if (((fileObject == rootFolder) || FileUtil.isParentOf(rootFolder, fileObject)) && javaSG.contains(fileObject)) { // activated FO is contained in the javaSG source group if (testRoots.length == 0 && sourceRoots.length > 0) { // javaSG has corresponding source root but no corresponding test root, // thus the activated FO is a test class, so activate action return true; } } } } } return false; }
private static List<SourceGroup> getTestTargets(SourceGroup sourceGroup, Map<FileObject, SourceGroup> foldersToSourceGroupsMap) { final URL[] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); if (rootURLs.length == 0) { return Collections.emptyList(); } List<SourceGroup> result = new ArrayList<SourceGroup>(); List<FileObject> sourceRoots = getFileObjects(rootURLs); for (int i = 0; i < sourceRoots.size(); i++) { FileObject sourceRoot = sourceRoots.get(i); SourceGroup srcGroup = foldersToSourceGroupsMap.get(sourceRoot); if (srcGroup != null) { result.add(srcGroup); } } return result; }
boolean hasTests(Project project) { for (SourceGroup g : ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)) { FileObject root = g.getRootFolder(); if (UnitTestForSourceQuery.findSources(root).length > 0 && containsSources(root)) { return true; } } return false; } boolean containsSources(FileObject f) {
private static void populateLocations(FileObject activeFO) { Collection<? extends CommonTestUtilProvider> testUtilProviders = Lookup.getDefault().lookupAll(CommonTestUtilProvider.class); for (CommonTestUtilProvider provider : testUtilProviders) { locations = provider.getTestTargets(activeFO); break; } if (locations != null && locations.length == 0) { SourceGroup sourceGroupOwner = findSourceGroupOwner(activeFO); if (sourceGroupOwner != null) { // get URLs of target SourceGroup's roots locations = UnitTestForSourceQuery.findUnitTests(sourceGroupOwner.getRootFolder()); } } }
public SourceGroup getMainSourceGroup() { Sources sources = ProjectUtils.getSources(projectContext.getProject()); SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); SourceGroup mainSourceGroup = null; if (sourceGroups != null && sourceGroups.length != 0) { for (SourceGroup sGroup : sourceGroups) { URL[] sourcesForUnitTest = UnitTestForSourceQuery.findSources(sGroup.getRootFolder()); if (sourcesForUnitTest == null || sourcesForUnitTest.length == 0) { mainSourceGroup = sGroup; break; } } } return mainSourceGroup; }
/** * Will find * Copied from JUnit module implementation in 4.1 and modified */ public static FileObject findTestForFile(final FileObject selectedFO) { if ((selectedFO == null) || !selectedFO.getExt().equalsIgnoreCase("java")) { return null; // NOI18N } ClassPath cp = ClassPath.getClassPath(selectedFO, ClassPath.SOURCE); if (cp == null) { return null; } FileObject packageRoot = cp.findOwnerRoot(selectedFO); if (packageRoot == null) { return null; // not a file in the source dirs - e.g. generated class in web app } URL[] testRoots = UnitTestForSourceQuery.findUnitTests(packageRoot); FileObject fileToOpen = null; for (int j = 0; j < testRoots.length; j++) { fileToOpen = findUnitTestInTestRoot(cp, selectedFO, testRoots[j]); if (fileToOpen != null) { return fileToOpen; } } return null; }
} else { FileObject root = cp.findOwnerRoot(fob); URL[] urls = UnitTestForSourceQuery.findSources(root); if (urls.length != 0) { showMsg("MSG_MasterTestPackage"); // NOI18N
URL[] testRoots = UnitTestForSourceQuery.findUnitTests(packageRoot); FileObject fileToOpen = null; for (int j = 0 ; j < testRoots.length; j++) {
protected void configureSourcesAndBinariesProperties(Version sonarQubeVersion, Properties properties) { SourceGroup mainSourceGroup = getMainSourceGroup(); if (mainSourceGroup != null) { String sourcePath = mainSourceGroup.getRootFolder().getPath(); if (SonarMvnProject.isMvnProject(projectContext.getProject()) && sonarQubeVersion.compareTo(4, 5) >= 0) { sourcePath = "pom.xml," + sourcePath; } ClassPath classPath = ClassPath.getClassPath(projectContext.getProject().getProjectDirectory(), ClassPath.COMPILE); if (classPath != null) { properties.setProperty(getPropertyName("sonar.java.libraries"), getLibrariesPath(classPath)); } properties.setProperty(getPropertyName("sonar.sources"), sourcePath); URL[] roots = BinaryForSourceQuery.findBinaryRoots(mainSourceGroup.getRootFolder().toURL()).getRoots(); if (roots.length > 0) { properties.setProperty(getPropertyName("sonar.java.binaries"), Utilities.toFile(roots[0]).getPath()); } URL[] testSources = UnitTestForSourceQuery.findUnitTests(mainSourceGroup.getRootFolder()); if (testSources != null && testSources.length != 0) { File testsDir = FileUtil.archiveOrDirForURL(testSources[0]); if (testsDir.exists()) { properties.setProperty(getPropertyName("sonar.tests"), testsDir.getPath()); } } } }