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

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