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.managed.ManagedRepositoryAdmin;
23 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.repository.RepositoryContentFactory;
26 import org.apache.archiva.repository.events.RepositoryListener;
27 import org.apache.archiva.repository.metadata.MetadataTools;
28 import org.apache.commons.io.FileUtils;
29 import org.custommonkey.xmlunit.XMLAssert;
30 import org.easymock.EasyMock;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.springframework.test.context.ContextConfiguration;
34
35 import javax.inject.Inject;
36 import java.io.File;
37 import java.nio.charset.Charset;
38 import java.util.Collections;
39 import java.util.List;
40
41 import static org.junit.Assert.assertTrue;
42
43
44
45
46 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml",
47 "classpath:/spring-context-cleanup-released-snapshots.xml" } )
48 public class CleanupReleasedSnapshotsRepositoryPurgeTest
49 extends AbstractRepositoryPurgeTest
50 {
51 private static final String INDEX_PATH = ".index\\nexus-maven-repository-index.zip";
52
53 private ArchivaConfiguration archivaConfiguration;
54
55 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
56 "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
57
58 public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO =
59 "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
60
61 public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO =
62 "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
63
64 @Inject
65 MetadataTools metadataTools;
66
67 @Before
68 @Override
69 public void setUp()
70 throws Exception
71 {
72 super.setUp();
73
74 RepositoryContentFactory factory =
75 applicationContext.getBean( "repositoryContentFactory#cleanup-released-snapshots",
76 RepositoryContentFactory.class );
77
78 archivaConfiguration =
79 applicationContext.getBean( "archivaConfiguration#cleanup-released-snapshots", ArchivaConfiguration.class );
80
81 listenerControl = EasyMock.createControl( );
82
83 listener = listenerControl.createMock( RepositoryListener.class );
84 List<RepositoryListener> listeners = Collections.singletonList( listener );
85 repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools,
86 applicationContext.getBean(
87 ManagedRepositoryAdmin.class ), factory,
88 repositorySession, listeners );
89
90 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
91 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( archivaConfiguration );
92 removeMavenIndexes();
93 }
94
95
96 public void testReleasedSnapshotsExistsInSameRepo()
97 throws Exception
98 {
99 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, true );
100 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
101 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
102
103 String repoRoot = prepareTestRepos();
104
105
106 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
107 "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
108 listenerControl.replay();
109
110 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
111
112 listenerControl.verify();
113
114 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
115
116
117 assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
118 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
119 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
120 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
121 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
122 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
123 assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
124
125
126 assertExists( projectRoot + "/2.3" );
127 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
128 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
129 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
130 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
131 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
132 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
133 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
134 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
135 assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
136
137
138 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
139
140 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
141
142 String expectedVersions =
143 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
144
145 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
146 XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
147 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
148 "//metadata/versioning/versions/version", metadataXml );
149 XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
150 }
151
152
153 public void testNonArtifactFile()
154 throws Exception
155 {
156
157 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
158 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
159 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
160
161 String repoRoot = prepareTestRepos();
162
163
164 listenerControl.replay();
165
166 File file = new File( repoRoot, INDEX_PATH );
167 if ( !file.exists() )
168 {
169
170 file.getParentFile().mkdirs();
171 file.createNewFile();
172 }
173 assertTrue( file.exists() );
174
175 repoPurge.process( INDEX_PATH );
176
177 listenerControl.verify();
178
179 assertTrue( file.exists() );
180 }
181
182
183 public void testReleasedSnapshotsExistsInDifferentRepo()
184 throws Exception
185 {
186
187 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
188 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
189 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
190
191 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
192 getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ), false, null );
193
194 String repoRoot = prepareTestRepos();
195
196
197 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
198 "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
199 "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
200 listenerControl.replay();
201
202 repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
203
204 listenerControl.verify();
205
206 String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
207
208
209 assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
210 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
211 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
212 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
213 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
214 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
215 assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
216
217 String releasesProjectRoot =
218 AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
219 + "/org/apache/archiva/released-artifact-in-diff-repo" );
220
221
222 assertExists( releasesProjectRoot + "/1.0" );
223 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
224 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
225 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
226 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
227 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
228 assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
229
230
231 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false );
232 }
233
234 @Test
235 public void testHigherSnapshotExistsInSameRepo()
236 throws Exception
237 {
238
239 applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
240 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
241 getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
242
243 String repoRoot = prepareTestRepos();
244
245
246 listenerControl.replay();
247
248 repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
249
250 listenerControl.verify();
251
252 String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
253
254
255 assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
256 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
257 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
258 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
259 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
260 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
261 assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
262
263
264 assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
265 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
266 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
267 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
268 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
269 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
270 assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
271
272
273 File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
274
275 String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
276
277 String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
278 + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
279
280 XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
281 XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
282 "//metadata/versioning/versions/version", metadataXml );
283 XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );
284 }
285 }