View Javadoc

1   package org.apache.maven.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 java.io.File;
23  import java.util.Properties;
24  
25  import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
26  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
27  
28  /**
29   * CachedFailuresPolicyTest
30   *
31   * @version $Id: CachedFailuresPolicyTest.java 718864 2008-11-19 06:33:35Z brett $
32   */
33  public class CachedFailuresPolicyTest
34      extends PlexusInSpringTestCase
35  {
36      private DownloadPolicy lookupPolicy()
37          throws Exception
38      {
39          return (DownloadPolicy) lookup( PreDownloadPolicy.class, "cache-failures" );
40      }
41  
42      private File getFile()
43      {
44          return new File( "target/cache-failures/" + getName() + ".txt" );
45      }
46  
47      private Properties createRequest()
48      {
49          Properties request = new Properties();
50  
51          return request;
52      }
53  
54      public void testPolicyNo()
55          throws Exception
56      {
57          DownloadPolicy policy = lookupPolicy();
58          File localFile = getFile();
59          Properties request = createRequest();
60  
61          request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
62  
63          policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
64      }
65  
66      public void testPolicyYesNotInCache()
67          throws Exception
68      {
69          DownloadPolicy policy = lookupPolicy();
70          File localFile = getFile();
71          Properties request = createRequest();
72  
73          request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
74  
75          policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
76      }
77  
78      public void testPolicyYesInCache()
79          throws Exception
80      {
81          DownloadPolicy policy = lookupPolicy();
82          File localFile = getFile();
83          Properties request = createRequest();
84  
85          String url = "http://a.bad.hostname.maven.org/path/to/resource.txt";
86  
87          UrlFailureCache urlFailureCache = (UrlFailureCache) lookup( "urlFailureCache" );
88          urlFailureCache.cacheFailure( url );
89  
90          request.setProperty( "url", url );
91  
92          try
93          {
94              policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
95              fail( "Expected a PolicyViolationException." );
96          }
97          catch ( PolicyViolationException e )
98          {
99              // expected path.
100         }
101     }
102 }