1 package org.apache.archiva.policies;
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 junit.framework.TestCase;
23 import org.apache.archiva.repository.storage.FilesystemStorage;
24 import org.apache.archiva.repository.storage.StorageAsset;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.springframework.test.context.ContextConfiguration;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import java.nio.charset.Charset;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.attribute.FileTime;
37 import java.util.Properties;
44 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
46 public class SnapshotsPolicyTest
49 private static final Charset FILE_ENCODING = Charset.forName( "UTF-8" );
51 private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
53 private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
55 private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
57 private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
59 private static final boolean WITH_LOCAL = true;
61 private static final boolean NO_LOCAL = false;
63 protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
65 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
67 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
69 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
71 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
73 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
75 protected static final long OLDER = ( -1 );
77 protected static final long NEWER = 0;
79 private long generatedLocalFileUpdateDelta = 0;
82 @Inject @Named(value="preDownloadPolicy#snapshots")
83 PreDownloadPolicy policy;
85 private FilesystemStorage filesystemStorage;
87 private PreDownloadPolicy lookupPolicy()
94 public void testSnapshotPolicyDailyProjectMetadata()
97 // Pass the policy when working with metadata, no matter what.
98 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
99 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
101 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
102 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
103 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
105 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
106 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
107 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
111 public void testSnapshotPolicyDailyReleaseArtifact()
114 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
115 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
117 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
118 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
119 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
121 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
122 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
123 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
127 public void testSnapshotPolicyDailySnapshotArtifact()
130 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
131 assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
133 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
134 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
135 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
137 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
138 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
139 assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
143 public void testSnapshotPolicyDailyVersionedMetadata()
146 // Pass the policy when working with metadata, no matter what.
147 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
148 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
150 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
151 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
152 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
154 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
155 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
156 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
160 public void testSnapshotPolicyRejectProjectMetadata()
163 // Pass the policy when working with metadata, no matter what.
164 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
165 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
169 public void testSnapshotPolicyRejectReleaseArtifact()
172 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
173 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
177 public void testSnapshotPolicyRejectSnapshotArtifact()
180 assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
181 assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
185 public void testSnapshotPolicyRejectVersionedMetadata()
188 // Pass the policy when working with metadata, no matter what.
189 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
190 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
194 public void testSnapshotPolicyHourlyProjectMetadata()
197 // Pass the policy when working with metadata, no matter what.
198 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
199 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
201 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
202 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
203 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
205 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
206 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
207 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
211 public void testSnapshotPolicyHourlyReleaseArtifact()
214 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
215 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
217 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
218 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
219 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
221 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
222 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
223 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
227 public void testSnapshotPolicyHourlySnapshotArtifact()
230 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
231 assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
233 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
234 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
235 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
237 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
238 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
239 assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
243 public void testSnapshotPolicyHourlyVersionedMetadata()
246 // Pass the policy when working with metadata, no matter what.
247 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
248 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
250 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
251 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
252 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
254 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
255 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
256 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
260 public void testSnapshotPolicyAlwaysProjectMetadata()
263 // Pass the policy when working with metadata, no matter what.
264 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
265 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
269 public void testSnapshotPolicyAlwaysReleaseArtifact()
272 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
273 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
277 public void testSnapshotPolicyAlwaysSnapshotArtifact()
280 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
281 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
285 public void testSnapshotPolicyAlwaysVersionedMetadata()
288 // Pass the policy when working with metadata, no matter what.
289 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
290 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
294 public void testSnapshotPolicyOnceProjectMetadata()
297 // Pass the policy when working with metadata, no matter what.
298 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
299 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
303 public void testSnapshotPolicyOnceReleaseArtifact()
306 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
307 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
311 public void testSnapshotPolicyOnceSnapshotArtifact()
314 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
315 assertSnapshotPolicyViolation( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
319 public void testSnapshotPolicyOnceVersionedMetadata()
322 // Pass the policy when working with metadata, no matter what.
323 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
324 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
327 private void assertSnapshotPolicy( String setting, String path, boolean createLocalFile )
330 PreDownloadPolicy policy = lookupPolicy();
331 Properties request = new Properties();
332 request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
334 if ( path.contains( "1.0-SNAPSHOT" ) )
336 request.setProperty( "version", "1.0-SNAPSHOT" );
339 if ( path.contains( "2.0" ) )
341 request.setProperty( "version", "2.0" );
344 StorageAsset targetDir = ChecksumPolicyTest.getTestFile( "target/test-policy/" );
345 StorageAsset localFile = targetDir.resolve( path );
347 Files.deleteIfExists( localFile.getFilePath() );
349 if ( createLocalFile )
351 Files.createDirectories( localFile.getParent().getFilePath() );
352 org.apache.archiva.common.utils.FileUtils.writeStringToFile( localFile.getFilePath(), FILE_ENCODING, "random-junk" );
353 Files.setLastModifiedTime( localFile.getFilePath(),
354 FileTime.fromMillis( Files.getLastModifiedTime( localFile.getFilePath() ).toMillis() - generatedLocalFileUpdateDelta ));
357 policy.applyPolicy( setting, request, localFile );
360 private void assertSnapshotPolicyViolation( String setting, String path, boolean createLocalFile )
365 assertSnapshotPolicy( setting, path, createLocalFile );
366 fail( "Expected a PolicyViolationException." );
368 catch ( PolicyViolationException e )
384 generatedLocalFileUpdateDelta = 0;