View Javadoc

1   package org.apache.maven.plugins.site;
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 java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.commons.io.FileUtils;
30  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
31  import org.apache.maven.doxia.tools.SiteTool;
32  import org.apache.maven.execution.DefaultMavenExecutionRequest;
33  import org.apache.maven.execution.MavenExecutionRequest;
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.plugin.AbstractMojo;
36  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
37  import org.apache.maven.plugins.site.SimpleDavServerHandler.HttpRequest;
38  import org.apache.maven.plugins.site.stubs.SiteMavenProjectStub;
39  import org.apache.maven.settings.Proxy;
40  import org.apache.maven.settings.Settings;
41  import org.codehaus.plexus.util.ReflectionUtils;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.junit.runner.RunWith;
45  import org.junit.runners.JUnit4;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * @author <a href="mailto:olamy@apache.org">olamy</a>
51   * @version $Id: AbstractSiteDeployWebDavTest.html 816568 2012-05-08 12:09:38Z hboutemy $
52   */
53  @RunWith(JUnit4.class)
54  public abstract class AbstractSiteDeployWebDavTest
55      extends AbstractMojoTestCase
56  {
57      
58      File siteTargetPath = new File( getBasedir() + File.separator + "target" + File.separator + "siteTargetDeploy" );
59      
60      private Logger log = LoggerFactory.getLogger( getClass() );
61      
62      @Before
63      public void setUp()
64          throws Exception
65      {
66          super.setUp();
67          if ( !siteTargetPath.exists() )
68          {
69              siteTargetPath.mkdirs();
70              FileUtils.cleanDirectory( siteTargetPath );
71          }
72      }
73      
74      abstract String getMojoName();
75      
76      abstract AbstractMojo getMojo( File pluginXmlFile )
77          throws Exception;
78      
79      @Test
80      public void noAuthzDavDeploy()
81          throws Exception
82      {
83          FileUtils.cleanDirectory( siteTargetPath );
84          SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( siteTargetPath );
85          try
86          {
87              File pluginXmlFile = getTestFile( "src/test/resources/unit/deploy-dav/pom.xml" );
88              AbstractMojo mojo = getMojo( pluginXmlFile );
89              assertNotNull( mojo );
90              SiteMavenProjectStub siteMavenProjectStub =
91                  new SiteMavenProjectStub( "src/test/resources/unit/deploy-dav/pom.xml" );
92              
93              siteMavenProjectStub.getDistributionManagement().getSite()
94                  .setUrl( "dav:http://localhost:" + simpleDavServerHandler.getPort() + "/site/" );
95              
96              setVariableValueToObject( mojo, "project", siteMavenProjectStub );
97              Settings settings = new Settings();
98              setVariableValueToObject( mojo, "settings", settings );
99              File inputDirectory = new File( "src/test/resources/unit/deploy-dav/target/site" );
100             
101             setVariableValueToObject( mojo, "inputDirectory", inputDirectory );
102             mojo.execute();
103             
104             assertContentInFiles();
105             assertFalse( requestsContainsProxyUse( simpleDavServerHandler.httpRequests ) );
106         }
107         finally
108         {
109             simpleDavServerHandler.stop();
110         }
111     }
112     
113     @Test
114     public void davDeployThruProxyWithoutAuthzInProxy()
115         throws Exception
116     {
117         
118         FileUtils.cleanDirectory( siteTargetPath );
119         SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( siteTargetPath );
120         try
121         {
122             File pluginXmlFile = getTestFile( "src/test/resources/unit/deploy-dav/pom.xml" );
123             AbstractMojo mojo = getMojo( pluginXmlFile );
124             assertNotNull( mojo );
125             SiteMavenProjectStub siteMavenProjectStub =
126                 new SiteMavenProjectStub( "src/test/resources/unit/deploy-dav/pom.xml" );
127             // olamy, Note : toto is something like foo or bar for french folks :-)
128             String siteUrl = "dav:http://toto.com/site/";
129             siteMavenProjectStub.getDistributionManagement().getSite().setUrl( siteUrl );
130             
131             setVariableValueToObject( mojo, "project", siteMavenProjectStub );
132             Settings settings = new Settings();
133             Proxy proxy = new Proxy();
134 
135             //dummy proxy
136             proxy.setActive( true );
137             proxy.setHost( "localhost" );
138             proxy.setPort( simpleDavServerHandler.getPort() );
139             proxy.setProtocol( "http" );
140             proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
141             settings.addProxy( proxy );
142             
143             setVariableValueToObject( mojo, "settings", settings );
144             
145             MavenExecutionRequest request = new DefaultMavenExecutionRequest();
146             request.setProxies( Arrays.asList( proxy ) );
147             MavenSession mavenSession = new MavenSession( getContainer(), null, request, null );
148             
149             setVariableValueToObject( mojo, "mavenSession", mavenSession );
150             
151             File inputDirectory = new File( "src/test/resources/unit/deploy-dav/target/site" );
152             
153             setVariableValueToObject( mojo, "inputDirectory", inputDirectory );
154             mojo.execute();
155             
156             assertContentInFiles();
157             
158             assertTrue( requestsContainsProxyUse( simpleDavServerHandler.httpRequests ) );
159             
160             for ( HttpRequest rq : simpleDavServerHandler.httpRequests )
161             {
162                 log.info( rq.toString() );
163             }
164             
165         }
166         finally
167         {
168             simpleDavServerHandler.stop();
169         }        
170         
171     }
172     
173     @Test
174     public void davDeployThruProxyWitAuthzInProxy() throws Exception
175     {
176 
177         FileUtils.cleanDirectory( siteTargetPath );
178         //SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( siteTargetPath );
179         
180         Map<String, String> authentications = new HashMap<String, String>();
181         authentications.put( "foo", "titi" );
182         
183         AuthAsyncProxyServlet servlet = new AuthAsyncProxyServlet( authentications, siteTargetPath );
184 
185         SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( servlet );        
186         try
187         {
188             File pluginXmlFile = getTestFile( "src/test/resources/unit/deploy-dav/pom.xml" );
189             AbstractMojo mojo = getMojo( pluginXmlFile );
190             assertNotNull( mojo );
191             SiteMavenProjectStub siteMavenProjectStub =
192                 new SiteMavenProjectStub( "src/test/resources/unit/deploy-dav/pom.xml" );
193             
194             siteMavenProjectStub.getDistributionManagement().getSite()
195                 .setUrl( "dav:http://toto.com/site/" );
196             
197             setVariableValueToObject( mojo, "project", siteMavenProjectStub );
198             Settings settings = new Settings();
199             Proxy proxy = new Proxy();
200 
201             //dummy proxy
202             proxy.setActive( true );
203             proxy.setHost( "localhost" );
204             proxy.setPort( simpleDavServerHandler.getPort() );
205             proxy.setProtocol( "dav" );
206             proxy.setUsername( "foo" );
207             proxy.setPassword( "titi" );
208             proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
209             settings.addProxy( proxy );
210             
211             setVariableValueToObject( mojo, "settings", settings );
212             
213             MavenExecutionRequest request = new DefaultMavenExecutionRequest();
214             request.setProxies( Arrays.asList( proxy ) );
215             MavenSession mavenSession = new MavenSession( getContainer(), null, request, null );
216             
217             setVariableValueToObject( mojo, "mavenSession", mavenSession );
218             
219             File inputDirectory = new File( "src/test/resources/unit/deploy-dav/target/site" );
220             
221             // test which mojo we are using
222             if ( ReflectionUtils.getFieldByNameIncludingSuperclasses( "inputDirectory", mojo.getClass() ) != null )
223             {
224                 setVariableValueToObject( mojo, "inputDirectory", inputDirectory );
225             }
226             else
227             {
228                 ArtifactRepositoryFactory artifactRepositoryFactory = getContainer().lookup( ArtifactRepositoryFactory.class );
229                 
230                 setVariableValueToObject( mojo, "stagingDirectory", inputDirectory );
231                 setVariableValueToObject( mojo, "reactorProjects", Collections.emptyList() );
232                 setVariableValueToObject( mojo, "localRepository",
233                                           artifactRepositoryFactory.createArtifactRepository( "local", "foo", "default",
234                                                                                               null, null ) );
235                 setVariableValueToObject( mojo, "siteTool", getContainer().lookup( SiteTool.class ) );
236                 setVariableValueToObject( mojo, "siteDirectory", new File("foo") );
237                 setVariableValueToObject( mojo, "repositories", Collections.emptyList() );
238             }
239             mojo.execute();
240             
241             assertContentInFiles();
242             assertTrue( requestsContainsProxyUse( servlet.httpRequests ) );
243             assertAtLeastOneRequestContainsHeader( servlet.httpRequests, "Proxy-Authorization" );
244             for ( HttpRequest rq : servlet.httpRequests )
245             {
246                 log.info( rq.toString() );
247             }
248         }
249         finally
250         {
251             simpleDavServerHandler.stop();
252         }  
253     }        
254     
255     private void assertContentInFiles()
256         throws Exception
257     {
258         File fileToTest = new File( siteTargetPath, "site" + File.separator + "index.html" );
259         assertTrue( fileToTest.exists() );
260         String fileContent = FileUtils.readFileToString( fileToTest );
261         assertTrue( fileContent.contains( "Welcome to Apache Maven" ) );
262 
263         fileToTest = new File( siteTargetPath, "site" + File.separator + "css" + File.separator + "maven-base.css" );
264         assertTrue( fileToTest.exists() );
265         fileContent = FileUtils.readFileToString( fileToTest );
266         assertTrue( fileContent.contains( "background-image: url(../images/collapsed.gif);" ) );
267     }
268     
269     /**
270      * @param requests
271      * @return true if at least on request use proxy http header Proxy-Connection : Keep-Alive
272      */
273     private boolean requestsContainsProxyUse( List<HttpRequest> requests )
274     {
275         return assertAtLeastOneRequestContainsHeader(requests, "Proxy-Connection");
276     }
277     
278     private boolean assertAtLeastOneRequestContainsHeader(List<HttpRequest> requests , String headerName)
279     {
280         for ( HttpRequest rq : requests )
281         {
282             boolean containsProxyHeader = rq.headers.containsKey( headerName );
283             if ( containsProxyHeader )
284             {
285                 return true;
286             }
287         }
288         return false;
289     }
290 }