1 | |
package org.apache.maven.wagon.tck.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.ConnectionException; |
23 | |
import org.apache.maven.wagon.ResourceDoesNotExistException; |
24 | |
import org.apache.maven.wagon.StreamWagon; |
25 | |
import org.apache.maven.wagon.TransferFailedException; |
26 | |
import org.apache.maven.wagon.authentication.AuthenticationException; |
27 | |
import org.apache.maven.wagon.authentication.AuthenticationInfo; |
28 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
29 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
30 | |
import org.apache.maven.wagon.tck.http.fixture.ErrorCodeServlet; |
31 | |
import org.apache.maven.wagon.tck.http.fixture.LatencyServlet; |
32 | |
import org.apache.maven.wagon.tck.http.fixture.ProxyConnectionVerifierFilter; |
33 | |
import org.apache.maven.wagon.tck.http.fixture.RedirectionServlet; |
34 | |
import org.apache.maven.wagon.tck.http.fixture.ServerFixture; |
35 | |
import org.apache.maven.wagon.tck.http.fixture.ServletExceptionServlet; |
36 | |
import org.apache.maven.wagon.tck.http.util.ValueHolder; |
37 | |
import org.codehaus.plexus.component.configurator.ComponentConfigurationException; |
38 | |
import org.junit.Ignore; |
39 | |
import org.junit.Test; |
40 | |
|
41 | |
import javax.servlet.Servlet; |
42 | |
import javax.servlet.http.HttpServletResponse; |
43 | |
import java.io.File; |
44 | |
import java.io.IOException; |
45 | |
|
46 | |
import static junit.framework.Assert.assertTrue; |
47 | |
import static junit.framework.Assert.fail; |
48 | |
import static org.apache.maven.wagon.tck.http.Assertions.assertFileContentsFromResource; |
49 | |
|
50 | 0 | public class GetWagonTests |
51 | |
extends HttpWagonTests |
52 | |
{ |
53 | |
|
54 | |
@Test |
55 | |
public void basic() |
56 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
57 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
58 | |
{ |
59 | 0 | testSuccessfulGet( "base.txt" ); |
60 | 0 | } |
61 | |
|
62 | |
@Test |
63 | |
@Ignore( "FIX ME!" ) |
64 | |
public void proxied() |
65 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
66 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
67 | |
{ |
68 | 0 | getServerFixture().addFilter( "*", new ProxyConnectionVerifierFilter() ); |
69 | |
|
70 | 0 | ProxyInfo info = newProxyInfo(); |
71 | 0 | if ( !initTest( null, info ) ) |
72 | |
{ |
73 | 0 | return; |
74 | |
} |
75 | |
|
76 | 0 | File target = newTempFile(); |
77 | 0 | getWagon().get( "base.txt", target ); |
78 | |
|
79 | 0 | assertFileContentsFromResource( ServerFixture.SERVER_ROOT_RESOURCE_PATH, "base.txt", target, |
80 | |
"Downloaded file doesn't match original." ); |
81 | 0 | } |
82 | |
|
83 | |
@Test |
84 | |
public void highLatencyHighTimeout() |
85 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
86 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
87 | |
{ |
88 | 0 | getServerFixture().addServlet( "/slow/*", new LatencyServlet( 2000 ) ); |
89 | 0 | testSuccessfulGet( "slow/large.txt", "large.txt" ); |
90 | 0 | } |
91 | |
|
92 | |
@Test |
93 | |
public void highLatencyLowTimeout() |
94 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
95 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
96 | |
{ |
97 | 0 | Servlet servlet = new LatencyServlet( 2000 ); |
98 | 0 | getServerFixture().addServlet( "/slow/*", servlet ); |
99 | 0 | testSuccessfulGet( "slow/large.txt", "large.txt" ); |
100 | 0 | } |
101 | |
|
102 | |
@Test |
103 | |
public void inifiniteLatencyTimeout() |
104 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
105 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
106 | |
{ |
107 | 0 | if ( !isSupported() ) |
108 | |
{ |
109 | 0 | return; |
110 | |
} |
111 | |
|
112 | 0 | final ValueHolder<Boolean> holder = new ValueHolder<Boolean>( false ); |
113 | |
|
114 | 0 | Runnable r = new Runnable() |
115 | 0 | { |
116 | |
public void run() |
117 | |
{ |
118 | 0 | Servlet servlet = new LatencyServlet( -1 ); |
119 | 0 | addNotificationTarget( servlet ); |
120 | |
|
121 | 0 | getServerFixture().addServlet( "/infinite/*", servlet ); |
122 | |
try |
123 | |
{ |
124 | 0 | if ( !initTest( null, null ) ) |
125 | |
{ |
126 | 0 | return; |
127 | |
} |
128 | |
|
129 | 0 | if ( getWagon() instanceof StreamWagon ) |
130 | |
{ |
131 | 0 | logger.info( "Connection timeout is: " + ( (StreamWagon) getWagon() ).getTimeout() ); |
132 | |
} |
133 | |
|
134 | 0 | File target = newTempFile(); |
135 | 0 | getWagon().get( "infinite/", target ); |
136 | |
|
137 | 0 | fail( "Should have failed to transfer due to transaction timeout." ); |
138 | |
} |
139 | 0 | catch ( ConnectionException e ) |
140 | |
{ |
141 | 0 | throw new IllegalStateException( e ); |
142 | |
} |
143 | 0 | catch ( AuthenticationException e ) |
144 | |
{ |
145 | 0 | throw new IllegalStateException( e ); |
146 | |
} |
147 | 0 | catch ( TransferFailedException e ) |
148 | |
{ |
149 | |
|
150 | 0 | holder.setValue( true ); |
151 | |
} |
152 | 0 | catch ( ResourceDoesNotExistException e ) |
153 | |
{ |
154 | 0 | throw new IllegalStateException( e ); |
155 | |
} |
156 | 0 | catch ( AuthorizationException e ) |
157 | |
{ |
158 | 0 | throw new IllegalStateException( e ); |
159 | |
} |
160 | 0 | catch ( ComponentConfigurationException e ) |
161 | |
{ |
162 | 0 | throw new IllegalStateException( e ); |
163 | |
} |
164 | 0 | catch ( IOException e ) |
165 | |
{ |
166 | 0 | throw new IllegalStateException( e ); |
167 | 0 | } |
168 | 0 | } |
169 | |
}; |
170 | |
|
171 | 0 | Thread t = new Thread( r ); |
172 | 0 | t.start(); |
173 | |
|
174 | |
try |
175 | |
{ |
176 | 0 | logger.info( "Waiting 60 seconds for wagon timeout." ); |
177 | 0 | t.join( 30000 ); |
178 | |
} |
179 | 0 | catch ( InterruptedException e ) |
180 | |
{ |
181 | 0 | e.printStackTrace(); |
182 | 0 | } |
183 | |
|
184 | 0 | logger.info( "Interrupting thread." ); |
185 | 0 | t.interrupt(); |
186 | |
|
187 | 0 | assertTrue( "TransferFailedException should have been thrown.", holder.getValue() ); |
188 | 0 | } |
189 | |
|
190 | |
@Test |
191 | |
public void nonExistentHost() |
192 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
193 | |
ResourceDoesNotExistException, AuthorizationException |
194 | |
{ |
195 | |
|
196 | |
|
197 | |
|
198 | 0 | if ( !initTest( "http://localhost:65520", null, null ) ) |
199 | |
{ |
200 | 0 | return; |
201 | |
} |
202 | |
|
203 | 0 | File target = newTempFile(); |
204 | |
try |
205 | |
{ |
206 | 0 | getWagon().get( "base.txt", target ); |
207 | 0 | fail( "Expected error related to host lookup failure." ); |
208 | |
} |
209 | 0 | catch ( TransferFailedException e ) |
210 | |
{ |
211 | |
|
212 | 0 | } |
213 | 0 | } |
214 | |
|
215 | |
@Test |
216 | |
public void oneLevelPermanentMove() |
217 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
218 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
219 | |
{ |
220 | 0 | getServerFixture().addServlet( "/moved.txt", |
221 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_PERMANENTLY, "/base.txt" ) ); |
222 | |
|
223 | 0 | testSuccessfulGet( "moved.txt" ); |
224 | 0 | } |
225 | |
|
226 | |
@Test |
227 | |
public void oneLevelTemporaryMove() |
228 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
229 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
230 | |
{ |
231 | 0 | getServerFixture().addServlet( "/moved.txt", |
232 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_TEMPORARILY, "/base.txt" ) ); |
233 | |
|
234 | 0 | testSuccessfulGet( "moved.txt" ); |
235 | 0 | } |
236 | |
|
237 | |
@Test |
238 | |
public void sixLevelPermanentMove() |
239 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
240 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
241 | |
{ |
242 | 0 | String myPath = "moved.txt"; |
243 | 0 | String targetPath = "/base.txt"; |
244 | |
|
245 | 0 | getServerFixture().addServlet( "/" + myPath + "/*", |
246 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_PERMANENTLY, myPath, |
247 | |
targetPath, 6 ) ); |
248 | |
|
249 | 0 | testSuccessfulGet( myPath ); |
250 | 0 | } |
251 | |
|
252 | |
@Test |
253 | |
public void sixLevelTemporaryMove() |
254 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
255 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
256 | |
{ |
257 | 0 | String myPath = "moved.txt"; |
258 | 0 | String targetPath = "/base.txt"; |
259 | |
|
260 | 0 | getServerFixture().addServlet( "/" + myPath + "/*", |
261 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_TEMPORARILY, myPath, |
262 | |
targetPath, 6 ) ); |
263 | |
|
264 | 0 | testSuccessfulGet( myPath ); |
265 | 0 | } |
266 | |
|
267 | |
@Test |
268 | |
public void infinitePermanentMove() |
269 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
270 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
271 | |
{ |
272 | 0 | String myPath = "moved.txt"; |
273 | 0 | String targetPath = "/base.txt"; |
274 | |
|
275 | 0 | getServerFixture().addServlet( |
276 | |
"/" + myPath, |
277 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_PERMANENTLY, myPath, |
278 | |
targetPath, -1 ) ); |
279 | |
|
280 | |
try |
281 | |
{ |
282 | 0 | testSuccessfulGet( myPath ); |
283 | 0 | fail( "Expected failure as a result of too many redirects." ); |
284 | |
} |
285 | 0 | catch ( TransferFailedException e ) |
286 | |
{ |
287 | |
|
288 | 0 | } |
289 | 0 | } |
290 | |
|
291 | |
@Test |
292 | |
public void infiniteTemporaryMove() |
293 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
294 | |
ResourceDoesNotExistException, AuthorizationException |
295 | |
{ |
296 | 0 | String myPath = "moved.txt"; |
297 | 0 | String targetPath = "/base.txt"; |
298 | |
|
299 | 0 | getServerFixture().addServlet( |
300 | |
"/" + myPath, |
301 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_TEMPORARILY, myPath, |
302 | |
targetPath, -1 ) ); |
303 | |
|
304 | |
try |
305 | |
{ |
306 | 0 | testSuccessfulGet( myPath ); |
307 | 0 | fail( "Expected failure as a result of too many redirects." ); |
308 | |
} |
309 | 0 | catch ( TransferFailedException e ) |
310 | |
{ |
311 | |
|
312 | 0 | } |
313 | 0 | } |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
@Test |
320 | |
public void permanentMove_TooManyRedirects_limit20() |
321 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
322 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
323 | |
{ |
324 | 0 | String myPath = "moved.txt"; |
325 | 0 | String targetPath = "/base.txt"; |
326 | |
|
327 | 0 | getServerFixture().addServlet( |
328 | |
"/" + myPath, |
329 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_PERMANENTLY, myPath, |
330 | |
targetPath, -1 ) ); |
331 | |
|
332 | |
try |
333 | |
{ |
334 | 0 | testSuccessfulGet( myPath ); |
335 | 0 | fail( "Expected failure as a result of too many redirects." ); |
336 | |
} |
337 | 0 | catch ( TransferFailedException e ) |
338 | |
{ |
339 | |
|
340 | 0 | } |
341 | 0 | } |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
@Test |
348 | |
public void temporaryMove_TooManyRedirects_limit20() |
349 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
350 | |
ResourceDoesNotExistException, AuthorizationException |
351 | |
{ |
352 | 0 | String myPath = "moved.txt"; |
353 | 0 | String targetPath = "/base.txt"; |
354 | |
|
355 | 0 | getServerFixture().addServlet( |
356 | |
"/" + myPath, |
357 | |
new RedirectionServlet( HttpServletResponse.SC_MOVED_TEMPORARILY, myPath, |
358 | |
targetPath, -1 ) ); |
359 | |
|
360 | |
try |
361 | |
{ |
362 | 0 | testSuccessfulGet( myPath ); |
363 | 0 | fail( "Expected failure as a result of too many redirects." ); |
364 | |
} |
365 | 0 | catch ( TransferFailedException e ) |
366 | |
{ |
367 | |
|
368 | 0 | } |
369 | 0 | } |
370 | |
|
371 | |
@Test |
372 | |
public void missing() |
373 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
374 | |
TransferFailedException, AuthorizationException |
375 | |
{ |
376 | 0 | if ( !initTest( null, null ) ) |
377 | |
{ |
378 | 0 | return; |
379 | |
} |
380 | |
|
381 | 0 | File target = newTempFile(); |
382 | |
try |
383 | |
{ |
384 | 0 | getWagon().get( "404.txt", target ); |
385 | 0 | fail( "should have received a 404, meaning the resource doesn't exist." ); |
386 | |
} |
387 | 0 | catch ( ResourceDoesNotExistException e ) |
388 | |
{ |
389 | |
|
390 | 0 | } |
391 | 0 | } |
392 | |
|
393 | |
@Test |
394 | |
public void error() |
395 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
396 | |
AuthorizationException, ResourceDoesNotExistException |
397 | |
{ |
398 | 0 | testErrorHandling( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); |
399 | 0 | } |
400 | |
|
401 | |
@Test |
402 | |
public void proxyTimeout() |
403 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
404 | |
AuthorizationException, ResourceDoesNotExistException |
405 | |
{ |
406 | 0 | testErrorHandling( HttpServletResponse.SC_GATEWAY_TIMEOUT ); |
407 | 0 | } |
408 | |
|
409 | |
@Test |
410 | |
public void forbidden() |
411 | |
throws ConnectionException, ComponentConfigurationException, IOException, ResourceDoesNotExistException, |
412 | |
TransferFailedException |
413 | |
{ |
414 | 0 | AuthenticationInfo info = new AuthenticationInfo(); |
415 | 0 | info.setUserName( "user" ); |
416 | 0 | info.setPassword( "password" ); |
417 | |
|
418 | 0 | getServerFixture().addUser( info.getUserName(), "password" ); |
419 | |
|
420 | 0 | getServerFixture().addServlet( "/403.txt", |
421 | |
new ErrorCodeServlet( HttpServletResponse.SC_FORBIDDEN, "Expected 403" ) ); |
422 | |
|
423 | 0 | testAuthFailure( "403.txt", info ); |
424 | 0 | } |
425 | |
|
426 | |
@Test |
427 | |
public void successfulAuthentication() |
428 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
429 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
430 | |
{ |
431 | 0 | AuthenticationInfo info = new AuthenticationInfo(); |
432 | 0 | info.setUserName( "user" ); |
433 | 0 | info.setPassword( "password" ); |
434 | |
|
435 | 0 | getServerFixture().addUser( info.getUserName(), info.getPassword() ); |
436 | |
|
437 | 0 | if ( !initTest( info, null ) ) |
438 | |
{ |
439 | 0 | return; |
440 | |
} |
441 | |
|
442 | 0 | File target = newTempFile(); |
443 | 0 | getWagon().get( "protected/base.txt", target ); |
444 | |
|
445 | 0 | assertFileContentsFromResource( ServerFixture.SERVER_ROOT_RESOURCE_PATH, "base.txt", target, |
446 | |
"Downloaded file doesn't match original." ); |
447 | 0 | } |
448 | |
|
449 | |
@Test |
450 | |
public void unsuccessfulAuthentication() |
451 | |
throws ConnectionException, ComponentConfigurationException, IOException, TransferFailedException, |
452 | |
ResourceDoesNotExistException |
453 | |
{ |
454 | 0 | AuthenticationInfo info = new AuthenticationInfo(); |
455 | 0 | info.setUserName( "user" ); |
456 | 0 | info.setPassword( "password" ); |
457 | |
|
458 | 0 | getServerFixture().addUser( info.getUserName(), "anotherPassword" ); |
459 | |
|
460 | 0 | testAuthFailure( "protected/base.txt", info ); |
461 | 0 | } |
462 | |
|
463 | |
protected void testAuthFailure( final String path, final AuthenticationInfo info ) |
464 | |
throws ConnectionException, ComponentConfigurationException, IOException, TransferFailedException, |
465 | |
ResourceDoesNotExistException |
466 | |
{ |
467 | 0 | boolean authFailure = false; |
468 | |
try |
469 | |
{ |
470 | 0 | if ( !initTest( info, null ) ) |
471 | |
{ |
472 | 0 | return; |
473 | |
} |
474 | |
} |
475 | 0 | catch ( AuthenticationException e ) |
476 | |
{ |
477 | |
|
478 | 0 | authFailure = true; |
479 | 0 | } |
480 | |
|
481 | 0 | File target = newTempFile(); |
482 | |
try |
483 | |
{ |
484 | 0 | getWagon().get( path, target ); |
485 | |
} |
486 | 0 | catch ( AuthorizationException e ) |
487 | |
{ |
488 | |
|
489 | 0 | authFailure = true; |
490 | 0 | } |
491 | |
|
492 | 0 | assertTrue( "Authentication/Authorization should have failed.", authFailure ); |
493 | 0 | } |
494 | |
|
495 | |
protected void testSuccessfulGet( final String path ) |
496 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
497 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
498 | |
{ |
499 | 0 | testSuccessfulGet( path, "base.txt" ); |
500 | 0 | } |
501 | |
|
502 | |
protected void testSuccessfulGet( final String path, final String checkPath ) |
503 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
504 | |
TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
505 | |
{ |
506 | 0 | if ( !initTest( null, null ) ) |
507 | |
{ |
508 | 0 | return; |
509 | |
} |
510 | |
|
511 | 0 | if ( getWagon() instanceof StreamWagon ) |
512 | |
{ |
513 | 0 | logger.info( "Connection timeout is: " + ( (StreamWagon) getWagon() ).getTimeout() ); |
514 | |
} |
515 | |
|
516 | 0 | File target = newTempFile(); |
517 | 0 | getWagon().get( path, target ); |
518 | |
|
519 | 0 | assertFileContentsFromResource( ServerFixture.SERVER_ROOT_RESOURCE_PATH, checkPath, target, |
520 | |
"Downloaded file doesn't match original." ); |
521 | 0 | } |
522 | |
|
523 | |
protected void testErrorHandling( final int code ) |
524 | |
throws ConnectionException, AuthenticationException, ComponentConfigurationException, IOException, |
525 | |
AuthorizationException, ResourceDoesNotExistException |
526 | |
{ |
527 | 0 | if ( code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR ) |
528 | |
{ |
529 | 0 | getServerFixture().addServlet( "/" + code + ".txt", new ServletExceptionServlet( "Expected " + code ) ); |
530 | |
} |
531 | |
else |
532 | |
{ |
533 | 0 | getServerFixture().addServlet( "/" + code + ".txt", new ErrorCodeServlet( code, "Expected " + code ) ); |
534 | |
} |
535 | |
|
536 | 0 | if ( !initTest( null, null ) ) |
537 | |
{ |
538 | 0 | return; |
539 | |
} |
540 | |
|
541 | 0 | File target = newTempFile(); |
542 | |
try |
543 | |
{ |
544 | 0 | getWagon().get( code + ".txt", target ); |
545 | 0 | fail( "should have received a " + code + " error code, meaning the resource doesn't exist." ); |
546 | |
} |
547 | 0 | catch ( TransferFailedException e ) |
548 | |
{ |
549 | |
|
550 | 0 | } |
551 | 0 | } |
552 | |
} |