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