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