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