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