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  import java.util.List;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.maven.it.Verifier;
31  import org.apache.maven.it.util.ResourceExtractor;
32  import org.mortbay.jetty.Handler;
33  import org.mortbay.jetty.Request;
34  import org.mortbay.jetty.Server;
35  import org.mortbay.jetty.handler.AbstractHandler;
36  
37  /**
38   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-3652">MNG-3652</a>.
39   * 
40   * @version $Id: MavenITmng3652UserAgentHeaderTest.java 1213229 2011-12-12 13:24:30Z olamy $
41   */
42  public class MavenITmng3652UserAgentHeaderTest
43      extends AbstractMavenIntegrationTestCase
44  {
45      private Server server;
46  
47      private int port;
48  
49      private String userAgent;
50      
51      private String customHeader;
52  
53      public MavenITmng3652UserAgentHeaderTest()
54      {
55          super( "[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,)" ); // 2.1.0-M1+
56      }
57  
58      public void setUp()
59          throws Exception
60      {
61          Handler handler = new AbstractHandler()
62          {
63              public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
64                  throws IOException, ServletException
65              {
66                  System.out.println( "Handling URL: '" + request.getRequestURL() + "'" );
67                  
68                  userAgent = request.getHeader( "User-Agent" );
69                  
70                  customHeader = request.getHeader( "Custom-Header" );
71                  
72                  System.out.println( "Got User-Agent: '" + userAgent + "'" );
73  
74                  response.setContentType( "text/plain" );
75                  response.setStatus( HttpServletResponse.SC_OK );
76                  response.getWriter().println( "some content" );
77                  response.getWriter().println();
78  
79                  ( (Request) request ).setHandled( true );
80              }
81          };
82  
83          server = new Server( 0 );
84          server.setHandler( handler );
85          server.start();
86  
87          port = server.getConnectors()[0].getLocalPort();
88      }
89  
90      protected void tearDown()
91          throws Exception
92      {
93          super.tearDown();
94  
95          if ( server != null)
96          {
97              server.stop();
98              server = null;
99          }
100     }
101 
102     /**
103      * Test that the user agent header is configured in the wagon manager.
104      */
105     public void testmng3652_UnConfiguredHttp()
106         throws Exception
107     {
108         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
109         File pluginDir = new File( testDir, "test-plugin" );
110         File projectDir = new File( testDir, "test-project" );
111 
112         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
113         verifier.executeGoal( "install" );
114         verifier.verifyErrorFreeLog();
115         verifier.resetStreams();
116 
117         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
118 
119         verifier.getCliOptions().add( "-DtestPort=" + port );
120         verifier.getCliOptions().add( "-X" );
121 
122         verifier.setLogFileName( "log-unConfiguredHttp.txt" );
123         verifier.executeGoal( "validate" );
124         verifier.verifyErrorFreeLog();
125         verifier.resetStreams();
126 
127         String userAgent = this.userAgent;
128         assertNotNull( userAgent );
129 
130         File touchFile = new File( projectDir, "target/touch.txt" );
131         assertTrue( touchFile.exists() );
132 
133         List lines = verifier.loadFile( touchFile, false );
134 
135         // NOTE: system property for maven.version may not exist if you use -Dtest
136         // surefire parameter to run this single test. Therefore, the plugin writes
137         // the maven version into the check file.
138         String mavenVersion = getMavenUAVersion( (String) lines.get( 0 ) );
139         String javaVersion = (String) lines.get( 1 );
140         String os = (String) lines.get( 2 ) + " " + (String) lines.get( 3 );
141         String artifactVersion = (String) lines.get( 4 );
142 
143         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
144         {
145             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
146                 + javaVersion + "; " + os + ")" + " maven-artifact/" + artifactVersion, userAgent );
147         }
148         else
149         {
150             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
151                 + javaVersion + "; " + os + ")", userAgent );
152         }
153     }
154 
155     public void testmng3652_UnConfiguredDAV()
156         throws Exception
157     {
158         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
159         File pluginDir = new File( testDir, "test-plugin" );
160         File projectDir = new File( testDir, "test-project" );
161 
162         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
163         verifier.executeGoal( "install" );
164         verifier.verifyErrorFreeLog();
165         verifier.resetStreams();
166 
167         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
168 
169         // test webdav
170         verifier.getCliOptions().add( "-DtestPort=" + port );
171         verifier.getCliOptions().add( "-DtestProtocol=dav:http" );
172         verifier.getCliOptions().add( "-X" );
173 
174         verifier.setLogFileName( "log-unConfiguredDAV.txt" );
175         verifier.executeGoal( "validate" );
176         verifier.verifyErrorFreeLog();
177         verifier.resetStreams();
178 
179         File touchFile = new File( projectDir, "target/touch.txt" );
180         assertTrue( touchFile.exists() );
181 
182         List lines = verifier.loadFile( touchFile, false );
183 
184         // NOTE: system property for maven.version may not exist if you use -Dtest
185         // surefire parameter to run this single test. Therefore, the plugin writes
186         // the maven version into the check file.
187         String mavenVersion = getMavenUAVersion( (String) lines.get( 0 ) );
188         String javaVersion = (String) lines.get( 1 );
189         String os = (String) lines.get( 2 ) + " " + (String) lines.get( 3 );
190         String artifactVersion = (String) lines.get( 4 );
191 
192         String userAgent = this.userAgent;
193         assertNotNull( userAgent );
194 
195         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
196         {
197             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
198                 + javaVersion + "; " + os + ")" + " maven-artifact/" + artifactVersion, userAgent );
199         }
200         else
201         {
202             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
203                 + javaVersion + "; " + os + ")", userAgent );
204         }
205     }
206 
207     public void testmng3652_ConfigurationInSettingsWithoutUserAgent()
208         throws Exception
209     {
210         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
211         File pluginDir = new File( testDir, "test-plugin" );
212         File projectDir = new File( testDir, "test-project" );
213 
214         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
215         verifier.executeGoal( "install" );
216         verifier.verifyErrorFreeLog();
217         verifier.resetStreams();
218 
219         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
220 
221         // test settings with no config
222 
223         verifier.getCliOptions().add( "-DtestPort=" + port );
224         verifier.getCliOptions().add( "--settings" );
225         verifier.getCliOptions().add( "settings-no-config.xml" );
226         verifier.getCliOptions().add( "-X" );
227 
228         verifier.setLogFileName( "log-configWithoutUserAgent.txt" );
229         verifier.executeGoal( "validate" );
230         verifier.verifyErrorFreeLog();
231         verifier.resetStreams();
232 
233         File touchFile = new File( projectDir, "target/touch.txt" );
234         assertTrue( touchFile.exists() );
235 
236         List lines = verifier.loadFile( touchFile, false );
237 
238         // NOTE: system property for maven.version may not exist if you use -Dtest
239         // surefire parameter to run this single test. Therefore, the plugin writes
240         // the maven version into the check file.
241         String mavenVersion = getMavenUAVersion( (String) lines.get( 0 ) );
242         String javaVersion = (String) lines.get( 1 );
243         String os = (String) lines.get( 2 ) + " " + (String) lines.get( 3 );
244         String artifactVersion = (String) lines.get( 4 );
245 
246         String userAgent = this.userAgent;
247         assertNotNull( userAgent );
248 
249         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
250         {
251             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
252                 + javaVersion + "; " + os + ")" + " maven-artifact/" + artifactVersion, userAgent );
253         }
254         else
255         {
256             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
257                 + javaVersion + "; " + os + ")", userAgent );
258         }
259     }
260 
261     public void testmng3652_UserAgentConfiguredInSettings()
262         throws Exception
263     {
264         // customizing version not supported in Maven 3
265         //requiresMavenVersion( "(,3.0-beta-3)" );
266         requiresMavenVersion("[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,)");
267 
268         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
269         File pluginDir = new File( testDir, "test-plugin" );
270         File projectDir = new File( testDir, "test-project" );
271 
272         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
273         verifier.executeGoal( "install" );
274         verifier.verifyErrorFreeLog();
275         verifier.resetStreams();
276 
277         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
278 
279         // test settings with config
280 
281         verifier.getCliOptions().add( "-DtestPort=" + port );
282         verifier.getCliOptions().add( "--settings" );
283         verifier.getCliOptions().add( "settings.xml" );
284         verifier.getCliOptions().add( "-X" );
285 
286         verifier.setLogFileName( "log-configWithUserAgent.txt" );
287         verifier.executeGoal( "validate" );
288         verifier.verifyErrorFreeLog();
289         verifier.resetStreams();
290 
291         String userAgent = this.userAgent;
292         assertNotNull( userAgent );
293 
294         assertEquals( "Maven Fu", userAgent );
295         assertEquals( "My wonderful header", customHeader );
296     }
297 
298     public void testmng3652_AdditionnalHttpHeaderConfiguredInSettings()
299         throws Exception
300     {
301         // customizing version not supported in Maven 3
302         requiresMavenVersion("[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,)");
303 
304         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
305         File pluginDir = new File( testDir, "test-plugin" );
306         File projectDir = new File( testDir, "test-project" );
307 
308         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
309         verifier.executeGoal( "install" );
310         verifier.verifyErrorFreeLog();
311         verifier.resetStreams();
312 
313         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
314 
315         // test settings with config
316 
317         verifier.getCliOptions().add( "-DtestPort=" + port );
318         verifier.getCliOptions().add( "--settings" );
319         verifier.getCliOptions().add( "settings.xml" );
320         verifier.getCliOptions().add( "-X" );
321 
322         verifier.setLogFileName( "log-configWithUserAgent.txt" );
323         verifier.executeGoal( "validate" );
324         verifier.verifyErrorFreeLog();
325         verifier.resetStreams();
326 
327         String userAgent = this.userAgent;
328         assertNotNull( userAgent );
329 
330         assertEquals( "Maven Fu", userAgent );
331         assertEquals( "My wonderful header", customHeader );
332     }
333 
334     private String getMavenUAVersion( String mavenVersion )
335     {
336         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
337         {
338             return mavenVersion.substring( 0, 3 );
339         }
340         else
341         {
342             // Maven 3 returns the whole version
343             return mavenVersion;
344         }
345     }
346 }