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.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.events.RepositoryListener;
28 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.maven.index.NexusIndexer;
32 import org.apache.maven.index.context.IndexingContext;
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;
42 import javax.inject.Inject;
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertTrue;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
52 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
53 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
54 public abstract class AbstractRepositoryPurgeTest
56 public static final String TEST_REPO_ID = "test-repo";
58 public static final String TEST_REPO_NAME = "Test Repository";
60 public static final int TEST_RETENTION_COUNT = 2;
62 public static final int TEST_DAYS_OLDER = 30;
64 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
65 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
67 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
68 "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";
73 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
74 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
76 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
77 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
79 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
81 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
83 private ManagedRepository config;
85 private ManagedRepositoryContent repo;
87 protected RepositoryPurge repoPurge;
89 protected IMocksControl listenerControl;
91 protected RepositoryListener listener;
93 protected RepositorySession repositorySession;
95 protected MetadataRepository metadataRepository;
98 protected ApplicationContext applicationContext;
101 protected NexusIndexer nexusIndexer;
109 removeMavenIndexes();
111 listenerControl = EasyMock.createControl();
113 listener = listenerControl.createMock( RepositoryListener.class );
115 repositorySession = mock( RepositorySession.class );
116 metadataRepository = mock( MetadataRepository.class );
117 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
123 public void tearDown()
126 removeMavenIndexes();
132 protected void removeMavenIndexes()
135 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
137 nexusIndexer.removeIndexingContext( indexingContext, false );
141 protected static String fixPath( String path )
143 if ( path.contains( " " ) )
145 LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
146 "You are building and testing with a path: \n {} containing space. Consider relocating.", path );
147 return path.replaceAll( " ", "&20" );
152 public ManagedRepository getRepoConfiguration( String repoId, String repoName )
154 config = new ManagedRepository();
155 config.setId( repoId );
156 config.setName( repoName );
157 config.setDaysOlder( TEST_DAYS_OLDER );
158 String path = AbstractRepositoryPurgeTest.fixPath(
159 new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
160 config.setLocation( path );
161 config.setReleases( true );
162 config.setSnapshots( true );
163 config.setDeleteReleasedSnapshots( true );
164 config.setRetentionCount( TEST_RETENTION_COUNT );
169 public ManagedRepositoryContent getRepository()
174 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
175 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
181 protected void assertDeleted( String path )
183 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
186 protected void assertExists( String path )
188 assertTrue( "File should exist: " + path, new File( path ).exists() );
191 protected File getTestRepoRoot()
193 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
196 protected String prepareTestRepos()
199 removeMavenIndexes();
200 File testDir = new File( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().getAbsolutePath() ) );
201 FileUtils.deleteDirectory( testDir );
202 File sourceDir = new File( new File( "target/test-classes/" + TEST_REPO_ID ).getAbsolutePath() );
203 FileUtils.copyDirectory( sourceDir, testDir );
205 File releasesTestDir = new File( AbstractRepositoryPurgeTest.fixPath(
206 new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() ) );
208 FileUtils.deleteDirectory( releasesTestDir );
209 File sourceReleasesDir =
210 new File( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() );
211 FileUtils.copyDirectory( sourceReleasesDir, releasesTestDir );
213 return AbstractRepositoryPurgeTest.fixPath( testDir.getAbsolutePath() );
216 public String getName()
218 return StringUtils.substringAfterLast( getClass().getName(), "." );