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