View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.coverage.testRedundancy;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.lang.reflect.Method;
11  import java.util.ArrayList;
12  import java.util.LinkedHashMap;
13  import java.util.List;
14  import java.util.Map;
15  import java.util.Map.Entry;
16  
17  import mockit.coverage.Configuration;
18  
19  import org.checkerframework.checker.index.qual.NonNegative;
20  
21  public final class TestCoverage {
22      @Nullable
23      public static final TestCoverage INSTANCE;
24  
25      static {
26          INSTANCE = "true".equals(Configuration.getProperty("redundancy")) ? new TestCoverage() : null;
27      }
28  
29      @NonNull
30      private final Map<Method, Integer> testsToItemsCovered = new LinkedHashMap<>();
31      @Nullable
32      private Method currentTestMethod;
33  
34      private TestCoverage() {
35      }
36  
37      public void setCurrentTestMethod(@Nullable Method testMethod) {
38          if (testMethod != null) {
39              testsToItemsCovered.put(testMethod, 0);
40          }
41  
42          currentTestMethod = testMethod;
43      }
44  
45      public void recordNewItemCoveredByTestIfApplicable(@NonNegative int previousExecutionCount) {
46          if (previousExecutionCount == 0 && currentTestMethod != null) {
47              Integer itemsCoveredByTest = testsToItemsCovered.get(currentTestMethod);
48              testsToItemsCovered.put(currentTestMethod, itemsCoveredByTest == null ? 1 : itemsCoveredByTest + 1);
49          }
50      }
51  
52      @NonNull
53      public List<Method> getRedundantTests() {
54          List<Method> redundantTests = new ArrayList<>();
55  
56          for (Entry<Method, Integer> testAndItemsCovered : testsToItemsCovered.entrySet()) {
57              Method testMethod = testAndItemsCovered.getKey();
58              Integer itemsCovered = testAndItemsCovered.getValue();
59  
60              if (itemsCovered == 0) {
61                  redundantTests.add(testMethod);
62              }
63          }
64  
65          return redundantTests;
66      }
67  }