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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.group.RepositoryGroupHandler;
  26. import org.apache.archiva.repository.maven.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.easymock.EasyMock;
  36. import org.easymock.IMocksControl;
  37. import org.junit.After;
  38. import org.junit.Before;
  39. import org.junit.runner.RunWith;
  40. import org.slf4j.LoggerFactory;
  41. import org.springframework.context.ApplicationContext;
  42. import org.springframework.test.context.ContextConfiguration;
  43. import javax.inject.Inject;
  44. import java.io.IOException;
  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.*;
  54. import static org.mockito.Mockito.mock;
  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 IMocksControl listenerControl;
  81. protected RepositoryListener listener;
  82. protected IMocksControl sessionControl;
  83. protected RepositorySession repositorySession;
  84. protected IMocksControl sessionFactoryControl;
  85. protected RepositorySessionFactory sessionFactory;
  86. protected MetadataRepository metadataRepository;
  87. @Inject
  88. protected ApplicationContext applicationContext;
  89. @SuppressWarnings( "unused" )
  90. @Inject
  91. RepositoryGroupHandler repositoryGroupHandler;
  92. @Before
  93. public void setUp()
  94. throws Exception
  95. {
  96. listenerControl = EasyMock.createControl();
  97. listener = listenerControl.createMock( RepositoryListener.class );
  98. sessionControl = EasyMock.createControl();
  99. sessionFactoryControl = EasyMock.createControl( );
  100. repositorySession = sessionControl.createMock( RepositorySession.class );
  101. metadataRepository = mock( MetadataRepository.class );
  102. sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
  103. EasyMock.expect( repositorySession.getRepository() ).andStubReturn( metadataRepository );
  104. EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
  105. }
  106. @After
  107. public void tearDown()
  108. throws Exception
  109. {
  110. config = null;
  111. repo = null;
  112. }
  113. protected static String fixPath( String path )
  114. {
  115. if ( path.contains( " " ) )
  116. {
  117. LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
  118. "You are building and testing with a path: \n {} containing space. Consider relocating.", path );
  119. return path.replaceAll( " ", "&20" );
  120. }
  121. return path;
  122. }
  123. public org.apache.archiva.repository.ManagedRepository getRepoConfiguration( String repoId, String repoName ) throws URISyntaxException, IOException {
  124. Path basePath = Paths.get("target/test-" + getName()).toAbsolutePath();
  125. config = BasicManagedRepository.newFilesystemInstance( repoId, repoName, basePath.resolve(repoId));
  126. config.addActiveReleaseScheme( ReleaseScheme.RELEASE );
  127. config.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
  128. ArtifactCleanupFeature atf = config.getFeature( ArtifactCleanupFeature.class ).get();
  129. atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER) );
  130. String path = AbstractRepositoryPurgeTest.fixPath(
  131. basePath.resolve( repoId ).toAbsolutePath().toString() );
  132. config.setLocation( Paths.get( path ).toFile().toURI() );
  133. atf.setDeleteReleasedSnapshots( true );
  134. atf.setRetentionCount( TEST_RETENTION_COUNT );
  135. return config;
  136. }
  137. public ManagedRepositoryContent getRepository()
  138. throws Exception
  139. {
  140. if ( repo == null )
  141. {
  142. org.apache.archiva.repository.ManagedRepository repoCfg = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  143. RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
  144. repo = provider.createManagedContent( repoCfg );
  145. }
  146. return repo;
  147. }
  148. protected void assertDeleted( String path )
  149. {
  150. assertFalse( "File should have been deleted: " + path, Files.exists(Paths.get( path )) );
  151. }
  152. protected void assertExists( String path )
  153. {
  154. assertTrue( "File should exist: " + path, Files.exists(Paths.get(path)) );
  155. }
  156. protected Path getTestRepoRoot()
  157. {
  158. return Paths.get( "target/test-" + getName() + "/" + TEST_REPO_ID );
  159. }
  160. protected Path getTestRepoRootPath() {
  161. return Paths.get("target/test-"+getName()+"/"+TEST_REPO_ID);
  162. }
  163. protected String prepareTestRepos()
  164. throws Exception
  165. {
  166. Path testDir = Paths.get( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().toAbsolutePath().toString() ) );
  167. org.apache.archiva.common.utils.FileUtils.deleteDirectory( testDir );
  168. Path sourceDir = Paths.get( Paths.get( "target/test-classes/" + TEST_REPO_ID ).toAbsolutePath().toString() );
  169. FileUtils.copyDirectory( sourceDir.toFile(), testDir.toFile() );
  170. Path releasesTestDir = Paths.get( AbstractRepositoryPurgeTest.fixPath(
  171. Paths.get( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() ) );
  172. org.apache.archiva.common.utils.FileUtils.deleteDirectory( releasesTestDir );
  173. Path sourceReleasesDir =
  174. Paths.get( Paths.get( "target/test-classes/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() );
  175. FileUtils.copyDirectory( sourceReleasesDir.toFile(), releasesTestDir.toFile() );
  176. return AbstractRepositoryPurgeTest.fixPath( testDir.toAbsolutePath().toString() );
  177. }
  178. public String getName()
  179. {
  180. return StringUtils.substringAfterLast( getClass().getName(), "." );
  181. }
  182. protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
  183. {
  184. Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>( ) );
  185. return Files.find(vDir, 1,
  186. (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
  187. .map( path ->
  188. translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
  189. ).collect( Collectors.toList());
  190. }
  191. }