選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbstractRepositoryPurgeTest.java 9.7KB

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