View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.invoker;
20  
21  import java.util.List;
22  import java.util.Locale;
23  
24  import org.apache.maven.doxia.sink.Sink;
25  import org.apache.maven.plugin.logging.Log;
26  import org.apache.maven.plugins.invoker.model.BuildJob;
27  import org.apache.maven.reporting.AbstractMavenReportRenderer;
28  import org.codehaus.plexus.i18n.I18N;
29  
30  public class InvokerReportRenderer extends AbstractMavenReportRenderer {
31      private final I18N i18n;
32      private final Locale locale;
33      private final Log log;
34      private final List<BuildJob> buildJobs;
35  
36      public InvokerReportRenderer(Sink sink, I18N i18n, Locale locale, Log log, List<BuildJob> buildJobs) {
37          super(sink);
38          this.i18n = i18n;
39          this.locale = locale;
40          this.log = log;
41          this.buildJobs = buildJobs;
42      }
43  
44      @Override
45      public String getTitle() {
46          return getI18nString("title");
47      }
48  
49      /**
50       * @param key The key to translate.
51       * @return the translated key.
52       */
53      private String getI18nString(String key) {
54          return i18n.getString("invoker-report", locale, "report.invoker." + key);
55      }
56  
57      /**
58       * @param key The key to translate.
59       * @param args The args to pass to translated string.
60       * @return the translated key.
61       */
62      private String formatI18nString(String key, Object... args) {
63          return i18n.format("invoker-report", locale, "report.invoker." + key, args);
64      }
65  
66      @Override
67      protected void renderBody() {
68          startSection(getTitle());
69          paragraph(getI18nString("description"));
70  
71          renderSectionSummary();
72  
73          renderSectionDetails();
74  
75          endSection();
76      }
77  
78      private void renderSectionSummary() {
79          startSection(getI18nString("summary.title"));
80  
81          startTable();
82  
83          tableHeader(new String[] {
84              getI18nString("summary.builds"),
85              getI18nString("summary.success"),
86              getI18nString("summary.failures"),
87              getI18nString("summary.skipped"),
88              getI18nString("summary.successrate"),
89              getI18nString("summary.time")
90          });
91  
92          int totalBuilds = buildJobs.size();
93          int totalSuccess = 0;
94          int totalFailures = 0;
95          int totalSkipped = 0;
96          float totalTime = 0.0f;
97  
98          for (BuildJob buildJob : buildJobs) {
99              switch (buildJob.getResult()) {
100                 case BuildJob.Result.SUCCESS:
101                     totalSuccess++;
102                     break;
103                 case BuildJob.Result.SKIPPED:
104                     totalSkipped++;
105                     break;
106                 default:
107                     totalFailures++;
108             }
109             totalTime += buildJob.getTime();
110         }
111 
112         tableRow(new String[] {
113             Integer.toString(totalBuilds),
114             Integer.toString(totalSuccess),
115             Integer.toString(totalFailures),
116             Integer.toString(totalSkipped),
117             (totalSuccess + totalFailures > 0)
118                     ? formatI18nString("value.successrate", (totalSuccess / (float) (totalSuccess + totalFailures)))
119                     : "",
120             formatI18nString("value.time", totalTime)
121         });
122 
123         endTable();
124 
125         endSection();
126     }
127 
128     private void renderSectionDetails() {
129         startSection(getI18nString("detail.title"));
130 
131         startTable();
132 
133         tableHeader(new String[] {
134             getI18nString("detail.name"),
135             getI18nString("detail.result"),
136             getI18nString("detail.time"),
137             getI18nString("detail.message")
138         });
139 
140         for (BuildJob buildJob : buildJobs) {
141             renderBuildJob(buildJob);
142         }
143 
144         endTable();
145 
146         endSection();
147     }
148 
149     private void renderBuildJob(BuildJob buildJob) {
150         tableRow(new String[] {
151             getBuildJobReportName(buildJob),
152             // FIXME image
153             buildJob.getResult(),
154             formatI18nString("value.time", buildJob.getTime()),
155             buildJob.getFailureMessage()
156         });
157     }
158 
159     private String getBuildJobReportName(BuildJob buildJob) {
160         String buildJobName = buildJob.getName();
161         String buildJobDescription = buildJob.getDescription();
162         boolean emptyJobName = buildJobName == null || buildJobName.isEmpty();
163         boolean emptyJobDescription = buildJobDescription == null || buildJobDescription.isEmpty();
164         boolean isReportJobNameComplete = !emptyJobName && !emptyJobDescription;
165         if (isReportJobNameComplete) {
166             return formatI18nString("text.name_with_description", buildJobName, buildJobDescription);
167         } else {
168             String buildJobProject = buildJob.getProject();
169             if (!emptyJobName) {
170                 log.warn(incompleteNameWarning("description", buildJobProject));
171             } else if (!emptyJobDescription) {
172                 log.warn(incompleteNameWarning("name", buildJobProject));
173             }
174             return buildJobProject;
175         }
176     }
177 
178     private static String incompleteNameWarning(String missing, String pom) {
179         return "Incomplete job name-description: " + missing + " is missing. POM (" + pom
180                 + ") will be used in place of job name!";
181     }
182 }