View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.maven.plugins.surefire.report;
21  
22  import org.apache.maven.doxia.site.decoration.DecorationModel;
23  import org.apache.maven.doxia.siterenderer.Renderer;
24  import org.apache.maven.doxia.siterenderer.RendererException;
25  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
26  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.apache.maven.shared.utils.WriterFactory;
29  import org.apache.maven.shared.utils.io.FileUtils;
30  import org.apache.maven.shared.utils.io.IOUtil;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.io.UnsupportedEncodingException;
35  import java.io.Writer;
36  import java.net.URL;
37  import java.net.URLDecoder;
38  import java.util.Locale;
39  
40  /**
41   * Prevent fom NPE if failure type and message is null however detail presents.
42   */
43  public class Surefire1183Test extends AbstractMojoTestCase
44  {
45      private Renderer renderer;
46  
47      @Override
48      protected void setUp()
49              throws Exception
50      {
51          super.setUp();
52          renderer = (Renderer) lookup( Renderer.ROLE );
53      }
54  
55      private File getTestBaseDir()
56              throws UnsupportedEncodingException
57      {
58          URL resource = getClass().getResource( "/surefire-1183" );
59          // URLDecoder.decode necessary for JDK 1.5+, where spaces are escaped to %20
60          return new File( URLDecoder.decode ( resource.getPath(), "UTF-8" ) ).getAbsoluteFile();
61      }
62  
63      /**
64       * Renderer the sink from the report mojo.
65       *
66       * @param mojo       not null
67       * @param outputHtml not null
68       * @throws RendererException if any
69       * @throws IOException       if any
70       */
71      private void renderer( SurefireReportMojo mojo, File outputHtml )
72              throws RendererException, IOException
73      {
74          Writer writer = null;
75          SiteRenderingContext context = new SiteRenderingContext();
76          context.setDecoration( new DecorationModel() );
77          context.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
78          context.setLocale( Locale.ENGLISH );
79  
80          try
81          {
82              outputHtml.getParentFile().mkdirs();
83              writer = WriterFactory.newXmlWriter ( outputHtml );
84  
85              renderer.generateDocument( writer, (SiteRendererSink ) mojo.getSink(), context );
86          }
87          finally
88          {
89              IOUtil.close ( writer );
90          }
91      }
92  
93      public void testCustomTitleAndDescriptionReport()
94              throws Exception
95      {
96          File testPom = new File( getTestBaseDir(), "plugin-config.xml" );
97          SurefireReportMojo mojo = (SurefireReportMojo) lookupMojo( "report", testPom );
98  
99          File outputDir = (File) getVariableValueFromObject( mojo, "outputDirectory" );
100         String outputName = (String) getVariableValueFromObject( mojo, "outputName" );
101         File reportsDir = (File) getVariableValueFromObject( mojo, "reportsDirectory" );
102         String title = (String) getVariableValueFromObject( mojo, "title" );
103         String description = (String) getVariableValueFromObject( mojo, "description" );
104 
105         assertEquals( new File( getBasedir() + "/target/site/surefire-1183" ), outputDir );
106         assertEquals( new File( getBasedir() + "/src/test/resources/surefire-1183/acceptancetest-reports" )
107                         .getAbsolutePath(), reportsDir.getAbsolutePath() );
108         assertEquals( "acceptance-test-report", outputName );
109         assertEquals( "Acceptance Test", title );
110         assertEquals( "Acceptance Test Description", description );
111 
112         mojo.execute();
113 
114         File report = new File( getBasedir(), "target/site/acceptance-test-report.html" );
115         renderer( mojo, report );
116 
117         assertTrue( report.exists() );
118 
119         String htmlContent = FileUtils.fileRead ( report );
120         assertTrue( htmlContent.contains ( "<h2><a name=\"Acceptance_Test\"></a>Acceptance Test</h2></div>" ) );
121     }
122 }