]> source.dussan.org Git - archiva.git/blob
5c71846879a79f55c13da9025115704b2a6069f5
[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         removeMavenIndexes();
89     }
90
91     @Test
92     public void testReleasedSnapshotsExistsInSameRepo()
93         throws Exception
94     {
95         applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, true );
96         applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
97             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
98
99         String repoRoot = prepareTestRepos();
100
101         // test listeners for the correct artifacts
102         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
103                                  "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar" );
104         listenerControl.replay();
105
106         repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
107
108         listenerControl.verify();
109
110         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
111
112         // check if the snapshot was removed
113         assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
114         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
115         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
116         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
117         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
118         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
119         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
120
121         // check if the released version was not removed
122         assertExists( projectRoot + "/2.3" );
123         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
124         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
125         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
126         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
127         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
128         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
129         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
130         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
131         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
132
133         // check if metadata file was updated
134         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
135
136         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
137
138         String expectedVersions =
139             "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
140
141         XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
142         XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
143         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
144                                      "//metadata/versioning/versions/version", metadataXml );
145         XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
146     }
147
148     @Test
149     public void testNonArtifactFile()
150         throws Exception
151     {
152
153         applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
154         applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
155             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
156
157         String repoRoot = prepareTestRepos();
158
159         // test listeners for the correct artifacts
160         listenerControl.replay();
161
162         File file = new File( repoRoot, INDEX_PATH );
163         if ( !file.exists() )
164         {
165             // help windauze to create directory with .
166             file.getParentFile().mkdirs();
167             file.createNewFile();
168         }
169         assertTrue( file.exists() );
170
171         repoPurge.process( INDEX_PATH );
172
173         listenerControl.verify();
174
175         assertTrue( file.exists() );
176     }
177
178     @Test
179     public void testReleasedSnapshotsExistsInDifferentRepo()
180         throws Exception
181     {
182
183         applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
184         applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
185             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
186
187         applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
188             getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ), false, null );
189
190         String repoRoot = prepareTestRepos();
191
192         // test listeners for the correct artifacts
193         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva",
194                                  "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
195                                  "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
196         listenerControl.replay();
197
198         repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
199
200         listenerControl.verify();
201
202         String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
203
204         // check if the snapshot was removed
205         assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
206         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
207         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
208         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
209         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
210         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
211         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
212
213         String releasesProjectRoot =
214             new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
215                 + "/org/apache/archiva/released-artifact-in-diff-repo";
216
217         // check if the released version was not removed
218         assertExists( releasesProjectRoot + "/1.0" );
219         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
220         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
221         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
222         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
223         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
224         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );
225     }
226
227     @Test
228     public void testHigherSnapshotExistsInSameRepo()
229         throws Exception
230     {
231
232         applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( TEST_REPO_ID, null, false );
233         applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository(
234             getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ), false, null );
235
236         String repoRoot = prepareTestRepos();
237
238         // test listeners for the correct artifacts - no deletions
239         listenerControl.replay();
240
241         repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
242
243         listenerControl.verify();
244
245         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
246
247         // check if the snapshot was not removed
248         assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
249         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
250         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
251         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
252         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
253         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
254         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
255
256         // check if the released version was not removed
257         assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
258         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
259         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
260         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
261         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
262         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
263         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
264
265         // check if metadata file was not updated (because nothing was removed)
266         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
267
268         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
269
270         String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
271             + "<version>2.0.4-SNAPSHOT</version></versions></expected>";
272
273         XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
274         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
275                                      "//metadata/versioning/versions/version", metadataXml );
276         XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );
277     }
278 }