Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support running tests from non default test folders #4924

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.ActionMap;
import java.util.stream.Collectors;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.java.queries.UnitTestForSourceQuery;
Expand All @@ -44,7 +44,6 @@
import org.netbeans.modules.maven.api.NbMavenProject;
import org.netbeans.modules.maven.classpath.MavenSourcesImpl;
import org.netbeans.modules.maven.configurations.M2ConfigProvider;
import org.netbeans.modules.maven.runjar.MavenExecuteUtils;
import org.netbeans.modules.maven.spi.actions.ActionConvertor;
import org.netbeans.modules.maven.spi.actions.ReplaceTokenProvider;
import org.netbeans.spi.project.ActionProvider;
Expand Down Expand Up @@ -215,8 +214,13 @@ private static FileObject[] extractFileObjectsfromLookup(Lookup lookup) {
HashSet<String> test = new HashSet<String>();
addSelectedFiles(false, fos, test);
addSelectedFiles(true, fos, test);
String files2test = test.toString().replace(" ", "");
packClassname.append(files2test.substring(1, files2test.length() - 1));

packClassname.append(test
.stream()
.map(String::trim)
.collect(Collectors.joining(","))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check whether syntax with comma-separated package names (not paths) works in ancient Surefire version - according to docs, there were some significant changes in Surefire 2.19. We recommend Surefire 2.22 for Junit5, and surefire 2.8 (which may fail with this syntax) for single test run. See org.netbeans.modules.maven.ActionProviderImp::checkSurefire for details.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a test with junit:4.12 and maven-surefire-plugin:2.8. In Netbeans I selected 2 packages and execute them. It generated -Dtest=some.it.pkg.other.**,some.it.pkg.** and it run the tests in those packages as expected:

Running some.it.pkg.ItTest ... Running some.it.pkg.other.It3Test ...

Is a such manual test enough?

);

}
}
if (packClassname.length() > 0) { //#213671
Expand Down Expand Up @@ -249,25 +253,34 @@ private static FileObject[] extractFileObjectsfromLookup(Lookup lookup) {
private void addSelectedFiles(boolean testRoots, FileObject[] candidates, HashSet<String> test) {
NbMavenProjectImpl prj = project.getLookup().lookup(NbMavenProjectImpl.class);
if (prj != null) {
URI[] roots = prj.getSourceRoots(testRoots);
for (URI uri : roots) {
FileObject root = FileUtil.toFileObject(Utilities.toFile(uri));
// test if root isn't null - NbMavenProjectImpl.getSourceRoots() might return a bogus
// non test uri in case there are only test source roots.
// NOTE that not sure if this is generaly the right place for the fix. Even though it is
// MavenProject which returns those uris, not sure if e.g. that behaviour wasn't somewhere on the way overriden
// by the nb maven module ...
if(root != null) {
for (FileObject candidate : candidates) {
String relativePath = FileUtil.getRelativePath(root, candidate);
if (relativePath != null) {
if (testRoots) {
relativePath = relativePath.replace(".java", "").replace('/', '.'); //NOI18N
} else {
relativePath = relativePath.replace(".java", "Test").replace('/', '.'); //NOI18N
addSelectedFilesInGivenRoot(prj.getSourceRoots(testRoots), candidates, testRoots, test);
addSelectedFilesInGivenRoot(prj.getGeneratedSourceRoots(testRoots), candidates, testRoots, test);
}
}

private void addSelectedFilesInGivenRoot(URI[] roots, FileObject[] candidates, boolean testRoots, HashSet<String> test) throws IllegalArgumentException {
for (URI rootUri : roots) {
FileObject root = FileUtil.toFileObject(Utilities.toFile(rootUri));
// test if root isn't null - NbMavenProjectImpl.getSourceRoots() might return a bogus
// non test uri in case there are only test source roots.
// NOTE that not sure if this is generaly the right place for the fix. Even though it is
// MavenProject which returns those uris, not sure if e.g. that behaviour wasn't somewhere on the way overriden
// by the nb maven module ...
if(root != null) {
for (FileObject candidate : candidates) {
String relativePath = FileUtil.getRelativePath(root, candidate);
if (relativePath != null) {
if (testRoots) {
relativePath = relativePath.replace(".java", "").replace('/', '.'); //NOI18N
if (candidate.isFolder()) {
relativePath += relativePath.isEmpty()
? "**"
: ".**";
}
test.add(relativePath);
} else {
relativePath = relativePath.replace(".java", "Test").replace('/', '.'); //NOI18N
}
test.add(relativePath);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.netbeans.modules.maven.execute;

import java.io.IOException;
import java.util.Map;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.junit.NbTestCase;
Expand All @@ -38,7 +39,8 @@ public DefaultReplaceTokenProviderTest(String name) {

private FileObject d;

@Override protected void setUp() throws Exception {
@Override
protected void setUp() throws Exception {
clearWorkDir();
d = FileUtil.toFileObject(getWorkDir());
}
Expand All @@ -64,23 +66,96 @@ public void testTestSingle() throws Exception {
// XXX test src/main/java selections
// XXX test selections across groups, or outside groups
// XXX test single methods

//#213671
assertEquals(null, ActionProviderImpl.replacements(p, ActionProvider.COMMAND_RUN, Lookup.EMPTY).get(DefaultReplaceTokenProvider.PACK_CLASSNAME));
assertEquals(null, ActionProviderImpl.replacements(p, ActionProvider.COMMAND_RUN, Lookup.EMPTY).get(DefaultReplaceTokenProvider.CLASSNAME));
assertEquals(null, ActionProviderImpl.replacements(p, ActionProvider.COMMAND_RUN, Lookup.EMPTY).get(DefaultReplaceTokenProvider.CLASSNAME_EXT));

}

public void testNgSingle() throws Exception {
TestFileUtils.writeFile(d, "pom.xml", "<project><modelVersion>4.0.0</modelVersion>"
+ "<groupId>g</groupId><artifactId>a</artifactId><version>0</version></project>");
TestFileUtils.writeFile(d, "src/test/java/p1/FirstNGTest.java", "package p1; class FirstNGTest {}");
TestFileUtils.writeFile(d, "src/main/java/p1/First.java", "package p1; class First {}");

Project p = ProjectManager.getDefault().findProject(d);
assertEquals("p1.FirstNGTest", ActionProviderImpl.replacements(p, ActionProvider.COMMAND_TEST_SINGLE, Lookups.singleton(d.getFileObject("src/main/java/p1/First.java"))).get(DefaultReplaceTokenProvider.PACK_CLASSNAME));

}

public void testIntegration2Files() throws Exception {
pomWithHelperPluginAndExtraTestFolder();
TestFileUtils.writeFile(d, "src/extra-test/java/p1/ItTest.java",
"//license\n"
+ "package p1;\n"
+ "class ItTest {}");
TestFileUtils.writeFile(d, "src/extra-test/java/p1/a/b/c/ItTestInlined.java",
"/*a comment*/ package p1.a. b.c ; class ItTestInlined {}"); //spabe between a and b is allowed

Project p = ProjectManager.getDefault().findProject(d);
DefaultReplaceTokenProvider instance = new DefaultReplaceTokenProvider(p);
Map<String, String> replacements = instance.createReplacements(ActionProviderImpl.COMMAND_INTEGRATION_TEST_SINGLE,
Lookups.fixed(
d.getFileObject("src/extra-test/java/p1/ItTest.java"),
d.getFileObject("src/extra-test/java/p1/a/b/c/ItTestInlined.java"))
);

final String packageClassName = replacements.get("packageClassName");
assertTrue(packageClassName.contains("p1.ItTest"));
assertTrue(packageClassName.contains("p1.a.b.c.ItTestInlined"));
}

private void pomWithHelperPluginAndExtraTestFolder() throws IOException {
TestFileUtils.writeFile(d, "pom.xml", "<project><modelVersion>4.0.0</modelVersion>"
+ "<groupId>g</groupId><artifactId>a</artifactId><version>0</version><build>"
+ "<plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId>"
+ "<executions><execution><id>add-test-source</id><phase>generate-test-sources</phase><goals>"
+ "<goal>add-test-source</goal></goals><configuration><sources><source>src/extra-test/java</source></sources>"
+ "</configuration></execution></executions></plugin></plugins></build></project>");
}

public void testIntegrationPackage() throws Exception {
pomWithHelperPluginAndExtraTestFolder();
TestFileUtils.writeFile(d, "src/extra-test/java/p1/a/b/c/ItTestInlined.java",
"/*a comment*/ package p1.a. b.c ; class ItTestInlined {}"); //space between a and b is allowed

Project p = ProjectManager.getDefault().findProject(d);

DefaultReplaceTokenProvider instance = new DefaultReplaceTokenProvider(p);
Map<String, String> replacements = instance.createReplacements(ActionProviderImpl.COMMAND_INTEGRATION_TEST_SINGLE,
Lookups.fixed(
d.getFileObject("src/extra-test/java/p1/a/b/c/"))
);

final String packageClassName = replacements.get("packageClassName");
assertTrue(packageClassName.contains("p1.a.b.c.**"));
}

public void testIntegration2FilesAndPacakge() throws Exception {
pomWithHelperPluginAndExtraTestFolder();
TestFileUtils.writeFile(d, "src/extra-test/java/p1/ItTest.java",
"//license\n"
+ "package p1;\n"
+ "class ItTest {}");
TestFileUtils.writeFile(d, "src/extra-test/java/p1/a/b/c/ItTestInlined.java",
"/*a comment*/ package p1.a. b.c ; class ItTestInlined {}"); //space between a and b is allowed
TestFileUtils.writeFile(d, "src/extra-test/java/p1/d/ItTest3.java",
"package p1.d;\nclass ItTest3 {}");

Project p = ProjectManager.getDefault().findProject(d);
DefaultReplaceTokenProvider instance = new DefaultReplaceTokenProvider(p);
Map<String, String> replacements = instance.createReplacements(ActionProviderImpl.COMMAND_INTEGRATION_TEST_SINGLE,
Lookups.fixed(
d.getFileObject("src/extra-test/java/p1/ItTest.java"),
d.getFileObject("src/extra-test/java/p1/a/b/c/ItTestInlined.java"),
d.getFileObject("src/extra-test/java/p1/d/"))
);

final String packageClassName = replacements.get("packageClassName");
assertTrue(packageClassName.contains("p1.ItTest"));
assertTrue(packageClassName.contains("p1.a.b.c.ItTestInlined"));
assertTrue(packageClassName.contains("p1.d.**"));
}
}