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

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