You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CachedFailuresPolicyTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package org.apache.archiva.policies;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import org.apache.archiva.policies.urlcache.UrlFailureCache;
  22. import org.junit.Test;
  23. import org.junit.runner.RunWith;
  24. import org.springframework.test.context.ContextConfiguration;
  25. import javax.inject.Inject;
  26. import javax.inject.Named;
  27. import java.io.File;
  28. import java.util.Properties;
  29. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  30. /**
  31. * CachedFailuresPolicyTest
  32. *
  33. *
  34. */
  35. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  36. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
  37. public class CachedFailuresPolicyTest
  38. extends TestCase
  39. {
  40. @Inject
  41. private UrlFailureCache urlFailureCache;
  42. @Inject
  43. @Named( value = "preDownloadPolicy#cache-failures" )
  44. DownloadPolicy downloadPolicy;
  45. private DownloadPolicy lookupPolicy()
  46. throws Exception
  47. {
  48. return downloadPolicy;
  49. }
  50. private File getFile()
  51. {
  52. return new File( "target/cache-failures/" + getName() + ".txt" );
  53. }
  54. private Properties createRequest()
  55. {
  56. Properties request = new Properties();
  57. return request;
  58. }
  59. @Test
  60. public void testPolicyNo()
  61. throws Exception
  62. {
  63. DownloadPolicy policy = lookupPolicy();
  64. File localFile = getFile();
  65. Properties request = createRequest();
  66. request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
  67. policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
  68. }
  69. @Test
  70. public void testPolicyYes()
  71. throws Exception
  72. {
  73. DownloadPolicy policy = lookupPolicy();
  74. File localFile = getFile();
  75. Properties request = createRequest();
  76. request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
  77. // should not fail
  78. policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
  79. // status Yes Not In cache
  80. // Yes in Cache
  81. String url = "http://a.bad.hostname.maven.org/path/to/resource.txt";
  82. urlFailureCache.cacheFailure( url );
  83. request.setProperty( "url", url );
  84. try
  85. {
  86. policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
  87. fail( "Expected a PolicyViolationException." );
  88. }
  89. catch ( PolicyViolationException e )
  90. {
  91. // expected path.
  92. }
  93. }
  94. }