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