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.net.URL;
23  
24  import com.meterware.httpunit.GetMethodWebRequest;
25  import com.meterware.httpunit.TextBlock;
26  import com.meterware.httpunit.WebConversation;
27  import com.meterware.httpunit.WebRequest;
28  import com.meterware.httpunit.WebResponse;
29  import com.meterware.httpunit.WebTable;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
32  import org.apache.maven.project.ProjectBuilder;
33  import org.apache.maven.project.ProjectBuildingRequest;
34  import org.apache.maven.project.ProjectBuildingResult;
35  import org.mockito.invocation.InvocationOnMock;
36  import org.mockito.stubbing.Answer;
37  
38  import static org.mockito.Mockito.isA;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.when;
41  
42  /**
43   * @author Nick Stolwijk
44   * @since 2.1
45   */
46  public class PluginManagementReportTest extends AbstractProjectInfoTestCase {
47      /**
48       * WebConversation object
49       */
50      private static final WebConversation WEB_CONVERSATION = new WebConversation();
51  
52      @Override
53      protected AbstractProjectInfoReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
54          AbstractProjectInfoReport mojo = super.createReportMojo(goal, pluginXmlFile);
55  
56          ProjectBuilder builder = mock(ProjectBuilder.class);
57  
58          when(builder.build(isA(Artifact.class), isA(ProjectBuildingRequest.class)))
59                  .thenAnswer(new Answer<ProjectBuildingResult>() {
60                      @Override
61                      public ProjectBuildingResult answer(InvocationOnMock invocation) throws Throwable {
62                          return createProjectBuildingResult((Artifact) invocation.getArgument(0), "http://m.a.o/");
63                      }
64                  });
65  
66          setVariableValueToObject(mojo, "projectBuilder", builder);
67  
68          return mojo;
69      }
70  
71      /**
72       * Test report
73       *
74       * @throws Exception if any
75       */
76      public void testReport() throws Exception {
77          generateReport("plugin-management", "plugin-management-plugin-config.xml");
78          assertTrue(
79                  "Test html generated",
80                  getGeneratedReport("plugin-management.html").exists());
81  
82          URL reportURL = getGeneratedReport("plugin-management.html").toURI().toURL();
83          assertNotNull(reportURL);
84  
85          // HTTPUnit
86          WebRequest request = new GetMethodWebRequest(reportURL.toString());
87          WebResponse response = WEB_CONVERSATION.getResponse(request);
88  
89          // Basic HTML tests
90          assertTrue(response.isHTML());
91          assertTrue(response.getContentLength() > 0);
92  
93          // Test the Page title
94          String expectedTitle =
95                  prepareTitle("plugin management project info", getString("report.plugin-management.title"));
96          assertEquals(expectedTitle, response.getTitle());
97  
98          // Test the tables
99          WebTable[] webTables = response.getTables();
100         assertEquals(1, webTables.length);
101 
102         assertEquals(3, webTables[0].getColumnCount());
103         assertEquals(3, webTables[0].getRowCount());
104 
105         // Test the texts
106         TextBlock[] textBlocks = response.getTextBlocks();
107         assertEquals(getString("report.plugin-management.title"), textBlocks[0].getText());
108     }
109 
110     /**
111      * Test report with excludes (to solve MPIR-375)
112      *
113      * @throws Exception if any
114      */
115     public void testReportEclipseM2EPluginLifecycleMapping() throws Exception {
116         generateReport("plugin-management", "plugin-management-plugin-config-MPIR-375.xml");
117         assertTrue(
118                 "Test html generated",
119                 getGeneratedReport("plugin-management.html").exists());
120 
121         URL reportURL = getGeneratedReport("plugin-management.html").toURI().toURL();
122         assertNotNull(reportURL);
123 
124         // HTTPUnit
125         WebRequest request = new GetMethodWebRequest(reportURL.toString());
126         WebResponse response = WEB_CONVERSATION.getResponse(request);
127 
128         // Basic HTML tests
129         assertTrue(response.isHTML());
130         assertTrue(response.getContentLength() > 0);
131 
132         // Test the Page title
133         String expectedTitle =
134                 prepareTitle("plugin management project info", getString("report.plugin-management.title"));
135         assertEquals(expectedTitle, response.getTitle());
136 
137         // Test the tables
138         WebTable[] webTables = response.getTables();
139         assertEquals(1, webTables.length);
140 
141         // generated table for the plugin management
142         assertEquals(3, webTables[0].getColumnCount());
143         assertEquals(2, webTables[0].getRowCount());
144         // row 0 are the table titles
145         // row 1 is the m-javadoc-plugin
146         assertEquals("org.apache.maven.plugins", webTables[0].getCellAsText(1, 0));
147         assertEquals("maven-javadoc-plugin", webTables[0].getCellAsText(1, 1));
148         assertEquals("3.0.1", webTables[0].getCellAsText(1, 2));
149 
150         // Test the texts
151         TextBlock[] textBlocks = response.getTextBlocks();
152         assertEquals(getString("report.plugin-management.title"), textBlocks[0].getText());
153     }
154 
155     private static ProjectBuildingResult createProjectBuildingResult(Artifact artifact, String url) {
156         ProjectBuildingResult result = mock(ProjectBuildingResult.class);
157         MavenProjectStub stub = new MavenProjectStub();
158         stub.setGroupId(artifact.getGroupId());
159         stub.setArtifactId(artifact.getArtifactId());
160         stub.setVersion(artifact.getVersion());
161         stub.setUrl(url);
162 
163         when(result.getProject()).thenReturn(stub);
164 
165         return result;
166     }
167 }