View Javadoc
1   package org.apache.maven.plugins.site.deploy;
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  
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 Olivier Lamy
51   *
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 pomFile )
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  
86          try
87          {
88              File pomFile = getTestFile( "src/test/resources/unit/deploy-dav/pom.xml" );
89              AbstractMojo mojo = getMojo( pomFile );
90              assertNotNull( mojo );
91              SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub( "deploy-dav" );
92  
93              assertTrue( "dav server port not available: " + simpleDavServerHandler.getPort(),
94                          simpleDavServerHandler.getPort() > 0 );
95  
96              siteMavenProjectStub.getDistributionManagement().getSite()
97                  .setUrl( "dav:http://localhost:" + simpleDavServerHandler.getPort() + "/site/" );
98  
99              setVariableValueToObject( mojo, "project", siteMavenProjectStub );
100             Settings settings = new Settings();
101             setVariableValueToObject( mojo, "settings", settings );
102             File inputDirectory = new File( "src/test/resources/unit/deploy-dav/target/site" );
103 
104             setVariableValueToObject( mojo, "inputDirectory", inputDirectory );
105             mojo.execute();
106 
107             assertContentInFiles();
108             assertFalse( requestsContainsProxyUse( simpleDavServerHandler.httpRequests ) );
109         }
110         finally
111         {
112             simpleDavServerHandler.stop();
113         }
114     }
115 
116     @Test
117     public void davDeployThruProxyWithoutAuthzInProxy()
118         throws Exception
119     {
120 
121         FileUtils.cleanDirectory( siteTargetPath );
122         SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( siteTargetPath );
123         try
124         {
125             File pluginXmlFile = getTestFile( "src/test/resources/unit/deploy-dav/pom.xml" );
126             AbstractMojo mojo = getMojo( pluginXmlFile );
127             assertNotNull( mojo );
128             SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub( "deploy-dav" );
129             // olamy, Note : toto is something like foo or bar for french folks :-)
130             String siteUrl = "dav:http://toto.com/site/";
131             siteMavenProjectStub.getDistributionManagement().getSite().setUrl( siteUrl );
132 
133             setVariableValueToObject( mojo, "project", siteMavenProjectStub );
134             Settings settings = new Settings();
135             Proxy proxy = new Proxy();
136 
137             //dummy proxy
138             proxy.setActive( true );
139             proxy.setHost( "localhost" );
140             proxy.setPort( simpleDavServerHandler.getPort() );
141             proxy.setProtocol( "http" );
142             proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
143             settings.addProxy( proxy );
144 
145             setVariableValueToObject( mojo, "settings", settings );
146 
147             MavenExecutionRequest request = new DefaultMavenExecutionRequest();
148             request.setProxies( Arrays.asList( proxy ) );
149             MavenSession mavenSession = new MavenSession( getContainer(), null, request, null );
150 
151             setVariableValueToObject( mojo, "mavenSession", mavenSession );
152 
153             File inputDirectory = new File( "src/test/resources/unit/deploy-dav/target/site" );
154 
155             setVariableValueToObject( mojo, "inputDirectory", inputDirectory );
156             mojo.execute();
157 
158             assertContentInFiles();
159 
160             assertTrue( requestsContainsProxyUse( simpleDavServerHandler.httpRequests ) );
161 
162             for ( HttpRequest rq : simpleDavServerHandler.httpRequests )
163             {
164                 log.info( rq.toString() );
165             }
166 
167         }
168         finally
169         {
170             simpleDavServerHandler.stop();
171         }        
172         
173     }
174 
175     @Test
176     public void davDeployThruProxyWitAuthzInProxy() throws Exception
177     {
178 
179         FileUtils.cleanDirectory( siteTargetPath );
180         //SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( siteTargetPath );
181 
182         Map<String, String> authentications = new HashMap<String, String>();
183         authentications.put( "foo", "titi" );
184 
185         AuthAsyncProxyServlet servlet = new AuthAsyncProxyServlet( authentications, siteTargetPath );
186 
187         SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( servlet );        
188         try
189         {
190             File pluginXmlFile = getTestFile( "src/test/resources/unit/deploy-dav/pom.xml" );
191             AbstractMojo mojo = getMojo( pluginXmlFile );
192             assertNotNull( mojo );
193             SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub( "deploy-dav" );
194 
195             siteMavenProjectStub.getDistributionManagement().getSite()
196                 .setUrl( "dav:http://toto.com/site/" );
197 
198             setVariableValueToObject( mojo, "project", siteMavenProjectStub );
199             Settings settings = new Settings();
200             Proxy proxy = new Proxy();
201 
202             //dummy proxy
203             proxy.setActive( true );
204             proxy.setHost( "localhost" );
205             proxy.setPort( simpleDavServerHandler.getPort() );
206             proxy.setProtocol( "dav" );
207             proxy.setUsername( "foo" );
208             proxy.setPassword( "titi" );
209             proxy.setNonProxyHosts( "www.google.com|*.somewhere.com" );
210             settings.addProxy( proxy );
211 
212             setVariableValueToObject( mojo, "settings", settings );
213 
214             MavenExecutionRequest request = new DefaultMavenExecutionRequest();
215             request.setProxies( Arrays.asList( proxy ) );
216             MavenSession mavenSession = new MavenSession( getContainer(), null, request, null );
217 
218             setVariableValueToObject( mojo, "mavenSession", mavenSession );
219 
220             File inputDirectory = new File( "src/test/resources/unit/deploy-dav/target/site" );
221 
222             // test which mojo we are using
223             if ( ReflectionUtils.getFieldByNameIncludingSuperclasses( "inputDirectory", mojo.getClass() ) != null )
224             {
225                 setVariableValueToObject( mojo, "inputDirectory", inputDirectory );
226             }
227             else
228             {
229                 ArtifactRepositoryFactory artifactRepositoryFactory = getContainer().lookup( ArtifactRepositoryFactory.class );
230 
231                 setVariableValueToObject( mojo, "stagingDirectory", inputDirectory );
232                 setVariableValueToObject( mojo, "reactorProjects", Collections.emptyList() );
233                 setVariableValueToObject( mojo, "localRepository",
234                                           artifactRepositoryFactory.createArtifactRepository( "local", "foo", "default",
235                                                                                               null, null ) );
236                 setVariableValueToObject( mojo, "siteTool", getContainer().lookup( SiteTool.class ) );
237                 setVariableValueToObject( mojo, "siteDirectory", new File("foo") );
238                 setVariableValueToObject( mojo, "repositories", Collections.emptyList() );
239             }
240             mojo.execute();
241 
242             assertContentInFiles();
243             assertTrue( requestsContainsProxyUse( servlet.httpRequests ) );
244             assertAtLeastOneRequestContainsHeader( servlet.httpRequests, "Proxy-Authorization" );
245             for ( HttpRequest rq : servlet.httpRequests )
246             {
247                 log.info( rq.toString() );
248             }
249         }
250         finally
251         {
252             simpleDavServerHandler.stop();
253         }  
254     }        
255 
256     private void assertContentInFiles()
257         throws Exception
258     {
259         File fileToTest = new File( siteTargetPath, "site" + File.separator + "index.html" );
260         assertTrue( fileToTest.exists() );
261         String fileContent = FileUtils.readFileToString( fileToTest );
262         assertTrue( fileContent.contains( "Welcome to Apache Maven" ) );
263 
264         fileToTest = new File( siteTargetPath, "site" + File.separator + "css" + File.separator + "maven-base.css" );
265         assertTrue( fileToTest.exists() );
266         fileContent = FileUtils.readFileToString( fileToTest );
267         assertTrue( fileContent.contains( "background-image: url(../images/collapsed.gif);" ) );
268     }
269 
270     /**
271      * @param requests
272      * @return true if at least on request use proxy http header Proxy-Connection : Keep-Alive
273      */
274     private boolean requestsContainsProxyUse( List<HttpRequest> requests )
275     {
276         return assertAtLeastOneRequestContainsHeader( requests, "Proxy-Connection" );
277     }
278 
279     private boolean assertAtLeastOneRequestContainsHeader( List<HttpRequest> requests, String headerName )
280     {
281         for ( HttpRequest rq : requests )
282         {
283             boolean containsProxyHeader = rq.headers.containsKey( headerName );
284             if ( containsProxyHeader )
285             {
286                 return true;
287             }
288         }
289         return false;
290     }
291 }