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