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              setupRepositories();
46  
47              setupWagonTestingFixtures();
48  
49              streamRoundTripTesting();
50  
51              tearDownWagonTestingFixtures();
52          }
53      }
54  
55      public void testFailedGetToStream()
56          throws Exception
57      {
58          setupRepositories();
59  
60          setupWagonTestingFixtures();
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             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 }