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.reporting;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.util.List;
11  import java.util.regex.Pattern;
12  
13  import mockit.coverage.CallPoint;
14  
15  public final class ListOfCallPoints {
16      @NonNull
17      private static final String EOL = System.lineSeparator();
18      private static final Pattern LESS_THAN_CHAR = Pattern.compile("<");
19  
20      @NonNull
21      private final StringBuilder content;
22  
23      public ListOfCallPoints() {
24          content = new StringBuilder(100);
25      }
26  
27      public void insertListOfCallPoints(@Nullable List<CallPoint> callPoints) {
28          if (content.length() == 0) {
29              content.append(EOL).append("      ");
30          }
31  
32          content.append("  <ol style='display:none'>");
33  
34          if (callPoints == null) {
35              content.append("</ol>").append(EOL).append("      ");
36              return;
37          }
38  
39          content.append(EOL);
40  
41          CallPoint currentCP = callPoints.get(0);
42          appendTestMethod(currentCP.getStackTraceElement());
43          appendRepetitionCountIfNeeded(currentCP);
44  
45          for (int i = 1, n = callPoints.size(); i < n; i++) {
46              CallPoint nextCP = callPoints.get(i);
47              StackTraceElement ste = nextCP.getStackTraceElement();
48  
49              if (nextCP.isSameTestMethod(currentCP)) {
50                  content.append(", ").append(ste.getLineNumber());
51              } else {
52                  content.append("</li>").append(EOL);
53                  appendTestMethod(ste);
54              }
55  
56              appendRepetitionCountIfNeeded(nextCP);
57              currentCP = nextCP;
58          }
59  
60          content.append("</li>").append(EOL).append("        </ol>").append(EOL).append("      ");
61      }
62  
63      private void appendTestMethod(@NonNull StackTraceElement current) {
64          content.append("          <li>");
65          content.append(current.getClassName()).append('#');
66          content.append(LESS_THAN_CHAR.matcher(current.getMethodName()).replaceFirst("&lt;")).append(": ");
67          content.append(current.getLineNumber());
68      }
69  
70      private void appendRepetitionCountIfNeeded(@NonNull CallPoint callPoint) {
71          int repetitionCount = callPoint.getRepetitionCount();
72  
73          if (repetitionCount > 0) {
74              content.append('x').append(1 + repetitionCount);
75          }
76      }
77  
78      @NonNull
79      public String getContents() {
80          String result = content.toString();
81          content.setLength(0);
82          return result;
83      }
84  }