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