]> source.dussan.org Git - archiva.git/blob
eb547668cb97693f6d1ba363d208c98db4e3b402
[archiva.git] /
1 package org.apache.maven.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.commons.io.FileUtils;
23 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.maven.archiva.database.ArchivaDatabaseException;
25 import org.apache.maven.archiva.database.ArtifactDAO;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
27 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
28 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
29 import org.codehaus.plexus.jdo.JdoFactory;
30 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31 import org.jpox.SchemaTool;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.net.URL;
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
39 import java.util.Properties;
40 import java.util.Map.Entry;
41
42 import javax.jdo.PersistenceManager;
43 import javax.jdo.PersistenceManagerFactory;
44
45 /**
46  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
47  */
48 public abstract class AbstractRepositoryPurgeTest
49     extends PlexusInSpringTestCase
50 {
51     public static final String TEST_REPO_ID = "test-repo";
52
53     public static final String TEST_REPO_NAME = "Test Repository";
54
55     public static final int TEST_RETENTION_COUNT = 2;
56
57     public static final int TEST_DAYS_OLDER = 30;
58
59     public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT = "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
60
61     public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT = "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
62
63     public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT = "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
64
65     public static final String PATH_TO_BY_RETENTION_COUNT_POM = "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
66
67     public static final String PATH_TO_RELEASED_SNAPSHOT = "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
68
69     public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS = "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
70     
71     public static final String PATH_TO_TEST_ORDER_OF_DELETION = "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
72
73     private ManagedRepositoryConfiguration config;
74
75     private ManagedRepositoryContent repo;
76
77     protected ArtifactDAO dao;
78
79     protected RepositoryPurge repoPurge;
80
81     protected void setUp()
82         throws Exception
83     {
84         super.setUp();
85
86         DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
87         assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
88
89         jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
90
91         jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
92         jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:testdb" ) );
93
94         jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
95
96         jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
97
98         jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
99
100         jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
101
102         jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
103
104         jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
105
106         jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
107
108         // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
109
110         jdoFactory.setProperty( "org.jpox.validateTables", "true" );
111
112         jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
113
114         jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
115
116         Properties properties = jdoFactory.getProperties();
117
118         for ( Entry<Object, Object> entry : properties.entrySet() )
119         {
120             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
121         }
122
123         URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) };
124
125         if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
126         {
127             fail( "Unable to process test " + getName() + " - missing package.jdo." );
128         }
129
130         File propsFile = null; // intentional
131         boolean verbose = true;
132
133         SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose );
134         SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null );
135
136         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
137
138         assertNotNull( pmf );
139
140         PersistenceManager pm = pmf.getPersistenceManager();
141
142         pm.close();
143
144         dao = (ArtifactDAO) lookup( ArtifactDAO.class.getName(), "jdo" );
145     }
146     
147     @Override
148     protected void tearDown()
149         throws Exception
150     {
151         super.tearDown();
152         config = null;
153         repo = null;
154     }
155
156     public ManagedRepositoryConfiguration getRepoConfiguration()
157     {
158         if ( config == null )
159         {
160             config = new ManagedRepositoryConfiguration();
161         }
162
163         config.setId( TEST_REPO_ID );
164         config.setName( TEST_REPO_NAME );
165         config.setDaysOlder( TEST_DAYS_OLDER );
166         config.setLocation( getTestRepoRoot().getAbsolutePath() );
167         config.setReleases( true );
168         config.setSnapshots( true );
169         config.setDeleteReleasedSnapshots( true );
170         config.setRetentionCount( TEST_RETENTION_COUNT );
171
172         return config;
173     }
174
175     public ManagedRepositoryContent getRepository()
176         throws Exception
177     {
178         if ( repo == null )
179         {
180             repo = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
181             repo.setRepository( getRepoConfiguration() );
182         }
183
184         return repo;
185     }
186
187     protected void populateDb( String groupId, String artifactId, List<String> versions )
188         throws ArchivaDatabaseException
189     {
190         for ( String version : versions )
191         {
192             ArchivaArtifact artifact = dao.createArtifact( groupId, artifactId, version, "", "jar" );
193             assertNotNull( artifact );
194             artifact.getModel().setLastModified( new Date() );
195             artifact.getModel().setOrigin( "test" );
196             ArchivaArtifact savedArtifact = dao.saveArtifact( artifact );
197             assertNotNull( savedArtifact );
198
199             //POM
200             artifact = dao.createArtifact( groupId, artifactId, version, "", "pom" );
201             assertNotNull( artifact );
202             artifact.getModel().setLastModified( new Date() );
203             artifact.getModel().setOrigin( "test" );
204             savedArtifact = dao.saveArtifact( artifact );
205             assertNotNull( savedArtifact );
206         }
207     }
208
209     protected void assertDeleted( String path )
210     {
211         assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
212     }
213
214     protected void assertExists( String path )
215     {
216         assertTrue( "File should exist: " + path, new File( path ).exists() );
217     }
218     
219     protected File getTestRepoRoot()
220     {
221         return getTestFile( "target/test-" + getName() + "/test-repo" );
222     }
223
224     protected String prepareTestRepo()
225         throws IOException
226     {
227         File testDir = getTestRepoRoot();
228         FileUtils.deleteDirectory( testDir );
229         FileUtils.copyDirectory( getTestFile( "target/test-classes/test-repo" ), testDir );
230         
231         return testDir.getAbsolutePath();
232     }
233     
234     protected void populateDbForTestOrderOfDeletion()
235         throws Exception
236     {
237         List<String> versions = new ArrayList<String>();
238         versions.add( "1.1.2-20070427.065136-1" );
239         versions.add( "1.1.2-20070506.163513-2" );
240         versions.add( "1.1.2-20070615.105019-3" );
241     
242         populateDb( "org.apache.maven.plugins", "maven-assembly-plugin", versions );
243     }
244 }