1 package org.apache.maven.wagon;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.security.NoSuchAlgorithmException;
29 import java.text.SimpleDateFormat;
30
31 import org.apache.maven.wagon.authentication.AuthenticationException;
32 import org.apache.maven.wagon.authorization.AuthorizationException;
33 import org.apache.maven.wagon.observers.ChecksumObserver;
34 import org.apache.maven.wagon.resource.Resource;
35 import org.codehaus.plexus.util.FileUtils;
36 import org.codehaus.plexus.util.IOUtil;
37
38
39
40
41
42 public abstract class StreamingWagonTestCase
43 extends WagonTestCase
44 {
45 public void testStreamingWagon()
46 throws Exception
47 {
48 setupRepositories();
49
50 setupWagonTestingFixtures();
51
52 streamRoundTripTesting();
53
54 tearDownWagonTestingFixtures();
55 }
56
57 public void testFailedGetToStream()
58 throws Exception
59 {
60 setupRepositories();
61
62 setupWagonTestingFixtures();
63
64 message( "Getting test artifact from test repository " + testRepository );
65
66 StreamingWagon wagon = (StreamingWagon) getWagon();
67
68 wagon.addTransferListener( checksumObserver );
69
70 wagon.connect( testRepository, getAuthInfo() );
71
72 destFile = FileTestUtils.createUniqueFile( getName(), getName() );
73
74 destFile.deleteOnExit();
75
76 OutputStream stream = null;
77
78 try
79 {
80 stream = new FileOutputStream( destFile );
81 wagon.getToStream( "fubar.txt", stream );
82 fail( "File was found when it shouldn't have been" );
83 }
84 catch ( ResourceDoesNotExistException e )
85 {
86
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 getIfNewerToStream( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ) + 30000,
110 false, expectedSize );
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, NoSuchAlgorithmException, IOException, ConnectionException, AuthenticationException,
142 TransferFailedException, ResourceDoesNotExistException, AuthorizationException
143 {
144 StreamingWagon wagon = (StreamingWagon) getWagon();
145
146 ProgressArgumentMatcher progressArgumentMatcher = setupGetIfNewerTest( wagon, expectedResult, expectedSize );
147
148 connectWagon( wagon );
149
150 OutputStream stream = new LazyFileOutputStream( destFile );
151
152 try
153 {
154 boolean result = wagon.getIfNewerToStream( this.resource, stream, timestamp );
155 assertEquals( expectedResult, result );
156 }
157 finally
158 {
159 IOUtil.close( stream );
160 }
161
162 disconnectWagon( wagon );
163
164 assertGetIfNewerTest( progressArgumentMatcher, expectedResult, expectedSize );
165
166 tearDownWagonTestingFixtures();
167 }
168
169 public void testFailedGetIfNewerToStream()
170 throws Exception
171 {
172 if ( supportsGetIfNewer() )
173 {
174 setupRepositories();
175 setupWagonTestingFixtures();
176 message( "Getting test artifact from test repository " + testRepository );
177 StreamingWagon wagon = (StreamingWagon) getWagon();
178 wagon.addTransferListener( checksumObserver );
179 wagon.connect( testRepository, getAuthInfo() );
180 destFile = FileTestUtils.createUniqueFile( getName(), getName() );
181 destFile.deleteOnExit();
182 OutputStream stream = null;
183 try
184 {
185 stream = new FileOutputStream( destFile );
186 wagon.getIfNewerToStream( "fubar.txt", stream, 0 );
187 fail( "File was found when it shouldn't have been" );
188 }
189 catch ( ResourceDoesNotExistException e )
190 {
191
192 assertTrue( true );
193 }
194 finally
195 {
196 wagon.removeTransferListener( checksumObserver );
197
198 wagon.disconnect();
199
200 IOUtil.close( stream );
201
202 tearDownWagonTestingFixtures();
203 }
204 }
205 }
206
207 protected void streamRoundTripTesting()
208 throws Exception
209 {
210 message( "Stream round trip testing ..." );
211
212 int expectedSize = putStream();
213
214 assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() );
215
216 assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() );
217
218 checksumObserver = new ChecksumObserver();
219
220 getStream( expectedSize );
221
222 assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() );
223
224 assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() );
225
226
227
228
229
230 String sourceContent = FileUtils.fileRead( sourceFile );
231
232 String destContent = FileUtils.fileRead( destFile );
233
234 assertEquals( sourceContent, destContent );
235 }
236
237 private int putStream()
238 throws Exception
239 {
240 String content = "test-resource.txt\n";
241 sourceFile = new File( FileTestUtils.getTestOutputDir(), "test-resource" );
242 sourceFile.getParentFile().mkdirs();
243 FileUtils.fileWrite( sourceFile.getAbsolutePath(), content );
244
245 StreamingWagon wagon = (StreamingWagon) getWagon();
246
247 ProgressArgumentMatcher progressArgumentMatcher = replayMockForPut( resource, content, wagon );
248
249 message( "Putting test artifact: " + resource + " into test repository " + testRepository );
250
251 connectWagon( wagon );
252
253 InputStream stream = null;
254
255 try
256 {
257 stream = new FileInputStream( sourceFile );
258 wagon.putFromStream( stream, resource, sourceFile.length(), sourceFile.lastModified() );
259 }
260 finally
261 {
262 IOUtil.close( stream );
263 }
264
265 disconnectWagon( wagon );
266
267 verifyMock( progressArgumentMatcher, content.length() );
268 return content.length();
269 }
270
271 private void getStream( int expectedSize )
272 throws Exception
273 {
274 destFile = FileTestUtils.createUniqueFile( getName(), getName() );
275 destFile.deleteOnExit();
276
277 StreamingWagon wagon = (StreamingWagon) getWagon();
278
279 ProgressArgumentMatcher progressArgumentMatcher = replaceMockForGet( wagon, expectedSize );
280
281 message( "Getting test artifact from test repository " + testRepository );
282
283 connectWagon( wagon );
284
285 OutputStream stream = null;
286
287 try
288 {
289 stream = new FileOutputStream( destFile );
290 wagon.getToStream( this.resource, stream );
291 }
292 finally
293 {
294 IOUtil.close( stream );
295 }
296
297 disconnectWagon( wagon );
298
299 verifyMock( progressArgumentMatcher, expectedSize );
300 }
301 }