View Javadoc

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 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(), createArtifact( "org.apache.maven.plugins", "maven-plugin-plugin",
82                                                                    "2.3-SNAPSHOT", "maven-plugin" ) );
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(), createArtifact( "org.apache.archiva",
162                                                                   "released-artifact-in-diff-repo", "1.0-SNAPSHOT",
163                                                                   "jar" ) );
164         listenerControl.replay();
165         
166         repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO );
167 
168         listenerControl.verify();
169         
170         String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo";
171         
172         // check if the snapshot was removed
173         assertDeleted( projectRoot + "/1.0-SNAPSHOT" );
174         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" );
175         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" );
176         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" );
177         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" );
178         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" );
179         assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
180 
181         String releasesProjectRoot =
182             getTestFile( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath() +
183                 "/org/apache/archiva/released-artifact-in-diff-repo";
184         
185         // check if the released version was not removed
186         assertExists( releasesProjectRoot + "/1.0" );        
187         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" );
188         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" );
189         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" );
190         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" );
191         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" );
192         assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" );        
193     }
194 
195     public void testHigherSnapshotExistsInSameRepo()
196         throws Exception
197     {   
198         Configuration config = archivaConfiguration.getConfiguration();
199         config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) );
200         config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
201         
202         String repoRoot = prepareTestRepos();
203 
204         // test listeners for the correct artifacts - no deletions
205         listenerControl.replay();
206         
207         repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO );
208 
209         listenerControl.verify();
210         
211         String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin";
212         
213         // check if the snapshot was not removed
214         assertExists( projectRoot + "/2.0.3-SNAPSHOT" );
215         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar" );
216         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.md5" );
217         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar.sha1" );
218         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom" );
219         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.md5" );
220         assertExists( projectRoot + "/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.pom.sha1" );
221 
222         // check if the released version was not removed
223         assertExists( projectRoot + "/2.0.4-SNAPSHOT" );
224         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar" );
225         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.md5" );
226         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.jar.sha1" );
227         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom" );
228         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.md5" );
229         assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
230 
231         // check if metadata file was not updated (because nothing was removed)
232         File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
233 
234         String metadataXml = FileUtils.readFileToString( artifactMetadataFile, null );
235         
236         String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>" +
237         		"<version>2.0.4-SNAPSHOT</version></versions></expected>";
238         
239         XMLAssert.assertXpathEvaluatesTo( "2.0.4-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
240         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
241                                      "//metadata/versioning/versions/version", metadataXml );
242         XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml );
243     }
244 }