View Javadoc

1   package org.apache.maven.continuum;
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.continuum.dao.DaoUtils;
23  import org.apache.continuum.dao.ProjectDao;
24  import org.apache.continuum.dao.ProjectGroupDao;
25  import org.apache.continuum.dao.ProjectScmRootDao;
26  import org.apache.continuum.dao.ScheduleDao;
27  import org.apache.maven.continuum.configuration.ConfigurationService;
28  import org.apache.maven.continuum.execution.ContinuumBuildExecutor;
29  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
30  import org.apache.maven.continuum.initialization.ContinuumInitializer;
31  import org.apache.maven.continuum.jdo.MemoryJdoFactory;
32  import org.apache.maven.continuum.model.project.BuildDefinition;
33  import org.apache.maven.continuum.model.project.Project;
34  import org.apache.maven.continuum.model.project.ProjectGroup;
35  import org.apache.maven.continuum.model.project.ProjectNotifier;
36  import org.apache.maven.continuum.model.scm.ScmResult;
37  import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
38  import org.apache.maven.continuum.store.ContinuumStoreException;
39  import org.codehaus.plexus.jdo.JdoFactory;
40  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41  import org.jpox.SchemaTool;
42  
43  import java.net.URL;
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.List;
47  import java.util.Map;
48  import java.util.Properties;
49  import javax.jdo.PersistenceManager;
50  import javax.jdo.PersistenceManagerFactory;
51  
52  /**
53   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
54   * @version $Id: AbstractContinuumTest.java 1372260 2012-08-13 04:29:09Z brett $
55   */
56  public abstract class AbstractContinuumTest
57      extends PlexusInSpringTestCase
58  {
59      private DaoUtils daoUtils;
60  
61      private ProjectDao projectDao;
62  
63      private ProjectGroupDao projectGroupDao;
64  
65      private ScheduleDao scheduleDao;
66  
67      private ProjectScmRootDao projectScmRootDao;
68  
69      // ----------------------------------------------------------------------
70      //
71      // ----------------------------------------------------------------------
72  
73      @Override
74      protected void setUp()
75          throws Exception
76      {
77          super.setUp();
78  
79          init();
80  
81          getProjectDao();
82  
83          getProjectGroupDao();
84  
85          getScheduleDao();
86  
87          getProjectScmRootDao();
88  
89          setUpConfigurationService( (ConfigurationService) lookup( "configurationService" ) );
90  
91          Collection<ProjectGroup> projectGroups = projectGroupDao.getAllProjectGroupsWithProjects();
92  
93          if ( projectGroups.size() == 0 ) //if ContinuumInitializer is loaded by Spring at startup, size == 1
94          {
95              createDefaultProjectGroup();
96  
97              projectGroups = projectGroupDao.getAllProjectGroupsWithProjects();
98          }
99  
100         assertEquals( 1, projectGroups.size() );
101     }
102 
103     @Override
104     protected void tearDown()
105         throws Exception
106     {
107         daoUtils.eraseDatabase();
108         super.tearDown();
109     }
110 
111     protected void createDefaultProjectGroup()
112         throws Exception
113     {
114         try
115         {
116             getDefaultProjectGroup();
117         }
118         catch ( ContinuumObjectNotFoundException e )
119         {
120             ProjectGroup group;
121 
122             group = new ProjectGroup();
123 
124             group.setName( "Default Project Group" );
125 
126             group.setGroupId( ContinuumInitializer.DEFAULT_PROJECT_GROUP_GROUP_ID );
127 
128             group.setDescription( "Contains all projects that do not have a group of their own" );
129 
130             projectGroupDao.addProjectGroup( group );
131         }
132     }
133 
134     public static void setUpConfigurationService( ConfigurationService configurationService )
135         throws Exception
136     {
137         configurationService.setBuildOutputDirectory( getTestFile( "target/build-output" ) );
138 
139         configurationService.setWorkingDirectory( getTestFile( "target/working-directory" ) );
140 
141         configurationService.setReleaseOutputDirectory( getTestFile( "target/release-output" ) );
142 
143         configurationService.store();
144     }
145 
146     protected ProjectGroup getDefaultProjectGroup()
147         throws ContinuumStoreException
148     {
149         return projectGroupDao.getProjectGroupByGroupIdWithProjects(
150             ContinuumInitializer.DEFAULT_PROJECT_GROUP_GROUP_ID );
151     }
152 
153     // ----------------------------------------------------------------------
154     // Store
155     // ----------------------------------------------------------------------
156 
157     private void init()
158         throws Exception
159     {
160         // ----------------------------------------------------------------------
161         // Set up the JDO factory
162         // ----------------------------------------------------------------------
163 
164         Object o = lookup( JdoFactory.ROLE, "continuum" );
165 
166         assertEquals( MemoryJdoFactory.class.getName(), o.getClass().getName() );
167 
168         MemoryJdoFactory jdoFactory = (MemoryJdoFactory) o;
169 
170 //        jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
171 //
172 //        jdoFactory.setDriverName( "org.hsqldb.jdbcDriver" );
173 
174         String url = "jdbc:hsqldb:mem:" + getClass().getName() + "." + getName();
175 
176         jdoFactory.setUrl( url );
177 
178         jdoFactory.reconfigure();
179 
180 //        jdoFactory.setUserName( "sa" );
181 //
182 //        jdoFactory.setPassword( "" );
183 //
184 //        jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_UNCOMMITTED" );
185 //
186 //        jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_UNCOMMITTED" );
187 //
188 //        jdoFactory.setProperty( "org.jpox.autoCreateTables", "true" );
189 
190         // ----------------------------------------------------------------------
191         // Check the configuration
192         // ----------------------------------------------------------------------
193 
194         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
195 
196         assertNotNull( pmf );
197 
198         assertEquals( url, pmf.getConnectionURL() );
199 
200         PersistenceManager pm = pmf.getPersistenceManager();
201 
202         pm.close();
203 
204         // ----------------------------------------------------------------------
205         //
206         // ----------------------------------------------------------------------
207 
208         Properties properties = jdoFactory.getProperties();
209 
210         for ( Map.Entry entry : properties.entrySet() )
211         {
212             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
213         }
214 
215         SchemaTool.createSchemaTables( new URL[]{getClass().getResource( "/package.jdo" )}, new URL[]{}, null, false,
216                                        null );
217 
218         // ----------------------------------------------------------------------
219         //
220         // ----------------------------------------------------------------------
221 
222         daoUtils = (DaoUtils) lookup( DaoUtils.class.getName() );
223     }
224 
225     protected ProjectDao getProjectDao()
226     {
227         if ( projectDao == null )
228         {
229             projectDao = (ProjectDao) lookup( ProjectDao.class.getName() );
230         }
231         return projectDao;
232     }
233 
234     protected ProjectGroupDao getProjectGroupDao()
235     {
236         if ( projectGroupDao == null )
237         {
238             projectGroupDao = (ProjectGroupDao) lookup( ProjectGroupDao.class.getName() );
239         }
240         return projectGroupDao;
241     }
242 
243     protected ScheduleDao getScheduleDao()
244     {
245         if ( scheduleDao == null )
246         {
247             scheduleDao = (ScheduleDao) lookup( ScheduleDao.class.getName() );
248         }
249         return scheduleDao;
250     }
251 
252     protected ProjectScmRootDao getProjectScmRootDao()
253     {
254         if ( projectScmRootDao == null )
255         {
256             projectScmRootDao = (ProjectScmRootDao) lookup( ProjectScmRootDao.class.getName() );
257         }
258         return projectScmRootDao;
259     }
260 
261     // ----------------------------------------------------------------------
262     // Build Executor
263     // ----------------------------------------------------------------------
264 
265     protected ContinuumBuildExecutor getBuildExecutor( String id )
266         throws Exception
267     {
268         ContinuumBuildExecutor buildExecutor = (ContinuumBuildExecutor) lookup( ContinuumBuildExecutor.ROLE, id );
269 
270         assertNotNull( "Could not look up build executor '" + id + "'", buildExecutor );
271 
272         return buildExecutor;
273     }
274 
275     // ----------------------------------------------------------------------
276     // Maven 2 Project Generators
277     // ----------------------------------------------------------------------
278 
279     public static Project makeStubProject( String name )
280     {
281         return makeProject( name, "foo@bar.com", "1.0" );
282     }
283 
284     public static Project makeProject( String name, String emailAddress, String version )
285     {
286         Project project = new Project();
287 
288         makeProject( project, name, version );
289 
290         List<ProjectNotifier> notifiers = createMailNotifierList( emailAddress );
291 
292         project.setNotifiers( notifiers );
293 
294         return project;
295     }
296 
297     // ----------------------------------------------------------------------
298     // Shell Project Generators
299     // ----------------------------------------------------------------------
300 
301     public static Project makeStubShellProject( String name, String script )
302     {
303         Project project = new Project();
304 
305         makeProject( project, name, "1.0" );
306         project.setExecutorId( ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR );
307 
308         BuildDefinition def = new BuildDefinition();
309         def.setBuildFile( script );
310         project.addBuildDefinition( def );
311 
312         return project;
313     }
314 
315     public static Project makeProject( Project project, String name, String version )
316     {
317         project.setExecutorId( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
318         project.setName( name );
319         project.setVersion( version );
320 
321         return project;
322     }
323 
324     protected static List<ProjectNotifier> createMailNotifierList( String emailAddress )
325     {
326         if ( emailAddress == null )
327         {
328             return null;
329         }
330 
331         ProjectNotifier notifier = new ProjectNotifier();
332 
333         notifier.setType( "mail" );
334 
335         Properties props = new Properties();
336 
337         props.put( "address", emailAddress );
338 
339         notifier.setConfiguration( props );
340 
341         List<ProjectNotifier> notifiers = new ArrayList<ProjectNotifier>();
342 
343         notifiers.add( notifier );
344 
345         return notifiers;
346     }
347 
348     // ----------------------------------------------------------------------
349     // Public utility methods
350     // ----------------------------------------------------------------------
351 
352     public Project addProject( Project project )
353         throws Exception
354     {
355         ProjectGroup defaultProjectGroup = getDefaultProjectGroup();
356 
357         // ----------------------------------------------------------------------
358         //
359         // ----------------------------------------------------------------------
360 
361         ScmResult scmResult = new ScmResult();
362 
363         scmResult.setSuccess( true );
364 
365         scmResult.setCommandOutput( "commandOutput" );
366 
367         scmResult.setProviderMessage( "providerMessage" );
368 
369         project.setCheckoutResult( scmResult );
370 
371         defaultProjectGroup.addProject( project );
372 
373         projectGroupDao.updateProjectGroup( defaultProjectGroup );
374 
375         project = projectDao.getProject( project.getId() );
376 
377         assertNotNull( "project group == null", project.getProjectGroup() );
378 
379         return project;
380     }
381 
382     public Project addProject( String name )
383         throws Exception
384     {
385         return addProject( makeStubProject( name ) );
386     }
387 
388     // ----------------------------------------------------------------------
389     // Assertions
390     // ----------------------------------------------------------------------
391 
392     public void assertProjectEquals( Project expected, Project actual )
393     {
394         assertProjectEquals( expected.getName(), expected.getNotifiers(), expected.getVersion(), actual );
395     }
396 
397     public void assertProjectEquals( String name, String emailAddress, String version, Project actual )
398     {
399         assertProjectEquals( name, createMailNotifierList( emailAddress ), version, actual );
400     }
401 
402     public void assertProjectEquals( String name, List<ProjectNotifier> notifiers, String version, Project actual )
403     {
404         assertEquals( "project.name", name, actual.getName() );
405 
406 //        assertEquals( "project.scmUrl", scmUrl, actual.getScmUrl() );
407 
408         if ( notifiers != null )
409         {
410             assertNotNull( "project.notifiers", actual.getNotifiers() );
411 
412             assertEquals( "project.notifiers.size", notifiers.size(), actual.getNotifiers().size() );
413 
414             for ( int i = 0; i < notifiers.size(); i++ )
415             {
416                 ProjectNotifier notifier = notifiers.get( i );
417 
418                 ProjectNotifier actualNotifier = (ProjectNotifier) actual.getNotifiers().get( i );
419 
420                 assertEquals( "project.notifiers.notifier.type", notifier.getType(), actualNotifier.getType() );
421 
422                 assertEquals( "project.notifiers.notifier.configuration.address", notifier.getConfiguration().get(
423                     "address" ), actualNotifier.getConfiguration().get( "address" ) );
424             }
425         }
426 
427         assertEquals( "project.version", version, actual.getVersion() );
428     }
429 
430     // ----------------------------------------------------------------------
431     // Simple utils
432     // ----------------------------------------------------------------------
433 
434     public ProjectGroup createStubProjectGroup( String name, String description )
435     {
436         ProjectGroup projectGroup = new ProjectGroup();
437 
438         projectGroup.setName( name );
439 
440         projectGroup.setGroupId( name );
441 
442         projectGroup.setDescription( description );
443 
444         return projectGroup;
445     }
446 
447     public Project addProject( String name, ProjectGroup group )
448         throws Exception
449     {
450         Project project = makeStubProject( name );
451 
452         project.setGroupId( group.getGroupId() );
453 
454         group.addProject( project );
455 
456         try
457         {
458             projectGroupDao.getProjectGroup( group.getId() );
459 
460             projectGroupDao.updateProjectGroup( group );
461         }
462         catch ( ContinuumObjectNotFoundException e )
463         {
464             projectGroupDao.addProjectGroup( group );
465         }
466 
467         return projectDao.getProject( project.getId() );
468     }
469 }