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