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   * @version $Id: StreamingWagonTestCase.html 849521 2013-02-05 22:14:15Z olamy $
37   */
38  public abstract class StreamingWagonTestCase
39      extends WagonTestCase
40  {
41      public void testStreamingWagon()
42          throws Exception
43      {
44          if ( supportsGetIfNewer() )
45          {
46              setupRepositories();
47  
48              setupWagonTestingFixtures();
49  
50              streamRoundTripTesting();
51  
52              tearDownWagonTestingFixtures();
53          }
54      }
55  
56      public void testFailedGetToStream()
57          throws Exception
58      {
59          setupRepositories();
60  
61          setupWagonTestingFixtures();
62  
63          message( "Getting test artifact from test repository " + testRepository );
64  
65          StreamingWagon wagon = (StreamingWagon) getWagon();
66  
67          wagon.addTransferListener( checksumObserver );
68  
69          wagon.connect( testRepository, getAuthInfo() );
70  
71          destFile = FileTestUtils.createUniqueFile( getName(), getName() );
72  
73          destFile.deleteOnExit();
74  
75          OutputStream stream = null;
76  
77          try
78          {
79              stream = new FileOutputStream( destFile );
80              wagon.getToStream( "fubar.txt", stream );
81              fail( "File was found when it shouldn't have been" );
82          }
83          catch ( ResourceDoesNotExistException e )
84          {
85              // expected
86              assertTrue( true );
87          }
88          finally
89          {
90              wagon.removeTransferListener( checksumObserver );
91  
92              wagon.disconnect();
93  
94              IOUtil.close( stream );
95  
96              tearDownWagonTestingFixtures();
97          }
98      }
99  
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 }