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.EasyMock;
36 import org.easymock.IMocksControl;
37 import org.junit.After;
38 import static org.junit.Assert.*;
39 import org.junit.Before;
40 import org.junit.runner.RunWith;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.test.context.ContextConfiguration;
49 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
50 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
51 public abstract class AbstractRepositoryPurgeTest
53 public static final String TEST_REPO_ID = "test-repo";
55 public static final String TEST_REPO_NAME = "Test Repository";
57 public static final int TEST_RETENTION_COUNT = 2;
59 public static final int TEST_DAYS_OLDER = 30;
61 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
62 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
64 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
65 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
67 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
68 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
70 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
71 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
73 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
74 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
76 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
78 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
80 private ManagedRepository config;
82 private ManagedRepositoryContent repo;
84 protected RepositoryPurge repoPurge;
86 protected IMocksControl listenerControl;
88 protected RepositoryListener listener;
90 protected RepositorySession repositorySession;
92 protected MetadataRepository metadataRepository;
95 protected ApplicationContext applicationContext;
98 protected PlexusSisuBridge plexusSisuBridge;
106 removeMavenIndexes();
108 listenerControl = EasyMock.createControl( );
110 listener = listenerControl.createMock( RepositoryListener.class );
112 repositorySession = mock( RepositorySession.class );
113 metadataRepository = mock( MetadataRepository.class );
114 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
121 public void tearDown()
124 removeMavenIndexes();
130 protected void removeMavenIndexes()
133 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
134 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
136 nexusIndexer.removeIndexingContext( indexingContext, false );
140 protected static String fixPath( String path )
142 if ( path.contains( " " ) )
144 LoggerFactory.getLogger(AbstractRepositoryPurgeTest.class.getName()).error(
145 "You are building and testing with a path: \n "
146 + path + " containing space. Consider relocating.");
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( new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
159 config.setLocation( path );
160 config.setReleases( true );
161 config.setSnapshots( true );
162 config.setDeleteReleasedSnapshots( true );
163 config.setRetentionCount( TEST_RETENTION_COUNT );
168 public ManagedRepositoryContent getRepository()
173 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
174 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
180 protected void assertDeleted( String path )
182 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
185 protected void assertExists( String path )
187 assertTrue( "File should exist: " + path, new File( path ).exists() );
190 protected File getTestRepoRoot()
192 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
195 protected String prepareTestRepos()
198 removeMavenIndexes();
199 File testDir = new File( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().getAbsolutePath() ) );
200 FileUtils.deleteDirectory( testDir );
201 File sourceDir = new File ( new File( "target/test-classes/" + TEST_REPO_ID).getAbsolutePath() );
202 FileUtils.copyDirectory( sourceDir, testDir );
204 File releasesTestDir = new File( AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() ) );
206 FileUtils.deleteDirectory( releasesTestDir );
207 File sourceReleasesDir = new File ( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID).getAbsolutePath() );
208 FileUtils.copyDirectory( sourceReleasesDir, releasesTestDir );
210 return AbstractRepositoryPurgeTest.fixPath( testDir.getAbsolutePath() );
213 public String getName()
215 return StringUtils.substringAfterLast( getClass().getName(), "." );