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.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;
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;
37 import static org.junit.Assert.*;
40 * SnapshotTransferTest
44 public class SnapshotTransferTest
45 extends AbstractProxyTestCase
48 public void testSnapshotNonExistant()
51 String path = "org/apache/maven/test/does-not-exist/1.0-SNAPSHOT/does-not-exist-1.0-SNAPSHOT.jar";
52 setupTestableManagedRepository( path );
54 Path expectedFile = managedDefaultDir.resolve(path);
55 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
56 Artifact artifact = layout.getArtifact( path );
58 Files.deleteIfExists(expectedFile);
59 assertFalse( Files.exists(expectedFile) );
61 // Configure Connector (usually done within archiva.xml configuration)
62 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
64 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
65 assertNotDownloaded( downloadedFile );
66 assertNoTempFiles( expectedFile );
70 public void testTimestampDrivenSnapshotNotPresentAlready()
73 String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
74 setupTestableManagedRepository( path );
76 Path expectedFile = managedDefaultDir.resolve(path);
77 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
79 Artifact artifact = layout.getArtifact( path );
81 Files.deleteIfExists(expectedFile);
82 assertFalse( Files.exists(expectedFile) );
84 // Configure Connector (usually done within archiva.xml configuration)
85 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
87 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
89 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
90 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
91 assertNoTempFiles( expectedFile );
95 public void testNewerTimestampDrivenSnapshotOnFirstRepo()
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 );
101 Path expectedFile = managedDefaultDir.resolve(path);
102 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
104 Artifact artifact = layout.getArtifact( path );
106 assertTrue( Files.exists(expectedFile) );
107 Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
109 // Configure Connector (usually done within archiva.xml configuration)
110 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
112 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
114 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
115 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
116 assertNoTempFiles( expectedFile );
120 public void testOlderTimestampDrivenSnapshotOnFirstRepo()
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 );
126 Path expectedFile = managedDefaultDir.resolve(path);
127 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
129 setManagedNewerThanRemote( expectedFile, remoteFile );
131 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
133 Artifact artifact = layout.getArtifact( path );
135 // Configure Connector (usually done within archiva.xml configuration)
136 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
138 // Attempt to download.
139 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
141 // Should not have downloaded as managed is newer than remote.
142 assertNotDownloaded( downloadedFile );
143 assertNoTempFiles( expectedFile );
147 * TODO: Has problems with wagon implementation not preserving timestamp.
150 public void testNewerTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
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 );
156 Path expectedFile = managedDefaultDir.resolve(path);
157 ArtifactReference artifact = createArtifactReference( "default", path );
159 Files.delete(expectedFile);
160 assertFalse( Files.exists(expectedFile) );
162 // Create customized proxy / target repository
163 File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED1_TARGET, REPOPATH_PROXIED1,
164 REPOPATH_PROXIED1_TARGET, "default" );
166 new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
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 );
174 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
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 );
182 public void testOlderTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
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 );
188 Path expectedFile = managedDefaultDir.resolve(path);
189 ArtifactReference artifact = createArtifactReference( "default", path );
191 Files.delete(expectedFile);
192 assertFalse( Files.exists(expectedFile) );
194 // Create customized proxy / target repository
195 File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED2_TARGET, REPOPATH_PROXIED2,
196 REPOPATH_PROXIED2_TARGET, "default" );
198 new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
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 );
206 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
208 File proxiedFile = new File( REPOPATH_PROXIED1_TARGET, path );
209 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
210 assertNoTempFiles( expectedFile );
214 public void testTimestampDrivenSnapshotNotExpired()
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 );
220 Path expectedFile = managedDefaultDir.resolve(path);
221 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
222 Artifact artifact = layout.getArtifact( path );
224 assertTrue( Files.exists(expectedFile) );
226 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
227 Files.setLastModifiedTime( proxiedFile, FileTime.from( getFutureDate().getTime(), TimeUnit.MILLISECONDS ));
229 // Configure Connector (usually done within archiva.xml configuration)
230 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
232 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
234 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
235 assertNoTempFiles( expectedFile );
239 public void testTimestampDrivenSnapshotNotUpdated()
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 );
245 Path expectedFile = managedDefaultDir.resolve(path);
246 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
248 setManagedNewerThanRemote( expectedFile, remoteFile, 12000000 );
249 long expectedTimestamp = Files.getLastModifiedTime( expectedFile ).toMillis();
251 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
253 Artifact artifact = layout.getArtifact( path );
255 // Configure Connector (usually done within archiva.xml configuration)
256 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
258 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
260 assertNotDownloaded( downloadedFile );
261 assertNotModified( expectedFile, expectedTimestamp );
262 assertNoTempFiles( expectedFile );
266 public void testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure()
269 String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
270 setupTestableManagedRepository( path );
272 Path expectedFile = managedDefaultDir.resolve(path);
274 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
276 Artifact artifact = layout.getArtifact( path );
278 Files.deleteIfExists(expectedFile);
279 assertFalse( Files.exists(expectedFile) );
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);
287 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
289 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
290 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
291 assertNoTempFiles( expectedFile );
295 public void testMetadataDrivenSnapshotNotPresentAlready()
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 );
301 Path expectedFile = managedDefaultDir.resolve(path);
303 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
305 Artifact artifact = layout.getArtifact( path );
307 Files.deleteIfExists(expectedFile);
308 assertFalse( Files.exists(expectedFile) );
310 // Configure Connector (usually done within archiva.xml configuration)
311 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
313 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
315 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
316 assertNotNull( downloadedFile );
317 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
318 assertNoTempFiles( expectedFile );
322 public void testGetMetadataDrivenSnapshotRemoteUpdate()
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
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 );
331 Path expectedFile = managedDefaultDir.resolve(path);
333 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
335 Artifact artifact = layout.getArtifact( path );
337 assertTrue( Files.exists(expectedFile) );
339 Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
341 // Configure Connector (usually done within archiva.xml configuration)
342 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
344 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
346 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
347 assertNotNull( downloadedFile );
348 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
349 assertNoTempFiles( expectedFile );