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.io.InputStream;
025import java.text.SimpleDateFormat;
026
027import junit.framework.TestCase;
028
029import org.apache.maven.wagon.authentication.AuthenticationException;
030import org.apache.maven.wagon.authorization.AuthorizationException;
031import org.apache.maven.wagon.events.TransferEvent;
032import org.apache.maven.wagon.events.TransferListener;
033import org.apache.maven.wagon.repository.Repository;
034import org.apache.maven.wagon.resource.Resource;
035import org.codehaus.plexus.util.FileUtils;
036import org.codehaus.plexus.util.IOUtil;
037import org.codehaus.plexus.util.StringInputStream;
038import org.codehaus.plexus.util.StringOutputStream;
039import org.easymock.MockControl;
040
041public class StreamWagonTest
042    extends TestCase
043{
044    private static class TestWagon
045        extends StreamWagon
046    {
047        public void closeConnection()
048            throws ConnectionException
049        {
050        }
051
052        public void fillInputData( InputData inputData )
053            throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
054        {
055        }
056
057        public void fillOutputData( OutputData outputData )
058            throws TransferFailedException
059        {
060        }
061
062        protected void openConnectionInternal()
063            throws ConnectionException, AuthenticationException
064        {
065        }
066    }
067
068    private Repository repository = new Repository( "id", "url" );
069
070    public void testNullInputStream()
071        throws Exception
072    {
073        StreamingWagon wagon = new TestWagon()
074        {
075            public void fillInputData( InputData inputData )
076            {
077                inputData.setInputStream( null );
078            }
079        };
080
081        MockControl control = MockControl.createControl( TransferListener.class );
082        TransferListener listener = (TransferListener) control.getMock();
083        listener.transferInitiated( null );
084        control.setMatcher( MockControl.ALWAYS_MATCHER );
085        TransferEvent transferEvent =
086            new TransferEvent( wagon, new Resource( "resource" ), new TransferFailedException( "" ),
087                               TransferEvent.REQUEST_GET );
088        listener.transferError( transferEvent );
089        control.replay();
090
091        wagon.connect( repository );
092        wagon.addTransferListener( listener );
093        try
094        {
095            wagon.getToStream( "resource", new StringOutputStream() );
096            fail();
097        }
098        catch ( TransferFailedException e )
099        {
100            assertTrue( true );
101        }
102        finally
103        {
104            wagon.disconnect();
105        }
106
107        control.verify();
108    }
109
110    public void testNullOutputStream()
111        throws Exception
112    {
113        StreamingWagon wagon = new TestWagon()
114        {
115            public void fillOutputData( OutputData inputData )
116            {
117                inputData.setOutputStream( null );
118            }
119        };
120
121        MockControl control = MockControl.createControl( TransferListener.class );
122        TransferListener listener = (TransferListener) control.getMock();
123        listener.transferInitiated( null );
124        control.setMatcher( MockControl.ALWAYS_MATCHER );
125        TransferEvent transferEvent =
126            new TransferEvent( wagon, new Resource( "resource" ), new TransferFailedException( "" ),
127                               TransferEvent.REQUEST_PUT );
128        listener.transferError( transferEvent );
129        control.replay();
130
131        wagon.connect( repository );
132        wagon.addTransferListener( listener );
133        try
134        {
135            wagon.putFromStream( new StringInputStream( "" ), "resource" );
136            fail();
137        }
138        catch ( TransferFailedException e )
139        {
140            assertTrue( true );
141        }
142        finally
143        {
144            wagon.disconnect();
145        }
146
147        control.verify();
148    }
149
150    public void testTransferFailedExceptionOnInput()
151        throws Exception
152    {
153        try
154        {
155            runTestTransferError( new TransferFailedException( "" ) );
156            fail();
157        }
158        catch ( TransferFailedException e )
159        {
160            assertTrue( true );
161        }
162    }
163
164    public void testTransferFailedExceptionOnOutput()
165        throws Exception
166    {
167        StreamingWagon wagon = new TestWagon()
168        {
169            public void fillOutputData( OutputData inputData )
170                throws TransferFailedException
171            {
172                throw (TransferFailedException) new TransferFailedException( "" );
173            }
174        };
175
176        MockControl control = MockControl.createControl( TransferListener.class );
177        TransferListener listener = (TransferListener) control.getMock();
178        listener.transferInitiated( null );
179        control.setMatcher( MockControl.ALWAYS_MATCHER );
180        TransferEvent transferEvent =
181            new TransferEvent( wagon, new Resource( "resource" ), new TransferFailedException( "" ),
182                               TransferEvent.REQUEST_PUT );
183        listener.transferError( transferEvent );
184        control.replay();
185
186        wagon.connect( repository );
187        wagon.addTransferListener( listener );
188        try
189        {
190            wagon.putFromStream( new StringInputStream( "" ), "resource" );
191            fail();
192        }
193        catch ( TransferFailedException e )
194        {
195            assertTrue( true );
196        }
197        finally
198        {
199            wagon.disconnect();
200            control.verify();
201        }
202    }
203
204    public void testResourceDoesNotExistException()
205        throws Exception
206    {
207        try
208        {
209            runTestTransferError( new ResourceDoesNotExistException( "" ) );
210            fail();
211        }
212        catch ( ResourceDoesNotExistException e )
213        {
214            assertTrue( true );
215        }
216    }
217
218    public void testAuthorizationException()
219        throws Exception
220    {
221        try
222        {
223            runTestTransferError( new AuthorizationException( "" ) );
224            fail();
225        }
226        catch ( AuthorizationException e )
227        {
228            assertTrue( true );
229        }
230    }
231
232    private void runTestTransferError( final WagonException exception )
233        throws ConnectionException, AuthenticationException, ResourceDoesNotExistException, AuthorizationException,
234        TransferFailedException
235    {
236        StreamingWagon wagon = new TestWagon()
237        {
238            public void fillInputData( InputData inputData )
239                throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
240            {
241                if ( exception instanceof TransferFailedException )
242                {
243                    throw (TransferFailedException) exception;
244                }
245                if ( exception instanceof ResourceDoesNotExistException )
246                {
247                    throw (ResourceDoesNotExistException) exception;
248                }
249                if ( exception instanceof AuthorizationException )
250                {
251                    throw (AuthorizationException) exception;
252                }
253            }
254        };
255
256        MockControl control = MockControl.createControl( TransferListener.class );
257        TransferListener listener = (TransferListener) control.getMock();
258        listener.transferInitiated( null );
259        control.setMatcher( MockControl.ALWAYS_MATCHER );
260        TransferEvent transferEvent =
261            new TransferEvent( wagon, new Resource( "resource" ), exception, TransferEvent.REQUEST_GET );
262        listener.transferError( transferEvent );
263        control.replay();
264
265        wagon.connect( repository );
266        wagon.addTransferListener( listener );
267        try
268        {
269            wagon.getToStream( "resource", new StringOutputStream() );
270            fail();
271        }
272        finally
273        {
274            wagon.disconnect();
275            control.verify();
276        }
277    }
278
279    public void testGetIfNewerWithNewerResource()
280        throws Exception
281    {
282        long resourceTime = System.currentTimeMillis();
283        long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
284        assertTrue( runTestGetIfNewer( resourceTime, comparisonTime ) );
285    }
286
287    public void testGetIfNewerWithOlderResource()
288        throws Exception
289    {
290        long comparisonTime = System.currentTimeMillis();
291        long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
292        assertFalse( runTestGetIfNewer( resourceTime, comparisonTime ) );
293    }
294
295    public void testGetIfNewerWithSameTimeResource()
296        throws Exception
297    {
298        long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
299        assertFalse( runTestGetIfNewer( resourceTime, resourceTime ) );
300    }
301
302    private boolean runTestGetIfNewer( final long resourceTime, long comparisonTime )
303        throws IOException, ConnectionException, AuthenticationException, TransferFailedException,
304        ResourceDoesNotExistException, AuthorizationException
305    {
306        StreamingWagon wagon = new TestWagon()
307        {
308            public void fillInputData( InputData inputData )
309            {
310                inputData.setInputStream( new StringInputStream( "" ) );
311                inputData.getResource().setLastModified( resourceTime );
312            }
313        };
314
315        File tempFile = File.createTempFile( "wagon", "tmp" );
316        tempFile.deleteOnExit();
317
318        wagon.connect( repository );
319        try
320        {
321            return wagon.getIfNewer( "resource", tempFile, comparisonTime );
322        }
323        finally
324        {
325            wagon.disconnect();
326            tempFile.delete();
327        }
328    }
329
330    public void testGetToStream()
331        throws Exception
332    {
333        final String content = "the content to return";
334        final long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
335        StreamingWagon wagon = new TestWagon()
336        {
337            public void fillInputData( InputData inputData )
338            {
339                inputData.setInputStream( new StringInputStream( content ) );
340                inputData.getResource().setLastModified( comparisonTime );
341            }
342        };
343
344        wagon.connect( repository );
345        try
346        {
347            StringOutputStream out = new StringOutputStream();
348            wagon.getToStream( "resource", out );
349            assertEquals( content, out.toString() );
350        }
351        finally
352        {
353            wagon.disconnect();
354        }
355    }
356
357    public void testGet()
358        throws Exception
359    {
360        final String content = "the content to return";
361        final long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
362        StreamingWagon wagon = new TestWagon()
363        {
364            public void fillInputData( InputData inputData )
365            {
366                inputData.setInputStream( new StringInputStream( content ) );
367                inputData.getResource().setLastModified( comparisonTime );
368            }
369        };
370
371        File tempFile = File.createTempFile( "wagon", "tmp" );
372        tempFile.deleteOnExit();
373
374        wagon.connect( repository );
375        try
376        {
377            wagon.get( "resource", tempFile );
378            assertEquals( content, FileUtils.fileRead( tempFile ) );
379        }
380        finally
381        {
382            wagon.disconnect();
383            tempFile.delete();
384        }
385    }
386
387    public void testGetIfNewerToStreamWithNewerResource()
388        throws Exception
389    {
390        long resourceTime = System.currentTimeMillis();
391        long comparisonTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
392        assertTrue( runTestGetIfNewerToStream( resourceTime, comparisonTime ) );
393    }
394
395    public void testGetIfNewerToStreamWithOlderResource()
396        throws Exception
397    {
398        long comparisonTime = System.currentTimeMillis();
399        long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
400        assertFalse( runTestGetIfNewerToStream( resourceTime, comparisonTime ) );
401    }
402
403    public void testGetIfNewerToStreamWithSameTimeResource()
404        throws Exception
405    {
406        long resourceTime = new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2008-01-01" ).getTime();
407        assertFalse( runTestGetIfNewerToStream( resourceTime, resourceTime ) );
408    }
409
410    private boolean runTestGetIfNewerToStream( final long resourceTime, long comparisonTime )
411        throws IOException, ConnectionException, AuthenticationException, TransferFailedException,
412        ResourceDoesNotExistException, AuthorizationException
413    {
414        StreamingWagon wagon = new TestWagon()
415        {
416            public void fillInputData( InputData inputData )
417            {
418                inputData.setInputStream( new StringInputStream( "" ) );
419                inputData.getResource().setLastModified( resourceTime );
420            }
421        };
422
423        wagon.connect( repository );
424        try
425        {
426            return wagon.getIfNewerToStream( "resource", new StringOutputStream(), comparisonTime );
427        }
428        finally
429        {
430            wagon.disconnect();
431        }
432    }
433
434    public void testPutFromStream()
435        throws Exception
436    {
437        final String content = "the content to return";
438
439        final StringOutputStream out = new StringOutputStream();
440        StreamingWagon wagon = new TestWagon()
441        {
442            public void fillOutputData( OutputData outputData )
443            {
444                assertEquals( "resource", outputData.getResource().getName() );
445                assertEquals( -1, outputData.getResource().getContentLength() );
446                assertEquals( 0, outputData.getResource().getLastModified() );
447                outputData.setOutputStream( out );
448            }
449        };
450
451        wagon.connect( repository );
452        try
453        {
454            wagon.putFromStream( new StringInputStream( content ), "resource" );
455            assertEquals( content, out.toString() );
456        }
457        finally
458        {
459            wagon.disconnect();
460        }
461    }
462
463    public void testPutFromStreamWithResourceInformation()
464        throws Exception
465    {
466        final String content = "the content to return";
467        final long lastModified = System.currentTimeMillis();
468
469        final StringOutputStream out = new StringOutputStream();
470        StreamingWagon wagon = new TestWagon()
471        {
472            public void fillOutputData( OutputData outputData )
473            {
474                assertEquals( "resource", outputData.getResource().getName() );
475                assertEquals( content.length(), outputData.getResource().getContentLength() );
476                assertEquals( lastModified, outputData.getResource().getLastModified() );
477                outputData.setOutputStream( out );
478            }
479        };
480
481        wagon.connect( repository );
482        try
483        {
484            wagon.putFromStream( new StringInputStream( content ), "resource", content.length(), lastModified );
485            assertEquals( content, out.toString() );
486        }
487        finally
488        {
489            wagon.disconnect();
490        }
491    }
492
493    public void testPut()
494        throws Exception
495    {
496        final String content = "the content to return";
497
498        final File tempFile = File.createTempFile( "wagon", "tmp" );
499        FileUtils.fileWrite( tempFile.getAbsolutePath(), content );
500        tempFile.deleteOnExit();
501
502        final StringOutputStream out = new StringOutputStream();
503        Wagon wagon = new TestWagon()
504        {
505            public void fillOutputData( OutputData outputData )
506            {
507                assertEquals( "resource", outputData.getResource().getName() );
508                assertEquals( content.length(), outputData.getResource().getContentLength() );
509                assertEquals( tempFile.lastModified(), outputData.getResource().getLastModified() );
510                outputData.setOutputStream( out );
511            }
512        };
513
514        wagon.connect( repository );
515        try
516        {
517            wagon.put( tempFile, "resource" );
518            assertEquals( content, out.toString() );
519        }
520        finally
521        {
522            wagon.disconnect();
523            tempFile.delete();
524        }
525    }
526
527    public void testPutFileDoesntExist()
528        throws Exception
529    {
530        final File tempFile = File.createTempFile( "wagon", "tmp" );
531        tempFile.delete();
532        assertFalse( tempFile.exists() );
533
534        Wagon wagon = new TestWagon();
535
536        wagon.connect( repository );
537        try
538        {
539            wagon.put( tempFile, "resource" );
540            fail();
541        }
542        catch ( TransferFailedException e )
543        {
544            assertTrue( true );
545        }
546        finally
547        {
548            wagon.disconnect();
549        }
550    }
551
552}