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 org.apache.maven.it.util.ResourceExtractor;
23  import org.eclipse.jetty.server.Handler;
24  import org.eclipse.jetty.server.NetworkConnector;
25  import org.eclipse.jetty.server.Request;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.server.handler.AbstractHandler;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.Properties;
35  
36  /**
37   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-5175">MNG-5175</a>.
38   * test correct integration of wagon http: read time out configuration from settings.xml
39   *
40   *
41   */
42  public class MavenITmng5175WagonHttpTest
43      extends AbstractMavenIntegrationTestCase
44  {
45      private Server server;
46  
47      private int port;
48  
49      public MavenITmng5175WagonHttpTest()
50      {
51          super( "[3.0.4,)" ); // 3.0.4+
52      }
53  
54      @Override
55      protected void setUp()
56          throws Exception
57      {
58          Handler handler = new AbstractHandler()
59          {
60              @Override
61              public void handle( String target, Request baseRequest, HttpServletRequest request,
62                                  HttpServletResponse response )
63                  throws IOException, ServletException
64              {
65                  try
66                  {
67                      // wait long enough for read timeout to happen in client
68                      Thread.sleep( 100 );
69                  }
70                  catch ( InterruptedException e )
71                  {
72                      throw new ServletException( e.getMessage() );
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          if ( server.isFailed() )
87          {
88              fail( "Couldn't bind the server socket to a free port!" );
89          }
90          port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
91          System.out.println( "Bound server socket to the port " + port );
92      }
93  
94      @Override
95      protected void tearDown()
96          throws Exception
97      {
98          if ( server != null )
99          {
100             server.stop();
101             server.join();
102         }
103     }
104 
105     /**
106      * Test that the read time out from settings is used.
107      * basically use a 1ms time out and wait a bit in the handler
108      *
109      * @throws Exception in case of failure
110      */
111     public void testmng5175_ReadTimeOutFromSettings()
112         throws Exception
113     {
114         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5175" );
115 
116         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
117 
118         Properties filterProps = new Properties();
119         filterProps.setProperty( "@port@", Integer.toString( port ) );
120 
121         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
122 
123         verifier.addCliOption( "-U" );
124         verifier.addCliOption( "--settings" );
125         verifier.addCliOption( "settings.xml" );
126         verifier.addCliOption( "--fail-never" );
127         verifier.addCliOption( "--errors" );
128         verifier.setMavenDebug( true );
129         verifier.executeGoal( "validate" );
130 
131         verifier.verifyTextInLog(
132                 "Could not transfer artifact org.apache.maven.its.mng5175:fake-dependency:pom:1.0-SNAPSHOT" );
133         verifier.verifyTextInLog( "Read timed out" );
134         verifier.resetStreams();
135     }
136 }