1 package org.apache.maven.wagon.providers.http;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.mortbay.jetty.servlet.ServletHolder;
27
28 import java.io.File;
29 import java.util.Random;
30
31
32
33
34 public class HttpWagonTimeoutTest
35 extends HttpWagonHttpServerTestCase
36 {
37 protected void setUp()
38 throws Exception
39 {
40 super.setUp();
41 ServletHolder servlets = new ServletHolder( new WaitForeverServlet() );
42 context.addServlet( servlets, "/*" );
43 startServer();
44 }
45
46 public void testGetTimeout()
47 throws Exception
48 {
49 Exception thrown = null;
50
51 try
52 {
53 Wagon wagon = getWagon();
54 wagon.setReadTimeout( 1000 );
55
56 Repository testRepository = new Repository();
57 testRepository.setUrl( "http://localhost:" + httpServerPort );
58
59 wagon.connect( testRepository );
60
61 File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
62 destFile.deleteOnExit();
63
64 wagon.get( "/timeoutfile", destFile );
65
66 wagon.disconnect();
67 }
68 catch ( Exception e )
69 {
70 thrown = e;
71 }
72 finally
73 {
74 stopServer();
75 }
76
77 assertNotNull( thrown );
78 assertEquals( TransferFailedException.class, thrown.getClass() );
79 }
80
81 public void testResourceExits()
82 throws Exception
83 {
84 Exception thrown = null;
85
86 try
87 {
88 Wagon wagon = getWagon();
89 wagon.setReadTimeout( 1000 );
90
91 Repository testRepository = new Repository();
92 testRepository.setUrl( "http://localhost:" + httpServerPort );
93
94 wagon.connect( testRepository );
95
96 wagon.resourceExists( "/timeoutfile" );
97
98 wagon.disconnect();
99 }
100 catch ( Exception e )
101 {
102 thrown = e;
103 }
104 finally
105 {
106 stopServer();
107 }
108
109 assertNotNull( thrown );
110 assertEquals( TransferFailedException.class, thrown.getClass() );
111 }
112
113 public void testGetFileList()
114 throws Exception
115 {
116 Exception thrown = null;
117
118 try
119 {
120 Wagon wagon = getWagon();
121 wagon.setReadTimeout( 1000 );
122
123 Repository testRepository = new Repository();
124 testRepository.setUrl( "http://localhost:" + httpServerPort );
125
126 wagon.connect( testRepository );
127
128 wagon.getFileList( "/timeoutfile" );
129
130 wagon.disconnect();
131 }
132 catch ( Exception e )
133 {
134 thrown = e;
135 }
136 finally
137 {
138 stopServer();
139 }
140
141 assertNotNull( thrown );
142 assertEquals( TransferFailedException.class, thrown.getClass() );
143 }
144
145 public void testPutTimeout()
146 throws Exception
147 {
148 Exception thrown = null;
149
150 try
151 {
152 Wagon wagon = getWagon();
153 wagon.setReadTimeout( 1000 );
154
155 Repository testRepository = new Repository();
156 testRepository.setUrl( "http://localhost:" + httpServerPort );
157
158 wagon.connect( testRepository );
159
160 File destFile = File.createTempFile( "Hello", null );
161 destFile.deleteOnExit();
162
163 wagon.put( destFile, "/timeoutfile" );
164
165 wagon.disconnect();
166 }
167 catch ( Exception e )
168 {
169 thrown = e;
170 }
171 finally
172 {
173 stopServer();
174 }
175
176 assertNotNull( thrown );
177 assertEquals( TransferFailedException.class, thrown.getClass() );
178 }
179
180 public void testConnectionTimeout()
181 throws Exception
182 {
183 Exception thrown = null;
184
185 try
186 {
187 HttpWagon wagon = (HttpWagon) getWagon();
188 wagon.setHttpConfiguration(
189 new HttpConfiguration().setAll( new HttpMethodConfiguration().setConnectionTimeout( 500 ) ) );
190
191 Repository testRepository = new Repository();
192 Random random = new Random( );
193 testRepository.setUrl( "http://localhost:" + random.nextInt( 2048 ));
194
195 wagon.connect( testRepository );
196
197 long start = System.currentTimeMillis();
198 wagon.getFileList( "/foobar" );
199 long end = System.currentTimeMillis();
200
201
202 assertTrue( (end - start) >= 500 && (end - start) < 1000 );
203
204 }
205 catch ( Exception e )
206 {
207 thrown = e;
208 }
209 finally
210 {
211 stopServer();
212 }
213
214 assertNotNull( thrown );
215 assertEquals( TransferFailedException.class, thrown.getClass() );
216 }
217
218 }