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