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.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.RepositorySession;
27 import org.apache.archiva.repository.ManagedRepositoryContent;
28 import org.apache.archiva.repository.events.RepositoryListener;
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.MockControl;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.runner.RunWith;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.test.context.ContextConfiguration;
40 import javax.inject.Inject;
42 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
49 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
50 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
51 public abstract class AbstractRepositoryPurgeTest
54 public static final String TEST_REPO_ID = "test-repo";
56 public static final String TEST_REPO_NAME = "Test Repository";
58 public static final int TEST_RETENTION_COUNT = 2;
60 public static final int TEST_DAYS_OLDER = 30;
62 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
63 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
65 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
66 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
68 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
69 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
71 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
72 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
74 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
75 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
77 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
79 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
81 private ManagedRepository config;
83 private ManagedRepositoryContent repo;
85 protected RepositoryPurge repoPurge;
87 protected MockControl listenerControl;
89 protected RepositoryListener listener;
91 protected RepositorySession repositorySession;
93 protected MetadataRepository metadataRepository;
96 protected ApplicationContext applicationContext;
99 protected PlexusSisuBridge plexusSisuBridge;
108 removeMavenIndexes();
110 listenerControl = MockControl.createControl( RepositoryListener.class );
112 listener = (RepositoryListener) listenerControl.getMock();
114 repositorySession = mock( RepositorySession.class );
115 metadataRepository = mock( MetadataRepository.class );
116 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
123 public void tearDown()
126 removeMavenIndexes();
133 protected void removeMavenIndexes()
136 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
137 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
139 nexusIndexer.removeIndexingContext( indexingContext, false );
143 public ManagedRepository getRepoConfiguration( String repoId, String repoName )
145 config = new ManagedRepository();
146 config.setId( repoId );
147 config.setName( repoName );
148 config.setDaysOlder( TEST_DAYS_OLDER );
149 config.setLocation( new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
150 config.setReleases( true );
151 config.setSnapshots( true );
152 config.setDeleteReleasedSnapshots( true );
153 config.setRetentionCount( TEST_RETENTION_COUNT );
158 public ManagedRepositoryContent getRepository()
163 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
164 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
170 protected void assertDeleted( String path )
172 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
175 protected void assertExists( String path )
177 assertTrue( "File should exist: " + path, new File( path ).exists() );
180 protected File getTestRepoRoot()
182 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
185 protected String prepareTestRepos()
188 removeMavenIndexes();
189 File testDir = getTestRepoRoot();
190 FileUtils.deleteDirectory( testDir );
191 FileUtils.copyDirectory( new File( "target/test-classes/" + TEST_REPO_ID ), testDir );
193 File releasesTestDir = new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID );
194 FileUtils.deleteDirectory( releasesTestDir );
195 FileUtils.copyDirectory( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ), releasesTestDir );
197 return testDir.getAbsolutePath();
201 public String getName()
203 return StringUtils.substringAfterLast( getClass().getName(), "." );