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