1 package org.apache.maven.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 org.codehaus.plexus.PlexusTestCase;
23 import org.codehaus.plexus.util.FileUtils;
26 import java.util.Properties;
31 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
34 public class SnapshotsPolicyTest
35 extends PlexusTestCase
37 private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
39 private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
41 private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
43 private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
45 private static final boolean WITH_LOCAL = true;
47 private static final boolean NO_LOCAL = false;
49 protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
51 protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
53 protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
55 protected static final long ONE_DAY = ( ONE_HOUR * 24 );
57 protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
59 protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
61 protected static final long OLDER = ( -1 );
63 protected static final long NEWER = 0;
65 private long generatedLocalFileUpdateDelta = 0;
67 public void testSnapshotPolicyDailyProjectMetadata()
70 // Pass the policy when working with metadata, no matter what.
71 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
72 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
74 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
75 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
76 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
78 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
79 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
80 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
83 public void testSnapshotPolicyDailyReleaseArtifact()
86 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
87 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
89 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
90 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
91 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
93 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
94 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
95 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
98 public void testSnapshotPolicyDailySnapshotArtifact()
101 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
102 assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
104 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
105 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
106 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
108 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
109 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
110 assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
113 public void testSnapshotPolicyDailyVersionedMetadata()
116 // Pass the policy when working with metadata, no matter what.
117 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
118 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
120 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
121 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
122 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
124 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
125 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
126 assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
129 public void testSnapshotPolicyRejectProjectMetadata()
132 // Pass the policy when working with metadata, no matter what.
133 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
134 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
137 public void testSnapshotPolicyRejectReleaseArtifact()
140 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
141 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
144 public void testSnapshotPolicyRejectSnapshotArtifact()
147 assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
148 assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
151 public void testSnapshotPolicyRejectVersionedMetadata()
154 // Pass the policy when working with metadata, no matter what.
155 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
156 assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
159 public void testSnapshotPolicyHourlyProjectMetadata()
162 // Pass the policy when working with metadata, no matter what.
163 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
164 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
166 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
167 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
168 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
170 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
171 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
172 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
175 public void testSnapshotPolicyHourlyReleaseArtifact()
178 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
179 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
181 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
182 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
183 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
185 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
186 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
187 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
190 public void testSnapshotPolicyHourlySnapshotArtifact()
193 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
194 assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
196 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
197 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
198 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
200 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
201 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
202 assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
205 public void testSnapshotPolicyHourlyVersionedMetadata()
208 // Pass the policy when working with metadata, no matter what.
209 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
210 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
212 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
213 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
214 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
216 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
217 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
218 assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
221 public void testSnapshotPolicyAlwaysProjectMetadata()
224 // Pass the policy when working with metadata, no matter what.
225 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
226 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
229 public void testSnapshotPolicyAlwaysReleaseArtifact()
232 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
233 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
236 public void testSnapshotPolicyAlwaysSnapshotArtifact()
239 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
240 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
243 public void testSnapshotPolicyAlwaysVersionedMetadata()
246 // Pass the policy when working with metadata, no matter what.
247 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
248 assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
251 public void testSnapshotPolicyOnceProjectMetadata()
254 // Pass the policy when working with metadata, no matter what.
255 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
256 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
259 public void testSnapshotPolicyOnceReleaseArtifact()
262 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
263 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
266 public void testSnapshotPolicyOnceSnapshotArtifact()
269 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
270 assertSnapshotPolicyViolation( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
273 public void testSnapshotPolicyOnceVersionedMetadata()
276 // Pass the policy when working with metadata, no matter what.
277 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
278 assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
281 private void assertSnapshotPolicy( String setting, String path, boolean createLocalFile )
284 PreDownloadPolicy policy = lookupPolicy();
285 Properties request = new Properties();
286 request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
288 if ( path.contains( "1.0-SNAPSHOT" ) )
290 request.setProperty( "version", "1.0-SNAPSHOT" );
293 if ( path.contains( "2.0" ) )
295 request.setProperty( "version", "2.0" );
298 File targetDir = getTestFile( "target/test-policy/" );
299 File localFile = new File( targetDir, path );
301 if ( localFile.exists() )
306 if ( createLocalFile )
308 localFile.getParentFile().mkdirs();
309 FileUtils.fileWrite( localFile.getAbsolutePath(), "random-junk" );
310 localFile.setLastModified( localFile.lastModified() - generatedLocalFileUpdateDelta );
313 policy.applyPolicy( setting, request, localFile );
316 private void assertSnapshotPolicyViolation( String setting, String path, boolean createLocalFile )
321 assertSnapshotPolicy( setting, path, createLocalFile );
322 fail( "Expected a PolicyViolationException." );
324 catch ( PolicyViolationException e )
330 private PreDownloadPolicy lookupPolicy()
333 PreDownloadPolicy policy = (PreDownloadPolicy) lookup( PreDownloadPolicy.class.getName(), "snapshots" );
334 assertNotNull( policy );
339 protected void setUp()
345 generatedLocalFileUpdateDelta = 0;