1 package org.apache.maven.wagon;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.text.SimpleDateFormat;
25
26 import junit.framework.TestCase;
27
28 import org.apache.maven.wagon.authentication.AuthenticationException;
29 import org.apache.maven.wagon.authorization.AuthorizationException;
30 import org.apache.maven.wagon.events.TransferEvent;
31 import org.apache.maven.wagon.events.TransferListener;
32 import org.apache.maven.wagon.repository.Repository;
33 import org.apache.maven.wagon.resource.Resource;
34 import org.codehaus.plexus.util.FileUtils;
35 import org.codehaus.plexus.util.StringInputStream;
36 import org.codehaus.plexus.util.StringOutputStream;
37
38 import static org.easymock.EasyMock.*;
39
40 public class StreamWagonTest
41 extends TestCase
42 {
43 private static class TestWagon
44 extends StreamWagon
45 {
46 public void closeConnection()
47 throws ConnectionException
48 {
49 }
50
51 public void fillInputData( InputData inputData )
52 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
53 {
54 }
55
56 public void fillOutputData( OutputData outputData )
57 throws TransferFailedException
58 {
59 }
60
61 protected void openConnectionInternal()
62 throws ConnectionException, AuthenticationException
63 {
64 }
65 }
66
67 private Repository repository = new Repository( "id", "url" );
68
69 public void testNullInputStream()
70 throws Exception
71 {
72 StreamingWagon wagon = new TestWagon()
73 {
74 public void fillInputData( InputData inputData )
75 {
76 inputData.setInputStream( null );
77 }
78 };
79
80 TransferListener listener = createMock( TransferListener.class );
81 listener.transferInitiated( anyObject( TransferEvent.class ) );
82 TransferEvent transferEvent =
83 new TransferEvent( wagon, new Resource( "resource" ), new TransferFailedException( "" ),
84 TransferEvent.REQUEST_GET );
85 listener.transferError( transferEvent );
86 replay( listener );
87
88 wagon.connect( repository );
89 wagon.addTransferListener( listener );
90 try
91 {
92 wagon.getToStream( "resource", new StringOutputStream() );
93 fail();
94 }
95 catch ( TransferFailedException e )
96 {
97 assertTrue( true );
98 }
99 finally
100 {
101 wagon.disconnect();
102 }
103
104 verify( listener );
105 }
106
107 public void testNullOutputStream()
108 throws Exception
109 {
110 StreamingWagon wagon = new TestWagon()
111 {
112 public void fillOutputData( OutputData inputData )
113 {
114 inputData.setOutputStream( null );
115 }
116 };
117
118 TransferListener listener = createMock( TransferListener.class );
119 listener.transferInitiated( anyObject( TransferEvent.class ) );
120 TransferEvent transferEvent =
121 new TransferEvent( wagon, new Resource( "resource" ), new TransferFailedException( "" ),
122 TransferEvent.REQUEST_PUT );
123 listener.transferError( transferEvent );
124 replay( listener );
125
126 wagon.connect( repository );
127 wagon.addTransferListener( listener );
128 try
129 {
130 wagon.putFromStream( new StringInputStream( "" ), "resource" );
131 fail();
132 }
133 catch ( TransferFailedException e )
134 {
135 assertTrue( true );
136 }
137 finally
138 {
139 wagon.disconnect();
140 }
141
142 verify( listener );
143 }
144
145 public void testTransferFailedExceptionOnInput()
146 throws Exception
147 {
148 try
149 {
150 runTestTransferError( new TransferFailedException( "" ) );
151 fail();
152 }
153 catch ( TransferFailedException e )
154 {
155 assertTrue( true );
156 }
157 }
158
159 public void testTransferFailedExceptionOnOutput()
160 throws Exception
161 {
162 StreamingWagon wagon = new TestWagon()
163 {
164 public void fillOutputData( OutputData inputData )
165 throws TransferFailedException
166 {
167 throw (TransferFailedException) new TransferFailedException( "" );
168 }
169 };
170
171 TransferListener listener = createMock( TransferListener.class );
172 listener.transferInitiated( anyObject( TransferEvent.class ) );
173 TransferEvent transferEvent =
174 new TransferEvent( wagon, new Resource( "resource" ), new TransferFailedException( "" ),
175 TransferEvent.REQUEST_PUT );
176 listener.transferError( transferEvent );
177 replay( listener );
178
179 wagon.connect( repository );
180 wagon.addTransferListener( listener );
181 try
182 {
183 wagon.putFromStream( new StringInputStream( "" ), "resource" );
184 fail();
185 }
186 catch ( TransferFailedException e )
187 {
188 assertTrue( true );
189 }
190 finally
191 {
192 wagon.disconnect();
193 verify( listener );
194 }
195 }
196
197 public void testResourceDoesNotExistException()
198 throws Exception
199 {
200 try
201 {
202 runTestTransferError( new ResourceDoesNotExistException( "" ) );
203 fail();
204 }
205 catch ( ResourceDoesNotExistException e )
206 {
207 assertTrue( true );
208 }
209 }
210
211 public void testAuthorizationException()
212 throws Exception
213 {
214 try
215 {
216 runTestTransferError( new AuthorizationException( "" ) );
217 fail();
218 }
219 catch ( AuthorizationException e )
220 {
221 assertTrue( true );
222 }
223 }
224
225 private void runTestTransferError( final WagonException exception )
226 throws ConnectionException, AuthenticationException, ResourceDoesNotExistException, AuthorizationException,
227 TransferFailedException
228 {
229 StreamingWagon wagon = new TestWagon()
230 {
231 public void fillInputData( InputData inputData )
232 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
233 {
234 if ( exception instanceof TransferFailedException )
235 {
236 throw (TransferFailedException) exception;
237 }
238 if ( exception instanceof ResourceDoesNotExistException )
239 {
240 throw (ResourceDoesNotExistException) exception;
241 }
242 if ( exception instanceof AuthorizationException )
243 {
244 throw (AuthorizationException) exception;
245 }
246 }
247 };
248
249 TransferListener listener = createMock( TransferListener.class );
250 listener.transferInitiated( anyObject( TransferEvent.class ) );
251 TransferEvent transferEvent =
252 new TransferEvent( wagon, new Resource( "resource" ), exception, TransferEvent.REQUEST_GET );
253 listener.transferError( transferEvent );
254 replay( listener );
255
256 wagon.connect( repository );
257 wagon.addTransferListener( listener );
258 try
259 {
260 wagon.getToStream( "resource", new StringOutputStream() );
261 fail();
262 }
263 finally
264 {
265 wagon.disconnect();
266 verify( listener );
267 }
268 }
269
270 public void testGetIfNewerWithNewerResource()
271 throws Exception
272 {
273 long resourceTime = System.currentTimeMillis();
274 long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
275 assertTrue( runTestGetIfNewer( resourceTime, comparisonTime ) );
276 }
277
278 public void testGetIfNewerWithOlderResource()
279 throws Exception
280 {
281 long comparisonTime = System.currentTimeMillis();
282 long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
283 assertFalse( runTestGetIfNewer( resourceTime, comparisonTime ) );
284 }
285
286 public void testGetIfNewerWithSameTimeResource()
287 throws Exception
288 {
289 long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
290 assertFalse( runTestGetIfNewer( resourceTime, resourceTime ) );
291 }
292
293 private boolean runTestGetIfNewer( final long resourceTime, long comparisonTime )
294 throws IOException, ConnectionException, AuthenticationException, TransferFailedException,
295 ResourceDoesNotExistException, AuthorizationException
296 {
297 StreamingWagon wagon = new TestWagon()
298 {
299 public void fillInputData( InputData inputData )
300 {
301 inputData.setInputStream( new StringInputStream( "" ) );
302 inputData.getResource().setLastModified( resourceTime );
303 }
304 };
305
306 File tempFile = File.createTempFile( "wagon", "tmp" );
307 tempFile.deleteOnExit();
308
309 wagon.connect( repository );
310 try
311 {
312 return wagon.getIfNewer( "resource", tempFile, comparisonTime );
313 }
314 finally
315 {
316 wagon.disconnect();
317 tempFile.delete();
318 }
319 }
320
321 public void testGetToStream()
322 throws Exception
323 {
324 final String content = "the content to return";
325 final long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
326 StreamingWagon wagon = new TestWagon()
327 {
328 public void fillInputData( InputData inputData )
329 {
330 inputData.setInputStream( new StringInputStream( content ) );
331 inputData.getResource().setLastModified( comparisonTime );
332 }
333 };
334
335 wagon.connect( repository );
336 try
337 {
338 StringOutputStream out = new StringOutputStream();
339 wagon.getToStream( "resource", out );
340 assertEquals( content, out.toString() );
341 }
342 finally
343 {
344 wagon.disconnect();
345 }
346 }
347
348 public void testGet()
349 throws Exception
350 {
351 final String content = "the content to return";
352 final long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
353 StreamingWagon wagon = new TestWagon()
354 {
355 public void fillInputData( InputData inputData )
356 {
357 inputData.setInputStream( new StringInputStream( content ) );
358 inputData.getResource().setLastModified( comparisonTime );
359 }
360 };
361
362 File tempFile = File.createTempFile( "wagon", "tmp" );
363 tempFile.deleteOnExit();
364
365 wagon.connect( repository );
366 try
367 {
368 wagon.get( "resource", tempFile );
369 assertEquals( content, FileUtils.fileRead( tempFile ) );
370 }
371 finally
372 {
373 wagon.disconnect();
374 tempFile.delete();
375 }
376 }
377
378 public void testGetIfNewerToStreamWithNewerResource()
379 throws Exception
380 {
381 long resourceTime = System.currentTimeMillis();
382 long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
383 assertTrue( runTestGetIfNewerToStream( resourceTime, comparisonTime ) );
384 }
385
386 public void testGetIfNewerToStreamWithOlderResource()
387 throws Exception
388 {
389 long comparisonTime = System.currentTimeMillis();
390 long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
391 assertFalse( runTestGetIfNewerToStream( resourceTime, comparisonTime ) );
392 }
393
394 public void testGetIfNewerToStreamWithSameTimeResource()
395 throws Exception
396 {
397 long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
398 assertFalse( runTestGetIfNewerToStream( resourceTime, resourceTime ) );
399 }
400
401 private boolean runTestGetIfNewerToStream( final long resourceTime, long comparisonTime )
402 throws IOException, ConnectionException, AuthenticationException, TransferFailedException,
403 ResourceDoesNotExistException, AuthorizationException
404 {
405 StreamingWagon wagon = new TestWagon()
406 {
407 public void fillInputData( InputData inputData )
408 {
409 inputData.setInputStream( new StringInputStream( "" ) );
410 inputData.getResource().setLastModified( resourceTime );
411 }
412 };
413
414 wagon.connect( repository );
415 try
416 {
417 return wagon.getIfNewerToStream( "resource", new StringOutputStream(), comparisonTime );
418 }
419 finally
420 {
421 wagon.disconnect();
422 }
423 }
424
425 public void testPutFromStream()
426 throws Exception
427 {
428 final String content = "the content to return";
429
430 final StringOutputStream out = new StringOutputStream();
431 StreamingWagon wagon = new TestWagon()
432 {
433 public void fillOutputData( OutputData outputData )
434 {
435 assertEquals( "resource", outputData.getResource().getName() );
436 assertEquals( -1, outputData.getResource().getContentLength() );
437 assertEquals( 0, outputData.getResource().getLastModified() );
438 outputData.setOutputStream( out );
439 }
440 };
441
442 wagon.connect( repository );
443 try
444 {
445 wagon.putFromStream( new StringInputStream( content ), "resource" );
446 assertEquals( content, out.toString() );
447 }
448 finally
449 {
450 wagon.disconnect();
451 }
452 }
453
454 public void testPutFromStreamWithResourceInformation()
455 throws Exception
456 {
457 final String content = "the content to return";
458 final long lastModified = System.currentTimeMillis();
459
460 final StringOutputStream out = new StringOutputStream();
461 StreamingWagon wagon = new TestWagon()
462 {
463 public void fillOutputData( OutputData outputData )
464 {
465 assertEquals( "resource", outputData.getResource().getName() );
466 assertEquals( content.length(), outputData.getResource().getContentLength() );
467 assertEquals( lastModified, outputData.getResource().getLastModified() );
468 outputData.setOutputStream( out );
469 }
470 };
471
472 wagon.connect( repository );
473 try
474 {
475 wagon.putFromStream( new StringInputStream( content ), "resource", content.length(), lastModified );
476 assertEquals( content, out.toString() );
477 }
478 finally
479 {
480 wagon.disconnect();
481 }
482 }
483
484 public void testPut()
485 throws Exception
486 {
487 final String content = "the content to return";
488
489 final File tempFile = File.createTempFile( "wagon", "tmp" );
490 FileUtils.fileWrite( tempFile.getAbsolutePath(), content );
491 tempFile.deleteOnExit();
492
493 final StringOutputStream out = new StringOutputStream();
494 Wagon wagon = new TestWagon()
495 {
496 public void fillOutputData( OutputData outputData )
497 {
498 assertEquals( "resource", outputData.getResource().getName() );
499 assertEquals( content.length(), outputData.getResource().getContentLength() );
500 assertEquals( tempFile.lastModified(), outputData.getResource().getLastModified() );
501 outputData.setOutputStream( out );
502 }
503 };
504
505 wagon.connect( repository );
506 try
507 {
508 wagon.put( tempFile, "resource" );
509 assertEquals( content, out.toString() );
510 }
511 finally
512 {
513 wagon.disconnect();
514 tempFile.delete();
515 }
516 }
517
518 public void testPutFileDoesntExist()
519 throws Exception
520 {
521 final File tempFile = File.createTempFile( "wagon", "tmp" );
522 tempFile.delete();
523 assertFalse( tempFile.exists() );
524
525 Wagon wagon = new TestWagon();
526
527 wagon.connect( repository );
528 try
529 {
530 wagon.put( tempFile, "resource" );
531 fail();
532 }
533 catch ( TransferFailedException e )
534 {
535 assertTrue( true );
536 }
537 finally
538 {
539 wagon.disconnect();
540 }
541 }
542
543 }