Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CachedFailuresPolicyTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.Paths;
  33. import java.util.Locale;
  34. import java.util.MissingResourceException;
  35. import java.util.Properties;
  36. /**
  37. * CachedFailuresPolicyTest
  38. *
  39. *
  40. */
  41. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  42. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
  43. public class CachedFailuresPolicyTest
  44. extends TestCase
  45. {
  46. @Inject
  47. private UrlFailureCache urlFailureCache;
  48. private FilesystemStorage filesystemStorage;
  49. @Inject
  50. @Named( value = "preDownloadPolicy#cache-failures" )
  51. DownloadPolicy downloadPolicy;
  52. private DownloadPolicy lookupPolicy()
  53. throws Exception
  54. {
  55. return downloadPolicy;
  56. }
  57. private StorageAsset getFile() throws IOException {
  58. if (filesystemStorage==null) {
  59. filesystemStorage = new FilesystemStorage(Paths.get("target/cache-failures"), new DefaultFileLockManager());
  60. }
  61. return filesystemStorage.getAsset( getName() + ".txt" );
  62. }
  63. private Properties createRequest()
  64. {
  65. Properties request = new Properties();
  66. return request;
  67. }
  68. @Test
  69. public void testPolicyNo()
  70. throws Exception
  71. {
  72. DownloadPolicy policy = lookupPolicy();
  73. StorageAsset localFile = getFile();
  74. Properties request = createRequest();
  75. request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
  76. policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
  77. }
  78. @Test( expected = PolicyViolationException.class )
  79. public void testPolicyYes()
  80. throws Exception
  81. {
  82. DownloadPolicy policy = lookupPolicy();
  83. StorageAsset localFile = getFile();
  84. Properties request = createRequest();
  85. // make unique name
  86. String url = "http://a.bad.hostname.maven.org/path/to/resource"+ System.currentTimeMillis() +".txt";
  87. request.setProperty( "url", url );
  88. // should not fail
  89. try {
  90. policy.applyPolicy(CachedFailuresPolicy.YES, request, localFile);
  91. } catch (PolicyViolationException e) {
  92. // Converting to runtime exception, because it should be thrown later
  93. throw new RuntimeException(e);
  94. }
  95. // status Yes Not In cache
  96. // Yes in Cache
  97. urlFailureCache.cacheFailure( url );
  98. request.setProperty( "url", url );
  99. policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
  100. }
  101. @Test
  102. public void testNamesAndDescriptions() throws Exception {
  103. DownloadPolicy policy = lookupPolicy();
  104. assertEquals("Cache Failures Policy", policy.getName());
  105. assertTrue(policy.getDescription(Locale.US).contains("if download failures will be cached"));
  106. assertEquals("Yes", policy.getOptionName(Locale.US, StandardOption.YES));
  107. assertEquals("No", policy.getOptionName(Locale.US, StandardOption.NO));
  108. assertTrue(policy.getOptionDescription(Locale.US, StandardOption.YES).contains("failures are cached and download is not attempted"));
  109. assertTrue(policy.getOptionDescription(Locale.US, StandardOption.NO).contains("failures are not cached"));
  110. try {
  111. policy.getOptionName(Locale.US, StandardOption.NOOP);
  112. // Exception should be thrown
  113. assertTrue(false);
  114. } catch (MissingResourceException e) {
  115. //
  116. }
  117. }
  118. }