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