1 /* 2 * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.testdoc.maven; 17 18 import java.io.File; 19 import java.util.Locale; 20 import java.util.ResourceBundle; 21 22 import org.apache.maven.artifact.factory.ArtifactFactory; 23 import org.apache.maven.artifact.repository.ArtifactRepository; 24 import org.apache.maven.artifact.resolver.ArtifactResolver; 25 import org.apache.maven.doxia.siterenderer.Renderer; 26 import org.apache.maven.plugin.MojoExecutionException; 27 import org.apache.maven.plugin.logging.Log; 28 import org.apache.maven.project.MavenProject; 29 import org.apache.maven.reporting.AbstractMavenReport; 30 import org.apache.maven.reporting.MavenReportException; 31 import org.codehaus.plexus.util.StringUtils; 32 33 import de.smartics.maven.io.MojoIoUtils; 34 import de.smartics.maven.util.LoggingUtils; 35 import de.smartics.maven.util.report.MessageHelper; 36 37 /** 38 * The abstract base implementation for reports. 39 * 40 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a> 41 * @version $Revision:591 $ 42 */ 43 public abstract class AbstractReportMojo extends AbstractMavenReport 44 { 45 // ********************************* Fields ********************************* 46 47 // --- constants ------------------------------------------------------------ 48 49 // --- members -------------------------------------------------------------- 50 51 /** 52 * The name of the report used as title. 53 * 54 * @parameter 55 * @since 1.0 56 */ 57 protected String name; 58 59 /** 60 * The description of the report. 61 * 62 * @parameter 63 * @since 1.0 64 */ 65 protected String description; 66 67 /** 68 * The name of the report. 69 * 70 * @parameter default-value="test-stories" 71 * @since 1.0 72 */ 73 protected String outputName; 74 75 /** 76 * The resource bundle containing the localized resource that provide labels 77 * to use for the report. 78 * <p> 79 * The bundle file allows to provide labels for the following information: 80 * </p> 81 * <ol> 82 * <li>The properties in this file override the default labels provided by 83 * this plugin (<code>de.smartics.testdoc.maven.Report.properties</code>).</li> 84 * <li>Provide values for scenario sentences. The id is built from the fully 85 * qualified class of the test case plus the name of the method, separated by 86 * the pound sign. E.g. 87 * <code>de.smartics.test.MyClass#myTestMethod=The sentence to show.</code> 88 * (includes the dot).</li> 89 * <li>Provide labels for test category types. The key is the fully qualified 90 * class name of the category specified by JUnit's <code>Category</code> 91 * annotation. E.g. 92 * <code>de.smartics.testdoc.categories.Technical=Technical</code></li> 93 * </ol> 94 * <p> 95 * Specify the name of the resource bundle. The bundle is required to be in 96 * the class path. 97 * </p> 98 * 99 * @parameter 100 * @since 1.0 101 */ 102 private String textBundleBaseName; 103 104 protected MessageHelper messages; 105 106 // ... plugin infrastructure ................................................ 107 108 /** 109 * The Maven project. 110 * 111 * @parameter expression="${project}" 112 * @required 113 * @readonly 114 * @since 1.0 115 */ 116 protected MavenProject project; 117 118 /** 119 * The Doxia site renderer. 120 * 121 * @component 122 * @required 123 * @readonly 124 * @since 1.0 125 */ 126 protected Renderer siteRenderer; 127 128 /** 129 * Local Repository. 130 * 131 * @parameter expression="${localRepository}" 132 * @required 133 * @readonly 134 * @since 1.0 135 */ 136 protected ArtifactRepository localRepository; 137 138 /** 139 * The resolver for resolving artifacts. 140 * 141 * @component 142 * @required 143 * @readonly 144 * @since 1.0 145 */ 146 protected ArtifactResolver resolver; 147 148 /** 149 * The factory to create dependent artifacts. 150 * 151 * @component 152 * @required 153 * @readonly 154 * @since 1.0 155 */ 156 protected ArtifactFactory factory; 157 158 // ... report configuration parameters ...................................... 159 160 /** 161 * Specifies the directory where the report will written to. This information 162 * is only used if the report is not part of the site generation process. 163 * 164 * @parameter expression="${project.reporting.outputDirectory}" 165 * @readonly 166 * @since 1.0 167 */ 168 protected File outputDirectory; 169 170 /** 171 * Specifies the log level used for this plugin. 172 * <p> 173 * Allowed values are <code>SEVERE</code>, <code>WARNING</code>, 174 * <code>INFO</code> and <code>FINEST</code>. 175 * </p> 176 * 177 * @parameter expression="${testdoc.test-stories-report.logLevel}" 178 * @since 1.0 179 */ 180 protected String logLevel; 181 182 /** 183 * The locale to use regardless of the report. This should be set to the 184 * locale the Javadoc comment is written in. If not set, the Maven provided 185 * locale is used. 186 * 187 * @parameter expression="${testdoc.locale}" 188 * @since 1.0 189 */ 190 protected String locale; 191 192 /** 193 * A simple flag to skip the generation of the report. If set on the command 194 * line use <code>-Dtestdoc.test-stories-report.skip</code>. 195 * 196 * @parameter expression="${testdoc.test-stories-report.skip}" default-value="false" 197 * @since 1.0 198 */ 199 private boolean skip; 200 201 // ****************************** Initializer ******************************* 202 203 // ****************************** Constructors ****************************** 204 205 // ****************************** Inner Classes ***************************** 206 207 // ********************************* Methods ******************************** 208 209 // --- init ----------------------------------------------------------------- 210 211 // --- get&set -------------------------------------------------------------- 212 213 // ... plugin infrastructure ................................................ 214 215 /** 216 * {@inheritDoc} 217 * 218 * @see org.apache.maven.reporting.AbstractMavenReport#getProject() 219 */ 220 @Override 221 protected MavenProject getProject() 222 { 223 return project; 224 } 225 226 /** 227 * {@inheritDoc} 228 * 229 * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer() 230 */ 231 @Override 232 protected Renderer getSiteRenderer() 233 { 234 return siteRenderer; 235 } 236 237 // ... report configuration parameters ...................................... 238 239 // ... basics 240 241 /** 242 * {@inheritDoc} 243 * 244 * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory() 245 */ 246 @Override 247 protected String getOutputDirectory() 248 { 249 return outputDirectory.getAbsolutePath(); 250 } 251 252 /** 253 * {@inheritDoc} 254 * 255 * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale) 256 */ 257 public String getName(final Locale locale) 258 { 259 initMessages(locale); 260 return StringUtils.isNotBlank(name) ? name : messages 261 .getLabel("report.name." + outputName); 262 } 263 264 /** 265 * {@inheritDoc} 266 * 267 * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale) 268 */ 269 public String getDescription(final Locale locale) 270 { 271 initMessages(locale); 272 return StringUtils.isNotBlank(description) ? description : messages 273 .getLabel("report.description." + outputName); 274 } 275 276 // --- business ------------------------------------------------------------- 277 278 /** 279 * Runs the report generation. 280 * 281 * @throws MojoExecutionException on any problem encountered. 282 */ 283 public void execute() throws MojoExecutionException 284 { 285 final Log log = getLog(); 286 if (!canGenerateReport()) 287 { 288 if (log.isInfoEnabled()) 289 { 290 log.info("Report '" + getName(Locale.getDefault()) 291 + "' skipped due to offline mode."); 292 } 293 return; 294 } 295 296 LoggingUtils.configureLogger(log, logLevel); 297 } 298 299 /** 300 * {@inheritDoc} 301 * 302 * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport() 303 */ 304 @Override 305 public boolean canGenerateReport() 306 { 307 return super.canGenerateReport() && !skip; 308 } 309 310 /** 311 * {@inheritDoc} 312 * <p> 313 * Configures the plugin logger. 314 * </p> 315 * 316 * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale) 317 */ 318 @Override 319 protected void executeReport(final Locale locale) throws MavenReportException 320 { 321 LoggingUtils.configureLogger(getLog(), logLevel); 322 MojoIoUtils.provideReportDirectory(outputDirectory); 323 initMessages(locale); 324 } 325 326 private void initMessages(final Locale locale) 327 { 328 if (messages == null) 329 { 330 final ResourceBundle primaryMessages = getPrimaryBundle(locale); 331 final ResourceBundle defaultMessages = getBundle(locale); 332 333 final String nameId = 334 StringUtils.isNotBlank(name) ? name : "report.name." + outputName; 335 final String descriptionId = 336 StringUtils.isNotBlank(description) ? description 337 : "report.description." + outputName; 338 messages = 339 new MessageHelper(nameId, descriptionId, primaryMessages, 340 defaultMessages); 341 } 342 } 343 344 /** 345 * Returns the resource bundle for the given locale. 346 * 347 * @param locale the locale for which the resource bundle is requested. 348 * @return the bundle for the given locale. 349 */ 350 protected ResourceBundle getBundle(final Locale locale) 351 { 352 return ResourceBundle.getBundle("de.smartics.testdoc.maven.Report", locale); 353 } 354 355 /** 356 * {@inheritDoc} 357 * 358 * @see org.apache.maven.reporting.MavenReport#getOutputName() 359 */ 360 public String getOutputName() 361 { 362 return outputName; 363 } 364 365 private ResourceBundle getPrimaryBundle(final Locale locale) 366 { 367 if (StringUtils.isNotBlank(textBundleBaseName)) 368 { 369 return ResourceBundle.getBundle(textBundleBaseName, locale); 370 } 371 return null; 372 } 373 374 // --- object basics -------------------------------------------------------- 375 376 }