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

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