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