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.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.utils.VersionUtil;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
29 import org.apache.archiva.repository.ManagedRepositoryContent;
30 import org.apache.archiva.repository.events.RepositoryListener;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.maven.index.NexusIndexer;
35 import org.apache.maven.index.context.IndexingContext;
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;
45 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.nio.file.Files;
49 import java.nio.file.LinkOption;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.ArrayList;
53 import java.util.List;
54 import java.util.stream.Collectors;
56 import static org.junit.Assert.assertFalse;
57 import static org.junit.Assert.assertTrue;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.when;
63 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
64 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
65 public abstract class AbstractRepositoryPurgeTest
67 public static final String TEST_REPO_ID = "test-repo";
69 public static final String TEST_REPO_NAME = "Test Repository";
71 public static final int TEST_RETENTION_COUNT = 2;
73 public static final int TEST_DAYS_OLDER = 30;
75 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
76 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
78 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
79 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
81 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
82 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
84 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
85 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
87 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
88 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
90 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
92 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
94 private ManagedRepository config;
96 private ManagedRepositoryContent repo;
98 protected RepositoryPurge repoPurge;
100 protected IMocksControl listenerControl;
102 protected RepositoryListener listener;
104 protected RepositorySession repositorySession;
106 protected MetadataRepository metadataRepository;
109 protected ApplicationContext applicationContext;
112 protected PlexusSisuBridge plexusSisuBridge;
120 removeMavenIndexes();
122 listenerControl = EasyMock.createControl();
124 listener = listenerControl.createMock( RepositoryListener.class );
126 repositorySession = mock( RepositorySession.class );
127 metadataRepository = mock( MetadataRepository.class );
128 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
134 public void tearDown()
137 removeMavenIndexes();
143 protected void removeMavenIndexes()
146 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
147 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
149 nexusIndexer.removeIndexingContext( indexingContext, false );
153 protected static String fixPath( String path )
155 if ( path.contains( " " ) )
157 LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
158 "You are building and testing with a path: \n " + path + " containing space. Consider relocating." );
159 return path.replaceAll( " ", "&20" );
164 public ManagedRepository getRepoConfiguration( String repoId, String repoName )
166 config = new ManagedRepository();
167 config.setId( repoId );
168 config.setName( repoName );
169 config.setDaysOlder( TEST_DAYS_OLDER );
170 String path = AbstractRepositoryPurgeTest.fixPath(
171 new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
172 config.setLocation( path );
173 config.setReleases( true );
174 config.setSnapshots( true );
175 config.setDeleteReleasedSnapshots( true );
176 config.setRetentionCount( TEST_RETENTION_COUNT );
181 public ManagedRepositoryContent getRepository()
186 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
187 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
193 protected void assertDeleted( String path )
195 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
198 protected void assertExists( String path )
200 assertTrue( "File should exist: " + path, new File( path ).exists() );
203 protected File getTestRepoRoot()
205 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
208 protected Path getTestRepoRootPath() {
209 return Paths.get("target/test-"+getName()+"/"+TEST_REPO_ID);
212 protected String prepareTestRepos()
215 removeMavenIndexes();
216 File testDir = new File( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().getAbsolutePath() ) );
217 FileUtils.deleteDirectory( testDir );
218 File sourceDir = new File( new File( "target/test-classes/" + TEST_REPO_ID ).getAbsolutePath() );
219 FileUtils.copyDirectory( sourceDir, testDir );
221 File releasesTestDir = new File( AbstractRepositoryPurgeTest.fixPath(
222 new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() ) );
224 FileUtils.deleteDirectory( releasesTestDir );
225 File sourceReleasesDir =
226 new File( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() );
227 FileUtils.copyDirectory( sourceReleasesDir, releasesTestDir );
229 return AbstractRepositoryPurgeTest.fixPath( testDir.getAbsolutePath() );
232 public String getName()
234 return StringUtils.substringAfterLast( getClass().getName(), "." );
237 protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
239 Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>( ) );
240 return Files.find(vDir, 1,
241 (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
243 translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
244 ).collect( Collectors.toList());