View Javadoc
1   package org.apache.maven.wagon.providers.http;
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.wagon.FileTestUtils;
23  import org.apache.maven.wagon.TransferFailedException;
24  import org.apache.maven.wagon.Wagon;
25  import org.apache.maven.wagon.repository.Repository;
26  import org.apache.maven.wagon.shared.http.HttpConfiguration;
27  import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
28  import org.eclipse.jetty.servlet.ServletHolder;
29  
30  import java.io.File;
31  import java.util.Random;
32  
33  /**
34   * User: jdumay Date: 24/01/2008 Time: 17:17:34
35   */
36  public class HttpWagonTimeoutTest
37      extends HttpWagonHttpServerTestCase
38  {
39      protected void setUp()
40          throws Exception
41      {
42          super.setUp();
43          ServletHolder servlets = new ServletHolder( new WaitForeverServlet() );
44          context.addServlet( servlets, "/*" );
45          startServer();
46      }
47  
48      public void testGetTimeout()
49          throws Exception
50      {
51          Exception thrown = null;
52  
53          try
54          {
55              Wagon wagon = getWagon();
56              wagon.setReadTimeout( 1000 );
57  
58              Repository testRepository = new Repository();
59              testRepository.setUrl( "http://localhost:" + getPort() );
60  
61              wagon.connect( testRepository );
62  
63              File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
64              destFile.deleteOnExit();
65  
66              wagon.get( "/timeoutfile", destFile );
67  
68              wagon.disconnect();
69          }
70          catch ( Exception e )
71          {
72              thrown = e;
73          }
74          finally
75          {
76              stopServer();
77          }
78  
79          assertNotNull( thrown );
80          assertEquals( TransferFailedException.class, thrown.getClass() );
81      }
82  
83      public void testResourceExits()
84          throws Exception
85      {
86          Exception thrown = null;
87  
88          try
89          {
90              Wagon wagon = getWagon();
91              wagon.setReadTimeout( 1000 );
92  
93              Repository testRepository = new Repository();
94              testRepository.setUrl( "http://localhost:" + getPort() );
95  
96              wagon.connect( testRepository );
97  
98              wagon.resourceExists( "/timeoutfile" );
99  
100             wagon.disconnect();
101         }
102         catch ( Exception e )
103         {
104             thrown = e;
105         }
106         finally
107         {
108             stopServer();
109         }
110 
111         assertNotNull( thrown );
112         assertEquals( TransferFailedException.class, thrown.getClass() );
113     }
114 
115     public void testPutTimeout()
116         throws Exception
117     {
118         Exception thrown = null;
119 
120         try
121         {
122             Wagon wagon = getWagon();
123             wagon.setReadTimeout( 1000 );
124 
125             Repository testRepository = new Repository();
126             testRepository.setUrl( "http://localhost:" + getPort() );
127 
128             wagon.connect( testRepository );
129 
130             File destFile = File.createTempFile( "Hello", null );
131             destFile.deleteOnExit();
132 
133             wagon.put( destFile, "/timeoutfile" );
134 
135             wagon.disconnect();
136         }
137         catch ( Exception e )
138         {
139             thrown = e;
140         }
141         finally
142         {
143             stopServer();
144         }
145 
146         assertNotNull( thrown );
147         assertEquals( TransferFailedException.class, thrown.getClass() );
148     }
149 
150     public void testConnectionTimeout()
151         throws Exception
152     {
153         Exception thrown = null;
154 
155         try
156         {
157             HttpWagon wagon = (HttpWagon) getWagon();
158             wagon.setHttpConfiguration(
159                 new HttpConfiguration().setAll( new HttpMethodConfiguration().setConnectionTimeout( 500 ) ) );
160 
161             Repository testRepository = new Repository();
162             Random random = new Random( );
163             testRepository.setUrl( "http://localhost:" + random.nextInt( 2048 ));
164 
165             wagon.connect( testRepository );
166 
167             long start = System.currentTimeMillis();
168             wagon.resourceExists( "/foobar" );
169             long end = System.currentTimeMillis();
170 
171             wagon.disconnect();
172 
173             // validate we have a default time out 60000
174             assertTrue( (end - start) >= 500 && (end - start) < 1000 );
175 
176         }
177         catch ( Exception e )
178         {
179             thrown = e;
180         }
181         finally
182         {
183             stopServer();
184         }
185 
186         assertNotNull( thrown );
187         assertEquals( TransferFailedException.class, thrown.getClass() );
188     }
189 
190 }