View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.common.junit48;
20  
21  import java.util.Map;
22  
23  import org.apache.maven.surefire.api.booter.ProviderParameterNames;
24  import org.apache.maven.surefire.api.testset.TestListResolver;
25  import org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil;
26  import org.apache.maven.surefire.group.match.GroupMatcher;
27  import org.apache.maven.surefire.group.parse.GroupMatcherParser;
28  import org.apache.maven.surefire.group.parse.ParseException;
29  import org.junit.runner.Description;
30  import org.junit.runner.manipulation.Filter;
31  
32  import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP;
33  import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_GROUPS_PROP;
34  import static org.apache.maven.surefire.shared.utils.StringUtils.isNotBlank;
35  
36  /**
37   * @author Todd Lipcon
38   */
39  public class FilterFactory {
40      private final ClassLoader testClassLoader;
41  
42      public FilterFactory(ClassLoader testClassLoader) {
43          this.testClassLoader = testClassLoader;
44      }
45  
46      /**
47       * @return {@code true} if non-blank
48       * {@link ProviderParameterNames#TESTNG_GROUPS_PROP} and/or
49       * {@link ProviderParameterNames#TESTNG_EXCLUDEDGROUPS_PROP} exists.
50       */
51      public boolean canCreateGroupFilter(Map<String, String> providerProperties) {
52          String groups = providerProperties.get(TESTNG_GROUPS_PROP);
53          String excludedGroups = providerProperties.get(TESTNG_EXCLUDEDGROUPS_PROP);
54          return isNotBlank(groups) || isNotBlank(excludedGroups);
55      }
56  
57      /**
58       * Creates filter using he key
59       * {@link ProviderParameterNames#TESTNG_GROUPS_PROP} and/or
60       * {@link ProviderParameterNames#TESTNG_EXCLUDEDGROUPS_PROP}.
61       */
62      public Filter createGroupFilter(Map<String, String> providerProperties) {
63          String groups = providerProperties.get(TESTNG_GROUPS_PROP);
64  
65          GroupMatcher included = null;
66          if (isNotBlank(groups)) {
67              try {
68                  included = new GroupMatcherParser(groups).parse();
69              } catch (ParseException e) {
70                  throw new IllegalArgumentException(
71                          "Invalid group expression: '" + groups + "'. Reason: " + e.getMessage(), e);
72              }
73          }
74  
75          String excludedGroups = providerProperties.get(TESTNG_EXCLUDEDGROUPS_PROP);
76  
77          GroupMatcher excluded = null;
78          if (isNotBlank(excludedGroups)) {
79              try {
80                  excluded = new GroupMatcherParser(excludedGroups).parse();
81              } catch (ParseException e) {
82                  throw new IllegalArgumentException(
83                          "Invalid group expression: '" + excludedGroups + "'. Reason: " + e.getMessage(), e);
84              }
85          }
86  
87          if (included != null && testClassLoader != null) {
88              included.loadGroupClasses(testClassLoader);
89          }
90  
91          if (excluded != null && testClassLoader != null) {
92              excluded.loadGroupClasses(testClassLoader);
93          }
94  
95          return new GroupMatcherCategoryFilter(included, excluded);
96      }
97  
98      public Filter createMethodFilter(String requestedTestMethod) {
99          return new MethodFilter(requestedTestMethod);
100     }
101 
102     public Filter createMethodFilter(TestListResolver resolver) {
103         return new MethodFilter(resolver);
104     }
105 
106     public Filter createMatchAnyDescriptionFilter(Iterable<Description> descriptions) {
107         return JUnit4ProviderUtil.createMatchAnyDescriptionFilter(descriptions);
108     }
109 
110     public Filter and(Filter filter1, Filter filter2) {
111         return new AndFilter(filter1, filter2);
112     }
113 }