View Javadoc

1   package org.apache.maven.it;
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.io.IOException;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.maven.it.Verifier;
30  import org.apache.maven.it.util.FileUtils;
31  import org.apache.maven.it.util.ResourceExtractor;
32  import org.apache.maven.it.util.StringUtils;
33  import org.mortbay.jetty.Handler;
34  import org.mortbay.jetty.Request;
35  import org.mortbay.jetty.Server;
36  import org.mortbay.jetty.handler.AbstractHandler;
37  
38  /**
39   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-3599">MNG-3599</a>.
40   * 
41   * @author Brett Porter
42   * @author John Casey
43   * @version $Id: MavenITmng3599useHttpProxyForWebDAVTest.java 1072641 2011-02-20 17:30:26Z bentmann $
44   */
45  public class MavenITmng3599useHttpProxyForWebDAVTest
46      extends AbstractMavenIntegrationTestCase
47  {
48      private static final String LS = System.getProperty( "line.separator" );
49      
50      private Server server;
51  
52      private int port;
53  
54      private static final String content = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
55                              "  xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" +
56                              "  <modelVersion>4.0.0</modelVersion>\n" +
57                              "  <groupId>org.apache.maven.its.mng3599</groupId>\n" +
58                              "  <artifactId>test</artifactId>\n" +
59                              "  <version>1.0-SNAPSHOT</version>\n" +
60                              "  <name>MNG-3599</name>\n" +
61                              "</project>";
62  
63      public MavenITmng3599useHttpProxyForWebDAVTest()
64      {
65          super( "(2.0.9,)" );
66      }
67  
68      public void setUp()
69          throws Exception
70      {
71          Handler handler = new AbstractHandler()
72          {
73              public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
74                  throws IOException, ServletException
75              {
76                  System.out.println( "Got request for URL: '" + request.getRequestURL() + "'" );
77                  System.out.flush();
78                  
79                  response.setContentType( "text/plain" );
80  
81                  System.out.println( "Checking for 'Proxy-Connection' header..." );
82                  if ( request.getHeader( "Proxy-Connection" ) != null )
83                  {
84                      response.setStatus( HttpServletResponse.SC_OK );
85                      response.getWriter().println( content );
86                      
87                      System.out.println( "Proxy-Connection found." );
88                  }
89                  /*
90                   * 2008-09-29 Oleg: "Proxy-Connection" is not part of http spec, but an extended header, and 
91                   * as such cannot be expected from all the clients.
92                   * Changing the code to test for more generalized case: local proxy receives a request with
93                   * correct server url and resource uri
94                   */
95                  else if( 
96                      request.getRequestURI().startsWith( "/org/apache/maven/its/mng3599/test-dependency" )
97                      && request.getRequestURL().toString().startsWith( "http://www.example.com" )
98                  )
99                  {
100                     response.setStatus( HttpServletResponse.SC_OK );
101                     response.getWriter().println( content );
102                     
103                     System.out.println( "Correct proxied request 'http://www.example.com' for resource '/org/apache/maven/its/mng3599/test-dependency' found." );
104                 }
105                 else
106                 {
107                     response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
108                     
109                     System.out.println( "Proxy-Connection not found." );
110                 }
111 
112                 ( (Request) request ).setHandled( true );
113             }
114         };
115 
116         server = new Server( 0 );
117         server.setHandler( handler );
118         server.start();
119 
120         port = server.getConnectors()[0].getLocalPort();
121     }
122 
123     protected void tearDown()
124         throws Exception
125     {
126         super.tearDown();
127 
128         if ( server != null )
129         {
130             server.stop();
131             server = null;
132         }
133     }
134 
135     public void testitUseHttpProxyForHttp()
136         throws Exception
137     {
138         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3599" );
139 
140         /*
141          * NOTE: Make sure the WebDAV extension required by the test project has been pulled down into the local
142          * repo before the actual test installs Jetty as a mirror for everything. Otherwise, we will get garbage
143          * for the JAR/POM of the extension and its dependencies when run against a vanilla repo.
144          */
145         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
146         verifier.executeGoal( "validate" );
147         verifier.verifyErrorFreeLog();
148         verifier.resetStreams();
149 
150         String settings = FileUtils.fileRead( new File( testDir, "settings-template.xml" ) );
151         settings = StringUtils.replace( settings, "@port@", Integer.toString( port ) );
152         String newSettings = StringUtils.replace( settings, "@protocol@", "http" );
153         
154         FileUtils.fileWrite( new File( testDir, "settings.xml" ).getAbsolutePath(), newSettings );
155         
156         verifier = newVerifier( testDir.getAbsolutePath() );
157 
158         verifier.getCliOptions().add( "--settings" );
159         verifier.getCliOptions().add( "settings.xml" );
160         verifier.getCliOptions().add( "-X" );
161 
162         verifier.deleteArtifacts( "org.apache.maven.its.mng3599" );
163 
164         verifier.setLogFileName( "logHttp.txt" );
165         verifier.executeGoal( "compile" );
166         verifier.verifyErrorFreeLog();
167         verifier.resetStreams();
168 
169         verifier.assertArtifactPresent( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar" );
170         verifier.assertArtifactContents( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar",
171                                          content + LS );
172     }
173 
174     /**
175      * Test that HTTP proxy is used for HTTP and for WebDAV.
176      */
177     public void testitUseHttpProxyForWebDAV()
178         throws Exception
179     {
180         requiresMavenVersion( "[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,)" );
181 
182         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3599" );
183 
184         /*
185          * NOTE: Make sure the WebDAV extension required by the test project has been pulled down into the local
186          * repo before the actual test installs Jetty as a mirror for everything. Otherwise, we will get garbage
187          * for the JAR/POM of the extension and its dependencies when run against a vanilla repo.
188          */
189         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
190         verifier.executeGoal( "validate" );
191         verifier.verifyErrorFreeLog();
192         verifier.resetStreams();
193 
194         String settings = FileUtils.fileRead( new File( testDir, "settings-template.xml" ) );
195         settings = StringUtils.replace( settings, "@port@", Integer.toString( port ) );
196         String newSettings = StringUtils.replace( settings, "@protocol@", "dav" );
197         
198         FileUtils.fileWrite( new File( testDir, "settings.xml" ).getAbsolutePath(), newSettings );
199 
200         verifier = newVerifier( testDir.getAbsolutePath() );
201 
202         verifier.getCliOptions().add( "--settings" );
203         verifier.getCliOptions().add( "settings.xml" );
204         verifier.getCliOptions().add( "-X" );
205 
206         verifier.deleteArtifacts( "org.apache.maven.its.mng3599" );
207 
208         verifier.setLogFileName( "logDAV.txt" );
209         verifier.executeGoal( "compile" );
210         verifier.verifyErrorFreeLog();
211         verifier.resetStreams();
212 
213         verifier.assertArtifactPresent( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar" );
214         verifier.assertArtifactContents( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar",
215                                          content + LS );
216     }
217 }