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.

CacheFailuresTransferTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package org.apache.archiva.proxy;
  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 org.apache.archiva.common.utils.PathUtil;
  21. import org.apache.archiva.model.ArtifactReference;
  22. import org.apache.archiva.policies.CachedFailuresPolicy;
  23. import org.apache.archiva.policies.ChecksumPolicy;
  24. import org.apache.archiva.policies.ReleasesPolicy;
  25. import org.apache.archiva.policies.SnapshotsPolicy;
  26. import org.apache.archiva.policies.urlcache.UrlFailureCache;
  27. import org.apache.maven.wagon.ResourceDoesNotExistException;
  28. import org.easymock.EasyMock;
  29. import org.junit.Test;
  30. import javax.inject.Inject;
  31. import java.io.File;
  32. import java.nio.file.Files;
  33. import java.nio.file.Path;
  34. import java.nio.file.Paths;
  35. import static org.junit.Assert.assertFalse;
  36. import static org.junit.Assert.assertNotNull;
  37. /**
  38. * CacheFailuresTransferTest
  39. *
  40. *
  41. */
  42. public class CacheFailuresTransferTest
  43. extends AbstractProxyTestCase
  44. {
  45. // TODO: test some hard failures (eg TransferFailedException)
  46. // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
  47. @Inject
  48. UrlFailureCache urlFailureCache;
  49. @Test
  50. public void testGetWithCacheFailuresOn()
  51. throws Exception
  52. {
  53. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  54. Path expectedFile = managedDefaultDir.resolve( path );
  55. setupTestableManagedRepository( path );
  56. assertNotExistsInManagedDefaultRepo( expectedFile );
  57. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  58. // Configure Repository (usually done within archiva.xml configuration)
  59. saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
  60. saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
  61. // Configure Connector (usually done within archiva.xml configuration)
  62. saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  63. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
  64. saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  65. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
  66. wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
  67. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
  68. wagonMockControl.replay();
  69. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  70. wagonMockControl.verify();
  71. // Second attempt to download same artifact use cache
  72. wagonMockControl.reset();
  73. wagonMockControl.replay();
  74. downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  75. wagonMockControl.verify();
  76. assertNotDownloaded( downloadedFile);
  77. assertNoTempFiles( expectedFile );
  78. }
  79. @Test
  80. public void testGetWithCacheFailuresOff()
  81. throws Exception
  82. {
  83. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  84. Path expectedFile = managedDefaultDir.resolve( path );
  85. setupTestableManagedRepository( path );
  86. assertNotExistsInManagedDefaultRepo( expectedFile );
  87. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  88. // Configure Repository (usually done within archiva.xml configuration)
  89. saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
  90. saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
  91. // Configure Connector (usually done within archiva.xml configuration)
  92. saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  93. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  94. saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  95. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  96. wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
  97. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
  98. wagonMockControl.replay();
  99. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  100. wagonMockControl.verify();
  101. // Second attempt to download same artifact DOES NOT use cache
  102. wagonMockControl.reset();
  103. wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
  104. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
  105. wagonMockControl.replay();
  106. downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  107. wagonMockControl.verify();
  108. assertNotDownloaded( downloadedFile );
  109. assertNoTempFiles( expectedFile );
  110. }
  111. @Test
  112. public void testGetWhenInBothProxiedButFirstCacheFailure()
  113. throws Exception
  114. {
  115. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  116. setupTestableManagedRepository( path );
  117. Path expectedFile = managedDefaultDir.resolve(path );
  118. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  119. Files.deleteIfExists(expectedFile);
  120. assertFalse( Files.exists(expectedFile) );
  121. String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
  122. // Intentionally set failure on url in proxied1 (for test)
  123. UrlFailureCache failurlCache = lookupUrlFailureCache();
  124. failurlCache.cacheFailure( url );
  125. // Configure Connector (usually done within archiva.xml configuration)
  126. saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  127. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
  128. saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  129. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
  130. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  131. // Validate that file actually came from proxied2 (as intended).
  132. Path proxied2File = Paths.get( REPOPATH_PROXIED2, path );
  133. assertFileEquals( expectedFile, downloadedFile, proxied2File );
  134. assertNoTempFiles( expectedFile );
  135. }
  136. protected UrlFailureCache lookupUrlFailureCache()
  137. throws Exception
  138. {
  139. assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
  140. return urlFailureCache;
  141. }
  142. }