]> source.dussan.org Git - archiva.git/blob
96f6bd899d68ffcee43da23fd734c2ed7f22cdae
[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  * SnapshotsPolicyTest 
30  *
31  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
32  * @version $Id$
33  */
34 public class SnapshotsPolicyTest
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 testSnapshotPolicyDailyProjectMetadata()
68         throws Exception
69     {
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 );
73
74         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
75         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
76         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
77
78         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
79         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
80         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
81     }
82
83     public void testSnapshotPolicyDailyReleaseArtifact()
84         throws Exception
85     {
86         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
87         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
88
89         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
90         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
91         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
92
93         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
94         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
95         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
96     }
97
98     public void testSnapshotPolicyDailySnapshotArtifact()
99         throws Exception
100     {
101         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
102         assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
103
104         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
105         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
106         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
107
108         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
109         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
110         assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
111     }
112
113     public void testSnapshotPolicyDailyVersionedMetadata()
114         throws Exception
115     {
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 );
119
120         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
121         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
122         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
123
124         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
125         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
126         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
127     }
128
129     public void testSnapshotPolicyDisabledProjectMetadata()
130         throws Exception
131     {
132         // Pass the policy when working with metadata, no matter what.
133         assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_PROJECT_METADATA, NO_LOCAL );
134         assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_PROJECT_METADATA, WITH_LOCAL );
135     }
136
137     public void testSnapshotPolicyDisabledReleaseArtifact()
138         throws Exception
139     {
140         assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_RELEASE_ARTIFACT, NO_LOCAL );
141         assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
142     }
143
144     public void testSnapshotPolicyDisabledSnapshotArtifact()
145         throws Exception
146     {
147         assertSnapshotPolicyViolation( SnapshotsPolicy.DISABLED, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
148         assertSnapshotPolicyViolation( SnapshotsPolicy.DISABLED, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
149     }
150
151     public void testSnapshotPolicyDisabledVersionedMetadata()
152         throws Exception
153     {
154         // Pass the policy when working with metadata, no matter what.
155         assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_VERSION_METADATA, NO_LOCAL );
156         assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_VERSION_METADATA, WITH_LOCAL );
157     }
158
159     public void testSnapshotPolicyHourlyProjectMetadata()
160         throws Exception
161     {
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 );
165
166         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
167         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
168         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
169
170         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
171         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
172         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
173     }
174
175     public void testSnapshotPolicyHourlyReleaseArtifact()
176         throws Exception
177     {
178         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
179         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
180
181         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
182         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
183         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
184
185         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
186         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
187         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
188     }
189
190     public void testSnapshotPolicyHourlySnapshotArtifact()
191         throws Exception
192     {
193         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
194         assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
195
196         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
197         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
198         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
199
200         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
201         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
202         assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
203     }
204
205     public void testSnapshotPolicyHourlyVersionedMetadata()
206         throws Exception
207     {
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 );
211
212         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
213         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
214         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
215
216         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
217         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
218         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
219     }
220
221     public void testSnapshotPolicyIgnoredProjectMetadata()
222         throws Exception
223     {
224         // Pass the policy when working with metadata, no matter what.
225         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_PROJECT_METADATA, NO_LOCAL );
226         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_PROJECT_METADATA, WITH_LOCAL );
227     }
228
229     public void testSnapshotPolicyIgnoredReleaseArtifact()
230         throws Exception
231     {
232         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_RELEASE_ARTIFACT, NO_LOCAL );
233         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
234     }
235
236     public void testSnapshotPolicyIgnoredSnapshotArtifact()
237         throws Exception
238     {
239         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
240         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
241     }
242
243     public void testSnapshotPolicyIgnoredVersionedMetadata()
244         throws Exception
245     {
246         // Pass the policy when working with metadata, no matter what.
247         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_VERSION_METADATA, NO_LOCAL );
248         assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_VERSION_METADATA, WITH_LOCAL );
249     }
250
251     public void testSnapshotPolicyOnceProjectMetadata()
252         throws Exception
253     {
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 );
257     }
258
259     public void testSnapshotPolicyOnceReleaseArtifact()
260         throws Exception
261     {
262         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
263         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
264     }
265
266     public void testSnapshotPolicyOnceSnapshotArtifact()
267         throws Exception
268     {
269         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
270         assertSnapshotPolicyViolation( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
271     }
272
273     public void testSnapshotPolicyOnceVersionedMetadata()
274         throws Exception
275     {
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 );
279     }
280
281     private void assertSnapshotPolicy( 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 assertSnapshotPolicyViolation( String setting, String path, boolean createLocalFile )
317         throws Exception
318     {
319         try
320         {
321             assertSnapshotPolicy( 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(), "snapshots" );
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 }