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