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.test.utils.ArchivaSpringJUnit4ClassRunner;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.springframework.test.context.ContextConfiguration;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import java.nio.charset.Charset;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.attribute.FileTime;
35 import java.util.Properties;
42 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
43 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
44 public class ReleasePolicyTest
48 private static final Charset FILE_ENCODING = Charset.forName( "UTF-8" );
50 private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
52 private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
54 private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
56 private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
58 private static final boolean WITH_LOCAL = true;
60 private static final boolean NO_LOCAL = false;
62 protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
64 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
66 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
68 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
70 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
72 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
74 protected static final long OLDER = ( -1 );
76 protected static final long NEWER = 0;
78 private long generatedLocalFileUpdateDelta = 0;
81 @Inject @Named(value = "preDownloadPolicy#releases")
82 PreDownloadPolicy preDownloadPolicy;
84 private PreDownloadPolicy lookupPolicy()
87 return preDownloadPolicy;
91 public void testReleasePolicyDailyProjectMetadata()
94 // Pass the policy when working with metadata, no matter what.
95 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
96 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
98 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
99 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
100 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
102 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
103 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
104 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
108 public void testReleasePolicyDailyReleaseArtifact()
111 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
112 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
114 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
115 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
116 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
118 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
119 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
120 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
124 public void testReleasePolicyDailySnapshotArtifact()
127 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
128 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
130 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
131 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
132 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
134 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
135 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
136 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
140 public void testReleasePolicyDailyVersionedMetadata()
143 // Pass the policy when working with metadata, no matter what.
144 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
145 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
147 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
148 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
149 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
151 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
152 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
153 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
157 public void testReleasePolicyRejectProjectMetadata()
160 // Pass the policy when working with metadata, no matter what.
161 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
162 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
166 public void testReleasePolicyRejectReleaseArtifact()
169 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
170 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
174 public void testReleasePolicyRejectSnapshotArtifact()
177 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
178 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
182 public void testReleasePolicyRejectVersionedMetadata()
185 // Pass the policy when working with metadata, no matter what.
186 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
187 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
191 public void testReleasePolicyHourlyProjectMetadata()
194 // Pass the policy when working with metadata, no matter what.
195 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
196 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
198 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
199 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
200 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
202 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
203 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
204 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
208 public void testReleasePolicyHourlyReleaseArtifact()
211 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
212 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
214 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
215 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
216 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
218 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
219 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
220 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
224 public void testReleasePolicyHourlySnapshotArtifact()
227 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
228 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
230 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
231 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
232 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
234 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
235 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
236 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
240 public void testReleasePolicyHourlyVersionedMetadata()
243 // Pass the policy when working with metadata, no matter what.
244 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
245 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
247 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
248 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
249 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
251 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
252 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
253 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
257 public void testReleasePolicyAlwaysProjectMetadata()
260 // Pass the policy when working with metadata, no matter what.
261 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
262 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
266 public void testReleasePolicyAlwaysReleaseArtifact()
269 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
270 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
274 public void testReleasePolicyAlwaysSnapshotArtifact()
277 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
278 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
282 public void testReleasePolicyAlwaysVersionedMetadata()
285 // Pass the policy when working with metadata, no matter what.
286 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
287 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
291 public void testReleasePolicyOnceProjectMetadata()
294 // Pass the policy when working with metadata, no matter what.
295 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
296 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
300 public void testReleasePolicyOnceReleaseArtifact()
303 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
304 assertReleasesPolicyViolation( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
308 public void testReleasePolicyOnceSnapshotArtifact()
311 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
312 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
316 public void testReleasePolicyOnceVersionedMetadata()
319 // Pass the policy when working with metadata, no matter what.
320 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
321 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
324 private void assertReleasesPolicy( String setting, String path, boolean createLocalFile )
327 PreDownloadPolicy policy = lookupPolicy();
328 Properties request = new Properties();
329 request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
331 if ( path.contains( "1.0-SNAPSHOT" ) )
333 request.setProperty( "version", "1.0-SNAPSHOT" );
336 if ( path.contains( "2.0" ) )
338 request.setProperty( "version", "2.0" );
341 Path targetDir = ChecksumPolicyTest.getTestFile( "target/test-policy/" );
342 Path localFile = targetDir.resolve( path );
344 Files.deleteIfExists( localFile );
346 if ( createLocalFile )
348 Files.createDirectories( localFile.getParent());
349 org.apache.archiva.common.utils.FileUtils.writeStringToFile( localFile, FILE_ENCODING, "random-junk" );
350 Files.setLastModifiedTime( localFile,
351 FileTime.fromMillis(Files.getLastModifiedTime(localFile).toMillis() - generatedLocalFileUpdateDelta));
354 policy.applyPolicy( setting, request, localFile );
357 private void assertReleasesPolicyViolation( String setting, String path, boolean createLocalFile )
362 assertReleasesPolicy( setting, path, createLocalFile );
363 fail( "Expected a PolicyViolationException." );
365 catch ( PolicyViolationException e )
381 generatedLocalFileUpdateDelta = 0;