View Javadoc
1   package org.apache.archiva.policies;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.archiva.policies.urlcache.UrlFailureCache;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  import org.springframework.test.context.ContextConfiguration;
27  
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  import java.io.File;
31  import java.util.Properties;
32  import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33  
34  /**
35   * CachedFailuresPolicyTest
36   *
37   *
38   */
39  @RunWith( ArchivaSpringJUnit4ClassRunner.class )
40  @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
41  public class CachedFailuresPolicyTest
42      extends TestCase
43  {
44  
45  
46      @Inject
47      private UrlFailureCache urlFailureCache;
48  
49      @Inject
50      @Named( value = "preDownloadPolicy#cache-failures" )
51      DownloadPolicy downloadPolicy;
52  
53      private DownloadPolicy lookupPolicy()
54          throws Exception
55      {
56          return downloadPolicy;
57      }
58  
59      private File getFile()
60      {
61          return new File( "target/cache-failures/" + getName() + ".txt" );
62      }
63  
64      private Properties createRequest()
65      {
66          Properties request = new Properties();
67  
68          return request;
69      }
70  
71      @Test
72      public void testPolicyNo()
73          throws Exception
74      {
75          DownloadPolicy policy = lookupPolicy();
76          File localFile = getFile();
77          Properties request = createRequest();
78  
79          request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
80  
81          policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
82      }
83  
84      @Test( expected = PolicyViolationException.class )
85      public void testPolicyYes()
86          throws Exception
87      {
88  
89          DownloadPolicy policy = lookupPolicy();
90          File localFile = getFile();
91          Properties request = createRequest();
92          // make unique name
93          String url = "http://a.bad.hostname.maven.org/path/to/resource"+ System.currentTimeMillis() +".txt";
94          
95          request.setProperty( "url", url );
96  
97          // should not fail
98          policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
99          // status Yes Not In cache
100 
101         // Yes in Cache
102         
103         urlFailureCache.cacheFailure( url );
104 
105         request.setProperty( "url", url );
106 
107         policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );       
108     }
109 }