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