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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
27 import org.apache.archiva.repository.ManagedRepositoryContent;
28 import org.apache.archiva.repository.events.RepositoryListener;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.maven.index.NexusIndexer;
33 import org.apache.maven.index.context.IndexingContext;
34 import org.easymock.EasyMock;
35 import org.easymock.IMocksControl;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.runner.RunWith;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.test.context.ContextConfiguration;
43 import javax.inject.Inject;
45 import java.io.IOException;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.stream.Collectors;
53 import static org.junit.Assert.assertFalse;
54 import static org.junit.Assert.assertTrue;
55 import static org.mockito.Mockito.mock;
56 import static org.mockito.Mockito.when;
60 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
61 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
62 public abstract class AbstractRepositoryPurgeTest
64 public static final String TEST_REPO_ID = "test-repo";
66 public static final String TEST_REPO_NAME = "Test Repository";
68 public static final int TEST_RETENTION_COUNT = 2;
70 public static final int TEST_DAYS_OLDER = 30;
72 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
73 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
75 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
76 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
78 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
79 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
81 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
82 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
84 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
85 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
87 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
89 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
91 private ManagedRepository config;
93 private ManagedRepositoryContent repo;
95 protected RepositoryPurge repoPurge;
97 protected IMocksControl listenerControl;
99 protected RepositoryListener listener;
101 protected RepositorySession repositorySession;
103 protected MetadataRepository metadataRepository;
106 protected ApplicationContext applicationContext;
109 protected PlexusSisuBridge plexusSisuBridge;
117 removeMavenIndexes();
119 listenerControl = EasyMock.createControl();
121 listener = listenerControl.createMock( RepositoryListener.class );
123 repositorySession = mock( RepositorySession.class );
124 metadataRepository = mock( MetadataRepository.class );
125 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
131 public void tearDown()
134 removeMavenIndexes();
140 protected void removeMavenIndexes()
143 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
144 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
146 nexusIndexer.removeIndexingContext( indexingContext, false );
150 protected static String fixPath( String path )
152 if ( path.contains( " " ) )
154 LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
155 "You are building and testing with a path: \n " + path + " containing space. Consider relocating." );
156 return path.replaceAll( " ", "&20" );
161 public ManagedRepository getRepoConfiguration( String repoId, String repoName )
163 config = new ManagedRepository();
164 config.setId( repoId );
165 config.setName( repoName );
166 config.setDaysOlder( TEST_DAYS_OLDER );
167 String path = AbstractRepositoryPurgeTest.fixPath(
168 new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
169 config.setLocation( path );
170 config.setReleases( true );
171 config.setSnapshots( true );
172 config.setDeleteReleasedSnapshots( true );
173 config.setRetentionCount( TEST_RETENTION_COUNT );
178 public ManagedRepositoryContent getRepository()
183 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
184 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
190 protected void assertDeleted( String path )
192 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
195 protected void assertExists( String path )
197 assertTrue( "File should exist: " + path, new File( path ).exists() );
200 protected File getTestRepoRoot()
202 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
205 protected Path getTestRepoRootPath() {
206 return Paths.get("target/test-"+getName()+"/"+TEST_REPO_ID);
209 protected String prepareTestRepos()
212 removeMavenIndexes();
213 File testDir = new File( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().getAbsolutePath() ) );
214 FileUtils.deleteDirectory( testDir );
215 File sourceDir = new File( new File( "target/test-classes/" + TEST_REPO_ID ).getAbsolutePath() );
216 FileUtils.copyDirectory( sourceDir, testDir );
218 File releasesTestDir = new File( AbstractRepositoryPurgeTest.fixPath(
219 new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() ) );
221 FileUtils.deleteDirectory( releasesTestDir );
222 File sourceReleasesDir =
223 new File( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() );
224 FileUtils.copyDirectory( sourceReleasesDir, releasesTestDir );
226 return AbstractRepositoryPurgeTest.fixPath( testDir.getAbsolutePath() );
229 public String getName()
231 return StringUtils.substringAfterLast( getClass().getName(), "." );
234 protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
236 Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>( ) );
237 return Files.find(vDir, 1,
238 (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
240 translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
241 ).collect( Collectors.toList());