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.testng.utils;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.maven.surefire.group.match.AndGroupMatcher;
26  import org.apache.maven.surefire.group.match.GroupMatcher;
27  import org.apache.maven.surefire.group.match.InverseGroupMatcher;
28  import org.apache.maven.surefire.group.parse.GroupMatcherParser;
29  import org.apache.maven.surefire.group.parse.ParseException;
30  import org.testng.IMethodSelector;
31  import org.testng.IMethodSelectorContext;
32  import org.testng.ITestNGMethod;
33  
34  /**
35   * Method selector delegating to {@link GroupMatcher} to decide if a method is included or not.
36   *
37   */
38  public class GroupMatcherMethodSelector implements IMethodSelector {
39  
40      private static final long serialVersionUID = 1L;
41  
42      private static GroupMatcher matcher;
43  
44      private Map<ITestNGMethod, Boolean> answers = new HashMap<>();
45  
46      @Override
47      public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
48          Boolean result = answers.get(method);
49          if (result != null) {
50              return result;
51          }
52  
53          if (matcher == null) {
54              return true;
55          }
56  
57          String[] groups = method.getGroups();
58          result = matcher.enabled(groups);
59          answers.put(method, result);
60          return result;
61      }
62  
63      @Override
64      public void setTestMethods(List<ITestNGMethod> testMethods) {}
65  
66      public static void setGroups(String groups, String excludedGroups) {
67          // System.out.println( "Processing group includes: '" + groups + "'\nExcludes: '" + excludedGroups + "'" );
68  
69          try {
70              AndGroupMatcher matcher = new AndGroupMatcher();
71              GroupMatcher in = null;
72              if (groups != null && !groups.trim().isEmpty()) {
73                  in = new GroupMatcherParser(groups).parse();
74              }
75  
76              if (in != null) {
77                  matcher.addMatcher(in);
78              }
79  
80              GroupMatcher ex = null;
81              if (excludedGroups != null && !excludedGroups.trim().isEmpty()) {
82                  ex = new GroupMatcherParser(excludedGroups).parse();
83              }
84  
85              if (ex != null) {
86                  matcher.addMatcher(new InverseGroupMatcher(ex));
87              }
88  
89              if (in != null || ex != null) {
90                  // System.out.println( "Group matcher: " + matcher );
91                  GroupMatcherMethodSelector.matcher = matcher;
92              }
93          } catch (ParseException e) {
94              throw new IllegalArgumentException(
95                      "Cannot parse group includes/excludes expression(s):\nIncludes: " + groups + "\nExcludes: "
96                              + excludedGroups,
97                      e);
98          }
99      }
100 
101     public static void setGroupMatcher(GroupMatcher matcher) {
102         GroupMatcherMethodSelector.matcher = matcher;
103     }
104 }