]> source.dussan.org Git - archiva.git/blob
edb85b8ab41d2ba1d9917887cdfadd45ff44d974
[archiva.git] /
1 package org.apache.maven.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 org.apache.maven.archiva.common.spring.PlexusFactory;
23 import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
24 import org.codehaus.plexus.PlexusTestCase;
25 import org.springframework.beans.factory.BeanFactory;
26 import org.springframework.beans.factory.xml.XmlBeanFactory;
27 import org.springframework.core.io.ClassPathResource;
28
29 import java.io.File;
30 import java.util.Properties;
31
32 /**
33  * CachedFailuresPolicyTest
34  *
35  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
36  * @version $Id$
37  */
38 public class CachedFailuresPolicyTest
39     extends PlexusTestCase
40 {
41     private BeanFactory factory;
42
43     private DownloadPolicy lookupPolicy()
44         throws Exception
45     {
46         return (DownloadPolicy) lookup( PreDownloadPolicy.class.getName(), "cache-failures" );
47     }
48
49     private File getFile()
50     {
51         return new File( "target/cache-failures/" + getName() + ".txt" );
52     }
53
54     private Properties createRequest()
55     {
56         Properties request = new Properties();
57
58         return request;
59     }
60
61     public void testPolicyNo()
62         throws Exception
63     {
64         DownloadPolicy policy = lookupPolicy();
65         File localFile = getFile();
66         Properties request = createRequest();
67
68         request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
69
70         policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
71     }
72
73     public void testPolicyYesNotInCache()
74         throws Exception
75     {
76         DownloadPolicy policy = lookupPolicy();
77         File localFile = getFile();
78         Properties request = createRequest();
79
80         request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
81
82         policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
83     }
84
85     public void testPolicyYesInCache()
86         throws Exception
87     {
88         DownloadPolicy policy = lookupPolicy();
89         File localFile = getFile();
90         Properties request = createRequest();
91
92         String url = "http://a.bad.hostname.maven.org/path/to/resource.txt";
93
94         UrlFailureCache urlFailureCache = (UrlFailureCache) factory.getBean( "urlFailureCache" );
95         urlFailureCache.cacheFailure( url );
96
97         request.setProperty( "url", url );
98
99         try
100         {
101             policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
102             fail( "Expected a PolicyViolationException." );
103         }
104         catch ( PolicyViolationException e )
105         {
106             // expected path.
107         }
108     }
109
110     protected void setUp()
111         throws Exception
112     {
113         super.setUp();
114         factory = new XmlBeanFactory(
115             new ClassPathResource( "/org/apache/maven/archiva/policies/CachedFailuresPolicyTest-context.xml" ) );
116         getContainer().getContext().put( BeanFactory.class, factory );
117         PlexusFactory plexusFactory = (PlexusFactory) factory.getBean( "plexusCacheFactory" );
118         plexusFactory.setContainer( container );
119     }
120 }