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