From cd1c27ae6f4741f65f8e57c6317671d1908e8f21 Mon Sep 17 00:00:00 2001 From: Dusan Balek Date: Thu, 5 Dec 2024 15:02:59 +0100 Subject: [PATCH] Test single file should work for test classes which names do not match the corresponding file names. --- java/gradle.java/nbproject/project.xml | 44 +++++++++---------- .../gradle/java/GradleJavaTokenProvider.java | 8 ++-- java/java.source.base/apichanges.xml | 12 +++++ .../nbproject/project.properties | 2 +- .../netbeans/api/java/source/SourceUtils.java | 35 +++++++++++++++ java/maven/nbproject/project.xml | 18 ++++---- .../execute/DefaultReplaceTokenProvider.java | 20 ++++----- 7 files changed, 92 insertions(+), 47 deletions(-) diff --git a/java/gradle.java/nbproject/project.xml b/java/gradle.java/nbproject/project.xml index 6a7d6ed8df3a..0c9f70e8e166 100644 --- a/java/gradle.java/nbproject/project.xml +++ b/java/gradle.java/nbproject/project.xml @@ -25,15 +25,6 @@ org.netbeans.modules.gradle.java - - org.netbeans.modules.gradle - - - - 2 - 2.40 - - org.netbeans.api.annotations.common @@ -113,6 +104,24 @@ 1.20 + + org.netbeans.modules.gradle + + + + 2 + 2.40 + + + + org.netbeans.modules.java.api.common + + + + 0 + 1.127 + + org.netbeans.modules.java.platform @@ -158,20 +167,19 @@ - org.netbeans.modules.java.api.common + org.netbeans.modules.java.source.base - 0 - 1.127 + 2.73 - org.netbeans.modules.java.source.base + org.netbeans.modules.project.dependency - 2.20.1.2.2.25.8.1 + @@ -306,14 +314,6 @@ 9.4.1 - - org.netbeans.modules.project.dependency - - - - - - diff --git a/java/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java b/java/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java index a3611cebe252..3e2723d802fd 100644 --- a/java/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java +++ b/java/gradle.java/src/org/netbeans/modules/gradle/java/GradleJavaTokenProvider.java @@ -31,6 +31,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.netbeans.api.java.source.ClasspathInfo; +import org.netbeans.api.java.source.SourceUtils; import org.netbeans.api.project.Project; import org.netbeans.spi.project.SingleMethod; import org.openide.filesystems.FileObject; @@ -139,11 +141,11 @@ private String evaluateClassName(GradleJavaProject gjp, FileObject fo) { if (sourceSet != null) { String relPath = sourceSet.relativePath(f); if (relPath != null) { - ret = (relPath.lastIndexOf('.') > 0 ? - relPath.substring(0, relPath.lastIndexOf('.')) : - relPath).replace('/', '.'); if (fo.isFolder()) { + ret = relPath.replace('/', '.'); ret = ret + '*'; + } else { + ret = SourceUtils.classNameFor(ClasspathInfo.create(fo), relPath); } } } diff --git a/java/java.source.base/apichanges.xml b/java/java.source.base/apichanges.xml index 8d6347cead60..5f7c07a442a0 100644 --- a/java/java.source.base/apichanges.xml +++ b/java/java.source.base/apichanges.xml @@ -25,6 +25,18 @@ Java Source API + + + Adding TreeMaker.RawText + + + + + + Adding SourceUtils.classNameFor. + + + Adding TreeMaker.RawText diff --git a/java/java.source.base/nbproject/project.properties b/java/java.source.base/nbproject/project.properties index 183dfae24847..9e2b1ca15845 100644 --- a/java/java.source.base/nbproject/project.properties +++ b/java/java.source.base/nbproject/project.properties @@ -23,7 +23,7 @@ javadoc.name=Java Source Base javadoc.title=Java Source Base javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=2.72.0 +spec.version.base=2.73.0 test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\ ${o.n.core.dir}/lib/boot.jar:\ diff --git a/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java b/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java index 390a9b19a8f3..b61c1a075a78 100644 --- a/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java +++ b/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java @@ -62,6 +62,9 @@ import com.sun.tools.javac.model.JavacElements; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.function.Predicate; import javax.lang.model.util.ElementScanner14; @@ -1468,4 +1471,36 @@ public static void forceSource(CompilationController cc, FileObject file) { } cc.addForceSource(file); } + + /** + * Computes class name for the corresponding input source file. + * + * @param info the ClasspathInfo used to resolve + * @param relativePath input source file path relative to the corresponding source root + * @return class name for the corresponding input source file + * @since 2.73 + */ + public static String classNameFor(ClasspathInfo info, String relativePath) { + ClassPath cachedCP = ClasspathInfoAccessor.getINSTANCE().getCachedClassPath(info, PathKind.COMPILE); + int idx = relativePath.indexOf('.'); + String rel = idx < 0 ? relativePath : relativePath.substring(0, idx); + String className = rel.replace('/', '.'); + FileObject rsFile = cachedCP.findResource(rel + '.' + FileObjects.RS); + if (rsFile != null) { + List lines = new ArrayList<>(); + try (BufferedReader in = new BufferedReader(new InputStreamReader(rsFile.getInputStream(), StandardCharsets.UTF_8))) { + String line; + while ((line = in.readLine())!=null) { + if (className.equals(line)) { + return className; + } + lines.add(line); + } + } catch (IOException ioe) {} + if (!lines.isEmpty()) { + return lines.get(0); + } + } + return className; + } } diff --git a/java/maven/nbproject/project.xml b/java/maven/nbproject/project.xml index 9fe5f86dd393..6490c0d6a501 100644 --- a/java/maven/nbproject/project.xml +++ b/java/maven/nbproject/project.xml @@ -201,7 +201,7 @@ - 2.32 + 2.73 @@ -284,6 +284,14 @@ 1.62 + + org.netbeans.modules.project.dependency + + + + + + org.netbeans.modules.project.indexingbridge @@ -517,14 +525,6 @@ 6.64 - - org.netbeans.modules.project.dependency - - - - - - diff --git a/java/maven/src/org/netbeans/modules/maven/execute/DefaultReplaceTokenProvider.java b/java/maven/src/org/netbeans/modules/maven/execute/DefaultReplaceTokenProvider.java index 9fe2386a7b3a..c8ba46e7b3e0 100644 --- a/java/maven/src/org/netbeans/modules/maven/execute/DefaultReplaceTokenProvider.java +++ b/java/maven/src/org/netbeans/modules/maven/execute/DefaultReplaceTokenProvider.java @@ -148,9 +148,9 @@ private static FileObject[] extractFileObjectsfromLookup(Lookup lookup) { classname.append(','); classnameExt.append(','); } + String rel = FileUtil.getRelativePath(group.getRootFolder(), file); + assert rel != null; if (file.isFolder()) { - String rel = FileUtil.getRelativePath(group.getRootFolder(), file); - assert rel != null; String pkg = rel.replace('/', '.'); if (!pkg.isEmpty()) { packClassname.append(pkg).append(".**."); // test everything under this package recusively @@ -162,22 +162,18 @@ private static FileObject[] extractFileObjectsfromLookup(Lookup lookup) { classname.append(pkg); // ? classnameExt.append(pkg); // ?? } else { // XXX do we need to limit to text/x-java? What about files of other type? - String relP = FileUtil.getRelativePath(group.getRootFolder(), file.getParent()); - assert relP != null; - StringBuilder cn = new StringBuilder(); - if (!relP.isEmpty()) { - cn.append(relP.replace('/', '.')).append('.'); - } - String n = file.getName(); - cn.append(n); - if (uniqueClassNames.add(cn.toString())) { + String cn = SourceUtils.classNameFor(ClasspathInfo.create(file), rel); + int idx = cn.lastIndexOf('.'); + String n = idx < 0 ? cn : cn.substring(idx + 1); + if (uniqueClassNames.add(cn)) { packClassname.append(cn); classname.append(n); } else { packClassname.deleteCharAt(packClassname.length() - 1); // Delete the comma classname.deleteCharAt(classname.length() - 1); } - classnameExt.append(file.getNameExt()); + classnameExt.append(n).append('.').append(file.getExt()); + String relP = FileUtil.getRelativePath(group.getRootFolder(), file.getParent()); if (MavenSourcesImpl.NAME_SOURCE.equals(group.getName()) && (ActionProvider.COMMAND_TEST_SINGLE.equals(actionName) || ActionProvider.COMMAND_DEBUG_TEST_SINGLE.equals(actionName) ||