1 package org.apache.maven.archiva.consumers.core.repository;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
34 import java.io.IOException;
36 import java.util.Date;
37 import java.util.List;
38 import java.util.Properties;
39 import java.util.Map.Entry;
41 import javax.jdo.PersistenceManager;
42 import javax.jdo.PersistenceManagerFactory;
45 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
47 public abstract class AbstractRepositoryPurgeTest
48 extends PlexusTestCase
50 public static final String TEST_REPO_ID = "test-repo";
52 public static final String TEST_REPO_NAME = "Test Repository";
54 public static final int TEST_RETENTION_COUNT = 2;
56 public static final int TEST_DAYS_OLDER = 30;
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-20061118.060401-2.jar";
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";
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";
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";
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";
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";
70 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 private ManagedRepositoryConfiguration config;
74 private ManagedRepositoryContent repo;
76 protected ArtifactDAO dao;
78 protected RepositoryPurge repoPurge;
80 protected void setUp()
85 DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
86 assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
88 jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
90 jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
91 jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:testdb" ) );
93 jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
95 jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
97 jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
99 jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
101 jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
103 jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
105 jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
107 // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
109 jdoFactory.setProperty( "org.jpox.validateTables", "true" );
111 jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
113 jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
115 Properties properties = jdoFactory.getProperties();
117 for ( Entry<Object, Object> entry : properties.entrySet() )
119 System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
122 URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) };
124 if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
126 fail( "Unable to process test " + getName() + " - missing package.jdo." );
129 File propsFile = null; // intentional
130 boolean verbose = true;
132 SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose );
133 SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null );
135 PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
137 assertNotNull( pmf );
139 PersistenceManager pm = pmf.getPersistenceManager();
143 dao = (ArtifactDAO) lookup( ArtifactDAO.class.getName(), "jdo" );
147 protected void tearDown()
155 public ManagedRepositoryConfiguration getRepoConfiguration()
157 if ( config == null )
159 config = new ManagedRepositoryConfiguration();
162 config.setId( TEST_REPO_ID );
163 config.setName( TEST_REPO_NAME );
164 config.setDaysOlder( TEST_DAYS_OLDER );
165 config.setLocation( getTestRepoRoot().getAbsolutePath() );
166 config.setReleases( true );
167 config.setSnapshots( true );
168 config.setDeleteReleasedSnapshots( true );
169 config.setRetentionCount( TEST_RETENTION_COUNT );
174 public ManagedRepositoryContent getRepository()
179 repo = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
180 repo.setRepository( getRepoConfiguration() );
186 protected void populateDb( String groupId, String artifactId, List<String> versions )
187 throws ArchivaDatabaseException
189 for ( String version : versions )
191 ArchivaArtifact artifact = dao.createArtifact( groupId, artifactId, version, "", "jar" );
192 assertNotNull( artifact );
193 artifact.getModel().setLastModified( new Date() );
194 artifact.getModel().setOrigin( "test" );
195 ArchivaArtifact savedArtifact = dao.saveArtifact( artifact );
196 assertNotNull( savedArtifact );
199 artifact = dao.createArtifact( groupId, artifactId, version, "", "pom" );
200 assertNotNull( artifact );
201 artifact.getModel().setLastModified( new Date() );
202 artifact.getModel().setOrigin( "test" );
203 savedArtifact = dao.saveArtifact( artifact );
204 assertNotNull( savedArtifact );
208 protected void assertDeleted( String path )
210 assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
213 protected void assertExists( String path )
215 assertTrue( "File should exist: " + path, new File( path ).exists() );
218 protected File getTestRepoRoot()
220 return getTestFile( "target/test-" + getName() + "/test-repo" );
223 protected String prepareTestRepo()
226 File testDir = getTestRepoRoot();
227 FileUtils.deleteDirectory( testDir );
228 FileUtils.copyDirectory( getTestFile( "target/test-classes/test-repo" ), testDir );
230 return testDir.getAbsolutePath();