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