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 org.apache.maven.surefire.api.testset.ResolvedTest;
22  import org.junit.runner.Description;
23  import org.junit.runner.manipulation.Filter;
24  
25  final class RequestedTest extends Filter {
26      private static final String CLASS_FILE_EXTENSION = ".class";
27  
28      private final ResolvedTest test;
29      private final boolean isPositiveFilter;
30  
31      RequestedTest(ResolvedTest test, boolean isPositiveFilter) {
32          this.test = test;
33          this.isPositiveFilter = isPositiveFilter;
34      }
35  
36      @Override
37      public boolean shouldRun(Description description) {
38          Class<?> realTestClass = description.getTestClass();
39          String methodName = description.getMethodName();
40          if (realTestClass == null && methodName == null) {
41              return true;
42          } else {
43              String testClass = classFile(realTestClass);
44              return isPositiveFilter
45                      ? test.matchAsInclusive(testClass, methodName)
46                      : !test.matchAsExclusive(testClass, methodName);
47          }
48      }
49  
50      @Override
51      public String describe() {
52          String description = test.toString();
53          return description.isEmpty() ? "*" : description;
54      }
55  
56      @Override
57      public boolean equals(Object o) {
58          return this == o || o != null && getClass() == o.getClass() && equals((RequestedTest) o);
59      }
60  
61      private boolean equals(RequestedTest o) {
62          return isPositiveFilter == o.isPositiveFilter && test.equals(o.test);
63      }
64  
65      @Override
66      public int hashCode() {
67          return test.hashCode();
68      }
69  
70      private String classFile(Class<?> realTestClass) {
71          return realTestClass == null ? null : realTestClass.getName().replace('.', '/') + CLASS_FILE_EXTENSION;
72      }
73  }