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