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