]> source.dussan.org Git - archiva.git/blob
b133c54d33bbc9e1f09a0388f47b003dbc194577
[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.commons.io.FileUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexStub;
26 import org.apache.maven.archiva.database.ArchivaDatabaseException;
27 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
28 import org.apache.maven.archiva.repository.RepositoryContentFactory;
29 import org.apache.maven.archiva.repository.metadata.MetadataTools;
30 import org.custommonkey.xmlunit.XMLAssert;
31
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 /**
39  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
40  */
41 public class CleanupReleasedSnapshotsRepositoryPurgeTest
42     extends AbstractRepositoryPurgeTest
43 {  
44     private ArchivaConfiguration archivaConfiguration;
45     
46     public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO =
47         "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar";
48     
49     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";
50
51     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";
52     
53     protected void setUp()
54         throws Exception
55     {
56         super.setUp(); 
57
58         Map<String, RepositoryContentIndex> map = new HashMap<String, RepositoryContentIndex>();
59         map.put( "filecontent", new LuceneRepositoryContentIndexStub() );
60         map.put( "hashcodes", new LuceneRepositoryContentIndexStub() );
61         map.put( "bytecode", new LuceneRepositoryContentIndexStub() );
62         
63         MetadataTools metadataTools = (MetadataTools) lookup( MetadataTools.class );
64         RepositoryContentFactory factory = (RepositoryContentFactory) lookup( RepositoryContentFactory.class, "cleanup-released-snapshots");
65         
66         archivaConfiguration =
67             (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "cleanup-released-snapshots" );
68                 
69         repoPurge =
70             new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), dao, metadataTools, map, archivaConfiguration, factory );
71     }
72
73     public void testReleasedSnapshotsExistsInSameRepo()
74         throws Exception
75     {
76         
77         Configuration config = archivaConfiguration.getConfiguration();
78         config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
79         config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
80       
81         populateReleasedSnapshotsTest();
82
83         String repoRoot = prepareTestRepos();        
84
85         repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
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         populateReleasedSnapshotsTestInDiffRepo();
134
135         String repoRoot = prepareTestRepos();        
136
137         repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
138
139         String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
140         
141         // check if the snapshot was removed
142         assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
143         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
144         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
145         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
146         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
147         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
148         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
149
150         String releasesProjectRoot =
151             getTestFile( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath() +
152                 "/org/apache/archiva/released-artifact-in-diff-repo";
153         
154         // check if the released version was not removed
155         assertExists( releasesProjectRoot + "/1.0" );        
156         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
157         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
158         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
159         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
160         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
161         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );        
162     }
163
164     public void testHigherSnapshotExistsInSameRepo()
165         throws Exception
166     {   
167         Configuration config = archivaConfiguration.getConfiguration();
168         config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
169         config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
170         
171         populateHigherSnapshotExistsTest();
172
173         String repoRoot = prepareTestRepos();
174
175         repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
176         
177         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
178         
179         // check if the snapshot was not removed
180         assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
181         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
182         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
183         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
184         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
185         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
186         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
187
188         // check if the released version was not removed
189         assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
190         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
191         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
192         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
193         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
194         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
195         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
196
197         // check if metadata file was not updated (because nothing was removed)
198         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
199
200         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
201         
202         String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>" +
203                         "<version>2.0.4-SNAPSHOT</version></versions></expected>";
204         
205         XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
206         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
207                                      "//metadata/versioning/versions/version", metadataXml );
208         XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );
209     }
210    
211     private void populateReleasedSnapshotsTest()
212         throws ArchivaDatabaseException
213     {
214         List<String> versions = new ArrayList<String>();
215         versions.add( "2.3-SNAPSHOT" );
216
217         populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions );
218     }
219
220     private void populateHigherSnapshotExistsTest()
221         throws Exception
222     {
223         List<String> versions = new ArrayList<String>();
224         versions.add( "2.0.3-SNAPSHOT" );
225
226         populateDb( "org.apache.maven.plugins", "maven-source-plugin", versions );
227     }
228     
229     private void populateReleasedSnapshotsTestInDiffRepo()
230         throws ArchivaDatabaseException
231     {
232         List<String> versions = new ArrayList<String>();
233         versions.add( "1.0-SNAPSHOT" );
234         
235         populateDb( "org.apache.archiva", "released-artifact-in-diff-repo", versions );
236     }
237
238 }