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