]> source.dussan.org Git - archiva.git/blob
50fec5799c4cab00ea76ef5bc8b4764d48f0cb8b
[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
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import java.io.File;
32 import java.util.Properties;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34
35 /**
36  * SnapshotsPolicyTest 
37  *
38  *
39  */
40 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
41 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
42 public class SnapshotsPolicyTest
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#snapshots")
77     PreDownloadPolicy policy;
78
79     private PreDownloadPolicy lookupPolicy()
80         throws Exception
81     {
82         return policy;
83     }
84
85     @Test
86     public void testSnapshotPolicyDailyProjectMetadata()
87         throws Exception
88     {
89         // Pass the policy when working with metadata, no matter what.
90         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
91         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
92
93         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
94         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
95         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
96
97         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
98         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
99         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
100     }
101
102     @Test
103     public void testSnapshotPolicyDailyReleaseArtifact()
104         throws Exception
105     {
106         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
107         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
108
109         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
110         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
111         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
112
113         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
114         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
115         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
116     }
117
118     @Test
119     public void testSnapshotPolicyDailySnapshotArtifact()
120         throws Exception
121     {
122         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
123         assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
124
125         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
126         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
127         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
128
129         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
130         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
131         assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
132     }
133
134     @Test
135     public void testSnapshotPolicyDailyVersionedMetadata()
136         throws Exception
137     {
138         // Pass the policy when working with metadata, no matter what.
139         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
140         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
141
142         generatedLocalFileUpdateDelta = OVER_ONE_DAY;
143         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
144         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
145
146         generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
147         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
148         assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
149     }
150
151     @Test
152     public void testSnapshotPolicyRejectProjectMetadata()
153         throws Exception
154     {
155         // Pass the policy when working with metadata, no matter what.
156         assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, NO_LOCAL );
157         assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_PROJECT_METADATA, WITH_LOCAL );
158     }
159
160     @Test
161     public void testSnapshotPolicyRejectReleaseArtifact()
162         throws Exception
163     {
164         assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, NO_LOCAL );
165         assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
166     }
167
168     @Test
169     public void testSnapshotPolicyRejectSnapshotArtifact()
170         throws Exception
171     {
172         assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
173         assertSnapshotPolicyViolation( SnapshotsPolicy.NEVER, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
174     }
175
176     @Test
177     public void testSnapshotPolicyRejectVersionedMetadata()
178         throws Exception
179     {
180         // Pass the policy when working with metadata, no matter what.
181         assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, NO_LOCAL );
182         assertSnapshotPolicy( SnapshotsPolicy.NEVER, PATH_VERSION_METADATA, WITH_LOCAL );
183     }
184
185     @Test
186     public void testSnapshotPolicyHourlyProjectMetadata()
187         throws Exception
188     {
189         // Pass the policy when working with metadata, no matter what.
190         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
191         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
192
193         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
194         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
195         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
196
197         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
198         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
199         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
200     }
201
202     @Test
203     public void testSnapshotPolicyHourlyReleaseArtifact()
204         throws Exception
205     {
206         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
207         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
208
209         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
210         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
211         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
212
213         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
214         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
215         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
216     }
217
218     @Test
219     public void testSnapshotPolicyHourlySnapshotArtifact()
220         throws Exception
221     {
222         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
223         assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
224
225         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
226         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
227         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
228
229         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
230         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
231         assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
232     }
233
234     @Test
235     public void testSnapshotPolicyHourlyVersionedMetadata()
236         throws Exception
237     {
238         // Pass the policy when working with metadata, no matter what.
239         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
240         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
241
242         generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
243         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
244         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
245
246         generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
247         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
248         assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
249     }
250
251     @Test
252     public void testSnapshotPolicyAlwaysProjectMetadata()
253         throws Exception
254     {
255         // Pass the policy when working with metadata, no matter what.
256         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, NO_LOCAL );
257         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_PROJECT_METADATA, WITH_LOCAL );
258     }
259
260     @Test
261     public void testSnapshotPolicyAlwaysReleaseArtifact()
262         throws Exception
263     {
264         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, NO_LOCAL );
265         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
266     }
267
268     @Test
269     public void testSnapshotPolicyAlwaysSnapshotArtifact()
270         throws Exception
271     {
272         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
273         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
274     }
275
276     @Test
277     public void testSnapshotPolicyAlwaysVersionedMetadata()
278         throws Exception
279     {
280         // Pass the policy when working with metadata, no matter what.
281         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, NO_LOCAL );
282         assertSnapshotPolicy( SnapshotsPolicy.ALWAYS, PATH_VERSION_METADATA, WITH_LOCAL );
283     }
284
285     @Test
286     public void testSnapshotPolicyOnceProjectMetadata()
287         throws Exception
288     {
289         // Pass the policy when working with metadata, no matter what.
290         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
291         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
292     }
293
294     @Test
295     public void testSnapshotPolicyOnceReleaseArtifact()
296         throws Exception
297     {
298         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
299         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
300     }
301
302     @Test
303     public void testSnapshotPolicyOnceSnapshotArtifact()
304         throws Exception
305     {
306         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
307         assertSnapshotPolicyViolation( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
308     }
309
310     @Test
311     public void testSnapshotPolicyOnceVersionedMetadata()
312         throws Exception
313     {
314         // Pass the policy when working with metadata, no matter what.
315         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
316         assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
317     }
318
319     private void assertSnapshotPolicy( 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 assertSnapshotPolicyViolation( String setting, String path, boolean createLocalFile )
355         throws Exception
356     {
357         try
358         {
359             assertSnapshotPolicy( setting, path, createLocalFile );
360             fail( "Expected a PolicyViolationException." );
361         }
362         catch ( PolicyViolationException e )
363         {
364             // expected path.
365         }
366     }
367
368
369
370     @Override
371     @Before
372     public void setUp()
373         throws Exception
374     {
375         super.setUp();
376
377         // reset delta to 0.
378         generatedLocalFileUpdateDelta = 0;
379     }
380 }