1 package org.apache.maven.archiva.policies;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
30 import java.util.Properties;
33 * CachedFailuresPolicyTest
35 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
38 public class CachedFailuresPolicyTest
39 extends PlexusTestCase
41 private BeanFactory factory;
43 private DownloadPolicy lookupPolicy()
46 return (DownloadPolicy) lookup( PreDownloadPolicy.class.getName(), "cache-failures" );
49 private File getFile()
51 return new File( "target/cache-failures/" + getName() + ".txt" );
54 private Properties createRequest()
56 Properties request = new Properties();
61 public void testPolicyNo()
64 DownloadPolicy policy = lookupPolicy();
65 File localFile = getFile();
66 Properties request = createRequest();
68 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
70 policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
73 public void testPolicyYesNotInCache()
76 DownloadPolicy policy = lookupPolicy();
77 File localFile = getFile();
78 Properties request = createRequest();
80 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
82 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
85 public void testPolicyYesInCache()
88 DownloadPolicy policy = lookupPolicy();
89 File localFile = getFile();
90 Properties request = createRequest();
92 String url = "http://a.bad.hostname.maven.org/path/to/resource.txt";
94 UrlFailureCache urlFailureCache = (UrlFailureCache) factory.getBean( "urlFailureCache" );
95 urlFailureCache.cacheFailure( url );
97 request.setProperty( "url", url );
101 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
102 fail( "Expected a PolicyViolationException." );
104 catch ( PolicyViolationException e )
110 protected void 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 );