]> source.dussan.org Git - archiva.git/blob
b2f66aa7f6793c27ab5831d2c6c101731d8d1293
[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.RepositorySessionFactory;
26 import org.apache.archiva.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.RepositoryRegistry;
28 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
29 import org.apache.archiva.repository.base.RepositoryGroupHandler;
30 import org.apache.archiva.repository.maven.metadata.storage.Maven2RepositoryPathTranslator;
31 import org.apache.archiva.repository.base.BasicManagedRepository;
32 import org.apache.archiva.repository.ReleaseScheme;
33 import org.apache.archiva.repository.RepositoryContentProvider;
34 import org.apache.archiva.metadata.audit.RepositoryListener;
35 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
36 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
37 import org.apache.commons.io.FileUtils;
38 import org.apache.commons.lang3.StringUtils;
39 import org.easymock.EasyMock;
40 import org.easymock.IMocksControl;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.runner.RunWith;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.context.ApplicationContext;
46 import org.springframework.test.annotation.DirtiesContext;
47 import org.springframework.test.context.ContextConfiguration;
48
49 import javax.inject.Inject;
50 import java.io.IOException;
51 import java.net.URISyntaxException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.time.Period;
56 import java.util.ArrayList;
57 import java.util.List;
58 import java.util.stream.Collectors;
59
60 import static org.junit.Assert.*;
61 import static org.mockito.Mockito.mock;
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 IMocksControl sessionControl;
107     protected RepositorySession repositorySession;
108
109     protected IMocksControl sessionFactoryControl;
110     protected RepositorySessionFactory sessionFactory;
111
112     protected MetadataRepository metadataRepository;
113
114     @Inject
115     protected ApplicationContext applicationContext;
116
117     @SuppressWarnings( "unused" )
118     @Inject
119     RepositoryGroupHandler repositoryGroupHandler;
120
121
122     @Before
123     public void setUp()
124         throws Exception
125     {
126
127         listenerControl = EasyMock.createControl();
128
129         listener = listenerControl.createMock( RepositoryListener.class );
130
131         sessionControl = EasyMock.createControl();
132         sessionFactoryControl = EasyMock.createControl( );
133
134         repositorySession = sessionControl.createMock( RepositorySession.class );
135         metadataRepository = mock( MetadataRepository.class );
136         sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
137         EasyMock.expect( repositorySession.getRepository() ).andStubReturn( metadataRepository );
138         EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
139
140     }
141
142     @After
143     public void tearDown()
144         throws Exception
145     {
146         config = null;
147         repo = null;
148     }
149
150
151     protected static String fixPath( String path )
152     {
153         if ( path.contains( " " ) )
154         {
155             LoggerFactory.getLogger( AbstractRepositoryPurgeTest.class.getName() ).error(
156                 "You are building and testing with a path: \n {} containing space. Consider relocating.", path );
157             return path.replaceAll( " ", "&20" );
158         }
159         return path;
160     }
161
162     public org.apache.archiva.repository.ManagedRepository getRepoConfiguration( String repoId, String repoName ) throws URISyntaxException, IOException {
163         Path basePath = Paths.get("target/test-" + getName()).toAbsolutePath();
164         config = BasicManagedRepository.newFilesystemInstance( repoId, repoName, basePath.resolve(repoId));
165         config.addActiveReleaseScheme( ReleaseScheme.RELEASE );
166         config.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
167         ArtifactCleanupFeature atf = config.getFeature( ArtifactCleanupFeature.class ).get();
168         atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER) );
169         String path = AbstractRepositoryPurgeTest.fixPath(
170             basePath.resolve( repoId ).toAbsolutePath().toString() );
171         config.setLocation( Paths.get( path ).toFile().toURI() );
172         atf.setDeleteReleasedSnapshots( true );
173         atf.setRetentionCount( TEST_RETENTION_COUNT );
174
175         return config;
176     }
177
178     public ManagedRepositoryContent getRepository()
179         throws Exception
180     {
181         if ( repo == null )
182         {
183             org.apache.archiva.repository.ManagedRepository repoCfg = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
184             RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
185             repo = provider.createManagedContent( repoCfg );
186         }
187
188         return repo;
189     }
190
191     protected void assertDeleted( String path )
192     {
193         assertFalse( "File should have been deleted: " + path, Files.exists(Paths.get( path )) );
194     }
195
196     protected void assertExists( String path )
197     {
198         assertTrue( "File should exist: " + path, Files.exists(Paths.get(path)) );
199     }
200
201     protected Path getTestRepoRoot()
202     {
203         return Paths.get( "target/test-" + getName() + "/" + TEST_REPO_ID );
204     }
205
206     protected Path getTestRepoRootPath() {
207         return Paths.get("target/test-"+getName()+"/"+TEST_REPO_ID);
208     }
209
210     protected String prepareTestRepos()
211         throws Exception
212     {
213         Path testDir = Paths.get( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().toAbsolutePath().toString() ) );
214         org.apache.archiva.common.utils.FileUtils.deleteDirectory( testDir );
215         Path sourceDir = Paths.get( Paths.get( "target/test-classes/" + TEST_REPO_ID ).toAbsolutePath().toString() );
216         FileUtils.copyDirectory( sourceDir.toFile(), testDir.toFile() );
217
218         Path releasesTestDir = Paths.get( AbstractRepositoryPurgeTest.fixPath(
219             Paths.get( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() ) );
220
221         org.apache.archiva.common.utils.FileUtils.deleteDirectory( releasesTestDir );
222         Path sourceReleasesDir =
223             Paths.get( Paths.get( "target/test-classes/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() );
224         FileUtils.copyDirectory( sourceReleasesDir.toFile(), releasesTestDir.toFile() );
225
226         return AbstractRepositoryPurgeTest.fixPath( testDir.toAbsolutePath().toString() );
227     }
228
229     public String getName()
230     {
231         return StringUtils.substringAfterLast( getClass().getName(), "." );
232     }
233
234     protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
235     {
236         Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>(  ) );
237         return Files.find(vDir, 1,
238                     (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
239             .map( path ->
240                                 translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
241             ).collect( Collectors.toList());
242     }
243 }