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