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