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 ReleasePolicyTest
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 testReleasePolicyDailyProjectMetadata()
70 // Pass the policy when working with metadata, no matter what.
71 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
72 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
74 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
75 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
76 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
78 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
79 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
80 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
83 public void testReleasePolicyDailyReleaseArtifact()
86 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
87 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
89 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
90 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
91 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
93 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
94 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
95 assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
98 public void testReleasePolicyDailySnapshotArtifact()
101 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
102 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
104 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
105 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
106 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
108 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
109 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
110 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
113 public void testReleasePolicyDailyVersionedMetadata()
116 // Pass the policy when working with metadata, no matter what.
117 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
118 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
120 generatedLocalFileUpdateDelta = OVER_ONE_DAY;
121 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
122 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
124 generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
125 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
126 assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
129 public void testReleasePolicyRejectProjectMetadata()
132 // Pass the policy when working with metadata, no matter what.
133 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
134 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
137 public void testReleasePolicyRejectReleaseArtifact()
140 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
141 assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
144 public void testReleasePolicyRejectSnapshotArtifact()
147 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
148 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
151 public void testReleasePolicyRejectVersionedMetadata()
154 // Pass the policy when working with metadata, no matter what.
155 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
156 assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
159 public void testReleasePolicyHourlyProjectMetadata()
162 // Pass the policy when working with metadata, no matter what.
163 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
164 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
166 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
167 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
168 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
170 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
171 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
172 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
175 public void testReleasePolicyHourlyReleaseArtifact()
178 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
179 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
181 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
182 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
183 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
185 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
186 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
187 assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
190 public void testReleasePolicyHourlySnapshotArtifact()
193 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
194 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
196 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
197 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
198 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
200 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
201 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
202 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
205 public void testReleasePolicyHourlyVersionedMetadata()
208 // Pass the policy when working with metadata, no matter what.
209 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
210 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
212 generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
213 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
214 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
216 generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
217 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
218 assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
221 public void testReleasePolicyAlwaysProjectMetadata()
224 // Pass the policy when working with metadata, no matter what.
225 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
226 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
229 public void testReleasePolicyAlwaysReleaseArtifact()
232 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
233 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
236 public void testReleasePolicyAlwaysSnapshotArtifact()
239 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
240 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
243 public void testReleasePolicyAlwaysVersionedMetadata()
246 // Pass the policy when working with metadata, no matter what.
247 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
248 assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
251 public void testReleasePolicyOnceProjectMetadata()
254 // Pass the policy when working with metadata, no matter what.
255 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
256 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
259 public void testReleasePolicyOnceReleaseArtifact()
262 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
263 assertReleasesPolicyViolation( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
266 public void testReleasePolicyOnceSnapshotArtifact()
269 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
270 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
273 public void testReleasePolicyOnceVersionedMetadata()
276 // Pass the policy when working with metadata, no matter what.
277 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
278 assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
281 private void assertReleasesPolicy( 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 assertReleasesPolicyViolation( String setting, String path, boolean createLocalFile )
321 assertReleasesPolicy( setting, path, createLocalFile );
322 fail( "Expected a PolicyViolationException." );
324 catch ( PolicyViolationException e )
330 private PreDownloadPolicy lookupPolicy()
333 PreDownloadPolicy policy = (PreDownloadPolicy) lookup( PreDownloadPolicy.class.getName(), "releases" );
334 assertNotNull( policy );
339 protected void setUp()
345 generatedLocalFileUpdateDelta = 0;