Skip to content

Commit

Permalink
Test single file should work for test classes which names do not matc…
Browse files Browse the repository at this point in the history
…h the corresponding file names.
  • Loading branch information
dbalek committed Dec 5, 2024
1 parent 583e9ca commit cd1c27a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 47 deletions.
44 changes: 22 additions & 22 deletions java/gradle.java/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@
<data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
<code-name-base>org.netbeans.modules.gradle.java</code-name-base>
<module-dependencies>
<dependency>
<code-name-base>org.netbeans.modules.gradle</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>2</release-version>
<specification-version>2.40</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.api.annotations.common</code-name-base>
<build-prerequisite/>
Expand Down Expand Up @@ -113,6 +104,24 @@
<specification-version>1.20</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.gradle</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>2</release-version>
<specification-version>2.40</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.java.api.common</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0</release-version>
<specification-version>1.127</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.java.platform</code-name-base>
<build-prerequisite/>
Expand Down Expand Up @@ -158,20 +167,19 @@
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.java.api.common</code-name-base>
<code-name-base>org.netbeans.modules.java.source.base</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0</release-version>
<specification-version>1.127</specification-version>
<specification-version>2.73</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.java.source.base</code-name-base>
<code-name-base>org.netbeans.modules.project.dependency</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>2.20.1.2.2.25.8.1</specification-version>
<implementation-version/>
</run-dependency>
</dependency>
<dependency>
Expand Down Expand Up @@ -306,14 +314,6 @@
<specification-version>9.4.1</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.project.dependency</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<implementation-version/>
</run-dependency>
</dependency>
</module-dependencies>
<test-dependencies>
<test-type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions java/java.source.base/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
<apidef name="javasource_base">Java Source API</apidef>
</apidefs>
<changes>
<change id="SourceUtils.classNameFor">
<api name="javasource_base" />
<summary>Adding TreeMaker.RawText</summary>
<version major="1" minor="2.73"/>
<date day="5" month="12" year="2024"/>
<author login="jlahoda"/>
<compatibility addition="yes" binary="compatible" source="compatible"/>
<description>
Adding SourceUtils.classNameFor.
</description>
<class name="SourceUtils" package="org.netbeans.api.java.source"/>
</change>
<change id="TreeMaker.RawText">
<api name="javasource_base" />
<summary>Adding TreeMaker.RawText</summary>
Expand Down
2 changes: 1 addition & 1 deletion java/java.source.base/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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:\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> 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;
}
}
18 changes: 9 additions & 9 deletions java/maven/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>2.32</specification-version>
<specification-version>2.73</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down Expand Up @@ -284,6 +284,14 @@
<specification-version>1.62</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.project.dependency</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<implementation-version/>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.project.indexingbridge</code-name-base>
<build-prerequisite/>
Expand Down Expand Up @@ -517,14 +525,6 @@
<specification-version>6.64</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.project.dependency</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<implementation-version/>
</run-dependency>
</dependency>
</module-dependencies>
<test-dependencies>
<test-type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) ||
Expand Down

0 comments on commit cd1c27a

Please sign in to comment.