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