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