]> source.dussan.org Git - archiva.git/blob
c322e2357fc2fa43c31fe751b1abfe8ea9b788c8
[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.policies.urlcache.UrlFailureCache;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.springframework.test.context.ContextConfiguration;
27
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import java.io.File;
31 import java.util.Properties;
32 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
33
34 /**
35  * CachedFailuresPolicyTest
36  *
37  * @version $Id$
38  */
39 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
40 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
41 public class CachedFailuresPolicyTest
42     extends TestCase
43 {
44
45
46     @Inject
47     private UrlFailureCache urlFailureCache;
48
49     @Inject
50     @Named( value = "preDownloadPolicy#cache-failures" )
51     DownloadPolicy downloadPolicy;
52
53     private DownloadPolicy lookupPolicy()
54         throws Exception
55     {
56         return downloadPolicy;
57     }
58
59     private File getFile()
60     {
61         return new File( "target/cache-failures/" + getName() + ".txt" );
62     }
63
64     private Properties createRequest()
65     {
66         Properties request = new Properties();
67
68         return request;
69     }
70
71     @Test
72     public void testPolicyNo()
73         throws Exception
74     {
75         DownloadPolicy policy = lookupPolicy();
76         File localFile = getFile();
77         Properties request = createRequest();
78
79         request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
80
81         policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
82     }
83
84     @Test
85     public void testPolicyYes()
86         throws Exception
87     {
88
89         DownloadPolicy policy = lookupPolicy();
90         File localFile = getFile();
91         Properties request = createRequest();
92
93         request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
94
95         // should not fail
96         policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
97         // status Yes Not In cache
98
99         // Yes in Cache
100         String url = "http://a.bad.hostname.maven.org/path/to/resource.txt";
101
102         urlFailureCache.cacheFailure( url );
103
104         request.setProperty( "url", url );
105
106         try
107         {
108             policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
109             fail( "Expected a PolicyViolationException." );
110         }
111         catch ( PolicyViolationException e )
112         {
113             // expected path.
114         }
115     }
116 }