1 package org.apache.archiva.consumers.core.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
26 import org.apache.archiva.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
28 import org.apache.archiva.maven.repository.metadata.storage.Maven2RepositoryPathTranslator;
29 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
30 import org.apache.archiva.repository.ReleaseScheme;
31 import org.apache.archiva.repository.RepositoryContentProvider;
32 import org.apache.archiva.metadata.audit.RepositoryListener;
33 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
34 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
35 import org.apache.commons.io.FileUtils;
36 import org.apache.commons.lang3.StringUtils;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.test.context.ContextConfiguration;
45 import javax.inject.Inject;
46 import java.io.IOException;
47 import java.net.URISyntaxException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.time.Period;
52 import java.util.ArrayList;
53 import java.util.List;
54 import java.util.stream.Collectors;
56 import static org.junit.Assert.*;
57 import static org.mockito.Mockito.mock;
58 import static org.mockito.Mockito.when;
62 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
63 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
64 public abstract class AbstractRepositoryPurgeTest
66 public static final String TEST_REPO_ID = "test-repo";
68 public static final String TEST_REPO_NAME = "Test Repository";
70 public static final int TEST_RETENTION_COUNT = 2;
72 public static final int TEST_DAYS_OLDER = 30;
74 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
75 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
77 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
78 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
80 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
81 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
83 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
84 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
86 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
87 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
89 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
91 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
93 private BasicManagedRepository config;
95 private ManagedRepositoryContent repo;
97 protected RepositoryPurge repoPurge;
99 protected RepositoryListener listener;
101 protected RepositorySession repositorySession;
103 protected RepositorySessionFactory sessionFactory;
105 protected MetadataRepository metadataRepository;
108 protected ApplicationContext applicationContext;
110 @SuppressWarnings( "unused" )
112 RepositoryHandlerDependencies repositoryHandlerDependencies;
120 listener = mock( RepositoryListener.class );
122 repositorySession = mock( RepositorySession.class );
123 metadataRepository = mock( MetadataRepository.class );
124 sessionFactory = mock( RepositorySessionFactory.class );
125 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
126 when( sessionFactory.createSession( ) ).thenReturn( repositorySession );
131 public void tearDown()
139 protected static String fixPath( String path )
141 if ( path.contains( " " ) )
143 LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
144 "You are building and testing with a path: \n {} containing space. Consider relocating.", path );
145 return path.replaceAll( " ", "&20" );
150 public org.apache.archiva.repository.ManagedRepository getRepoConfiguration( String repoId, String repoName ) throws URISyntaxException, IOException {
151 Path basePath = Paths.get("target/test-" + getName()).toAbsolutePath();
152 config = BasicManagedRepository.newFilesystemInstance( repoId, repoName, basePath.resolve(repoId));
153 config.addActiveReleaseScheme( ReleaseScheme.RELEASE );
154 config.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
155 ArtifactCleanupFeature atf = config.getFeature( ArtifactCleanupFeature.class ).get();
156 atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER) );
157 String path = AbstractRepositoryPurgeTest.fixPath(
158 basePath.resolve( repoId ).toAbsolutePath().toString() );
159 config.setLocation( Paths.get( path ).toFile().toURI() );
160 atf.setDeleteReleasedSnapshots( true );
161 atf.setRetentionCount( TEST_RETENTION_COUNT );
166 public ManagedRepositoryContent getRepository()
171 org.apache.archiva.repository.ManagedRepository repoCfg = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
172 RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
173 repo = provider.createManagedContent( repoCfg );
179 protected void assertDeleted( String path )
181 assertFalse( "File should have been deleted: " + path, Files.exists(Paths.get( path )) );
184 protected void assertExists( String path )
186 assertTrue( "File should exist: " + path, Files.exists(Paths.get(path)) );
189 protected Path getTestRepoRoot()
191 return Paths.get( "target/test-" + getName() + "/" + TEST_REPO_ID );
194 protected Path getTestRepoRootPath() {
195 return Paths.get("target/test-"+getName()+"/"+TEST_REPO_ID);
198 protected String prepareTestRepos()
201 Path testDir = Paths.get( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().toAbsolutePath().toString() ) );
202 org.apache.archiva.common.utils.FileUtils.deleteDirectory( testDir );
203 Path sourceDir = Paths.get( Paths.get( "target/test-classes/" + TEST_REPO_ID ).toAbsolutePath().toString() );
204 FileUtils.copyDirectory( sourceDir.toFile(), testDir.toFile() );
206 Path releasesTestDir = Paths.get( AbstractRepositoryPurgeTest.fixPath(
207 Paths.get( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() ) );
209 org.apache.archiva.common.utils.FileUtils.deleteDirectory( releasesTestDir );
210 Path sourceReleasesDir =
211 Paths.get( Paths.get( "target/test-classes/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() );
212 FileUtils.copyDirectory( sourceReleasesDir.toFile(), releasesTestDir.toFile() );
214 return AbstractRepositoryPurgeTest.fixPath( testDir.toAbsolutePath().toString() );
217 public String getName()
219 return StringUtils.substringAfterLast( getClass().getName(), "." );
222 protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
224 Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>( ) );
225 return Files.find(vDir, 1,
226 (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
228 translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
229 ).collect( Collectors.toList());