1 package org.apache.maven.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.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.repository.events.RepositoryListener;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
30 import org.easymock.MockControl;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.runner.RunWith;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38 import javax.inject.Inject;
40 import java.io.IOException;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
47 @RunWith( SpringJUnit4ClassRunner.class )
48 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
49 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 ManagedRepositoryConfiguration 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;
102 listenerControl = MockControl.createControl( RepositoryListener.class );
104 listener = (RepositoryListener) listenerControl.getMock();
106 repositorySession = mock( RepositorySession.class );
107 metadataRepository = mock( MetadataRepository.class );
108 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
112 public void tearDown()
120 public ManagedRepositoryConfiguration getRepoConfiguration( String repoId, String repoName )
122 config = new ManagedRepositoryConfiguration();
123 config.setId( repoId );
124 config.setName( repoName );
125 config.setDaysOlder( TEST_DAYS_OLDER );
126 config.setLocation( new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
127 config.setReleases( true );
128 config.setSnapshots( true );
129 config.setDeleteReleasedSnapshots( true );
130 config.setRetentionCount( TEST_RETENTION_COUNT );
135 public ManagedRepositoryContent getRepository()
140 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
141 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
147 protected void assertDeleted( String path )
149 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
152 protected void assertExists( String path )
154 assertTrue( "File should exist: " + path, new File( path ).exists() );
157 protected File getTestRepoRoot()
159 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
162 protected String prepareTestRepos()
165 File testDir = getTestRepoRoot();
166 FileUtils.deleteDirectory( testDir );
167 FileUtils.copyDirectory( new File( "target/test-classes/" + TEST_REPO_ID ), testDir );
169 File releasesTestDir = new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID );
170 FileUtils.deleteDirectory( releasesTestDir );
171 FileUtils.copyDirectory( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ), releasesTestDir );
173 return testDir.getAbsolutePath();
177 public String getName()
179 return StringUtils.substringAfterLast( getClass().getName(), "." );