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