View Javadoc
1   package org.apache.maven.wagon;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.wagon.observers.ChecksumObserver;
23  import org.apache.maven.wagon.resource.Resource;
24  import org.codehaus.plexus.util.FileUtils;
25  import org.codehaus.plexus.util.IOUtil;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileOutputStream;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.text.SimpleDateFormat;
33  
34  /**
35   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
36   */
37  public abstract class StreamingWagonTestCase
38      extends WagonTestCase
39  {
40      public void testStreamingWagon()
41          throws Exception
42      {
43          if ( supportsGetIfNewer() )
44          {
45              setupWagonTestingFixtures();
46  
47              setupRepositories();
48  
49              streamRoundTripTesting();
50  
51              tearDownWagonTestingFixtures();
52          }
53      }
54  
55      public void testFailedGetToStream()
56          throws Exception
57      {
58          setupWagonTestingFixtures();
59  
60          setupRepositories();
61  
62          message( "Getting test artifact from test repository " + testRepository );
63  
64          StreamingWagon wagon = (StreamingWagon) getWagon();
65  
66          wagon.addTransferListener( checksumObserver );
67  
68          wagon.connect( testRepository, getAuthInfo() );
69  
70          destFile = FileTestUtils.createUniqueFile( getName(), getName() );
71  
72          destFile.deleteOnExit();
73  
74          OutputStream stream = null;
75  
76          try
77          {
78              stream = new FileOutputStream( destFile );
79              wagon.getToStream( "fubar.txt", stream );
80              fail( "File was found when it shouldn't have been" );
81              stream.close();
82              stream = null;
83          }
84          catch ( ResourceDoesNotExistException e )
85          {
86              // expected
87              assertTrue( true );
88          }
89          finally
90          {
91              wagon.removeTransferListener( checksumObserver );
92  
93              wagon.disconnect();
94  
95              IOUtil.close( stream );
96  
97              tearDownWagonTestingFixtures();
98          }
99      }
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 }