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 junit.framework.TestCase;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.metadata.repository.MetadataRepository;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.repository.ManagedRepositoryContent;
31 import org.apache.archiva.repository.events.RepositoryListener;
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.MockControl;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.runner.RunWith;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.test.context.ContextConfiguration;
42 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44 import javax.inject.Inject;
46 import java.io.IOException;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
53 @RunWith( SpringJUnit4ClassRunner.class )
54 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
55 public abstract class AbstractRepositoryPurgeTest
58 public static final String TEST_REPO_ID = "test-repo";
60 public static final String TEST_REPO_NAME = "Test Repository";
62 public static final int TEST_RETENTION_COUNT = 2;
64 public static final int TEST_DAYS_OLDER = 30;
66 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
67 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
69 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
70 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
72 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
73 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
75 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
76 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
78 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
79 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
81 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
83 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
85 private ManagedRepository config;
87 private ManagedRepositoryContent repo;
89 protected RepositoryPurge repoPurge;
91 protected MockControl listenerControl;
93 protected RepositoryListener listener;
95 protected RepositorySession repositorySession;
97 protected MetadataRepository metadataRepository;
100 protected ApplicationContext applicationContext;
103 protected PlexusSisuBridge plexusSisuBridge;
112 removeMavenIndexes();
114 listenerControl = MockControl.createControl( RepositoryListener.class );
116 listener = (RepositoryListener) listenerControl.getMock();
118 repositorySession = mock( RepositorySession.class );
119 metadataRepository = mock( MetadataRepository.class );
120 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
127 public void tearDown()
130 removeMavenIndexes();
137 protected void removeMavenIndexes()
140 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
141 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
143 nexusIndexer.removeIndexingContext( indexingContext, false );
147 public ManagedRepository getRepoConfiguration( String repoId, String repoName )
149 config = new ManagedRepository();
150 config.setId( repoId );
151 config.setName( repoName );
152 config.setDaysOlder( TEST_DAYS_OLDER );
153 config.setLocation( new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
154 config.setReleases( true );
155 config.setSnapshots( true );
156 config.setDeleteReleasedSnapshots( true );
157 config.setRetentionCount( TEST_RETENTION_COUNT );
162 public ManagedRepositoryContent getRepository()
167 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
168 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
174 protected void assertDeleted( String path )
176 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
179 protected void assertExists( String path )
181 assertTrue( "File should exist: " + path, new File( path ).exists() );
184 protected File getTestRepoRoot()
186 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
189 protected String prepareTestRepos()
192 removeMavenIndexes();
193 File testDir = getTestRepoRoot();
194 FileUtils.deleteDirectory( testDir );
195 FileUtils.copyDirectory( new File( "target/test-classes/" + TEST_REPO_ID ), testDir );
197 File releasesTestDir = new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID );
198 FileUtils.deleteDirectory( releasesTestDir );
199 FileUtils.copyDirectory( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ), releasesTestDir );
201 return testDir.getAbsolutePath();
205 public String getName()
207 return StringUtils.substringAfterLast( getClass().getName(), "." );