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 java.io.File;
23  import java.io.IOException;
24  import java.io.UnsupportedEncodingException;
25  import java.io.Writer;
26  import java.net.URL;
27  import java.net.URLDecoder;
28  import java.util.Locale;
29  import org.apache.maven.doxia.site.decoration.DecorationModel;
30  import org.apache.maven.doxia.siterenderer.RendererException;
31  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
32  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
33  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
34  import org.apache.maven.shared.utils.WriterFactory;
35  import org.apache.maven.shared.utils.io.FileUtils;
36  import org.apache.maven.shared.utils.io.IOUtil;
37  
38  /**
39   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
40   */
41  public class SurefireReportMojoTest
42      extends AbstractMojoTestCase
43  {
44      public void testBasicSurefireReport()
45          throws Exception
46      {
47          File testPom = new File( getUnitBaseDir(), "basic-surefire-report-test/plugin-config.xml" );
48  
49          SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
50  
51          assertNotNull( mojo );
52  
53          File outputDir = (File) getVariableValueFromObject( mojo, "outputDirectory" );
54  
55          boolean showSuccess = ( (Boolean) getVariableValueFromObject( mojo, "showSuccess" ) ).booleanValue();
56  
57          File reportsDir = (File) getVariableValueFromObject( mojo, "reportsDirectory" );
58  
59          String outputName = (String) getVariableValueFromObject( mojo, "outputName" );
60  
61          File xrefLocation = (File) getVariableValueFromObject( mojo, "xrefLocation" );
62  
63          boolean linkXRef = ( (Boolean) getVariableValueFromObject( mojo, "linkXRef" ) ).booleanValue();
64  
65          assertEquals( new File( getBasedir() + "/target/site/unit/basic-surefire-report-test" ), outputDir );
66  
67          assertTrue( showSuccess );
68  
69          assertEquals( new File(
70              getBasedir() + "/src/test/resources/unit/basic-surefire-report-test/surefire-reports" ).getAbsolutePath(),
71                        reportsDir.getAbsolutePath() );
72  
73          assertEquals( "surefire-report", outputName );
74          assertEquals(
75              new File( getBasedir() + "/target/site/unit/basic-surefire-report-test/xref-test" ).getAbsolutePath(),
76              xrefLocation.getAbsolutePath() );
77  
78          assertTrue( linkXRef );
79  
80          mojo.execute();
81  
82          File report = new File( getBasedir(), "target/site/unit/basic-surefire-report-test/surefire-report.html" );
83  
84          renderer( mojo, report );
85  
86          assertTrue( report.exists() );
87  
88          String htmlContent = FileUtils.fileRead( report );
89  
90          int idx = htmlContent.indexOf( "images/icon_success_sml.gif" );
91  
92          assertTrue( idx >= 0 );
93      }
94  
95      private File getUnitBaseDir()
96          throws UnsupportedEncodingException
97      {
98          URL resource = getClass().getResource( "/unit" );
99          // URLDecoder.decode necessary for JDK 1.5+, where spaces are escaped to %20
100         return new File( URLDecoder.decode( resource.getPath(), "UTF-8" ) ).getAbsoluteFile();
101     }
102 
103     public void testBasicSurefireReportIfShowSuccessIsFalse()
104         throws Exception
105     {
106         File testPom = new File( getUnitBaseDir(), "basic-surefire-report-success-false/plugin-config.xml" );
107 
108         SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
109 
110         assertNotNull( mojo );
111 
112         boolean showSuccess = ( (Boolean) getVariableValueFromObject( mojo, "showSuccess" ) ).booleanValue();
113 
114         assertFalse( showSuccess );
115 
116         mojo.execute();
117 
118         File report =
119             new File( getBasedir(), "target/site/unit/basic-surefire-report-success-false/surefire-report.html" );
120 
121         renderer( mojo, report );
122 
123         assertTrue( report.exists() );
124 
125         String htmlContent = FileUtils.fileRead( report );
126 
127         int idx = htmlContent.indexOf( "images/icon_success_sml.gif" );
128 
129         assertTrue( idx < 0 );
130     }
131 
132     public void testBasicSurefireReportIfLinkXrefIsFalse()
133         throws Exception
134     {
135         File testPom = new File( getUnitBaseDir(), "basic-surefire-report-linkxref-false/plugin-config.xml" );
136 
137         SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
138 
139         assertNotNull( mojo );
140 
141         boolean linkXRef = ( (Boolean) getVariableValueFromObject( mojo, "linkXRef" ) ).booleanValue();
142 
143         assertFalse( linkXRef );
144 
145         mojo.execute();
146 
147         File report =
148             new File( getBasedir(), "target/site/unit/basic-surefire-report-success-false/surefire-report.html" );
149 
150         renderer( mojo, report );
151 
152         assertTrue( report.exists() );
153 
154         String htmlContent = FileUtils.fileRead( report );
155 
156         int idx = htmlContent.indexOf( "./xref-test/com/shape/CircleTest.html#44" );
157 
158         assertTrue( idx == -1 );
159     }
160 
161     public void testBasicSurefireReportIfReportingIsNull()
162         throws Exception
163     {
164         File testPom = new File( getUnitBaseDir(), "basic-surefire-report-reporting-null/plugin-config.xml" );
165 
166         SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
167 
168         assertNotNull( mojo );
169 
170         mojo.execute();
171 
172         File report =
173             new File( getBasedir(), "target/site/unit/basic-surefire-report-reporting-null/surefire-report.html" );
174 
175         renderer( mojo, report );
176 
177         assertTrue( report.exists() );
178 
179         String htmlContent = FileUtils.fileRead( report );
180 
181         int idx = htmlContent.indexOf( "./xref-test/com/shape/CircleTest.html#44" );
182 
183         assertTrue( idx < 0 );
184     }
185 
186     /**
187      * Renderer the sink from the report mojo.
188      *
189      * @param mojo       not null
190      * @param outputHtml not null
191      * @throws RendererException if any
192      * @throws IOException       if any
193      */
194     private void renderer( SurefireReportMojo mojo, File outputHtml )
195         throws RendererException, IOException
196     {
197         Writer writer = null;
198         SiteRenderingContext context = new SiteRenderingContext();
199         context.setDecoration( new DecorationModel() );
200         context.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
201         context.setLocale( Locale.ENGLISH );
202 
203         try
204         {
205             outputHtml.getParentFile().mkdirs();
206             writer = WriterFactory.newXmlWriter( outputHtml );
207 
208             mojo.getSiteRenderer().generateDocument( writer, (SiteRendererSink) mojo.getSink(), context );
209         }
210         finally
211         {
212             IOUtil.close( writer );
213         }
214     }
215 }