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