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