View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.logging;
26  
27  import java.io.IOException;
28  
29  import org.apache.maven.plugin.logging.Log;
30  import org.eluder.coveralls.maven.plugin.ProcessingException;
31  import org.eluder.coveralls.maven.plugin.domain.Branch;
32  import org.eluder.coveralls.maven.plugin.domain.Source;
33  import org.eluder.coveralls.maven.plugin.source.ChainingSourceCallback;
34  import org.eluder.coveralls.maven.plugin.source.SourceCallback;
35  
36  /**
37   * The Class CoverageTracingLogger.
38   */
39  public class CoverageTracingLogger extends ChainingSourceCallback implements Logger {
40  
41      /** The files. */
42      private long files;
43  
44      /** The lines. */
45      private long lines;
46  
47      /** The relevant. */
48      private long relevant;
49  
50      /** The covered. */
51      private long covered;
52  
53      /** The branches. */
54      private long branches;
55  
56      /** The covered branches. */
57      private long coveredBranches;
58  
59      /**
60       * Instantiates a new coverage tracing logger.
61       *
62       * @param chained
63       *            the chained
64       */
65      public CoverageTracingLogger(final SourceCallback chained) {
66          super(chained);
67      }
68  
69      /**
70       * Gets the files.
71       *
72       * @return the files
73       */
74      public long getFiles() {
75          return this.files;
76      }
77  
78      /**
79       * Gets the lines.
80       *
81       * @return the lines
82       */
83      public final long getLines() {
84          return this.lines;
85      }
86  
87      /**
88       * Gets the relevant.
89       *
90       * @return the relevant
91       */
92      public final long getRelevant() {
93          return this.relevant;
94      }
95  
96      /**
97       * Gets the covered.
98       *
99       * @return the covered
100      */
101     public final long getCovered() {
102         return this.covered;
103     }
104 
105     /**
106      * Gets the missed.
107      *
108      * @return the missed
109      */
110     public final long getMissed() {
111         return this.relevant - this.covered;
112     }
113 
114     /**
115      * Gets the branches.
116      *
117      * @return the branches
118      */
119     public final long getBranches() {
120         return this.branches;
121     }
122 
123     /**
124      * Gets the covered branches.
125      *
126      * @return the covered branches
127      */
128     public final long getCoveredBranches() {
129         return this.coveredBranches;
130     }
131 
132     /**
133      * Gets the missed branches.
134      *
135      * @return the missed branches
136      */
137     public final long getMissedBranches() {
138         return this.branches - this.coveredBranches;
139     }
140 
141     @Override
142     public Position getPosition() {
143         return Position.AFTER;
144     }
145 
146     @Override
147     public void log(final Log log) {
148         log.info("Gathered code coverage metrics for " + this.getFiles() + " source files with " + this.getLines()
149                 + " lines of code:");
150         log.info("- " + this.getRelevant() + " relevant lines");
151         log.info("- " + this.getCovered() + " covered lines");
152         log.info("- " + this.getMissed() + " missed lines");
153         log.info("- " + this.getBranches() + " branches");
154         log.info("- " + this.getCoveredBranches() + " covered branches");
155         log.info("- " + this.getMissedBranches() + " missed branches");
156     }
157 
158     @Override
159     protected void onSourceInternal(final Source source) throws ProcessingException, IOException {
160         this.files++;
161         this.lines += source.getCoverage().length;
162         for (final Integer coverage : source.getCoverage()) {
163             if (coverage != null) {
164                 this.relevant++;
165                 if (coverage > 0) {
166                     this.covered++;
167                 }
168             }
169         }
170 
171         this.branches += source.getBranchesList().size();
172         for (final Branch b : source.getBranchesList()) {
173             if (b.getHits() > 0) {
174                 this.coveredBranches++;
175             }
176         }
177     }
178 }