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.commons.io.FileUtils;
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;
32 import java.util.Properties;
33 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
40 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
41 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
42 public class ReleasePolicyTest
45 private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
47 private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
49 private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
51 private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
53 private static final boolean WITH_LOCAL = true;
55 private static final boolean NO_LOCAL = false;
57 protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
59 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
61 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
63 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
65 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
67 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
69 protected static final long OLDER = ( -1 );
71 protected static final long NEWER = 0;
73 private long generatedLocalFileUpdateDelta = 0;
76 @Inject @Named(value = "preDownloadPolicy#releases")
77 PreDownloadPolicy preDownloadPolicy;
79 private PreDownloadPolicy lookupPolicy()
82 return preDownloadPolicy;
86 public void testReleasePolicyDailyProjectMetadata()
89 // Pass the policy when working with metadata, no matter what.
90 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
91 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
93 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
94 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
95 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
97 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
98 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
99 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
103 public void testReleasePolicyDailyReleaseArtifact()
106 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
107 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
109 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
110 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
111 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
113 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
114 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
115 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
119 public void testReleasePolicyDailySnapshotArtifact()
122 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
123 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
125 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
126 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
127 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
129 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
130 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
131 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
135 public void testReleasePolicyDailyVersionedMetadata()
138 // Pass the policy when working with metadata, no matter what.
139 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
140 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
142 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
143 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
144 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
146 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
147 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
148 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
152 public void testReleasePolicyRejectProjectMetadata()
155 // Pass the policy when working with metadata, no matter what.
156 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
157 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
161 public void testReleasePolicyRejectReleaseArtifact()
164 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
165 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
169 public void testReleasePolicyRejectSnapshotArtifact()
172 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
173 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
177 public void testReleasePolicyRejectVersionedMetadata()
180 // Pass the policy when working with metadata, no matter what.
181 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
182 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
186 public void testReleasePolicyHourlyProjectMetadata()
189 // Pass the policy when working with metadata, no matter what.
190 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
191 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
193 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
194 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
195 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
197 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
198 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
199 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
203 public void testReleasePolicyHourlyReleaseArtifact()
206 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
207 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
209 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
210 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
211 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
213 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
214 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
215 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
219 public void testReleasePolicyHourlySnapshotArtifact()
222 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
223 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
225 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
226 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
227 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
229 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
230 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
231 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
235 public void testReleasePolicyHourlyVersionedMetadata()
238 // Pass the policy when working with metadata, no matter what.
239 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
240 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
242 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
243 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
244 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
246 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
247 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
248 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
252 public void testReleasePolicyAlwaysProjectMetadata()
255 // Pass the policy when working with metadata, no matter what.
256 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
257 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
261 public void testReleasePolicyAlwaysReleaseArtifact()
264 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
265 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
269 public void testReleasePolicyAlwaysSnapshotArtifact()
272 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
273 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
277 public void testReleasePolicyAlwaysVersionedMetadata()
280 // Pass the policy when working with metadata, no matter what.
281 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
282 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
286 public void testReleasePolicyOnceProjectMetadata()
289 // Pass the policy when working with metadata, no matter what.
290 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
291 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
295 public void testReleasePolicyOnceReleaseArtifact()
298 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
299 assertReleasesPolicyViolation( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
303 public void testReleasePolicyOnceSnapshotArtifact()
306 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
307 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
311 public void testReleasePolicyOnceVersionedMetadata()
314 // Pass the policy when working with metadata, no matter what.
315 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
316 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
319 private void assertReleasesPolicy( String setting, String path, boolean createLocalFile )
322 PreDownloadPolicy policy = lookupPolicy();
323 Properties request = new Properties();
324 request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
326 if ( path.contains( "1.0-SNAPSHOT" ) )
328 request.setProperty( "version", "1.0-SNAPSHOT" );
331 if ( path.contains( "2.0" ) )
333 request.setProperty( "version", "2.0" );
336 File targetDir = ChecksumPolicyTest.getTestFile( "target/test-policy/" );
337 File localFile = new File( targetDir, path );
339 if ( localFile.exists() )
344 if ( createLocalFile )
346 localFile.getParentFile().mkdirs();
347 FileUtils.writeStringToFile( localFile, "random-junk" );
348 localFile.setLastModified( localFile.lastModified() - generatedLocalFileUpdateDelta );
351 policy.applyPolicy( setting, request, localFile );
354 private void assertReleasesPolicyViolation( String setting, String path, boolean createLocalFile )
359 assertReleasesPolicy( setting, path, createLocalFile );
360 fail( "Expected a PolicyViolationException." );
362 catch ( PolicyViolationException e )
377 generatedLocalFileUpdateDelta = 0;