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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  71. wagonMockControl.verify();
  72. // Second attempt to download same artifact use cache
  73. wagonMockControl.reset();
  74. wagonMockControl.replay();
  75. downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  76. wagonMockControl.verify();
  77. assertNotDownloaded( downloadedFile.getFilePath());
  78. assertNoTempFiles( expectedFile );
  79. }
  80. @Test
  81. public void testGetWithCacheFailuresOff()
  82. throws Exception
  83. {
  84. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  85. Path expectedFile = managedDefaultDir.resolve( path );
  86. setupTestableManagedRepository( path );
  87. assertNotExistsInManagedDefaultRepo( expectedFile );
  88. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  89. // Configure Repository (usually done within archiva.xml configuration)
  90. saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
  91. saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
  92. // Configure Connector (usually done within archiva.xml configuration)
  93. saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  94. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  95. saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  96. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  97. wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
  98. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
  99. wagonMockControl.replay();
  100. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  101. wagonMockControl.verify();
  102. // Second attempt to download same artifact DOES NOT use cache
  103. wagonMockControl.reset();
  104. wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
  105. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
  106. wagonMockControl.replay();
  107. downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  108. wagonMockControl.verify();
  109. assertNotDownloaded( downloadedFile.getFilePath() );
  110. assertNoTempFiles( expectedFile );
  111. }
  112. @Test
  113. public void testGetWhenInBothProxiedButFirstCacheFailure()
  114. throws Exception
  115. {
  116. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  117. setupTestableManagedRepository( path );
  118. Path expectedFile = managedDefaultDir.resolve(path );
  119. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  120. Files.deleteIfExists(expectedFile);
  121. assertFalse( Files.exists(expectedFile) );
  122. String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
  123. // Intentionally set failure on url in proxied1 (for test)
  124. UrlFailureCache failurlCache = lookupUrlFailureCache();
  125. failurlCache.cacheFailure( url );
  126. // Configure Connector (usually done within archiva.xml configuration)
  127. saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  128. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
  129. saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  130. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
  131. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  132. // Validate that file actually came from proxied2 (as intended).
  133. Path proxied2File = Paths.get( REPOPATH_PROXIED2, path );
  134. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
  135. assertNoTempFiles( expectedFile );
  136. }
  137. protected UrlFailureCache lookupUrlFailureCache()
  138. throws Exception
  139. {
  140. assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
  141. return urlFailureCache;
  142. }
  143. }