1 package org.apache.archiva.consumers.core.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.events.RepositoryListener;
28 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
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.EasyMock;
34 import org.easymock.IMocksControl;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.runner.RunWith;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.test.context.ContextConfiguration;
41
42 import javax.inject.Inject;
43 import java.io.File;
44
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertTrue;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
49
50
51
52 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
53 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
54 public abstract class AbstractRepositoryPurgeTest
55 {
56 public static final String TEST_REPO_ID = "test-repo";
57
58 public static final String TEST_REPO_NAME = "Test Repository";
59
60 public static final int TEST_RETENTION_COUNT = 2;
61
62 public static final int TEST_DAYS_OLDER = 30;
63
64 public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
65 "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
66
67 public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
68 "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
69
70 public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
71 "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
72
73 public static final String PATH_TO_BY_RETENTION_COUNT_POM =
74 "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
75
76 public static final String PATH_TO_TEST_ORDER_OF_DELETION =
77 "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
78
79 protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
80
81 protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
82
83 private ManagedRepository config;
84
85 private ManagedRepositoryContent repo;
86
87 protected RepositoryPurge repoPurge;
88
89 protected IMocksControl listenerControl;
90
91 protected RepositoryListener listener;
92
93 protected RepositorySession repositorySession;
94
95 protected MetadataRepository metadataRepository;
96
97 @Inject
98 protected ApplicationContext applicationContext;
99
100 @Inject
101 protected PlexusSisuBridge plexusSisuBridge;
102
103
104 @Before
105 public void setUp()
106 throws Exception
107 {
108
109 removeMavenIndexes();
110
111 listenerControl = EasyMock.createControl();
112
113 listener = listenerControl.createMock( RepositoryListener.class );
114
115 repositorySession = mock( RepositorySession.class );
116 metadataRepository = mock( MetadataRepository.class );
117 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
118
119
120 }
121
122 @After
123 public void tearDown()
124 throws Exception
125 {
126 removeMavenIndexes();
127 config = null;
128 repo = null;
129
130 }
131
132 protected void removeMavenIndexes()
133 throws Exception
134 {
135 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
136 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
137 {
138 nexusIndexer.removeIndexingContext( indexingContext, false );
139 }
140 }
141
142 protected static String fixPath( String path )
143 {
144 if ( path.contains( " " ) )
145 {
146 LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
147 "You are building and testing with a path: \n " + path + " containing space. Consider relocating." );
148 return path.replaceAll( " ", "&20" );
149 }
150 return path;
151 }
152
153 public ManagedRepository getRepoConfiguration( String repoId, String repoName )
154 {
155 config = new ManagedRepository();
156 config.setId( repoId );
157 config.setName( repoName );
158 config.setDaysOlder( TEST_DAYS_OLDER );
159 String path = AbstractRepositoryPurgeTest.fixPath(
160 new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
161 config.setLocation( path );
162 config.setReleases( true );
163 config.setSnapshots( true );
164 config.setDeleteReleasedSnapshots( true );
165 config.setRetentionCount( TEST_RETENTION_COUNT );
166
167 return config;
168 }
169
170 public ManagedRepositoryContent getRepository()
171 throws Exception
172 {
173 if ( repo == null )
174 {
175 repo = applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
176 repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
177 }
178
179 return repo;
180 }
181
182 protected void assertDeleted( String path )
183 {
184 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
185 }
186
187 protected void assertExists( String path )
188 {
189 assertTrue( "File should exist: " + path, new File( path ).exists() );
190 }
191
192 protected File getTestRepoRoot()
193 {
194 return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
195 }
196
197 protected String prepareTestRepos()
198 throws Exception
199 {
200 removeMavenIndexes();
201 File testDir = new File( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().getAbsolutePath() ) );
202 FileUtils.deleteDirectory( testDir );
203 File sourceDir = new File( new File( "target/test-classes/" + TEST_REPO_ID ).getAbsolutePath() );
204 FileUtils.copyDirectory( sourceDir, testDir );
205
206 File releasesTestDir = new File( AbstractRepositoryPurgeTest.fixPath(
207 new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() ) );
208
209 FileUtils.deleteDirectory( releasesTestDir );
210 File sourceReleasesDir =
211 new File( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() );
212 FileUtils.copyDirectory( sourceReleasesDir, releasesTestDir );
213
214 return AbstractRepositoryPurgeTest.fixPath( testDir.getAbsolutePath() );
215 }
216
217 public String getName()
218 {
219 return StringUtils.substringAfterLast( getClass().getName(), "." );
220 }
221 }