]> source.dussan.org Git - archiva.git/blob
345d257fa93df3c528e63bd9cf7d21a22459bc33
[archiva.git] /
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.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
26 import org.apache.archiva.repository.BasicManagedRepository;
27 import org.apache.archiva.repository.ManagedRepositoryContent;
28 import org.apache.archiva.repository.ReleaseScheme;
29 import org.apache.archiva.repository.RepositoryContentProvider;
30 import org.apache.archiva.repository.events.RepositoryListener;
31 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.maven.index.NexusIndexer;
36 import org.apache.maven.index.context.IndexingContext;
37 import org.easymock.EasyMock;
38 import org.easymock.IMocksControl;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.runner.RunWith;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.test.context.ContextConfiguration;
45
46 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.net.URI;
49 import java.net.URISyntaxException;
50 import java.nio.file.Files;
51 import java.nio.file.Path;
52 import java.nio.file.Paths;
53 import java.time.Period;
54 import java.util.ArrayList;
55 import java.util.List;
56 import java.util.stream.Collectors;
57
58 import static org.junit.Assert.assertFalse;
59 import static org.junit.Assert.assertTrue;
60 import static org.mockito.Mockito.mock;
61 import static org.mockito.Mockito.when;
62
63 /**
64  */
65 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
66 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
67 public abstract class AbstractRepositoryPurgeTest
68 {
69     public static final String TEST_REPO_ID = "test-repo";
70
71     public static final String TEST_REPO_NAME = "Test Repository";
72
73     public static final int TEST_RETENTION_COUNT = 2;
74
75     public static final int TEST_DAYS_OLDER = 30;
76
77     public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
78         "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
79
80     public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT =
81         "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
82
83     public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
84         "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
85
86     public static final String PATH_TO_BY_RETENTION_COUNT_POM =
87         "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
88
89     public static final String PATH_TO_TEST_ORDER_OF_DELETION =
90         "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
91
92     protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
93
94     protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
95
96     private BasicManagedRepository config;
97
98     private ManagedRepositoryContent repo;
99
100     protected RepositoryPurge repoPurge;
101
102     protected IMocksControl listenerControl;
103
104     protected RepositoryListener listener;
105
106     protected RepositorySession repositorySession;
107
108     protected MetadataRepository metadataRepository;
109
110     @Inject
111     protected ApplicationContext applicationContext;
112
113     @Inject
114     protected NexusIndexer nexusIndexer;
115
116
117     @Before
118     public void setUp()
119         throws Exception
120     {
121
122         removeMavenIndexes();
123
124         listenerControl = EasyMock.createControl();
125
126         listener = listenerControl.createMock( RepositoryListener.class );
127
128         repositorySession = mock( RepositorySession.class );
129         metadataRepository = mock( MetadataRepository.class );
130         when( repositorySession.getRepository() ).thenReturn( metadataRepository );
131
132
133     }
134
135     @After
136     public void tearDown()
137         throws Exception
138     {
139         removeMavenIndexes();
140         config = null;
141         repo = null;
142
143     }
144
145     protected void removeMavenIndexes()
146         throws Exception
147     {
148         for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
149         {
150             nexusIndexer.removeIndexingContext( indexingContext, false );
151         }
152     }
153
154     protected static String fixPath( String path )
155     {
156         if ( path.contains( " " ) )
157         {
158             LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
159                 "You are building and testing with a path: \n {} containing space. Consider relocating.", path );
160             return path.replaceAll( " ", "&20" );
161         }
162         return path;
163     }
164
165     public org.apache.archiva.repository.ManagedRepository getRepoConfiguration( String repoId, String repoName ) throws URISyntaxException
166     {
167         Path basePath = Paths.get("target/test-" + getName()).toAbsolutePath();
168         config = new BasicManagedRepository( repoId, repoName, basePath);
169         config.addActiveReleaseScheme( ReleaseScheme.RELEASE );
170         config.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
171         ArtifactCleanupFeature atf = config.getFeature( ArtifactCleanupFeature.class ).get();
172         atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER) );
173         String path = AbstractRepositoryPurgeTest.fixPath(
174             basePath.resolve( repoId ).toAbsolutePath().toString() );
175         config.setLocation( new URI( path ) );
176         atf.setDeleteReleasedSnapshots( true );
177         atf.setRetentionCount( TEST_RETENTION_COUNT );
178
179         return config;
180     }
181
182     public ManagedRepositoryContent getRepository()
183         throws Exception
184     {
185         if ( repo == null )
186         {
187             org.apache.archiva.repository.ManagedRepository repoCfg = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
188             RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
189             repo = provider.createManagedContent( repoCfg );
190         }
191
192         return repo;
193     }
194
195     protected void assertDeleted( String path )
196     {
197         assertFalse( "File should have been deleted: " + path, Files.exists(Paths.get( path )) );
198     }
199
200     protected void assertExists( String path )
201     {
202         assertTrue( "File should exist: " + path, Files.exists(Paths.get(path)) );
203     }
204
205     protected Path getTestRepoRoot()
206     {
207         return Paths.get( "target/test-" + getName() + "/" + TEST_REPO_ID );
208     }
209
210     protected Path getTestRepoRootPath() {
211         return Paths.get("target/test-"+getName()+"/"+TEST_REPO_ID);
212     }
213
214     protected String prepareTestRepos()
215         throws Exception
216     {
217         removeMavenIndexes();
218         Path testDir = Paths.get( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().toAbsolutePath().toString() ) );
219         org.apache.archiva.common.utils.FileUtils.deleteDirectory( testDir );
220         Path sourceDir = Paths.get( Paths.get( "target/test-classes/" + TEST_REPO_ID ).toAbsolutePath().toString() );
221         FileUtils.copyDirectory( sourceDir.toFile(), testDir.toFile() );
222
223         Path releasesTestDir = Paths.get( AbstractRepositoryPurgeTest.fixPath(
224             Paths.get( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() ) );
225
226         org.apache.archiva.common.utils.FileUtils.deleteDirectory( releasesTestDir );
227         Path sourceReleasesDir =
228             Paths.get( Paths.get( "target/test-classes/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() );
229         FileUtils.copyDirectory( sourceReleasesDir.toFile(), releasesTestDir.toFile() );
230
231         return AbstractRepositoryPurgeTest.fixPath( testDir.toAbsolutePath().toString() );
232     }
233
234     public String getName()
235     {
236         return StringUtils.substringAfterLast( getClass().getName(), "." );
237     }
238
239     protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
240     {
241         Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>(  ) );
242         return Files.find(vDir, 1,
243                     (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
244             .map( path ->
245                                 translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
246             ).collect( Collectors.toList());
247     }
248 }