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