From ce78107b995eacc52ee253dcaf2e7e9b9fb67dba Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Tue, 24 Aug 2021 19:51:33 +0200 Subject: [PATCH] [junit] explicitly add filter on package name (IDEA-276463) this way non-JVM engines won't need to filter by class name, when package name is enough avoid filtering by class names when package name can be used instead = keep filters for pattern configurations only GitOrigin-RevId: 25e9a87cbb39c6d50e4bcfa3d8cb7956b90b4110 --- .../module7/tests1/MyTest5.java | 6 ++++++ .../execution/BaseConfigurationTestCase.java | 16 +++++++++++---- .../java/execution/ConfigurationsTest.java | 19 +++++++++++++++++- .../intellij/execution/junit/TestPackage.java | 2 +- .../intellij/junit5/JUnit5TestRunnerUtil.java | 20 +++++-------------- 5 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 java/java-tests/testData/junit/configurations/module7/tests1/MyTest5.java diff --git a/java/java-tests/testData/junit/configurations/module7/tests1/MyTest5.java b/java/java-tests/testData/junit/configurations/module7/tests1/MyTest5.java new file mode 100644 index 000000000000..f6335325aed8 --- /dev/null +++ b/java/java-tests/testData/junit/configurations/module7/tests1/MyTest5.java @@ -0,0 +1,6 @@ +package tests1; + +class MyTest5 { + @org.junit.jupiter.api.Test + void method() { } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/execution/BaseConfigurationTestCase.java b/java/java-tests/testSrc/com/intellij/java/execution/BaseConfigurationTestCase.java index c3b4ffe63644..0a1c3cae37b0 100644 --- a/java/java-tests/testSrc/com/intellij/java/execution/BaseConfigurationTestCase.java +++ b/java/java-tests/testSrc/com/intellij/java/execution/BaseConfigurationTestCase.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.java.execution; import com.intellij.execution.Location; @@ -67,6 +67,12 @@ public abstract class BaseConfigurationTestCase extends JavaProjectTestCase { } protected void createModule(VirtualFile module1Content, boolean addSource) { + createModule(module1Content, addSource, "JUnit4"); + } + + protected void createModule(VirtualFile module1Content, + boolean addSource, + String junitLibName) { Module module = createEmptyModule(); if (addSource) { PsiTestUtil.addSourceRoot(module, module1Content, true); @@ -75,11 +81,13 @@ public abstract class BaseConfigurationTestCase extends JavaProjectTestCase { PsiTestUtil.addContentRoot(module, module1Content); } - IntelliJProjectConfiguration.LibraryRoots junit4Library = IntelliJProjectConfiguration.getProjectLibrary("JUnit4"); - ModuleRootModificationUtil.addModuleLibrary(module, "JUnit4", junit4Library.getClassesUrls(), junit4Library.getSourcesUrls()); + IntelliJProjectConfiguration.LibraryRoots junit4Library = IntelliJProjectConfiguration.getProjectLibrary(junitLibName); + ModuleRootModificationUtil.addModuleLibrary(module, junitLibName, junit4Library.getClassesUrls(), junit4Library.getSourcesUrls()); ModuleRootModificationUtil.setModuleSdk(module, ModuleRootManager.getInstance(myModule).getSdk()); GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module); - assertNotNull(JavaPsiFacade.getInstance(getProject()).findClass(JUnitUtil.TEST_CASE_CLASS, scope)); + if ("JUnit4".equals(junitLibName)) { + assertNotNull(JavaPsiFacade.getInstance(getProject()).findClass(JUnitUtil.TEST_CASE_CLASS, scope)); + } Module missingModule = createTempModule(); addDependency(module, missingModule); ModuleManager.getInstance(myProject).disposeModule(missingModule); diff --git a/java/java-tests/testSrc/com/intellij/java/execution/ConfigurationsTest.java b/java/java-tests/testSrc/com/intellij/java/execution/ConfigurationsTest.java index 1fdfd68a069f..016240eaa3d8 100644 --- a/java/java-tests/testSrc/com/intellij/java/execution/ConfigurationsTest.java +++ b/java/java-tests/testSrc/com/intellij/java/execution/ConfigurationsTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.java.execution; import com.intellij.application.options.ModuleDescriptionsComboBox; @@ -158,6 +158,23 @@ public class ConfigurationsTest extends BaseConfigurationTestCase { assertEquals(myJdk.getHomeDirectory().getPresentableUrl(), parameters.getJdkPath()); } + + public void testRunAllInPackageJUnit5() throws ExecutionException, IOException { + + VirtualFile module1Content = findFile("module7"); + createModule(module1Content, true, "JUnit5"); + Module module = getModule(3); + PsiPackage psiPackage = JavaPsiFacade.getInstance(myProject).findPackage("tests1"); + JUnitConfiguration configuration = createConfiguration(psiPackage, module); + configuration.setSearchScope(TestSearchScope.SINGLE_MODULE); + JavaParameters parameters = checkCanRun(configuration); + List lines = extractAllInPackageTests(parameters, psiPackage); + assertEquals(Arrays.asList("", //category + "" //filters + ), + lines); + } + public void testRunningAllInPackage() throws IOException, ExecutionException { Module module1 = getModule1(); GlobalSearchScope module1AndLibraries = GlobalSearchScope.moduleWithLibrariesScope(module1); diff --git a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java index da2c0803725b..b41846d7d398 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java @@ -159,7 +159,7 @@ public class TestPackage extends TestObject { } protected @NlsSafe String getFilters(Set> foundClasses, @NlsSafe String packageName) { - return foundClasses.isEmpty() ? packageName.isEmpty() ? ".*" : packageName + "\\..*" : ""; + return ""; } protected void searchTests5(Module module, Set> classes) throws CantRunException { } diff --git a/plugins/junit5_rt/src/com/intellij/junit5/JUnit5TestRunnerUtil.java b/plugins/junit5_rt/src/com/intellij/junit5/JUnit5TestRunnerUtil.java index 90c590a3d52b..00b4c22a2115 100644 --- a/plugins/junit5_rt/src/com/intellij/junit5/JUnit5TestRunnerUtil.java +++ b/plugins/junit5_rt/src/com/intellij/junit5/JUnit5TestRunnerUtil.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.junit5; import org.junit.platform.commons.util.AnnotationUtils; @@ -20,6 +6,7 @@ import org.junit.platform.engine.DiscoverySelector; import org.junit.platform.engine.discovery.ClassNameFilter; import org.junit.platform.engine.discovery.ClasspathRootSelector; import org.junit.platform.engine.discovery.DiscoverySelectors; +import org.junit.platform.engine.discovery.PackageNameFilter; import org.junit.platform.launcher.LauncherDiscoveryRequest; import org.junit.platform.launcher.TagFilter; import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; @@ -93,6 +80,9 @@ public class JUnit5TestRunnerUtil { } else { builder = builder.selectors(selectors); + if (!packageName.isEmpty()) { + builder = builder.filters(PackageNameFilter.includePackageNames(packageName)); + } } if (filters != null && !filters.isEmpty()) { String[] classNames = filters.split("\\|\\|");