View Javadoc
1   package org.apache.maven.shared.io.download;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.Collections;
25  
26  import org.apache.maven.artifact.manager.WagonManager;
27  import org.apache.maven.shared.io.Utils;
28  import org.apache.maven.shared.io.logging.DefaultMessageHolder;
29  import org.apache.maven.shared.io.logging.MessageHolder;
30  import org.apache.maven.wagon.ConnectionException;
31  import org.apache.maven.wagon.ResourceDoesNotExistException;
32  import org.apache.maven.wagon.TransferFailedException;
33  import org.apache.maven.wagon.UnsupportedProtocolException;
34  import org.apache.maven.wagon.Wagon;
35  import org.apache.maven.wagon.authentication.AuthenticationException;
36  import org.apache.maven.wagon.authentication.AuthenticationInfo;
37  import org.apache.maven.wagon.authorization.AuthorizationException;
38  import org.apache.maven.wagon.events.TransferListener;
39  import org.apache.maven.wagon.proxy.ProxyInfo;
40  import org.apache.maven.wagon.repository.Repository;
41  import org.codehaus.plexus.PlexusTestCase;
42  
43  import static org.easymock.EasyMock.*;
44  
45  public class DefaultDownloadManagerTest
46      extends PlexusTestCase
47  {
48  
49      private WagonManager wagonManager;
50  
51      private Wagon wagon;
52  
53      public void setUp()
54          throws Exception
55      {
56          super.setUp();
57  
58          wagonManager = createMock( WagonManager.class );
59          wagon = createMock( Wagon.class );
60      }
61  
62      public void testShouldConstructWithNoParamsAndHaveNonNullMessageHolder()
63      {
64          new DefaultDownloadManager();
65      }
66  
67      public void testShouldConstructWithWagonManager()
68      {
69          replay( wagonManager );
70  
71          new DefaultDownloadManager( wagonManager );
72  
73          verify( wagonManager );
74      }
75  
76      public void testShouldLookupInstanceDefaultRoleHint()
77          throws Exception
78      {
79          lookup( DownloadManager.ROLE, DefaultDownloadManager.ROLE_HINT );
80      }
81  
82      public void testShouldFailToDownloadMalformedURL()
83      {
84          replay( wagonManager );
85  
86          DownloadManager mgr = new DefaultDownloadManager( wagonManager );
87  
88          try
89          {
90              mgr.download( "://nothing.com/index.html", new DefaultMessageHolder() );
91  
92              fail( "Should not download with invalid URL." );
93          }
94          catch ( DownloadFailedException e )
95          {
96              assertTrue( e.getMessage().indexOf( "invalid URL" ) > -1 );
97          }
98  
99          verify( wagonManager );
100     }
101 
102     public void testShouldDownloadFromTempFileWithNoTransferListeners()
103         throws IOException, DownloadFailedException
104     {
105         File tempFile = File.createTempFile( "download-source", "test" );
106         tempFile.deleteOnExit();
107 
108         setupDefaultMockConfiguration();
109 
110         replay( wagon, wagonManager );
111 
112         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
113 
114         downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
115 
116         verify( wagon, wagonManager );
117     }
118 
119     public void testShouldDownloadFromTempFileTwiceAndUseCache()
120         throws IOException, DownloadFailedException
121     {
122         File tempFile = File.createTempFile( "download-source", "test" );
123         tempFile.deleteOnExit();
124 
125         setupDefaultMockConfiguration();
126 
127         replay( wagon, wagonManager );
128 
129         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
130 
131         File first = downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
132 
133         MessageHolder mh = new DefaultMessageHolder();
134 
135         File second = downloadManager.download( tempFile.toURL().toExternalForm(), mh );
136 
137         assertSame( first, second );
138         assertEquals( 1, mh.size() );
139         assertTrue( mh.render().indexOf( "Using cached" ) > -1 );
140 
141         verify( wagon, wagonManager );
142     }
143 
144     public void testShouldDownloadFromTempFileWithOneTransferListener()
145         throws IOException, DownloadFailedException
146     {
147         File tempFile = File.createTempFile( "download-source", "test" );
148         tempFile.deleteOnExit();
149 
150         setupDefaultMockConfiguration();
151 
152         TransferListener transferListener = createMock( TransferListener.class );
153 
154         wagon.addTransferListener( transferListener );
155 
156         wagon.removeTransferListener( transferListener );
157 
158         replay( wagon, wagonManager, transferListener );
159 
160         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
161 
162         downloadManager.download( tempFile.toURL().toExternalForm(), Collections.singletonList( transferListener ),
163                                   new DefaultMessageHolder() );
164 
165         verify( wagon, wagonManager, transferListener );
166     }
167 
168     public void testShouldFailToDownloadWhenWagonProtocolNotFound()
169         throws IOException
170     {
171         File tempFile = File.createTempFile( "download-source", "test" );
172         tempFile.deleteOnExit();
173 
174         setupMocksWithWagonManagerGetException( new UnsupportedProtocolException( "not supported" ) );
175 
176         replay( wagon, wagonManager );
177 
178         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
179 
180         try
181         {
182             downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
183 
184             fail( "should have failed to retrieve wagon." );
185         }
186         catch ( DownloadFailedException e )
187         {
188             assertTrue( Utils.toString( e ).indexOf( "UnsupportedProtocolException" ) > -1 );
189         }
190 
191         verify( wagon, wagonManager );
192     }
193 
194     public void testShouldFailToDownloadWhenWagonConnectThrowsConnectionException()
195         throws IOException
196     {
197         File tempFile = File.createTempFile( "download-source", "test" );
198         tempFile.deleteOnExit();
199 
200         setupMocksWithWagonConnectionException( new ConnectionException( "connect error" ) );
201 
202         replay( wagon, wagonManager );
203 
204         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
205 
206         try
207         {
208             downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
209 
210             fail( "should have failed to connect wagon." );
211         }
212         catch ( DownloadFailedException e )
213         {
214             assertTrue( Utils.toString( e ).indexOf( "ConnectionException" ) > -1 );
215         }
216 
217         verify( wagon, wagonManager );
218     }
219 
220     public void testShouldFailToDownloadWhenWagonConnectThrowsAuthenticationException()
221         throws IOException
222     {
223         File tempFile = File.createTempFile( "download-source", "test" );
224         tempFile.deleteOnExit();
225 
226         setupMocksWithWagonConnectionException( new AuthenticationException( "bad credentials" ) );
227 
228         replay( wagon, wagonManager );
229 
230         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
231 
232         try
233         {
234             downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
235 
236             fail( "should have failed to connect wagon." );
237         }
238         catch ( DownloadFailedException e )
239         {
240             assertTrue( Utils.toString( e ).indexOf( "AuthenticationException" ) > -1 );
241         }
242 
243         verify( wagon, wagonManager );
244     }
245 
246     public void testShouldFailToDownloadWhenWagonGetThrowsTransferFailedException()
247         throws IOException
248     {
249         File tempFile = File.createTempFile( "download-source", "test" );
250         tempFile.deleteOnExit();
251 
252         setupMocksWithWagonGetException( new TransferFailedException( "bad transfer" ) );
253 
254         replay( wagon, wagonManager );
255 
256         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
257 
258         try
259         {
260             downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
261 
262             fail( "should have failed to get resource." );
263         }
264         catch ( DownloadFailedException e )
265         {
266             assertTrue( Utils.toString( e ).indexOf( "TransferFailedException" ) > -1 );
267         }
268 
269         verify( wagon, wagonManager );
270     }
271 
272     public void testShouldFailToDownloadWhenWagonGetThrowsResourceDoesNotExistException()
273         throws IOException
274     {
275         File tempFile = File.createTempFile( "download-source", "test" );
276         tempFile.deleteOnExit();
277 
278         setupMocksWithWagonGetException( new ResourceDoesNotExistException( "bad resource" ) );
279 
280         replay( wagon, wagonManager );
281 
282         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
283 
284         try
285         {
286             downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
287 
288             fail( "should have failed to get resource." );
289         }
290         catch ( DownloadFailedException e )
291         {
292             assertTrue( Utils.toString( e ).indexOf( "ResourceDoesNotExistException" ) > -1 );
293         }
294 
295         verify( wagon, wagonManager );
296     }
297 
298     public void testShouldFailToDownloadWhenWagonGetThrowsAuthorizationException()
299         throws IOException
300     {
301         File tempFile = File.createTempFile( "download-source", "test" );
302         tempFile.deleteOnExit();
303 
304         setupMocksWithWagonGetException( new AuthorizationException( "bad transfer" ) );
305 
306         replay( wagon, wagonManager );
307 
308         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
309 
310         try
311         {
312             downloadManager.download( tempFile.toURL().toExternalForm(), new DefaultMessageHolder() );
313 
314             fail( "should have failed to get resource." );
315         }
316         catch ( DownloadFailedException e )
317         {
318             assertTrue( Utils.toString( e ).indexOf( "AuthorizationException" ) > -1 );
319         }
320 
321         verify( wagon, wagonManager );
322     }
323 
324     public void testShouldFailToDownloadWhenWagonDisconnectThrowsConnectionException()
325         throws IOException, DownloadFailedException
326     {
327         File tempFile = File.createTempFile( "download-source", "test" );
328         tempFile.deleteOnExit();
329 
330         setupMocksWithWagonDisconnectException( new ConnectionException( "not connected" ) );
331 
332         replay( wagon, wagonManager );
333 
334         DownloadManager downloadManager = new DefaultDownloadManager( wagonManager );
335 
336         MessageHolder mh = new DefaultMessageHolder();
337 
338         downloadManager.download( tempFile.toURL().toExternalForm(), mh );
339 
340         assertTrue( mh.render().indexOf( "ConnectionException" ) > -1 );
341 
342         verify( wagon, wagonManager );
343     }
344 
345     private void setupDefaultMockConfiguration()
346     {
347         try
348         {
349             expect( wagonManager.getWagon( "file" ) ).andReturn( wagon );
350         }
351         catch ( UnsupportedProtocolException e )
352         {
353             fail( "This shouldn't happen!!" );
354         }
355 
356         expect( wagonManager.getAuthenticationInfo( anyString() ) ).andReturn( null );
357 
358         expect( wagonManager.getProxy( anyString() ) ).andReturn( null );
359 
360         try
361         {
362             wagon.connect( anyObject( Repository.class ) , anyObject( AuthenticationInfo.class ), anyObject( ProxyInfo.class ) );
363         }
364         catch ( ConnectionException e )
365         {
366             fail( "This shouldn't happen!!" );
367         }
368         catch ( AuthenticationException e )
369         {
370             fail( "This shouldn't happen!!" );
371         }
372 
373         try
374         {
375             wagon.get( anyString(), anyObject( File.class ) );
376         }
377         catch ( TransferFailedException e )
378         {
379             fail( "This shouldn't happen!!" );
380         }
381         catch ( ResourceDoesNotExistException e )
382         {
383             fail( "This shouldn't happen!!" );
384         }
385         catch ( AuthorizationException e )
386         {
387             fail( "This shouldn't happen!!" );
388         }
389 
390         try
391         {
392             wagon.disconnect();
393         }
394         catch ( ConnectionException e )
395         {
396             fail( "This shouldn't happen!!" );
397         }
398     }
399 
400     private void setupMocksWithWagonManagerGetException( Throwable error )
401     {
402         try
403         {
404             expect( wagonManager.getWagon( "file" ) ).andThrow( error );
405         }
406         catch ( UnsupportedProtocolException e )
407         {
408             fail( "This shouldn't happen!!" );
409         }
410     }
411 
412     private void setupMocksWithWagonConnectionException( Throwable error )
413     {
414         try
415         {
416             expect( wagonManager.getWagon( "file" ) ).andReturn( wagon );
417         }
418         catch ( UnsupportedProtocolException e )
419         {
420             fail( "This shouldn't happen!!" );
421         }
422 
423         expect( wagonManager.getAuthenticationInfo( anyString() ) ).andReturn( null );
424 
425         expect( wagonManager.getProxy( anyString() ) ).andReturn( null );
426 
427         try
428         {
429             wagon.connect( anyObject( Repository.class ) , anyObject( AuthenticationInfo.class ), anyObject( ProxyInfo.class ) );
430             expectLastCall().andThrow( error );
431         }
432         catch ( ConnectionException e )
433         {
434             fail( "This shouldn't happen!!" );
435         }
436         catch ( AuthenticationException e )
437         {
438             fail( "This shouldn't happen!!" );
439         }
440     }
441 
442     private void setupMocksWithWagonGetException( Throwable error )
443     {
444         try
445         {
446             expect( wagonManager.getWagon( "file" ) ).andReturn( wagon );
447         }
448         catch ( UnsupportedProtocolException e )
449         {
450             fail( "This shouldn't happen!!" );
451         }
452 
453         expect( wagonManager.getAuthenticationInfo( anyString() ) ).andReturn( null );
454 
455         expect( wagonManager.getProxy( anyString() ) ).andReturn( null );
456 
457         try
458         {
459             wagon.connect( anyObject( Repository.class ) , anyObject( AuthenticationInfo.class ), anyObject( ProxyInfo.class ) );
460         }
461         catch ( ConnectionException e )
462         {
463             fail( "This shouldn't happen!!" );
464         }
465         catch ( AuthenticationException e )
466         {
467             fail( "This shouldn't happen!!" );
468         }
469 
470         try
471         {
472             wagon.get( anyString(), anyObject( File.class ) );
473             expectLastCall().andThrow( error );
474         }
475         catch ( TransferFailedException e )
476         {
477             fail( "This shouldn't happen!!" );
478         }
479         catch ( ResourceDoesNotExistException e )
480         {
481             fail( "This shouldn't happen!!" );
482         }
483         catch ( AuthorizationException e )
484         {
485             fail( "This shouldn't happen!!" );
486         }
487 
488         try
489         {
490             wagon.disconnect();
491         }
492         catch ( ConnectionException e )
493         {
494             fail( "This shouldn't happen!!" );
495         }
496     }
497 
498     private void setupMocksWithWagonDisconnectException( Throwable error )
499     {
500         try
501         {
502             expect( wagonManager.getWagon( "file" ) ).andReturn( wagon );
503         }
504         catch ( UnsupportedProtocolException e )
505         {
506             fail( "This shouldn't happen!!" );
507         }
508 
509         expect( wagonManager.getAuthenticationInfo( anyString() ) ).andReturn( null );
510 
511         expect( wagonManager.getProxy( anyString() ) ).andReturn( null );
512 
513         try
514         {
515             wagon.connect( anyObject( Repository.class ) , anyObject( AuthenticationInfo.class ), anyObject( ProxyInfo.class ) );
516         }
517         catch ( ConnectionException e )
518         {
519             fail( "This shouldn't happen!!" );
520         }
521         catch ( AuthenticationException e )
522         {
523             fail( "This shouldn't happen!!" );
524         }
525 
526         try
527         {
528             wagon.get( anyString(), anyObject( File.class ) );
529         }
530         catch ( TransferFailedException e )
531         {
532             fail( "This shouldn't happen!!" );
533         }
534         catch ( ResourceDoesNotExistException e )
535         {
536             fail( "This shouldn't happen!!" );
537         }
538         catch ( AuthorizationException e )
539         {
540             fail( "This shouldn't happen!!" );
541         }
542 
543         try
544         {
545             wagon.disconnect();
546             expectLastCall().andThrow( error );
547         }
548         catch ( ConnectionException e )
549         {
550             fail( "This shouldn't happen!!" );
551         }
552     }
553 }