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