]> source.dussan.org Git - archiva.git/blob
292e7a99b62bc84361df243c450aec03722a1563
[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 java.io.File;
23 import java.util.Collections;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.repository.RepositoryContentFactory;
29 import org.apache.maven.archiva.repository.events.RepositoryListener;
30 import org.apache.maven.archiva.repository.metadata.MetadataTools;
31 import org.custommonkey.xmlunit.XMLAssert;
32 import org.easymock.MockControl;
33
34
35 /**
36  */
37 public class CleanupReleasedSnapshotsRepositoryPurgeTest
38     extends AbstractRepositoryPurgeTest
39 {  
40     private ArchivaConfiguration archivaConfiguration;
41
42     public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
43         "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
44     
45     public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO = "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
46
47     public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO = "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
48     
49     @Override
50     protected void setUp()
51         throws Exception
52     {
53         super.setUp();
54         
55         MetadataTools metadataTools = (MetadataTools) lookup( MetadataTools.class );
56         RepositoryContentFactory factory = (RepositoryContentFactory) lookup( RepositoryContentFactory.class, "cleanup-released-snapshots");
57         
58         archivaConfiguration =
59             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "cleanup-released-snapshots" );
60
61         listenerControl = MockControl.createControl( RepositoryListener.class );
62         
63         listener = (RepositoryListener) listenerControl.getMock();
64         repoPurge =
65             new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools, archivaConfiguration, factory,
66                                                          Collections.singletonList( listener ) );
67     }
68
69     public void testReleasedSnapshotsExistsInSameRepo()
70         throws Exception
71     {
72         Configuration config = archivaConfiguration.getConfiguration();
73         config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
74         config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
75       
76         String repoRoot = prepareTestRepos();        
77
78         // test listeners for the correct artifacts
79         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-plugin-plugin",
80                                                                   "2.3-SNAPSHOT", "maven-plugin" ) );
81         listenerControl.replay();
82         
83         repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
84         
85         listenerControl.verify();
86
87         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin";
88         
89         // check if the snapshot was removed
90         assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
91         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
92         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
93         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
94         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
95         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
96         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
97
98         // check if the released version was not removed
99         assertExists( projectRoot + "/2.3" );
100         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar" );
101         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5" );
102         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1" );
103         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar" );
104         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5" );
105         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1" );
106         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom" );
107         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5" );
108         assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
109
110         // check if metadata file was updated
111         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
112         
113         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
114         
115         String expectedVersions = "<expected><versions><version>2.2</version>" +
116                         "<version>2.3</version></versions></expected>";
117         
118         XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/release", metadataXml );
119         XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
120         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
121                                      "//metadata/versioning/versions/version", metadataXml );
122         XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
123     }
124     
125     public void testReleasedSnapshotsExistsInDifferentRepo()
126         throws Exception
127     {   
128         Configuration config = archivaConfiguration.getConfiguration();
129         config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
130         config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
131         config.addManagedRepository( getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ) );
132         
133         String repoRoot = prepareTestRepos();        
134
135         // test listeners for the correct artifacts
136         listener.deleteArtifact( getRepository(), createArtifact( "org.apache.archiva",
137                                                                   "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
138                                                                   "jar" ) );
139         listenerControl.replay();
140         
141         repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
142
143         listenerControl.verify();
144         
145         String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
146         
147         // check if the snapshot was removed
148         assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
149         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
150         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
151         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
152         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
153         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
154         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
155
156         String releasesProjectRoot =
157             getTestFile( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath() +
158                 "/org/apache/archiva/released-artifact-in-diff-repo";
159         
160         // check if the released version was not removed
161         assertExists( releasesProjectRoot + "/1.0" );        
162         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
163         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
164         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
165         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
166         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
167         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );        
168     }
169
170     public void testHigherSnapshotExistsInSameRepo()
171         throws Exception
172     {   
173         Configuration config = archivaConfiguration.getConfiguration();
174         config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
175         config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
176         
177         String repoRoot = prepareTestRepos();
178
179         // test listeners for the correct artifacts - no deletions
180         listenerControl.replay();
181         
182         repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
183
184         listenerControl.verify();
185         
186         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
187         
188         // check if the snapshot was not removed
189         assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
190         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
191         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
192         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
193         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
194         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
195         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
196
197         // check if the released version was not removed
198         assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
199         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
200         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
201         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
202         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
203         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
204         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
205
206         // check if metadata file was not updated (because nothing was removed)
207         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
208
209         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
210         
211         String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>" +
212                         "<version>2.0.4-SNAPSHOT</version></versions></expected>";
213         
214         XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
215         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
216                                      "//metadata/versioning/versions/version", metadataXml );
217         XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );
218     }
219 }