]> source.dussan.org Git - archiva.git/blob
d99d216d98d3dc77ee4ee591b470230b4e566d2b
[archiva.git] /
1 package org.apache.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 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;
29
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import java.io.File;
33 import java.util.Properties;
34
35 /**
36  * ReleasePolicyTest
37  *
38  * @version $Id$
39  */
40 @RunWith( value = SpringJUnit4ClassRunner.class )
41 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
42 public class ReleasePolicyTest
43     extends TestCase
44 {
45     private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
46
47     private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
48
49     private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
50
51     private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
52
53     private static final boolean WITH_LOCAL = true;
54
55     private static final boolean NO_LOCAL = false;
56
57     protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
58
59     protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
60
61     protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
62
63     protected static final long ONE_DAY = ( ONE_HOUR * 24 );
64
65     protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
66
67     protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
68
69     protected static final long OLDER = ( -1 );
70
71     protected static final long NEWER = 0;
72
73     private long generatedLocalFileUpdateDelta = 0;
74
75
76     @Inject @Named(value = "preDownloadPolicy#releases")
77     PreDownloadPolicy preDownloadPolicy;
78
79     private PreDownloadPolicy lookupPolicy()
80         throws Exception
81     {
82         return preDownloadPolicy;
83     }
84
85     @Test
86     public void testReleasePolicyDailyProjectMetadata()
87         throws Exception
88     {
89         // Pass the policy when working with metadata, no matter what.
90         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
91         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
92
93         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
94         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
95         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
96
97         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
98         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
99         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
100     }
101
102     @Test
103     public void testReleasePolicyDailyReleaseArtifact()
104         throws Exception
105     {
106         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
107         assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
108
109         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
110         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
111         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
112
113         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
114         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
115         assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
116     }
117
118     @Test
119     public void testReleasePolicyDailySnapshotArtifact()
120         throws Exception
121     {
122         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
123         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
124
125         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
126         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
127         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
128
129         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
130         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
131         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
132     }
133
134     @Test
135     public void testReleasePolicyDailyVersionedMetadata()
136         throws Exception
137     {
138         // Pass the policy when working with metadata, no matter what.
139         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
140         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
141
142         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
143         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
144         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
145
146         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
147         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
148         assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
149     }
150
151     @Test
152     public void testReleasePolicyRejectProjectMetadata()
153         throws Exception
154     {
155         // Pass the policy when working with metadata, no matter what.
156         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
157         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
158     }
159
160     @Test
161     public void testReleasePolicyRejectReleaseArtifact()
162         throws Exception
163     {
164         assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
165         assertReleasesPolicyViolation( ReleasesPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
166     }
167
168     @Test
169     public void testReleasePolicyRejectSnapshotArtifact()
170         throws Exception
171     {
172         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
173         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
174     }
175
176     @Test
177     public void testReleasePolicyRejectVersionedMetadata()
178         throws Exception
179     {
180         // Pass the policy when working with metadata, no matter what.
181         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
182         assertReleasesPolicy( ReleasesPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
183     }
184
185     @Test
186     public void testReleasePolicyHourlyProjectMetadata()
187         throws Exception
188     {
189         // Pass the policy when working with metadata, no matter what.
190         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
191         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
192
193         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
194         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
195         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
196
197         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
198         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
199         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
200     }
201
202     @Test
203     public void testReleasePolicyHourlyReleaseArtifact()
204         throws Exception
205     {
206         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
207         assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
208
209         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
210         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
211         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
212
213         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
214         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
215         assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
216     }
217
218     @Test
219     public void testReleasePolicyHourlySnapshotArtifact()
220         throws Exception
221     {
222         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
223         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
224
225         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
226         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
227         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
228
229         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
230         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
231         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
232     }
233
234     @Test
235     public void testReleasePolicyHourlyVersionedMetadata()
236         throws Exception
237     {
238         // Pass the policy when working with metadata, no matter what.
239         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
240         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
241
242         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
243         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
244         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
245
246         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
247         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
248         assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
249     }
250
251     @Test
252     public void testReleasePolicyAlwaysProjectMetadata()
253         throws Exception
254     {
255         // Pass the policy when working with metadata, no matter what.
256         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
257         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
258     }
259
260     @Test
261     public void testReleasePolicyAlwaysReleaseArtifact()
262         throws Exception
263     {
264         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
265         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
266     }
267
268     @Test
269     public void testReleasePolicyAlwaysSnapshotArtifact()
270         throws Exception
271     {
272         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
273         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
274     }
275
276     @Test
277     public void testReleasePolicyAlwaysVersionedMetadata()
278         throws Exception
279     {
280         // Pass the policy when working with metadata, no matter what.
281         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
282         assertReleasesPolicy( ReleasesPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
283     }
284
285     @Test
286     public void testReleasePolicyOnceProjectMetadata()
287         throws Exception
288     {
289         // Pass the policy when working with metadata, no matter what.
290         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
291         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
292     }
293
294     @Test
295     public void testReleasePolicyOnceReleaseArtifact()
296         throws Exception
297     {
298         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
299         assertReleasesPolicyViolation( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
300     }
301
302     @Test
303     public void testReleasePolicyOnceSnapshotArtifact()
304         throws Exception
305     {
306         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
307         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
308     }
309
310     @Test
311     public void testReleasePolicyOnceVersionedMetadata()
312         throws Exception
313     {
314         // Pass the policy when working with metadata, no matter what.
315         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
316         assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
317     }
318
319     private void assertReleasesPolicy( String setting, String path, boolean createLocalFile )
320         throws Exception
321     {
322         PreDownloadPolicy policy = lookupPolicy();
323         Properties request = new Properties();
324         request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
325
326         if ( path.contains( "1.0-SNAPSHOT" ) )
327         {
328             request.setProperty( "version", "1.0-SNAPSHOT" );
329         }
330
331         if ( path.contains( "2.0" ) )
332         {
333             request.setProperty( "version", "2.0" );
334         }
335
336         File targetDir = ChecksumPolicyTest.getTestFile( "target/test-policy/" );
337         File localFile = new File( targetDir, path );
338
339         if ( localFile.exists() )
340         {
341             localFile.delete();
342         }
343
344         if ( createLocalFile )
345         {
346             localFile.getParentFile().mkdirs();
347             FileUtils.writeStringToFile( localFile, "random-junk" );
348             localFile.setLastModified( localFile.lastModified() - generatedLocalFileUpdateDelta );
349         }
350
351         policy.applyPolicy( setting, request, localFile );
352     }
353
354     private void assertReleasesPolicyViolation( String setting, String path, boolean createLocalFile )
355         throws Exception
356     {
357         try
358         {
359             assertReleasesPolicy( setting, path, createLocalFile );
360             fail( "Expected a PolicyViolationException." );
361         }
362         catch ( PolicyViolationException e )
363         {
364             // expected path.
365         }
366     }
367
368
369
370     @Before
371     public void setUp()
372         throws Exception
373     {
374         super.setUp();
375
376         // reset delta to 0.
377         generatedLocalFileUpdateDelta = 0;
378     }
379 }