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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import org.springframework.test.context.ContextConfiguration;
  26. import javax.inject.Inject;
  27. import javax.inject.Named;
  28. import java.nio.file.Path;
  29. import java.nio.file.Paths;
  30. import java.util.Properties;
  31. /**
  32. * CachedFailuresPolicyTest
  33. *
  34. *
  35. */
  36. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  37. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
  38. public class CachedFailuresPolicyTest
  39. extends TestCase
  40. {
  41. @Inject
  42. private UrlFailureCache urlFailureCache;
  43. @Inject
  44. @Named( value = "preDownloadPolicy#cache-failures" )
  45. DownloadPolicy downloadPolicy;
  46. private DownloadPolicy lookupPolicy()
  47. throws Exception
  48. {
  49. return downloadPolicy;
  50. }
  51. private Path getFile()
  52. {
  53. return Paths.get( "target/cache-failures/" + getName() + ".txt" );
  54. }
  55. private Properties createRequest()
  56. {
  57. Properties request = new Properties();
  58. return request;
  59. }
  60. @Test
  61. public void testPolicyNo()
  62. throws Exception
  63. {
  64. DownloadPolicy policy = lookupPolicy();
  65. Path localFile = getFile();
  66. Properties request = createRequest();
  67. request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
  68. policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
  69. }
  70. @Test( expected = PolicyViolationException.class )
  71. public void testPolicyYes()
  72. throws Exception
  73. {
  74. DownloadPolicy policy = lookupPolicy();
  75. Path localFile = getFile();
  76. Properties request = createRequest();
  77. // make unique name
  78. String url = "http://a.bad.hostname.maven.org/path/to/resource"+ System.currentTimeMillis() +".txt";
  79. request.setProperty( "url", url );
  80. // should not fail
  81. policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
  82. // status Yes Not In cache
  83. // Yes in Cache
  84. urlFailureCache.cacheFailure( url );
  85. request.setProperty( "url", url );
  86. policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
  87. }
  88. }