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 org.apache.maven.wagon.observers.ChecksumObserver;
023import org.apache.maven.wagon.resource.Resource;
024import org.codehaus.plexus.util.FileUtils;
025import org.codehaus.plexus.util.IOUtil;
026
027import java.io.File;
028import java.io.FileInputStream;
029import java.io.FileOutputStream;
030import java.io.InputStream;
031import java.io.OutputStream;
032import java.text.SimpleDateFormat;
033
034/**
035 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
036 */
037public abstract class StreamingWagonTestCase
038    extends WagonTestCase
039{
040    public void testStreamingWagon()
041        throws Exception
042    {
043        if ( supportsGetIfNewer() )
044        {
045            setupRepositories();
046
047            setupWagonTestingFixtures();
048
049            streamRoundTripTesting();
050
051            tearDownWagonTestingFixtures();
052        }
053    }
054
055    public void testFailedGetToStream()
056        throws Exception
057    {
058        setupRepositories();
059
060        setupWagonTestingFixtures();
061
062        message( "Getting test artifact from test repository " + testRepository );
063
064        StreamingWagon wagon = (StreamingWagon) getWagon();
065
066        wagon.addTransferListener( checksumObserver );
067
068        wagon.connect( testRepository, getAuthInfo() );
069
070        destFile = FileTestUtils.createUniqueFile( getName(), getName() );
071
072        destFile.deleteOnExit();
073
074        OutputStream stream = null;
075
076        try
077        {
078            stream = new FileOutputStream( destFile );
079            wagon.getToStream( "fubar.txt", stream );
080            fail( "File was found when it shouldn't have been" );
081            stream.close();
082            stream = null;
083        }
084        catch ( ResourceDoesNotExistException e )
085        {
086            // expected
087            assertTrue( true );
088        }
089        finally
090        {
091            wagon.removeTransferListener( checksumObserver );
092
093            wagon.disconnect();
094
095            IOUtil.close( stream );
096
097            tearDownWagonTestingFixtures();
098        }
099    }
100
101    public void testWagonGetIfNewerToStreamIsNewer()
102        throws Exception
103    {
104        if ( supportsGetIfNewer() )
105        {
106            setupRepositories();
107            setupWagonTestingFixtures();
108            int expectedSize = putFile();
109            // CHECKSTYLE_OFF: MagicNumber
110            getIfNewerToStream( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ) + 30000, false,
111                                expectedSize );
112            // CHECKSTYLE_ON: MagicNumber
113        }
114    }
115
116    public void testWagonGetIfNewerToStreamIsOlder()
117        throws Exception
118    {
119        if ( supportsGetIfNewer() )
120        {
121            setupRepositories();
122            setupWagonTestingFixtures();
123            int expectedSize = putFile();
124            getIfNewerToStream( new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2006-01-01" ).getTime(), true,
125                                expectedSize );
126        }
127    }
128
129    public void testWagonGetIfNewerToStreamIsSame()
130        throws Exception
131    {
132        if ( supportsGetIfNewer() )
133        {
134            setupRepositories();
135            setupWagonTestingFixtures();
136            int expectedSize = putFile();
137            getIfNewerToStream( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ), false,
138                                expectedSize );
139        }
140    }
141
142    private void getIfNewerToStream( long timestamp, boolean expectedResult, int expectedSize )
143        throws Exception
144    {
145        StreamingWagon wagon = (StreamingWagon) getWagon();
146
147        ProgressAnswer progressAnswer = setupGetIfNewerTest( wagon, expectedResult, expectedSize );
148
149        connectWagon( wagon );
150
151        OutputStream stream = new LazyFileOutputStream( destFile );
152
153        try
154        {
155            boolean result = wagon.getIfNewerToStream( this.resource, stream, timestamp );
156            assertEquals( expectedResult, result );
157        }
158        finally
159        {
160            stream.close();
161        }
162
163        disconnectWagon( wagon );
164
165        assertGetIfNewerTest( progressAnswer, expectedResult, expectedSize );
166
167        tearDownWagonTestingFixtures();
168    }
169
170    public void testFailedGetIfNewerToStream()
171        throws Exception
172    {
173        if ( supportsGetIfNewer() )
174        {
175            setupRepositories();
176            setupWagonTestingFixtures();
177            message( "Getting test artifact from test repository " + testRepository );
178            StreamingWagon wagon = (StreamingWagon) getWagon();
179            wagon.addTransferListener( checksumObserver );
180            wagon.connect( testRepository, getAuthInfo() );
181            destFile = FileTestUtils.createUniqueFile( getName(), getName() );
182            destFile.deleteOnExit();
183            OutputStream stream = null;
184            try
185            {
186                stream = new FileOutputStream( destFile );
187                wagon.getIfNewerToStream( "fubar.txt", stream, 0 );
188                fail( "File was found when it shouldn't have been" );
189                stream.close();
190                stream = null;
191            }
192            catch ( ResourceDoesNotExistException e )
193            {
194                // expected
195                assertTrue( true );
196            }
197            finally
198            {
199                wagon.removeTransferListener( checksumObserver );
200
201                wagon.disconnect();
202
203                IOUtil.close( stream );
204
205                tearDownWagonTestingFixtures();
206            }
207        }
208    }
209
210    protected void streamRoundTripTesting()
211        throws Exception
212    {
213        message( "Stream round trip testing ..." );
214
215        int expectedSize = putStream();
216
217        assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() );
218
219        assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() );
220
221        checksumObserver = new ChecksumObserver();
222
223        getStream( expectedSize );
224
225        assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() );
226
227        assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() );
228
229        // Now compare the conents of the artifact that was placed in
230        // the repository with the contents of the artifact that was
231        // retrieved from the repository.
232
233        String sourceContent = FileUtils.fileRead( sourceFile );
234
235        String destContent = FileUtils.fileRead( destFile );
236
237        assertEquals( sourceContent, destContent );
238    }
239
240    private int putStream()
241        throws Exception
242    {
243        String content = "test-resource.txt\n";
244        sourceFile = new File( FileTestUtils.getTestOutputDir(), "test-resource" );
245        sourceFile.getParentFile().mkdirs();
246        FileUtils.fileWrite( sourceFile.getAbsolutePath(), content );
247
248        StreamingWagon wagon = (StreamingWagon) getWagon();
249
250        ProgressAnswer progressAnswer = replayMockForPut( resource, content, wagon );
251
252        message( "Putting test artifact: " + resource + " into test repository " + testRepository );
253
254        connectWagon( wagon );
255
256        InputStream stream = null;
257
258        try
259        {
260            stream = new FileInputStream( sourceFile );
261            wagon.putFromStream( stream, resource, sourceFile.length(), sourceFile.lastModified() );
262            stream.close();
263            stream = null;
264        }
265        catch ( Exception e )
266        {
267            logger.error( "error while putting resources to the FTP Server", e );
268        }
269        finally
270        {
271            IOUtil.close( stream );
272        }
273
274        disconnectWagon( wagon );
275
276        verifyMock( progressAnswer, content.length() );
277        return content.length();
278    }
279
280    private void getStream( int expectedSize )
281        throws Exception
282    {
283        destFile = FileTestUtils.createUniqueFile( getName(), getName() );
284        destFile.deleteOnExit();
285
286        StreamingWagon wagon = (StreamingWagon) getWagon();
287
288        ProgressAnswer progressAnswer = replaceMockForGet( wagon, expectedSize );
289
290        message( "Getting test artifact from test repository " + testRepository );
291
292        connectWagon( wagon );
293
294        OutputStream stream = null;
295
296        try
297        {
298            stream = new FileOutputStream( destFile );
299            wagon.getToStream( this.resource, stream );
300            stream.close();
301            stream = null;
302        }
303        catch ( Exception e )
304        {
305            logger.error( "error while reading resources from the FTP Server", e );
306        }
307        finally
308        {
309            IOUtil.close( stream );
310        }
311
312        disconnectWagon( wagon );
313
314        verifyMock( progressAnswer, expectedSize );
315    }
316}