]> source.dussan.org Git - archiva.git/blob
fd162de8c06dee2a4d21643520a514f5bb5bb2cf
[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.codehaus.plexus.PlexusTestCase;
23 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
24 import org.codehaus.plexus.jdo.JdoFactory;
25 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
26 import org.apache.maven.archiva.model.ArchivaRepository;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
28 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
29 import org.apache.maven.archiva.repository.layout.LayoutException;
30 import org.apache.maven.archiva.repository.layout.DefaultBidirectionalRepositoryLayout;
31 import org.apache.maven.archiva.database.ArtifactDAO;
32 import org.apache.maven.archiva.database.ArchivaDatabaseException;
33 import org.jpox.SchemaTool;
34
35 import javax.jdo.PersistenceManagerFactory;
36 import javax.jdo.PersistenceManager;
37 import java.util.Properties;
38 import java.util.Iterator;
39 import java.util.Map;
40 import java.util.List;
41 import java.util.Date;
42 import java.net.URL;
43 import java.io.File;
44
45 /**
46  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
47  */
48 public class AbstractRepositoryPurgeTest
49     extends PlexusTestCase
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 String TEST_REPO_URL = "file://" + getBasedir() + "/target/test/test-repo/";
56
57     public static final int TEST_RETENTION_COUNT = 2;
58
59     public static final int TEST_DAYS_OLDER = 30;
60
61     public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT =
62            "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar";
63
64     public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT =
65         "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
66
67     public static final String PATH_TO_BY_RETENTION_COUNT_POM =
68         "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
69
70     public static final String PATH_TO_RELEASED_SNAPSHOT =
71         "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar";
72
73     public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS =
74         "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar";
75
76     private RepositoryConfiguration config;
77
78     private ArchivaRepository repo;
79
80     private BidirectionalRepositoryLayout layout;
81
82     protected ArtifactDAO dao;
83
84     protected RepositoryPurge repoPurge;
85
86     protected void setUp()
87         throws Exception
88     {
89         super.setUp();
90
91         DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
92         assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
93
94         jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
95
96         jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
97         jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:testdb" ) );
98
99         jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
100
101         jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
102
103         jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
104
105         jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
106
107         jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
108
109         jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
110
111         jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
112
113         // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" );
114
115         jdoFactory.setProperty( "org.jpox.validateTables", "true" );
116
117         jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
118
119         jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
120
121         Properties properties = jdoFactory.getProperties();
122
123         for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); )
124         {
125             Map.Entry entry = (Map.Entry) it.next();
126
127             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
128         }
129
130         URL jdoFileUrls[] = new URL[]{getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" )};
131
132         if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
133         {
134             fail( "Unable to process test " + getName() + " - missing package.jdo." );
135         }
136
137         File propsFile = null; // intentional
138         boolean verbose = true;
139
140         SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[]{}, propsFile, verbose );
141         SchemaTool.createSchemaTables( jdoFileUrls, new URL[]{}, propsFile, verbose, null );
142
143         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
144
145         assertNotNull( pmf );
146
147         PersistenceManager pm = pmf.getPersistenceManager();
148
149         pm.close();
150
151         dao = (ArtifactDAO) lookup( ArtifactDAO.class.getName(), "jdo" );
152     }
153
154     public RepositoryConfiguration getRepoConfiguration()
155     {
156         if ( config == null )
157         {
158             config = new RepositoryConfiguration();
159         }
160
161         config.setId( TEST_REPO_ID );
162         config.setName( TEST_REPO_NAME );
163         config.setDaysOlder( TEST_DAYS_OLDER );
164         config.setUrl( TEST_REPO_URL );
165         config.setReleases( true );
166         config.setSnapshots( true );
167         config.setRetentionCount( TEST_RETENTION_COUNT );
168
169         return config;
170     }
171
172     public ArchivaRepository getRepository()
173     {
174         if ( repo == null )
175         {
176             repo = new ArchivaRepository( TEST_REPO_ID, TEST_REPO_NAME, TEST_REPO_URL );
177         }
178
179         return repo;
180     }
181
182     public BidirectionalRepositoryLayout getLayout()
183         throws LayoutException
184     {
185         if ( layout == null )
186         {
187             layout = new DefaultBidirectionalRepositoryLayout();
188         }
189
190         return layout;
191     }
192
193     protected void populateDb( String groupId, String artifactId, List versions )
194         throws ArchivaDatabaseException
195     {
196         for( Iterator iter = versions.iterator(); iter.hasNext(); )
197         {
198             String version = (String) iter.next();
199             
200             ArchivaArtifact artifact =
201             dao.createArtifact( groupId, artifactId, version, "", "jar" );
202             assertNotNull( artifact );
203             artifact.getModel().setLastModified( new Date() );
204             artifact.getModel().setOrigin( "test" );
205             ArchivaArtifact savedArtifact = dao.saveArtifact( artifact );
206             assertNotNull( savedArtifact );
207
208             //POM
209             artifact =
210                 dao.createArtifact( groupId, artifactId, version, "", "pom" );
211             assertNotNull( artifact );
212             artifact.getModel().setLastModified( new Date() );
213             artifact.getModel().setOrigin( "test" );
214             savedArtifact = dao.saveArtifact( artifact );
215             assertNotNull( savedArtifact );
216         }
217     }
218 }