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