]> source.dussan.org Git - archiva.git/blob
9ab0585bf6260e94ca15ec03ea97ead922194667
[archiva.git] /
1 package org.apache.archiva.proxy;
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.archiva.model.ArtifactReference;
23 import org.apache.archiva.policies.CachedFailuresPolicy;
24 import org.apache.archiva.policies.ChecksumPolicy;
25 import org.apache.archiva.policies.ReleasesPolicy;
26 import org.apache.archiva.policies.SnapshotsPolicy;
27 import org.junit.Test;
28
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.nio.file.attribute.FileTime;
33 import java.util.concurrent.TimeUnit;
34
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertTrue;
37
38 /**
39  * SnapshotTransferTest 
40  *
41  *
42  */
43 public class SnapshotTransferTest
44     extends AbstractProxyTestCase
45 {
46     @Test
47     public void testSnapshotNonExistant()
48         throws Exception
49     {
50         String path = "org/apache/maven/test/does-not-exist/1.0-SNAPSHOT/does-not-exist-1.0-SNAPSHOT.jar";
51         setupTestableManagedRepository( path );
52         
53         Path expectedFile = managedDefaultDir.resolve(path);
54         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
55
56         Files.deleteIfExists(expectedFile);
57         assertFalse( Files.exists(expectedFile) );
58
59         // Configure Connector (usually done within archiva.xml configuration)
60         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
61
62         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
63         assertNotDownloaded( downloadedFile );
64         assertNoTempFiles( expectedFile );
65     }
66
67     @Test
68     public void testTimestampDrivenSnapshotNotPresentAlready()
69         throws Exception
70     {
71         String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
72         setupTestableManagedRepository( path );
73         
74         Path expectedFile = managedDefaultDir.resolve(path);
75         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
76
77         Files.deleteIfExists(expectedFile);
78         assertFalse( Files.exists(expectedFile) );
79
80         // Configure Connector (usually done within archiva.xml configuration)
81         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
82
83         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
84
85         Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
86         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
87         assertNoTempFiles( expectedFile );
88     }
89
90     @Test
91     public void testNewerTimestampDrivenSnapshotOnFirstRepo()
92         throws Exception
93     {
94         String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
95         setupTestableManagedRepository( path );
96         
97         Path expectedFile = managedDefaultDir.resolve(path);
98         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
99
100         assertTrue( Files.exists(expectedFile) );
101         Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
102
103         // Configure Connector (usually done within archiva.xml configuration)
104         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
105
106         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
107
108         Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
109         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
110         assertNoTempFiles( expectedFile );
111     }
112
113     @Test
114     public void testOlderTimestampDrivenSnapshotOnFirstRepo()
115         throws Exception
116     {
117         String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
118         setupTestableManagedRepository( path );
119         
120         Path expectedFile = managedDefaultDir.resolve(path);
121         Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
122         
123         setManagedNewerThanRemote( expectedFile, remoteFile );
124         
125         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
126
127         // Configure Connector (usually done within archiva.xml configuration)
128         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
129
130         // Attempt to download.
131         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
132
133         // Should not have downloaded as managed is newer than remote.
134         assertNotDownloaded( downloadedFile );
135         assertNoTempFiles( expectedFile );
136     }
137
138     /**
139      * TODO: Has problems with wagon implementation not preserving timestamp.
140      */
141     /*
142     public void testNewerTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
143         throws Exception
144     {
145         String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
146         setupTestableManagedRepository( path );
147         
148         Path expectedFile = managedDefaultDir.resolve(path);
149         ArtifactReference artifact = createArtifactReference( "default", path );
150
151         Files.delete(expectedFile);
152         assertFalse( Files.exists(expectedFile) );
153
154         // Create customized proxy / target repository
155         File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED1_TARGET, REPOPATH_PROXIED1,
156                                                             REPOPATH_PROXIED1_TARGET, "default" );
157
158         new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
159
160         // Configure Connector (usually done within archiva.xml configuration)
161         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
162                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
163         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
164                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
165
166         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
167
168         // Should have downloaded the content from proxy2, as proxy1 has an old (by file.lastModified check) version.
169         Path proxiedFile = Paths.get(REPOPATH_PROXIED2, path);
170         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
171         assertNoTempFiles( expectedFile );
172     } 
173
174     public void testOlderTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
175         throws Exception
176     {
177         String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
178         setupTestableManagedRepository( path );
179         
180         Path expectedFile = managedDefaultDir.resolve(path);
181         ArtifactReference artifact = createArtifactReference( "default", path );
182
183         Files.delete(expectedFile);
184         assertFalse( Files.exists(expectedFile) );
185
186         // Create customized proxy / target repository
187         File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED2_TARGET, REPOPATH_PROXIED2,
188                                                             REPOPATH_PROXIED2_TARGET, "default" );
189
190         new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
191
192         // Configure Connector (usually done within archiva.xml configuration)
193         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
194                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
195         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
196                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
197
198         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
199
200         File proxiedFile = new File( REPOPATH_PROXIED1_TARGET, path );
201         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
202         assertNoTempFiles( expectedFile );
203     } */
204
205     @Test
206     public void testTimestampDrivenSnapshotNotExpired()
207         throws Exception
208     {
209         String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
210         setupTestableManagedRepository( path );
211         
212         Path expectedFile = managedDefaultDir.resolve(path);
213         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
214
215         assertTrue( Files.exists(expectedFile) );
216
217         Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
218         Files.setLastModifiedTime( proxiedFile, FileTime.from( getFutureDate().getTime(), TimeUnit.MILLISECONDS ));
219
220         // Configure Connector (usually done within archiva.xml configuration)
221         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
222
223         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
224
225         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
226         assertNoTempFiles( expectedFile );
227     }
228
229     @Test
230     public void testTimestampDrivenSnapshotNotUpdated()
231         throws Exception
232     {
233         String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
234         setupTestableManagedRepository( path );
235         
236         Path expectedFile = managedDefaultDir.resolve(path);
237         Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
238
239         setManagedNewerThanRemote( expectedFile, remoteFile, 12000000 );
240         long expectedTimestamp = Files.getLastModifiedTime( expectedFile ).toMillis();
241         
242         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
243
244         // Configure Connector (usually done within archiva.xml configuration)
245         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
246
247         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
248
249         assertNotDownloaded( downloadedFile );
250         assertNotModified( expectedFile, expectedTimestamp );
251         assertNoTempFiles( expectedFile );
252     }
253
254     @Test
255     public void testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure()
256         throws Exception
257     {
258         String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
259         setupTestableManagedRepository( path );
260         
261         Path expectedFile = managedDefaultDir.resolve(path);
262         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
263
264         Files.deleteIfExists(expectedFile);
265         assertFalse( Files.exists(expectedFile) );
266
267         // Configure Connector (usually done within archiva.xml configuration)
268         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
269                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES , false);
270         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
271                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES , false);
272
273         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
274
275         Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
276         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
277         assertNoTempFiles( expectedFile );
278     }
279
280     @Test
281     public void testMetadataDrivenSnapshotNotPresentAlready()
282         throws Exception
283     {
284         String path = "org/apache/maven/test/get-metadata-snapshot/1.0-SNAPSHOT/get-metadata-snapshot-1.0-20050831.101112-1.jar";
285         setupTestableManagedRepository( path );
286         
287         Path expectedFile = managedDefaultDir.resolve(path);
288         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
289
290         Files.deleteIfExists(expectedFile);
291         assertFalse( Files.exists(expectedFile) );
292
293         // Configure Connector (usually done within archiva.xml configuration)
294         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
295
296         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
297
298         Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
299         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
300         assertNoTempFiles( expectedFile );
301     }
302
303     @Test
304     public void testGetMetadataDrivenSnapshotRemoteUpdate()
305         throws Exception
306     {
307         // Metadata driven snapshots (using a full timestamp) are treated like a release. It is the timing of the
308         // updates to the metadata files that triggers which will be downloaded
309
310         String path = "org/apache/maven/test/get-present-metadata-snapshot/1.0-SNAPSHOT/get-present-metadata-snapshot-1.0-20050831.101112-1.jar";
311         setupTestableManagedRepository( path );
312         
313         Path expectedFile = managedDefaultDir.resolve(path);
314         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
315
316         assertTrue( Files.exists(expectedFile) );
317
318         Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
319
320         // Configure Connector (usually done within archiva.xml configuration)
321         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
322
323         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
324
325         Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
326         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
327         assertNoTempFiles( expectedFile );
328     }
329 }