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(getGoal(), "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          // Last one is footer noise
83          assertEquals(4, textBlocks.length - 1);
84          assertEquals(getString("report.modules.title"), textBlocks[1].getText());
85          assertEquals(getString("report.modules.intro"), textBlocks[2].getText());
86  
87          String[][] cellTexts = response.getTables()[0].asText();
88          assertEquals(3, cellTexts.length);
89          assertEquals(2, cellTexts[0].length);
90          assertEquals(getString("report.modules.header.name"), cellTexts[0][0]);
91          assertEquals(getString("report.modules.header.description"), cellTexts[0][1]);
92          assertEquals("project1", cellTexts[1][0]);
93          assertEquals("-", cellTexts[1][1]);
94          assertEquals("project2", cellTexts[2][0]);
95          assertEquals("project2 description", cellTexts[2][1]);
96      }
97  
98      /**
99       * Test report with variable from settings interpolation in modules URL links (MPIR-349)
100      *
101      * @throws Exception if any
102      */
103     public void testReportModuleLinksVariableSettingsInterpolated() throws Exception {
104         String pluginXml = "modules-variable-settings-interpolated-plugin-config.xml";
105         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
106         AbstractProjectInfoReport mojo = createReportMojo(getGoal(), pluginXmlFile);
107 
108         class SubProjectStub extends SubProject1Stub {
109             @Override
110             public File getBasedir() {
111                 return new File("src/test/resources/plugin-configs/subproject-site-url").getAbsoluteFile();
112             }
113 
114             @Override
115             protected String getPOM() {
116                 return "pom.xml";
117             }
118         }
119         Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses("reactorProjects", mojo.getClass());
120         field.setAccessible(true);
121         field.set(mojo, Collections.singletonList(new SubProjectStub()));
122 
123         generateReport(mojo, pluginXmlFile);
124 
125         assertFalse(
126                 "Variable 'sitePublishLocation' should be interpolated",
127                 new String(Files.readAllBytes(getGeneratedReport("modules.html").toPath()), StandardCharsets.UTF_8)
128                         .contains("sitePublishLocation"));
129     }
130 
131     @Override
132     protected String getGoal() {
133         return "modules";
134     }
135 }