View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013 - 2023, Tapio Rautonen
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package org.eluder.coveralls.maven.plugin.logging;
25  
26  import org.apache.maven.plugin.logging.Log;
27  import org.eluder.coveralls.maven.plugin.ProcessingException;
28  import org.eluder.coveralls.maven.plugin.domain.Branch;
29  import org.eluder.coveralls.maven.plugin.domain.Source;
30  import org.eluder.coveralls.maven.plugin.source.ChainingSourceCallback;
31  import org.eluder.coveralls.maven.plugin.source.SourceCallback;
32  
33  import java.io.IOException;
34  
35  public class CoverageTracingLogger extends ChainingSourceCallback implements Logger {
36  
37      private long files = 0;
38      private long lines = 0;
39      private long relevant = 0;
40      private long covered = 0;
41      private long branches = 0;
42      private long coveredBranches = 0;
43  
44      public CoverageTracingLogger(final SourceCallback chained) {
45          super(chained);
46      }
47  
48      public long getFiles() {
49          return files;
50      }
51  
52      public final long getLines() {
53          return lines;
54      }
55  
56      public final long getRelevant() {
57          return relevant;
58      }
59  
60      public final long getCovered() {
61          return covered;
62      }
63  
64      public final long getMissed() {
65          return relevant - covered;
66      }
67  
68      public final long getBranches() {
69          return branches;
70      }
71  
72      public final long getCoveredBranches() {
73          return coveredBranches;
74      }
75  
76      public final long getMissedBranches() {
77          return branches - coveredBranches;
78      }
79  
80      @Override
81      public Position getPosition() {
82          return Position.AFTER;
83      }
84  
85      @Override
86      public void log(final Log log) {
87          log.info("Gathered code coverage metrics for " + getFiles() + " source files with " + getLines() + " lines of code:");
88          log.info("- " + getRelevant() + " relevant lines");
89          log.info("- " + getCovered() + " covered lines");
90          log.info("- " + getMissed() + " missed lines");
91          log.info("- " + getBranches() + " branches");
92          log.info("- " + getCoveredBranches() + " covered branches");
93          log.info("- " + getMissedBranches() + " missed branches");
94      }
95  
96      @Override
97      protected void onSourceInternal(final Source source) throws ProcessingException, IOException {
98          files++;
99          lines += source.getCoverage().length;
100         for (Integer coverage : source.getCoverage()) {
101             if (coverage != null) {
102                 relevant++;
103                 if (coverage > 0) {
104                     covered++;
105                 }
106             }
107         }
108 
109         this.branches += source.getBranchesList().size();
110         for (final Branch b : source.getBranchesList()) {
111             if (b.getHits() > 0) {
112                 coveredBranches++;
113             }
114         }
115     }
116 }