1 package org.apache.maven.archiva.database;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.net.URL;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import javax.jdo.PersistenceManager;
30 import javax.jdo.PersistenceManagerFactory;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
34 import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer;
35 import org.apache.maven.archiva.database.updater.TestDatabaseCleanupConsumer;
36 import org.apache.maven.archiva.database.updater.TestDatabaseUnprocessedConsumer;
37 import org.apache.maven.archiva.model.ArtifactReference;
38 import org.apache.maven.archiva.model.VersionedReference;
39 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
40 import org.codehaus.plexus.jdo.JdoFactory;
41 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
42 import org.jpox.SchemaTool;
43
44
45
46
47
48
49 public abstract class AbstractArchivaDatabaseTestCase
50 extends PlexusInSpringTestCase
51 {
52 private static final String TIMESTAMP = "yyyy/MM/dd HH:mm:ss";
53
54 protected ArchivaDAO dao;
55
56 @Override
57 protected void setUp()
58 throws Exception
59 {
60 super.setUp();
61
62 DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" );
63 assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() );
64
65 jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
66
67
68
69
70
71
72
73
74
75
76
77
78 jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) );
79 jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:" + getName() ) );
80
81 jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) );
82
83 jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) );
84
85 jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
86
87 jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" );
88
89 jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" );
90
91 jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" );
92
93 jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" );
94
95
96
97 jdoFactory.setProperty( "org.jpox.validateTables", "true" );
98
99 jdoFactory.setProperty( "org.jpox.validateColumns", "true" );
100
101 jdoFactory.setProperty( "org.jpox.validateConstraints", "true" );
102
103 Properties properties = jdoFactory.getProperties();
104
105 for ( Map.Entry<Object,Object> entry : properties.entrySet() )
106 {
107 System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
108 }
109
110 URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) };
111
112 if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) )
113 {
114 fail( "Unable to process test " + getName() + " - missing package.jdo." );
115 }
116
117 File propsFile = null;
118 boolean verbose = true;
119
120 SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose );
121 SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null );
122
123 PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
124
125 assertNotNull( pmf );
126
127 PersistenceManager pm = pmf.getPersistenceManager();
128
129 pm.close();
130
131 this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" );
132 }
133
134 protected TestDatabaseCleanupConsumer lookupTestCleanupConsumer()
135 throws Exception
136 {
137 TestDatabaseCleanupConsumer consumer = (TestDatabaseCleanupConsumer) lookup( DatabaseCleanupConsumer.class,
138 "test-db-cleanup" );
139 assertNotNull( "Test Database Cleanup Consumer should not be null.", consumer );
140 return consumer;
141 }
142
143 protected TestDatabaseUnprocessedConsumer lookupTestUnprocessedConsumer()
144 throws Exception
145 {
146 TestDatabaseUnprocessedConsumer consumer = (TestDatabaseUnprocessedConsumer) lookup(
147 DatabaseUnprocessedArtifactConsumer.class,
148 "test-db-unprocessed" );
149 assertNotNull( "Test Database Unprocessed Consumer should not be null.", consumer );
150 return consumer;
151 }
152
153 protected Date toDate( String txt )
154 throws Exception
155 {
156 SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP );
157 return sdf.parse( txt );
158 }
159
160 protected String fromDate( Date date )
161 throws Exception
162 {
163 SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP );
164 return sdf.format( date );
165 }
166
167 protected VersionedReference toVersionedReference( String id )
168 {
169 String parts[] = StringUtils.splitPreserveAllTokens( id, ':' );
170 assertEquals( "Should have 3 parts [" + id + "]", 3, parts.length );
171
172 VersionedReference ref = new VersionedReference();
173 ref.setGroupId( parts[0] );
174 ref.setArtifactId( parts[1] );
175 ref.setVersion( parts[2] );
176
177 assertTrue( "Group ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getGroupId() ) );
178 assertTrue( "Artifact ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getArtifactId() ) );
179 assertTrue( "Version should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getVersion() ) );
180
181 return ref;
182 }
183
184 protected ArtifactReference toArtifactReference( String id )
185 {
186 String parts[] = StringUtils.splitPreserveAllTokens( id, ':' );
187 assertEquals( "Should have 6 parts [" + id + "]", 6, parts.length );
188
189 ArtifactReference ref = new ArtifactReference();
190 ref.setGroupId( parts[0] );
191 ref.setArtifactId( parts[1] );
192 ref.setVersion( parts[2] );
193 ref.setClassifier( parts[3] );
194 ref.setType( parts[4] );
195
196 assertTrue( "Group ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getGroupId() ) );
197 assertTrue( "Artifact ID should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getArtifactId() ) );
198 assertTrue( "Version should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getVersion() ) );
199
200 assertNotNull( "Classifier should not be null [" + id + "]", ref.getClassifier() );
201 assertTrue( "Type should not be blank [" + id + "]", StringUtils.isNotBlank( ref.getType() ) );
202
203 return ref;
204 }
205 }