1 package org.apache.archiva.proxy;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertTrue;
39 * SnapshotTransferTest
43 public class SnapshotTransferTest
44 extends AbstractProxyTestCase
47 public void testSnapshotNonExistant()
50 String path = "org/apache/maven/test/does-not-exist/1.0-SNAPSHOT/does-not-exist-1.0-SNAPSHOT.jar";
51 setupTestableManagedRepository( path );
53 Path expectedFile = managedDefaultDir.resolve(path);
54 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
56 Files.deleteIfExists(expectedFile);
57 assertFalse( Files.exists(expectedFile) );
59 // Configure Connector (usually done within archiva.xml configuration)
60 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
62 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
63 assertNotDownloaded( downloadedFile );
64 assertNoTempFiles( expectedFile );
68 public void testTimestampDrivenSnapshotNotPresentAlready()
71 String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
72 setupTestableManagedRepository( path );
74 Path expectedFile = managedDefaultDir.resolve(path);
75 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
77 Files.deleteIfExists(expectedFile);
78 assertFalse( Files.exists(expectedFile) );
80 // Configure Connector (usually done within archiva.xml configuration)
81 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
83 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
85 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
86 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
87 assertNoTempFiles( expectedFile );
91 public void testNewerTimestampDrivenSnapshotOnFirstRepo()
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 );
97 Path expectedFile = managedDefaultDir.resolve(path);
98 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
100 assertTrue( Files.exists(expectedFile) );
101 Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
103 // Configure Connector (usually done within archiva.xml configuration)
104 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
106 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
108 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
109 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
110 assertNoTempFiles( expectedFile );
114 public void testOlderTimestampDrivenSnapshotOnFirstRepo()
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 );
120 Path expectedFile = managedDefaultDir.resolve(path);
121 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
123 setManagedNewerThanRemote( expectedFile, remoteFile );
125 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
127 // Configure Connector (usually done within archiva.xml configuration)
128 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
130 // Attempt to download.
131 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
133 // Should not have downloaded as managed is newer than remote.
134 assertNotDownloaded( downloadedFile );
135 assertNoTempFiles( expectedFile );
139 * TODO: Has problems with wagon implementation not preserving timestamp.
142 public void testNewerTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
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 );
148 Path expectedFile = managedDefaultDir.resolve(path);
149 ArtifactReference artifact = createArtifactReference( "default", path );
151 Files.delete(expectedFile);
152 assertFalse( Files.exists(expectedFile) );
154 // Create customized proxy / target repository
155 File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED1_TARGET, REPOPATH_PROXIED1,
156 REPOPATH_PROXIED1_TARGET, "default" );
158 new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
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 );
166 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
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 );
174 public void testOlderTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
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 );
180 Path expectedFile = managedDefaultDir.resolve(path);
181 ArtifactReference artifact = createArtifactReference( "default", path );
183 Files.delete(expectedFile);
184 assertFalse( Files.exists(expectedFile) );
186 // Create customized proxy / target repository
187 File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED2_TARGET, REPOPATH_PROXIED2,
188 REPOPATH_PROXIED2_TARGET, "default" );
190 new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
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 );
198 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
200 File proxiedFile = new File( REPOPATH_PROXIED1_TARGET, path );
201 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
202 assertNoTempFiles( expectedFile );
206 public void testTimestampDrivenSnapshotNotExpired()
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 );
212 Path expectedFile = managedDefaultDir.resolve(path);
213 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
215 assertTrue( Files.exists(expectedFile) );
217 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
218 Files.setLastModifiedTime( proxiedFile, FileTime.from( getFutureDate().getTime(), TimeUnit.MILLISECONDS ));
220 // Configure Connector (usually done within archiva.xml configuration)
221 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
223 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
225 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
226 assertNoTempFiles( expectedFile );
230 public void testTimestampDrivenSnapshotNotUpdated()
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 );
236 Path expectedFile = managedDefaultDir.resolve(path);
237 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
239 setManagedNewerThanRemote( expectedFile, remoteFile, 12000000 );
240 long expectedTimestamp = Files.getLastModifiedTime( expectedFile ).toMillis();
242 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
244 // Configure Connector (usually done within archiva.xml configuration)
245 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
247 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
249 assertNotDownloaded( downloadedFile );
250 assertNotModified( expectedFile, expectedTimestamp );
251 assertNoTempFiles( expectedFile );
255 public void testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure()
258 String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
259 setupTestableManagedRepository( path );
261 Path expectedFile = managedDefaultDir.resolve(path);
262 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
264 Files.deleteIfExists(expectedFile);
265 assertFalse( Files.exists(expectedFile) );
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);
273 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
275 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
276 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
277 assertNoTempFiles( expectedFile );
281 public void testMetadataDrivenSnapshotNotPresentAlready()
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 );
287 Path expectedFile = managedDefaultDir.resolve(path);
288 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
290 Files.deleteIfExists(expectedFile);
291 assertFalse( Files.exists(expectedFile) );
293 // Configure Connector (usually done within archiva.xml configuration)
294 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
296 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
298 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
299 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
300 assertNoTempFiles( expectedFile );
304 public void testGetMetadataDrivenSnapshotRemoteUpdate()
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
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 );
313 Path expectedFile = managedDefaultDir.resolve(path);
314 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
316 assertTrue( Files.exists(expectedFile) );
318 Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
320 // Configure Connector (usually done within archiva.xml configuration)
321 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
323 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
325 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
326 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
327 assertNoTempFiles( expectedFile );