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

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