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