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.

AbstractRepositoryPurgeTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package org.apache.archiva.consumers.core.repository;
  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.metadata.model.ArtifactMetadata;
  21. import org.apache.archiva.metadata.repository.MetadataRepository;
  22. import org.apache.archiva.metadata.repository.RepositorySession;
  23. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  24. import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
  25. import org.apache.archiva.repository.base.BasicManagedRepository;
  26. import org.apache.archiva.repository.ManagedRepositoryContent;
  27. import org.apache.archiva.repository.ReleaseScheme;
  28. import org.apache.archiva.repository.RepositoryContentProvider;
  29. import org.apache.archiva.metadata.audit.RepositoryListener;
  30. import org.apache.archiva.repository.features.ArtifactCleanupFeature;
  31. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  32. import org.apache.commons.io.FileUtils;
  33. import org.apache.commons.lang3.StringUtils;
  34. import org.easymock.EasyMock;
  35. import org.easymock.IMocksControl;
  36. import org.junit.After;
  37. import org.junit.Before;
  38. import org.junit.runner.RunWith;
  39. import org.slf4j.LoggerFactory;
  40. import org.springframework.context.ApplicationContext;
  41. import org.springframework.test.context.ContextConfiguration;
  42. import javax.inject.Inject;
  43. import java.io.IOException;
  44. import java.net.URI;
  45. import java.net.URISyntaxException;
  46. import java.nio.file.Files;
  47. import java.nio.file.Path;
  48. import java.nio.file.Paths;
  49. import java.time.Period;
  50. import java.util.ArrayList;
  51. import java.util.List;
  52. import java.util.stream.Collectors;
  53. import static org.junit.Assert.assertFalse;
  54. import static org.junit.Assert.assertTrue;
  55. import static org.mockito.Mockito.mock;
  56. /**
  57. */
  58. @RunWith(ArchivaSpringJUnit4ClassRunner.class)
  59. @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
  60. public abstract class AbstractRepositoryPurgeTest
  61. {
  62. public static final String TEST_REPO_ID = "test-repo";
  63. public static final String TEST_REPO_NAME = "Test Repository";
  64. public static final int TEST_RETENTION_COUNT = 2;
  65. public static final int TEST_DAYS_OLDER = 30;
  66. public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
  67. "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
  68. public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
  69. "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
  70. public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
  71. "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
  72. public static final String PATH_TO_BY_RETENTION_COUNT_POM =
  73. "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
  74. public static final String PATH_TO_TEST_ORDER_OF_DELETION =
  75. "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
  76. protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
  77. protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
  78. private BasicManagedRepository config;
  79. private ManagedRepositoryContent repo;
  80. protected RepositoryPurge repoPurge;
  81. protected IMocksControl listenerControl;
  82. protected RepositoryListener listener;
  83. protected IMocksControl sessionControl;
  84. protected RepositorySession repositorySession;
  85. protected IMocksControl sessionFactoryControl;
  86. protected RepositorySessionFactory sessionFactory;
  87. protected MetadataRepository metadataRepository;
  88. @Inject
  89. protected ApplicationContext applicationContext;
  90. @Before
  91. public void setUp()
  92. throws Exception
  93. {
  94. listenerControl = EasyMock.createControl();
  95. listener = listenerControl.createMock( RepositoryListener.class );
  96. sessionControl = EasyMock.createControl();
  97. sessionFactoryControl = EasyMock.createControl( );
  98. repositorySession = sessionControl.createMock( RepositorySession.class );
  99. metadataRepository = mock( MetadataRepository.class );
  100. sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
  101. EasyMock.expect( repositorySession.getRepository() ).andStubReturn( metadataRepository );
  102. EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
  103. }
  104. @After
  105. public void tearDown()
  106. throws Exception
  107. {
  108. config = null;
  109. repo = null;
  110. }
  111. protected static String fixPath( String path )
  112. {
  113. if ( path.contains( " " ) )
  114. {
  115. LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
  116. "You are building and testing with a path: \n {} containing space. Consider relocating.", path );
  117. return path.replaceAll( " ", "&20" );
  118. }
  119. return path;
  120. }
  121. public org.apache.archiva.repository.ManagedRepository getRepoConfiguration( String repoId, String repoName ) throws URISyntaxException, IOException {
  122. Path basePath = Paths.get("target/test-" + getName()).toAbsolutePath();
  123. config = BasicManagedRepository.newFilesystemInstance( repoId, repoName, basePath.resolve(repoId));
  124. config.addActiveReleaseScheme( ReleaseScheme.RELEASE );
  125. config.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
  126. ArtifactCleanupFeature atf = config.getFeature( ArtifactCleanupFeature.class ).get();
  127. atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER) );
  128. String path = AbstractRepositoryPurgeTest.fixPath(
  129. basePath.resolve( repoId ).toAbsolutePath().toString() );
  130. config.setLocation( new URI( path ) );
  131. atf.setDeleteReleasedSnapshots( true );
  132. atf.setRetentionCount( TEST_RETENTION_COUNT );
  133. return config;
  134. }
  135. public ManagedRepositoryContent getRepository()
  136. throws Exception
  137. {
  138. if ( repo == null )
  139. {
  140. org.apache.archiva.repository.ManagedRepository repoCfg = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  141. RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
  142. repo = provider.createManagedContent( repoCfg );
  143. }
  144. return repo;
  145. }
  146. protected void assertDeleted( String path )
  147. {
  148. assertFalse( "File should have been deleted: " + path, Files.exists(Paths.get( path )) );
  149. }
  150. protected void assertExists( String path )
  151. {
  152. assertTrue( "File should exist: " + path, Files.exists(Paths.get(path)) );
  153. }
  154. protected Path getTestRepoRoot()
  155. {
  156. return Paths.get( "target/test-" + getName() + "/" + TEST_REPO_ID );
  157. }
  158. protected Path getTestRepoRootPath() {
  159. return Paths.get("target/test-"+getName()+"/"+TEST_REPO_ID);
  160. }
  161. protected String prepareTestRepos()
  162. throws Exception
  163. {
  164. Path testDir = Paths.get( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().toAbsolutePath().toString() ) );
  165. org.apache.archiva.common.utils.FileUtils.deleteDirectory( testDir );
  166. Path sourceDir = Paths.get( Paths.get( "target/test-classes/" + TEST_REPO_ID ).toAbsolutePath().toString() );
  167. FileUtils.copyDirectory( sourceDir.toFile(), testDir.toFile() );
  168. Path releasesTestDir = Paths.get( AbstractRepositoryPurgeTest.fixPath(
  169. Paths.get( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() ) );
  170. org.apache.archiva.common.utils.FileUtils.deleteDirectory( releasesTestDir );
  171. Path sourceReleasesDir =
  172. Paths.get( Paths.get( "target/test-classes/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() );
  173. FileUtils.copyDirectory( sourceReleasesDir.toFile(), releasesTestDir.toFile() );
  174. return AbstractRepositoryPurgeTest.fixPath( testDir.toAbsolutePath().toString() );
  175. }
  176. public String getName()
  177. {
  178. return StringUtils.substringAfterLast( getClass().getName(), "." );
  179. }
  180. protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
  181. {
  182. Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>( ) );
  183. return Files.find(vDir, 1,
  184. (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
  185. .map( path ->
  186. translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
  187. ).collect( Collectors.toList());
  188. }
  189. }