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.common.filelock.DefaultFileLockManager;
24 import org.apache.archiva.policies.urlcache.UrlFailureCache;
25 import org.apache.archiva.repository.storage.FilesystemStorage;
26 import org.apache.archiva.repository.storage.StorageAsset;
27 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.springframework.test.context.ContextConfiguration;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import java.io.IOException;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Properties;
40 * CachedFailuresPolicyTest
44 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
46 public class CachedFailuresPolicyTest
52 private UrlFailureCache urlFailureCache;
54 private FilesystemStorage filesystemStorage;
57 @Named( value = "preDownloadPolicy#cache-failures" )
58 DownloadPolicy downloadPolicy;
60 private DownloadPolicy lookupPolicy()
63 return downloadPolicy;
66 private StorageAsset getFile() throws IOException {
67 if (filesystemStorage==null) {
68 filesystemStorage = new FilesystemStorage(Paths.get("target/cache-failures"), new DefaultFileLockManager());
70 return filesystemStorage.getAsset( getName() + ".txt" );
73 private Properties createRequest()
75 Properties request = new Properties();
81 public void testPolicyNo()
84 DownloadPolicy policy = lookupPolicy();
85 StorageAsset localFile = getFile();
86 Properties request = createRequest();
88 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
90 policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
93 @Test( expected = PolicyViolationException.class )
94 public void testPolicyYes()
98 DownloadPolicy policy = lookupPolicy();
99 StorageAsset localFile = getFile();
100 Properties request = createRequest();
102 String url = "http://a.bad.hostname.maven.org/path/to/resource"+ System.currentTimeMillis() +".txt";
104 request.setProperty( "url", url );
107 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
108 // status Yes Not In cache
112 urlFailureCache.cacheFailure( url );
114 request.setProperty( "url", url );
116 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );