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.common.junit4.JUnit4ProviderUtil;
23  import org.apache.maven.surefire.group.match.GroupMatcher;
24  import org.apache.maven.surefire.group.parse.GroupMatcherParser;
25  import org.apache.maven.surefire.group.parse.ParseException;
26  import org.apache.maven.surefire.testset.TestListResolver;
27  import org.junit.runner.Description;
28  import org.junit.runner.manipulation.Filter;
29  
30  import java.util.Map;
31  
32  import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP;
33  import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_GROUPS_PROP;
34  import static org.apache.maven.surefire.util.internal.StringUtils.isNotBlank;
35  
36  /**
37   * @author Todd Lipcon
38   */
39  public class FilterFactory
40  {
41      private final ClassLoader testClassLoader;
42  
43      public FilterFactory( ClassLoader testClassLoader )
44      {
45          this.testClassLoader = testClassLoader;
46      }
47  
48      /**
49       * @return {@code true} if non-blank
50       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_GROUPS_PROP} and/or
51       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_EXCLUDEDGROUPS_PROP} exists.
52       */
53      public boolean canCreateGroupFilter( Map<String, String> providerProperties )
54      {
55          String groups = providerProperties.get( TESTNG_GROUPS_PROP );
56          String excludedGroups = providerProperties.get( TESTNG_EXCLUDEDGROUPS_PROP );
57          return isNotBlank( groups ) || isNotBlank( excludedGroups );
58      }
59  
60      /**
61       * Creates filter using he key
62       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_GROUPS_PROP} and/or
63       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_EXCLUDEDGROUPS_PROP}.
64       */
65      public Filter createGroupFilter( Map<String, String> providerProperties )
66      {
67          String groups = providerProperties.get( TESTNG_GROUPS_PROP );
68  
69          GroupMatcher included = null;
70          if ( isNotBlank( groups ) )
71          {
72              try
73              {
74                  included = new GroupMatcherParser( groups ).parse();
75              }
76              catch ( ParseException e )
77              {
78                  throw new IllegalArgumentException(
79                      "Invalid group expression: '" + groups + "'. Reason: " + e.getMessage(), e );
80              }
81          }
82  
83          String excludedGroups = providerProperties.get( TESTNG_EXCLUDEDGROUPS_PROP );
84  
85          GroupMatcher excluded = null;
86          if ( isNotBlank( excludedGroups ) )
87          {
88              try
89              {
90                  excluded = new GroupMatcherParser( excludedGroups ).parse();
91              }
92              catch ( ParseException e )
93              {
94                  throw new IllegalArgumentException(
95                      "Invalid group expression: '" + excludedGroups + "'. Reason: " + e.getMessage(), e );
96              }
97          }
98  
99          if ( included != null && testClassLoader != null )
100         {
101             included.loadGroupClasses( testClassLoader );
102         }
103 
104         if ( excluded != null && testClassLoader != null )
105         {
106             excluded.loadGroupClasses( testClassLoader );
107         }
108 
109         return new GroupMatcherCategoryFilter( included, excluded );
110     }
111 
112     public Filter createMethodFilter( String requestedTestMethod )
113     {
114         return new MethodFilter( requestedTestMethod );
115     }
116 
117     public Filter createMethodFilter( TestListResolver resolver )
118     {
119         return new MethodFilter( resolver );
120     }
121 
122     public Filter createMatchAnyDescriptionFilter( Iterable<Description> descriptions )
123     {
124         return JUnit4ProviderUtil.createMatchAnyDescriptionFilter( descriptions );
125     }
126 
127     public Filter and( Filter filter1, Filter filter2 )
128     {
129         return new AndFilter( filter1, filter2 );
130     }
131 }