2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.junit5;
18 import org.junit.platform.commons.util.AnnotationUtils;
19 import org.junit.platform.engine.DiscoverySelector;
20 import org.junit.platform.engine.discovery.ClassNameFilter;
21 import org.junit.platform.engine.discovery.DiscoverySelectors;
22 import org.junit.platform.launcher.LauncherDiscoveryRequest;
23 import org.junit.platform.launcher.TagFilter;
24 import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
26 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.lang.annotation.Annotation;
30 import java.util.ArrayList;
31 import java.util.List;
33 public class JUnit5TestRunnerUtil {
35 public static final String DISABLED_ANNO = "org.junit.jupiter.api.Disabled";
37 public static LauncherDiscoveryRequest buildRequest(String[] suiteClassNames, String[] packageNameRef) {
38 if (suiteClassNames.length == 0) {
42 LauncherDiscoveryRequestBuilder builder = LauncherDiscoveryRequestBuilder.request();
45 if (suiteClassNames.length == 1 && suiteClassNames[0].charAt(0) == '@') {
46 // all tests in the package specified
48 BufferedReader reader = new BufferedReader(new FileReader(suiteClassNames[0].substring(1)));
50 final String packageName = reader.readLine();
51 if (packageName == null) return null;
53 String tags = reader.readLine();
54 String filters = reader.readLine();
57 List<DiscoverySelector> selectors = new ArrayList<>();
58 while ((line = reader.readLine()) != null) {
59 selectors.add(createSelector(line));
61 packageNameRef[0] = packageName.length() == 0 ? "<default package>" : packageName;
62 if (selectors.isEmpty()) {
63 builder = builder.selectors(DiscoverySelectors.selectPackage(packageName));
64 if (filters != null && !filters.isEmpty()) {
65 builder = builder.filters(ClassNameFilter.includeClassNamePatterns(filters.split("\\|\\|")));
69 builder = builder.selectors(selectors);
71 if (tags != null && !tags.isEmpty()) {
73 .filters(TagFilter.includeTags(tags.split(" ")))
74 .filters(ClassNameFilter.excludeClassNamePatterns("com\\.intellij\\.rt.*"));
76 return builder.build();
82 catch (IOException e) {
88 boolean disableDisabledCondition = isDisabledConditionDisabled(suiteClassNames[0]);
89 if (disableDisabledCondition) {
90 builder = builder.configurationParameter("junit.jupiter.conditions.deactivate", "org.junit.*DisabledCondition");
93 return builder.selectors(createSelector(suiteClassNames[0])).build();
99 public static boolean isDisabledConditionDisabled(String name) {
100 int commaIdx = name.indexOf(",");
101 boolean disableDisabledCondition = true;
104 ClassLoader loader = JUnit5TestRunnerUtil.class.getClassLoader();
105 Class<?> aClass = Class.forName(name, false, loader);
106 Class<? extends Annotation> disabledAnnotation = (Class<? extends Annotation>)Class.forName(DISABLED_ANNO, false, loader);
107 disableDisabledCondition = AnnotationUtils.findAnnotation(aClass, disabledAnnotation).isPresent();
109 catch (ClassNotFoundException e) {
110 disableDisabledCondition = false;
113 return disableDisabledCondition;
117 * Unique id is prepended with prefix: @see com.intellij.execution.junit.TestUniqueId#getUniqueIdPresentation()
118 * Method contains ','
120 protected static DiscoverySelector createSelector(String line) {
121 if (line.startsWith("\u001B")) {
122 String uniqueId = line.substring("\u001B".length());
123 return DiscoverySelectors.selectUniqueId(uniqueId);
125 else if (line.contains(",")) {
126 return DiscoverySelectors.selectMethod(line.replaceFirst(",", "#"));
129 return DiscoverySelectors.selectClass(line);