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.Locale;
37 import java.util.MissingResourceException;
38 import java.util.Properties;
41 * CachedFailuresPolicyTest
45 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
46 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
47 public class CachedFailuresPolicyTest
53 private UrlFailureCache urlFailureCache;
55 private FilesystemStorage filesystemStorage;
58 @Named( value = "preDownloadPolicy#cache-failures" )
59 DownloadPolicy downloadPolicy;
61 private DownloadPolicy lookupPolicy()
64 return downloadPolicy;
67 private StorageAsset getFile() throws IOException {
68 if (filesystemStorage==null) {
69 filesystemStorage = new FilesystemStorage(Paths.get("target/cache-failures"), new DefaultFileLockManager());
71 return filesystemStorage.getAsset( getName() + ".txt" );
74 private Properties createRequest()
76 Properties request = new Properties();
82 public void testPolicyNo()
85 DownloadPolicy policy = lookupPolicy();
86 StorageAsset localFile = getFile();
87 Properties request = createRequest();
89 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
91 policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
94 @Test( expected = PolicyViolationException.class )
95 public void testPolicyYes()
99 DownloadPolicy policy = lookupPolicy();
100 StorageAsset localFile = getFile();
101 Properties request = createRequest();
103 String url = "http://a.bad.hostname.maven.org/path/to/resource"+ System.currentTimeMillis() +".txt";
105 request.setProperty( "url", url );
109 policy.applyPolicy(CachedFailuresPolicy.YES, request, localFile);
110 } catch (PolicyViolationException e) {
111 // Converting to runtime exception, because it should be thrown later
112 throw new RuntimeException(e);
114 // status Yes Not In cache
118 urlFailureCache.cacheFailure( url );
120 request.setProperty( "url", url );
122 policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
126 public void testNamesAndDescriptions() throws Exception {
127 DownloadPolicy policy = lookupPolicy();
128 assertEquals("Cache Failures Policy", policy.getName());
129 assertTrue(policy.getDescription(Locale.US).contains("if download failures will be cached"));
130 assertEquals("Yes", policy.getOptionName(Locale.US, StandardOption.YES));
131 assertEquals("No", policy.getOptionName(Locale.US, StandardOption.NO));
132 assertTrue(policy.getOptionDescription(Locale.US, StandardOption.YES).contains("failures are cached and download is not attempted"));
133 assertTrue(policy.getOptionDescription(Locale.US, StandardOption.NO).contains("failures are not cached"));
135 policy.getOptionName(Locale.US, StandardOption.NOOP);
136 // Exception should be thrown
138 } catch (MissingResourceException e) {