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