1 package org.apache.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 junit.framework.TestCase;
23 import org.apache.archiva.policies.urlcache.UrlFailureCache;
24 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.springframework.test.context.ContextConfiguration;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.Properties;
36 * CachedFailuresPolicyTest
40 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
41 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
42 public class CachedFailuresPolicyTest
48 private UrlFailureCache urlFailureCache;
51 @Named( value = "preDownloadPolicy#cache-failures" )
52 DownloadPolicy downloadPolicy;
54 private DownloadPolicy lookupPolicy()
57 return downloadPolicy;
60 private Path getFile()
62 return Paths.get( "target/cache-failures/" + getName() + ".txt" );
65 private Properties createRequest()
67 Properties request = new Properties();
73 public void testPolicyNo()
76 DownloadPolicy policy = lookupPolicy();
77 Path localFile = getFile();
78 Properties request = createRequest();
80 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
82 policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
85 @Test( expected = PolicyViolationException.class )
86 public void testPolicyYes()
90 DownloadPolicy policy = lookupPolicy();
91 Path localFile = getFile();
92 Properties request = createRequest();
94 String url = "http://a.bad.hostname.maven.org/path/to/resource"+ System.currentTimeMillis() +".txt";
96 request.setProperty( "url", url );
99 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
100 // status Yes Not In cache
104 urlFailureCache.cacheFailure( url );
106 request.setProperty( "url", url );
108 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );