View Javadoc
1   package org.apache.maven.plugins.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugins.annotations.Mojo;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.apache.maven.project.MavenProject;
25  
26  import java.io.File;
27  import java.util.Locale;
28  import java.util.ResourceBundle;
29  
30  import static org.apache.maven.shared.utils.StringUtils.isEmpty;
31  
32  /**
33   * Creates a nicely formatted Failsafe Test Report in html format.
34   * This goal does not run the tests, it only builds the reports.
35   * See <a href="https://issues.apache.org/jira/browse/SUREFIRE-257">
36   *     https://issues.apache.org/jira/browse/SUREFIRE-257</a>
37   *
38   * @author Stephen Connolly
39   * @since 2.10
40   */
41  @Mojo( name = "failsafe-report-only" )
42  @SuppressWarnings( "unused" )
43  public class FailsafeReportMojo
44      extends AbstractSurefireReportMojo
45  {
46  
47      /**
48       * The filename to use for the report.
49       */
50      @Parameter( defaultValue = "failsafe-report", property = "outputName", required = true )
51      private String outputName;
52  
53      /**
54       * If set to true the failsafe report will be generated even when there are no failsafe result files.
55       * Defaults to {@code false} to preserve legacy behaviour pre 2.10.
56       * @since 2.11
57       */
58      @Parameter( defaultValue = "false", property = "alwaysGenerateFailsafeReport" )
59      private boolean alwaysGenerateFailsafeReport;
60  
61      /**
62       * If set to true the failsafe report generation will be skipped.
63       * @since 2.11
64       */
65      @Parameter( defaultValue = "false", property = "skipFailsafeReport" )
66      private boolean skipFailsafeReport;
67  
68      /**
69       * A custom title of the report for the menu and the project reports page.
70       * @since 2.21.0
71       */
72      @Parameter( defaultValue = "", property = "failsafe.report.title" )
73      private String title;
74  
75      /**
76       * A custom description for the project reports page.
77       * @since 2.21.0
78       */
79      @Parameter( defaultValue = "", property = "failsafe.report.description" )
80      private String description;
81  
82      @Override
83      protected File getSurefireReportsDirectory( MavenProject subProject )
84      {
85          String buildDir = subProject.getBuild().getDirectory();
86          return new File( buildDir + "/failsafe-reports" );
87      }
88  
89      @Override
90      public String getOutputName()
91      {
92          return outputName;
93      }
94  
95      @Override
96      protected LocalizedProperties getBundle( Locale locale, ClassLoader resourceBundleClassLoader )
97      {
98          ResourceBundle bundle = ResourceBundle.getBundle( "surefire-report", locale, resourceBundleClassLoader );
99          return new LocalizedProperties( bundle )
100         {
101             @Override
102             public String getReportName()
103             {
104                 return isEmpty( FailsafeReportMojo.this.getTitle() )
105                         ? toLocalizedValue( "report.failsafe.name" ) : FailsafeReportMojo.this.getTitle();
106             }
107 
108             @Override
109             public String getReportDescription()
110             {
111                 return isEmpty( FailsafeReportMojo.this.getDescription() )
112                         ? toLocalizedValue( "report.failsafe.description" ) : FailsafeReportMojo.this.getDescription();
113             }
114 
115             @Override
116             public String getReportHeader()
117             {
118                 return isEmpty( FailsafeReportMojo.this.getTitle() )
119                         ? toLocalizedValue( "report.failsafe.header" ) : FailsafeReportMojo.this.getTitle();
120             }
121         };
122     }
123 
124     @Override
125     protected boolean isSkipped()
126     {
127         return skipFailsafeReport;
128     }
129 
130     @Override
131     protected boolean isGeneratedWhenNoResults()
132     {
133         return alwaysGenerateFailsafeReport;
134     }
135 
136     @Override
137     public void setTitle( String title )
138     {
139         this.title = title;
140     }
141 
142     @Override
143     public String getTitle()
144     {
145         return title;
146     }
147 
148     @Override
149     public void setDescription( String description )
150     {
151         this.description = description;
152     }
153 
154     @Override
155     public String getDescription()
156     {
157         return description;
158     }
159 }