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.Paths;
36 import java.util.Properties;
39 * CachedFailuresPolicyTest
43 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
44 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
45 public class CachedFailuresPolicyTest
51 private UrlFailureCache urlFailureCache;
53 private FilesystemStorage filesystemStorage;
56 @Named( value = "preDownloadPolicy#cache-failures" )
57 DownloadPolicy downloadPolicy;
59 private DownloadPolicy lookupPolicy()
62 return downloadPolicy;
65 private StorageAsset getFile() throws IOException {
66 if (filesystemStorage==null) {
67 filesystemStorage = new FilesystemStorage(Paths.get("target/cache-failures"), new DefaultFileLockManager());
69 return filesystemStorage.getAsset( getName() + ".txt" );
72 private Properties createRequest()
74 Properties request = new Properties();
80 public void testPolicyNo()
83 DownloadPolicy policy = lookupPolicy();
84 StorageAsset localFile = getFile();
85 Properties request = createRequest();
87 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
89 policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
92 @Test( expected = PolicyViolationException.class )
93 public void testPolicyYes()
97 DownloadPolicy policy = lookupPolicy();
98 StorageAsset localFile = getFile();
99 Properties request = createRequest();
101 String url = "http://a.bad.hostname.maven.org/path/to/resource"+ System.currentTimeMillis() +".txt";
103 request.setProperty( "url", url );
106 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
107 // status Yes Not In cache
111 urlFailureCache.cacheFailure( url );
113 request.setProperty( "url", url );
115 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );