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