001package org.apache.maven.wagon;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024import java.text.SimpleDateFormat;
025
026import junit.framework.TestCase;
027
028import org.apache.maven.wagon.authentication.AuthenticationException;
029import org.apache.maven.wagon.authorization.AuthorizationException;
030import org.apache.maven.wagon.events.TransferEvent;
031import org.apache.maven.wagon.events.TransferListener;
032import org.apache.maven.wagon.repository.Repository;
033import org.apache.maven.wagon.resource.Resource;
034import org.codehaus.plexus.util.FileUtils;
035import org.codehaus.plexus.util.StringInputStream;
036import org.codehaus.plexus.util.StringOutputStream;
037
038import static org.easymock.EasyMock.*;
039
040public class StreamWagonTest
041    extends TestCase
042{
043    private static class TestWagon
044        extends StreamWagon
045    {
046        public void closeConnection()
047            throws ConnectionException
048        {
049        }
050
051        public void fillInputData( InputData inputData )
052            throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
053        {
054        }
055
056        public void fillOutputData( OutputData outputData )
057            throws TransferFailedException
058        {
059        }
060
061        protected void openConnectionInternal()
062            throws ConnectionException, AuthenticationException
063        {
064        }
065    }
066
067    private Repository repository = new Repository( "id", "url" );
068
069    public void testNullInputStream()
070        throws Exception
071    {
072        StreamingWagon wagon = new TestWagon()
073        {
074            public void fillInputData( InputData inputData )
075            {
076                inputData.setInputStream( null );
077            }
078        };
079
080        TransferListener listener = createMock( TransferListener.class );
081        listener.transferInitiated( anyObject( TransferEvent.class ) );
082        TransferEvent transferEvent =
083            new TransferEvent( wagon, new Resource( "resource" ), new TransferFailedException( "" ),
084                               TransferEvent.REQUEST_GET );
085        listener.transferError( transferEvent );
086        replay( listener );
087
088        wagon.connect( repository );
089        wagon.addTransferListener( listener );
090        try
091        {
092            wagon.getToStream( "resource", new StringOutputStream() );
093            fail();
094        }
095        catch ( TransferFailedException e )
096        {
097            assertTrue( true );
098        }
099        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}