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.util;
25  
26  import org.apache.maven.project.MavenProject;
27  import org.eluder.coveralls.maven.plugin.CoverageParser;
28  import org.eluder.coveralls.maven.plugin.parser.CloverParser;
29  import org.eluder.coveralls.maven.plugin.parser.CoberturaParser;
30  import org.eluder.coveralls.maven.plugin.parser.JaCoCoParser;
31  import org.eluder.coveralls.maven.plugin.parser.SagaParser;
32  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
33  
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.List;
39  
40  public class CoverageParsersFactory {
41  
42      private static final String JACOCO_FILE = "/jacoco.xml";
43      private static final String JACOCO_PREFIX = "/jacoco";
44      private static final String JACOCO_IT_PREFIX = "/jacoco-it";
45  
46      private static final String COBERTURA_FILE = "/coverage.xml";
47      private static final String COBERTURA_PREFIX = "/cobertura";
48  
49      private static final String CLOVER_FILE = "/clover.xml";
50      private static final String CLOVER_PREFIX = "/clover";
51  
52      private static final String SAGA_FILE = "/total-coverage.xml";
53      private static final String SAGA_PREFIX = "/saga-coverage";
54  
55      private final MavenProject project;
56      private final SourceLoader sourceLoader;
57      private List<File> jacocoReports;
58      private List<File> coberturaReports;
59      private List<File> sagaReports;
60      private List<File> cloverReports;
61      private List<String> relativeReportDirs;
62  
63      public CoverageParsersFactory(final MavenProject project, final SourceLoader sourceLoader) {
64          this.project = project;
65          this.sourceLoader = sourceLoader;
66      }
67  
68      public CoverageParsersFactory withJaCoCoReports(final List<File> jacocoReports) {
69          this.jacocoReports = jacocoReports;
70          return this;
71      }
72  
73      public CoverageParsersFactory withCoberturaReports(final List<File> coberturaReports) {
74          this.coberturaReports = coberturaReports;
75          return this;
76      }
77  
78      public CoverageParsersFactory withSagaReports(final List<File> sagaReports) {
79          this.sagaReports = sagaReports;
80          return this;
81      }
82  
83      public CoverageParsersFactory withRelativeReportDirs(final List<String> relativeReportDirs) {
84          this.relativeReportDirs = relativeReportDirs;
85          return this;
86      }
87  
88      public List<CoverageParser> createParsers() throws IOException {
89          List<CoverageParser> parsers = new ArrayList<>();
90          List<MavenProject> projects = new MavenProjectCollector(project).collect();
91  
92          ExistingFiles jacocoFiles = ExistingFiles.create(jacocoReports);
93          ExistingFiles coberturaFiles = ExistingFiles.create(coberturaReports);
94          ExistingFiles sagaFiles = ExistingFiles.create(sagaReports);
95          ExistingFiles cloverFiles = ExistingFiles.create(cloverReports);
96          for (MavenProject p : projects) {
97              File reportingDirectory = new File(p.getModel().getReporting().getOutputDirectory());
98              File buildDirectory = new File(p.getBuild().getDirectory());
99  
100             jacocoFiles.add(new File(reportingDirectory, JACOCO_PREFIX + JACOCO_FILE));
101             jacocoFiles.add(new File(reportingDirectory, JACOCO_IT_PREFIX + JACOCO_FILE));
102             coberturaFiles.add(new File(reportingDirectory, COBERTURA_PREFIX + COBERTURA_FILE));
103             sagaFiles.add(new File(buildDirectory, SAGA_PREFIX + SAGA_FILE));
104             cloverFiles.add(new File(reportingDirectory, CLOVER_PREFIX + CLOVER_FILE));
105 
106             if (relativeReportDirs != null) {
107                 for (String relativeReportPath : relativeReportDirs) {
108                     File relativeReportingDirectory = reportingDirectory;
109                     File relativeBuildDirectory = buildDirectory;
110                     if (!relativeReportPath.isEmpty() && !File.separator.equals(relativeReportPath)) {
111                         relativeReportingDirectory = new File(reportingDirectory, relativeReportPath);
112                         relativeBuildDirectory = new File(buildDirectory, relativeReportPath);
113                     }
114 
115                     jacocoFiles.add(new File(relativeReportingDirectory, JACOCO_FILE));
116                     jacocoFiles.add(new File(relativeBuildDirectory, JACOCO_FILE));
117                     coberturaFiles.add(new File(relativeReportingDirectory, COBERTURA_FILE));
118                     coberturaFiles.add(new File(relativeBuildDirectory, COBERTURA_FILE));
119                     sagaFiles.add(new File(relativeReportingDirectory, SAGA_FILE));
120                     sagaFiles.add(new File(relativeBuildDirectory, SAGA_FILE));
121                     cloverFiles.add(new File(relativeReportingDirectory, CLOVER_FILE));
122                     cloverFiles.add(new File(relativeBuildDirectory, CLOVER_FILE));
123                 }
124             }
125         }
126 
127         for (File jacocoFile : jacocoFiles) {
128             parsers.add(new JaCoCoParser(jacocoFile, sourceLoader));
129         }
130         for (File coberturaFile : coberturaFiles) {
131             parsers.add(new CoberturaParser(coberturaFile, sourceLoader));
132         }
133         for (File sagaFile : sagaFiles) {
134             parsers.add(new SagaParser(sagaFile, sourceLoader));
135         }
136         for (File cloverFile : cloverFiles) {
137             parsers.add(new CloverParser(cloverFile, sourceLoader));
138         }
139 
140         if (parsers.isEmpty()) {
141             throw new IOException("No coverage report files found");
142         }
143 
144         return Collections.unmodifiableList(parsers);
145     }
146 }