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

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