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
23 import javax.inject.Inject;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.repository.ManagedRepositoryContent;
29 import org.apache.archiva.repository.events.RepositoryListener;
30 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.index.NexusIndexer;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.easymock.MockControl;
36 import org.junit.After;
37 import static org.junit.Assert.*;
38 import org.junit.Before;
39 import org.junit.runner.RunWith;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.test.context.ContextConfiguration;
48 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
49 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
50 public abstract class AbstractRepositoryPurgeTest
52 public static final String TEST_REPO_ID = "test-repo";
54 public static final String TEST_REPO_NAME = "Test Repository";
56 public static final int TEST_RETENTION_COUNT = 2;
58 public static final int TEST_DAYS_OLDER = 30;
60 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
61 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
63 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
64 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
66 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
67 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
69 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
70 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
72 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
73 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
75 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";
79 private ManagedRepository config;
81 private ManagedRepositoryContent repo;
83 protected RepositoryPurge repoPurge;
85 protected MockControl listenerControl;
87 protected RepositoryListener listener;
89 protected RepositorySession repositorySession;
91 protected MetadataRepository metadataRepository;
94 protected ApplicationContext applicationContext;
97 protected PlexusSisuBridge plexusSisuBridge;
105 removeMavenIndexes();
107 listenerControl = MockControl.createControl( RepositoryListener.class );
109 listener = (RepositoryListener) listenerControl.getMock();
111 repositorySession = mock( RepositorySession.class );
112 metadataRepository = mock( MetadataRepository.class );
113 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
120 public void tearDown()
123 removeMavenIndexes();
129 protected void removeMavenIndexes()
132 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
133 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
135 nexusIndexer.removeIndexingContext( indexingContext, false );
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 "
145 + path + " containing space. Consider relocating.");
146 return path.replaceAll(" ", "&20");
151 public ManagedRepository getRepoConfiguration( String repoId, String repoName )
153 config = new ManagedRepository();
154 config.setId( repoId );
155 config.setName( repoName );
156 config.setDaysOlder( TEST_DAYS_OLDER );
157 String path = AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
158 config.setLocation( path );
159 config.setReleases( true );
160 config.setSnapshots( true );
161 config.setDeleteReleasedSnapshots( true );
162 config.setRetentionCount( TEST_RETENTION_COUNT );
167 public ManagedRepositoryContent getRepository()
172 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
173 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
179 protected void assertDeleted( String path )
181 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
184 protected void assertExists( String path )
186 assertTrue( "File should exist: " + path, new File( path ).exists() );
189 protected File getTestRepoRoot()
191 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
194 protected String prepareTestRepos()
197 removeMavenIndexes();
198 File testDir = new File( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().getAbsolutePath() ) ) ;// AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot() );
199 FileUtils.deleteDirectory( testDir );
200 File sourceDir = new File ( new File( "target/test-classes/" + TEST_REPO_ID).getAbsolutePath() );
201 FileUtils.copyDirectory( sourceDir, testDir );
203 File releasesTestDir = new File( AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() ) );
205 FileUtils.deleteDirectory( releasesTestDir );
206 File sourceReleasesDir = new File ( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID).getAbsolutePath() );
207 FileUtils.copyDirectory( sourceReleasesDir, releasesTestDir );
209 return AbstractRepositoryPurgeTest.fixPath( testDir.getAbsolutePath() );
212 public String getName()
214 return StringUtils.substringAfterLast( getClass().getName(), "." );