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;
28 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30 import javax.inject.Inject;
31 import javax.inject.Named;
33 import java.util.Properties;
40 @RunWith( value = SpringJUnit4ClassRunner.class )
41 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
42 public class SnapshotsPolicyTest
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#snapshots")
77 PreDownloadPolicy policy;
79 private PreDownloadPolicy lookupPolicy()
86 public void testSnapshotPolicyDailyProjectMetadata()
89 // Pass the policy when working with metadata, no matter what.
90 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
91 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
93 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
94 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
95 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
97 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
98 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
99 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
103 public void testSnapshotPolicyDailyReleaseArtifact()
106 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
107 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
109 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
110 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
111 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
113 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
114 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
115 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
119 public void testSnapshotPolicyDailySnapshotArtifact()
122 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
123 assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
125 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
126 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
127 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
129 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
130 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
131 assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
135 public void testSnapshotPolicyDailyVersionedMetadata()
138 // Pass the policy when working with metadata, no matter what.
139 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
140 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
142 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
143 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
144 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
146 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
147 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
148 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
152 public void testSnapshotPolicyRejectProjectMetadata()
155 // Pass the policy when working with metadata, no matter what.
156 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
157 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
161 public void testSnapshotPolicyRejectReleaseArtifact()
164 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
165 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
169 public void testSnapshotPolicyRejectSnapshotArtifact()
172 assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
173 assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
177 public void testSnapshotPolicyRejectVersionedMetadata()
180 // Pass the policy when working with metadata, no matter what.
181 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
182 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
186 public void testSnapshotPolicyHourlyProjectMetadata()
189 // Pass the policy when working with metadata, no matter what.
190 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
191 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
193 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
194 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
195 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
197 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
198 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
199 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
203 public void testSnapshotPolicyHourlyReleaseArtifact()
206 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
207 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
209 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
210 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
211 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
213 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
214 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
215 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
219 public void testSnapshotPolicyHourlySnapshotArtifact()
222 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
223 assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
225 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
226 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
227 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
229 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
230 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
231 assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
235 public void testSnapshotPolicyHourlyVersionedMetadata()
238 // Pass the policy when working with metadata, no matter what.
239 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
240 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
242 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
243 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
244 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
246 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
247 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
248 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
252 public void testSnapshotPolicyAlwaysProjectMetadata()
255 // Pass the policy when working with metadata, no matter what.
256 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
257 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
261 public void testSnapshotPolicyAlwaysReleaseArtifact()
264 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
265 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
269 public void testSnapshotPolicyAlwaysSnapshotArtifact()
272 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
273 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
277 public void testSnapshotPolicyAlwaysVersionedMetadata()
280 // Pass the policy when working with metadata, no matter what.
281 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
282 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
286 public void testSnapshotPolicyOnceProjectMetadata()
289 // Pass the policy when working with metadata, no matter what.
290 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
291 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
295 public void testSnapshotPolicyOnceReleaseArtifact()
298 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
299 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
303 public void testSnapshotPolicyOnceSnapshotArtifact()
306 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
307 assertSnapshotPolicyViolation( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
311 public void testSnapshotPolicyOnceVersionedMetadata()
314 // Pass the policy when working with metadata, no matter what.
315 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
316 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
319 private void assertSnapshotPolicy( 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 assertSnapshotPolicyViolation( String setting, String path, boolean createLocalFile )
359 assertSnapshotPolicy( setting, path, createLocalFile );
360 fail( "Expected a PolicyViolationException." );
362 catch ( PolicyViolationException e )
378 generatedLocalFileUpdateDelta = 0;