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.StorageAsset;
24 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.springframework.test.context.ContextConfiguration;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import java.nio.charset.Charset;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.attribute.FileTime;
36 import java.util.Properties;
43 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
44 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
45 public class ReleasePolicyTest
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#releases")
83 PreDownloadPolicy preDownloadPolicy;
85 private PreDownloadPolicy lookupPolicy()
88 return preDownloadPolicy;
92 public void testReleasePolicyDailyProjectMetadata()
95 // Pass the policy when working with metadata, no matter what.
96 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
97 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
99 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
100 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
101 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
103 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
104 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
105 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
109 public void testReleasePolicyDailyReleaseArtifact()
112 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
113 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
115 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
116 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
117 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
119 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
120 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
121 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
125 public void testReleasePolicyDailySnapshotArtifact()
128 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
129 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
131 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
132 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
133 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
135 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
136 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
137 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
141 public void testReleasePolicyDailyVersionedMetadata()
144 // Pass the policy when working with metadata, no matter what.
145 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
146 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
148 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
149 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
150 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
152 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
153 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
154 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
158 public void testReleasePolicyRejectProjectMetadata()
161 // Pass the policy when working with metadata, no matter what.
162 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
163 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
167 public void testReleasePolicyRejectReleaseArtifact()
170 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
171 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
175 public void testReleasePolicyRejectSnapshotArtifact()
178 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
179 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
183 public void testReleasePolicyRejectVersionedMetadata()
186 // Pass the policy when working with metadata, no matter what.
187 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
188 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
192 public void testReleasePolicyHourlyProjectMetadata()
195 // Pass the policy when working with metadata, no matter what.
196 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
197 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
199 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
200 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
201 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
203 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
204 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
205 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
209 public void testReleasePolicyHourlyReleaseArtifact()
212 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
213 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
215 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
216 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
217 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
219 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
220 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
221 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
225 public void testReleasePolicyHourlySnapshotArtifact()
228 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
229 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
231 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
232 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
233 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
235 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
236 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
237 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
241 public void testReleasePolicyHourlyVersionedMetadata()
244 // Pass the policy when working with metadata, no matter what.
245 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
246 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
248 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
249 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
250 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
252 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
253 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
254 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
258 public void testReleasePolicyAlwaysProjectMetadata()
261 // Pass the policy when working with metadata, no matter what.
262 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
263 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
267 public void testReleasePolicyAlwaysReleaseArtifact()
270 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
271 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
275 public void testReleasePolicyAlwaysSnapshotArtifact()
278 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
279 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
283 public void testReleasePolicyAlwaysVersionedMetadata()
286 // Pass the policy when working with metadata, no matter what.
287 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
288 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
292 public void testReleasePolicyOnceProjectMetadata()
295 // Pass the policy when working with metadata, no matter what.
296 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
297 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
301 public void testReleasePolicyOnceReleaseArtifact()
304 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
305 assertReleasesPolicyViolation( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
309 public void testReleasePolicyOnceSnapshotArtifact()
312 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
313 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
317 public void testReleasePolicyOnceVersionedMetadata()
320 // Pass the policy when working with metadata, no matter what.
321 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
322 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
325 private void assertReleasesPolicy( String setting, String path, boolean createLocalFile )
328 PreDownloadPolicy policy = lookupPolicy();
329 Properties request = new Properties();
330 request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
332 if ( path.contains( "1.0-SNAPSHOT" ) )
334 request.setProperty( "version", "1.0-SNAPSHOT" );
337 if ( path.contains( "2.0" ) )
339 request.setProperty( "version", "2.0" );
342 StorageAsset targetDir = ChecksumPolicyTest.getTestFile( "target/test-policy/" );
343 StorageAsset localFile = targetDir.resolve( path );
345 Files.deleteIfExists( localFile.getFilePath() );
347 if ( createLocalFile )
349 Files.createDirectories( localFile.getParent().getFilePath());
350 org.apache.archiva.common.utils.FileUtils.writeStringToFile( localFile.getFilePath(), FILE_ENCODING, "random-junk" );
351 Files.setLastModifiedTime( localFile.getFilePath(),
352 FileTime.fromMillis(Files.getLastModifiedTime(localFile.getFilePath()).toMillis() - generatedLocalFileUpdateDelta));
355 policy.applyPolicy( setting, request, localFile );
358 private void assertReleasesPolicyViolation( String setting, String path, boolean createLocalFile )
363 assertReleasesPolicy( setting, path, createLocalFile );
364 fail( "Expected a PolicyViolationException." );
366 catch ( PolicyViolationException e )
382 generatedLocalFileUpdateDelta = 0;