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