]> source.dussan.org Git - archiva.git/blob
3ede659be9c87492be2a908786849b862337ba89
[archiva.git] /
1 package org.apache.maven.archiva.policies;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.codehaus.plexus.PlexusTestCase;
23 import org.codehaus.plexus.util.FileUtils;
24
25 import java.io.File;
26 import java.util.Properties;
27
28 /**
29  * ReleasePolicyTest 
30  *
31  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
32  * @version $Id$
33  */
34 public class ReleasePolicyTest
35     extends PlexusTestCase
36 {
37     private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
38
39     private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
40
41     private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
42
43     private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
44
45     private static final boolean WITH_LOCAL = true;
46
47     private static final boolean NO_LOCAL = false;
48
49     protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
50
51     protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
52
53     protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
54
55     protected static final long ONE_DAY = ( ONE_HOUR * 24 );
56
57     protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
58
59     protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
60
61     protected static final long OLDER = ( -1 );
62
63     protected static final long NEWER = 0;
64
65     private long generatedLocalFileUpdateDelta = 0;
66
67     public void testReleasePolicyDailyProjectMetadata()
68         throws Exception
69     {
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 );
73
74         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
75         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
76         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
77
78         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
79         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
80         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
81     }
82
83     public void testReleasePolicyDailyReleaseArtifact()
84         throws Exception
85     {
86         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
87         assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
88
89         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
90         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
91         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
92
93         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
94         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
95         assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
96     }
97
98     public void testReleasePolicyDailySnapshotArtifact()
99         throws Exception
100     {
101         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
102         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
103
104         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
105         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
106         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
107
108         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
109         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
110         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
111     }
112
113     public void testReleasePolicyDailyVersionedMetadata()
114         throws Exception
115     {
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 );
119
120         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
121         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
122         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
123
124         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
125         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
126         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
127     }
128
129     public void testReleasePolicyRejectProjectMetadata()
130         throws Exception
131     {
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 );
135     }
136
137     public void testReleasePolicyRejectReleaseArtifact()
138         throws Exception
139     {
140         assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
141         assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
142     }
143
144     public void testReleasePolicyRejectSnapshotArtifact()
145         throws Exception
146     {
147         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
148         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
149     }
150
151     public void testReleasePolicyRejectVersionedMetadata()
152         throws Exception
153     {
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 );
157     }
158
159     public void testReleasePolicyHourlyProjectMetadata()
160         throws Exception
161     {
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 );
165
166         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
167         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
168         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
169
170         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
171         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
172         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
173     }
174
175     public void testReleasePolicyHourlyReleaseArtifact()
176         throws Exception
177     {
178         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
179         assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
180
181         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
182         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
183         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
184
185         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
186         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
187         assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
188     }
189
190     public void testReleasePolicyHourlySnapshotArtifact()
191         throws Exception
192     {
193         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
194         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
195
196         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
197         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
198         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
199
200         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
201         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
202         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
203     }
204
205     public void testReleasePolicyHourlyVersionedMetadata()
206         throws Exception
207     {
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 );
211
212         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
213         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
214         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
215
216         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
217         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
218         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
219     }
220
221     public void testReleasePolicyAlwaysProjectMetadata()
222         throws Exception
223     {
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 );
227     }
228
229     public void testReleasePolicyAlwaysReleaseArtifact()
230         throws Exception
231     {
232         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
233         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
234     }
235
236     public void testReleasePolicyAlwaysSnapshotArtifact()
237         throws Exception
238     {
239         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
240         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
241     }
242
243     public void testReleasePolicyAlwaysVersionedMetadata()
244         throws Exception
245     {
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 );
249     }
250
251     public void testReleasePolicyOnceProjectMetadata()
252         throws Exception
253     {
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 );
257     }
258
259     public void testReleasePolicyOnceReleaseArtifact()
260         throws Exception
261     {
262         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
263         assertReleasesPolicyViolation( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
264     }
265
266     public void testReleasePolicyOnceSnapshotArtifact()
267         throws Exception
268     {
269         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
270         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
271     }
272
273     public void testReleasePolicyOnceVersionedMetadata()
274         throws Exception
275     {
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 );
279     }
280
281     private void assertReleasesPolicy( String setting, String path, boolean createLocalFile )
282         throws Exception
283     {
284         PreDownloadPolicy policy = lookupPolicy();
285         Properties request = new Properties();
286         request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
287
288         if ( path.contains( "1.0-SNAPSHOT" ) )
289         {
290             request.setProperty( "version", "1.0-SNAPSHOT" );
291         }
292
293         if ( path.contains( "2.0" ) )
294         {
295             request.setProperty( "version", "2.0" );
296         }
297
298         File targetDir = getTestFile( "target/test-policy/" );
299         File localFile = new File( targetDir, path );
300
301         if ( localFile.exists() )
302         {
303             localFile.delete();
304         }
305
306         if ( createLocalFile )
307         {
308             localFile.getParentFile().mkdirs();
309             FileUtils.fileWrite( localFile.getAbsolutePath(), "random-junk" );
310             localFile.setLastModified( localFile.lastModified() - generatedLocalFileUpdateDelta );
311         }
312
313         policy.applyPolicy( setting, request, localFile );
314     }
315
316     private void assertReleasesPolicyViolation( String setting, String path, boolean createLocalFile )
317         throws Exception
318     {
319         try
320         {
321             assertReleasesPolicy( setting, path, createLocalFile );
322             fail( "Expected a PolicyViolationException." );
323         }
324         catch ( PolicyViolationException e )
325         {
326             // expected path.
327         }
328     }
329
330     private PreDownloadPolicy lookupPolicy()
331         throws Exception
332     {
333         PreDownloadPolicy policy = (PreDownloadPolicy) lookup( PreDownloadPolicy.class.getName(), "releases" );
334         assertNotNull( policy );
335         return policy;
336     }
337
338     @Override
339     protected void setUp()
340         throws Exception
341     {
342         super.setUp();
343
344         // reset delta to 0.
345         generatedLocalFileUpdateDelta = 0;
346     }
347 }