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