001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *  
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017     
018    package org.apache.geronimo.mavenplugins.testsuite.report;
019    
020    import org.apache.maven.artifact.handler.ArtifactHandler;
021    import org.apache.maven.model.ReportPlugin;
022    import org.apache.maven.project.MavenProject;
023    import org.apache.maven.reporting.AbstractMavenReport;
024    import org.apache.maven.reporting.MavenReportException;
025    import org.codehaus.doxia.site.renderer.SiteRenderer;
026    import org.codehaus.plexus.util.PathTool;
027    import org.codehaus.plexus.util.StringUtils;
028    
029    import java.io.File;
030    import java.util.Iterator;
031    import java.util.Locale;
032    import java.util.ResourceBundle;
033    
034    
035    /**
036     * Creates a nicely formatted Surefire Test Report in html format.
037     *
038     * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
039     * @version $Id: SurefireReportMojo.java 514085 2007-03-03 06:09:54Z jdillon $
040     * @goal generate-surefire-report
041     */
042    public class SurefireReportMojo
043        extends AbstractMavenReport
044    {
045        /**
046         * Location where generated html will be created.
047         *
048         * @parameter expression="${project.build.directory}/site"
049         */
050        private String outputDirectory;
051    
052        /**
053         * Doxia Site Renderer
054         *
055         * @parameter expression="${component.org.codehaus.doxia.site.renderer.SiteRenderer}"
056         * @required @readonly
057         */
058        private SiteRenderer siteRenderer;
059    
060        /**
061         * Maven Project
062         *
063         * @parameter expression="${project}"
064         * @required @readonly
065         */
066        private MavenProject project;
067    
068        /**
069         * If set to false, only failures are shown.
070         *
071         * @parameter expression="${showSuccess}" default-value="true"
072         * @required
073         */
074        private boolean showSuccess;
075    
076        /**
077         * This directory contains the XML Report files that will be parsed and rendered to HTML format.
078         *
079         * @parameter expression="${project.build.directory}/surefire-reports"
080         * @required
081         */
082        private File reportsDirectory;
083    
084        /**
085         * The filename to use for the report.
086         *
087         * @parameter expression="${outputName}" default-value="surefire-report"
088         * @required
089         */
090        private String outputName;
091    
092        /**
093         * Location of the Xrefs to link.
094         *
095         * @parameter default-value="${project.reporting.outputDirectory}/xref-test"
096         */
097        private File xrefLocation;
098    
099        /**
100         * Whether to link the XRef if found.
101         *
102         * @parameter expression="${linkXRef}" default-value="true"
103         */
104        private boolean linkXRef;
105    
106        public void executeReport( Locale locale )
107            throws MavenReportException
108        {
109            /*
110            if ( !"pom".equalsIgnoreCase(project.getPackaging()) ) {
111                System.out.println("Not a pom packaging.");
112                return;
113            }
114            */
115    
116            SurefireReportGenerator report =
117                new SurefireReportGenerator( reportsDirectory, locale, showSuccess, determineXrefLocation() );
118    
119            report.doGenerateReport( getBundle( locale ), getSink() );
120    
121            System.out.println("surefire-report.html generated.");
122        }
123    
124        private String determineXrefLocation()
125        {
126            String location = null;
127    
128            if ( linkXRef )
129            {
130                String relativePath = PathTool.getRelativePath( outputDirectory, xrefLocation.getAbsolutePath() );
131                if ( StringUtils.isEmpty( relativePath ) )
132                {
133                    relativePath = ".";
134                }
135                relativePath = relativePath + "/" + xrefLocation.getName();
136                if ( xrefLocation.exists() )
137                {
138                    // XRef was already generated by manual execution of a lifecycle binding
139                    location = relativePath;
140                }
141                else
142                {
143                    // Not yet generated - check if the report is on its way
144                    for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); )
145                    {
146                        ReportPlugin report = (ReportPlugin) reports.next();
147    
148                        String artifactId = report.getArtifactId();
149                        if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
150                        {
151                            location = relativePath;
152                        }
153                    }
154                }
155    
156                if ( location == null )
157                {
158                    getLog().warn( "Unable to locate Test Source XRef to link to - DISABLED" );
159                }
160            }
161            return location;
162        }
163    
164        public String getName( Locale locale )
165        {
166            return getBundle( locale ).getString( "report.surefire.name" );
167        }
168    
169        public String getDescription( Locale locale )
170        {
171            return getBundle( locale ).getString( "report.surefire.description" );
172        }
173    
174        protected SiteRenderer getSiteRenderer()
175        {
176            return siteRenderer;
177        }
178    
179        protected MavenProject getProject()
180        {
181            return project;
182        }
183    
184        public String getOutputName()
185        {
186            return outputName;
187        }
188    
189        protected String getOutputDirectory()
190        {
191            return outputDirectory;
192        }
193    
194        private ResourceBundle getBundle( Locale locale )
195        {
196            return ResourceBundle.getBundle( "surefire-report", locale, this.getClass().getClassLoader() );
197        }
198    
199        /**
200         * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
201         */
202        public boolean canGenerateReport()
203        {
204            // Only execute reports for "pom" projects
205            return "pom".equalsIgnoreCase(project.getPackaging());
206        }
207    }