View Javadoc
1   package org.apache.maven.report.projectinfo;
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.lang.reflect.Field;
24  import java.net.URL;
25  import java.util.Collections;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.maven.plugin.testing.SilentLog;
29  import org.apache.maven.report.projectinfo.stubs.SubProject1Stub;
30  import org.codehaus.plexus.util.ReflectionUtils;
31  
32  import com.meterware.httpunit.GetMethodWebRequest;
33  import com.meterware.httpunit.TextBlock;
34  import com.meterware.httpunit.WebConversation;
35  import com.meterware.httpunit.WebRequest;
36  import com.meterware.httpunit.WebResponse;
37  
38  /**
39   * @author ltheussl
40   * @version $Id$
41   */
42  public class ModulesReportTest
43      extends AbstractProjectInfoTestCase
44  {
45      /**
46       * WebConversation object
47       */
48      private static final WebConversation WEB_CONVERSATION = new WebConversation();
49      
50      @Override
51      protected AbstractProjectInfoReport createReportMojo( String goal, File pluginXmlFile )
52          throws Exception
53      {
54          AbstractProjectInfoReport mojo = super.createReportMojo( goal, pluginXmlFile );
55          
56          mojo.setLog( new SilentLog() );
57          
58          return mojo;
59      }
60  
61      /**
62       * Test report
63       *
64       * @throws Exception if any
65       */
66      public void testReport()
67          throws Exception
68      {
69          generateReport( "modules", "modules-plugin-config.xml" );
70          assertTrue( "Test html generated", getGeneratedReport( "modules.html" ).exists() );
71  
72          URL reportURL = getGeneratedReport( "modules.html" ).toURI().toURL();
73          assertNotNull( reportURL );
74  
75          // HTTPUnit
76          WebRequest request = new GetMethodWebRequest( reportURL.toString() );
77          WebResponse response = WEB_CONVERSATION.getResponse( request );
78  
79          // Basic HTML tests
80          assertTrue( response.isHTML() );
81          assertTrue( response.getContentLength() > 0 );
82  
83          // Test the Page title
84          String expectedTitle = prepareTitle( getString( "report.modules.name" ), getString( "report.modules.title" ) );
85          assertEquals( expectedTitle, response.getTitle() );
86  
87          // Test the texts
88          TextBlock[] textBlocks = response.getTextBlocks();
89          assertEquals( 2, textBlocks.length );
90          assertEquals( getString( "report.modules.title" ), textBlocks[0].getText() );
91          assertEquals( getString( "report.modules.intro" ), textBlocks[1].getText() );
92  
93          String[][] cellTexts = response.getTables()[0].asText();
94          assertEquals( 3, cellTexts.length );
95          assertEquals( 2, cellTexts[0].length );
96          assertEquals( getString( "report.modules.header.name" ), cellTexts[0][0] );
97          assertEquals( getString( "report.modules.header.description" ), cellTexts[0][1] );
98          assertEquals( "project1", cellTexts[1][0] );
99          assertEquals( "-", cellTexts[1][1] );
100         assertEquals( "project2", cellTexts[2][0] );
101         assertEquals( "project2 description", cellTexts[2][1] );
102     }
103 
104     /**
105      * Test report with variable from settings interpolation in modules URL links (MPIR-349)
106      *
107      * @throws Exception if any
108      */
109     public void testReportModuleLinksVariableSettingsInterpolated()
110         throws Exception
111     {
112         String pluginXml = "modules-variable-settings-interpolated-plugin-config.xml";
113         File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
114         AbstractProjectInfoReport mojo = createReportMojo( "modules", pluginXmlFile );
115 
116         class SubProjectStub
117             extends SubProject1Stub
118         {
119             @Override
120             public File getBasedir()
121             {
122                 return new File( "src/test/resources/plugin-configs/subproject-site-url" ).getAbsoluteFile();
123             }
124 
125             @Override
126             protected String getPOM()
127             {
128                 return "pom.xml";
129             }
130         }
131         Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "reactorProjects", mojo.getClass() );
132         field.setAccessible( true );
133         field.set( mojo, Collections.singletonList( new SubProjectStub() ) );
134 
135         generateReport( mojo, pluginXmlFile );
136 
137         assertFalse( "Variable 'sitePublishLocation' should be interpolated",
138                      FileUtils.readFileToString( getGeneratedReport( "modules.html" ) ).contains( "sitePublishLocation" ) );
139     }
140 }