View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.continuum.builddefinition;
20  
21  import java.util.List;
22  
23  import org.apache.continuum.dao.DaoUtils;
24  import org.apache.maven.continuum.AbstractContinuumTest;
25  import org.apache.maven.continuum.model.project.BuildDefinition;
26  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
27  import org.apache.maven.continuum.model.project.Project;
28  import org.apache.maven.continuum.model.project.ProjectGroup;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * @author <a href="mailto:olamy@apache.org">olamy</a>
34   * @version $Id: DefaultBuildDefinitionServiceTest.java 794266 2009-07-15 13:35:45Z jzurbano $
35   * @since 15 sept. 07
36   */
37  public class DefaultBuildDefinitionServiceTest
38      extends AbstractContinuumTest
39  {
40      private static final Logger logger = LoggerFactory.getLogger( DefaultBuildDefinitionServiceTest.class );
41  
42      private Project project;
43  
44      private BuildDefinitionTemplate buildDefinitionTemplate;
45  
46      protected void setUp()
47          throws Exception
48      {
49          super.setUp();
50          DaoUtils daoUtils = (DaoUtils) lookup( DaoUtils.class.getName() );
51          daoUtils.eraseDatabase();
52  
53          ProjectGroup projectGroup = new ProjectGroup();
54          projectGroup.setName( "test" );
55          projectGroup = getProjectGroupDao().addProjectGroup( projectGroup );
56  
57          project = new Project();
58          project.setGroupId( "foo" );
59          project.setArtifactId( "bar" );
60          project.setVersion( "0.1-alpha-1-SNAPSHOT" );
61          projectGroup.addProject( project );
62          getProjectGroupDao().updateProjectGroup( projectGroup );
63  
64          BuildDefinition buildDefinition = new BuildDefinition();
65          buildDefinition.setTemplate( true );
66          buildDefinition.setArguments( "-N" );
67          buildDefinition.setGoals( "clean test-compile" );
68          buildDefinition.setBuildFile( "pom.xml" );
69          buildDefinition.setDescription( "desc template" );
70          buildDefinition = getBuildDefinitionService().addBuildDefinition( buildDefinition );
71  
72          buildDefinitionTemplate = new BuildDefinitionTemplate();
73          buildDefinitionTemplate.setName( "test" );
74          buildDefinitionTemplate = getBuildDefinitionService().addBuildDefinitionTemplate( buildDefinitionTemplate );
75          buildDefinitionTemplate =
76              getBuildDefinitionService().addBuildDefinitionInTemplate( buildDefinitionTemplate, buildDefinition, false );
77  
78  
79      }
80  
81      protected BuildDefinitionService getBuildDefinitionService()
82          throws Exception
83      {
84          return (BuildDefinitionService) lookup( BuildDefinitionService.class );
85      }
86  
87      public void testaddTemplateInProject()
88          throws Exception
89      {
90          try
91          {
92              List<BuildDefinitionTemplate> templates = getBuildDefinitionService().getAllBuildDefinitionTemplate();
93              assertEquals( 5, templates.size() );
94              assertEquals( 5, getBuildDefinitionService().getAllBuildDefinitions().size() );
95  
96              getBuildDefinitionService().addTemplateInProject( buildDefinitionTemplate.getId(), project );
97              project = getProjectDao().getProjectWithAllDetails( project.getId() );
98              templates = getBuildDefinitionService().getAllBuildDefinitionTemplate();
99              assertEquals( 1, project.getBuildDefinitions().size() );
100             assertEquals( 5, templates.size() );
101             List<BuildDefinition> all = getBuildDefinitionService().getAllBuildDefinitions();
102             assertEquals( 6, all.size() );
103 
104             getBuildDefinitionService().addTemplateInProject( buildDefinitionTemplate.getId(), project );
105 
106             project = getProjectDao().getProjectWithAllDetails( project.getId() );
107             templates = getBuildDefinitionService().getAllBuildDefinitionTemplate();
108             assertEquals( 2, project.getBuildDefinitions().size() );
109             assertEquals( 5, templates.size() );
110             all = getBuildDefinitionService().getAllBuildDefinitions();
111             assertEquals( 7, all.size() );
112 
113         }
114         catch ( Exception e )
115         {
116             logger.error( e.getMessage(), e );
117             throw e;
118         }
119     }
120 
121 
122     public void testGetDefaultBuildDef()
123         throws Exception
124     {
125         BuildDefinition bd =
126             (BuildDefinition) getBuildDefinitionService().getDefaultAntBuildDefinitionTemplate().getBuildDefinitions().get(
127                 0 );
128         assertNotNull( bd );
129         assertEquals( "build.xml", bd.getBuildFile() );
130 
131         bd =
132             (BuildDefinition) getBuildDefinitionService().getDefaultMavenTwoBuildDefinitionTemplate().getBuildDefinitions().get(
133                 0 );
134         BuildDefinitionService buildDefinitionService = (BuildDefinitionService) lookup( BuildDefinitionService.class );
135 
136         assertEquals( 5, buildDefinitionService.getAllBuildDefinitionTemplate().size() );
137         assertNotNull( bd );
138         assertEquals( "pom.xml", bd.getBuildFile() );
139         assertEquals( "clean install", bd.getGoals() );
140     }
141 
142 
143     public void testAddBuildDefinitionTemplate()
144         throws Exception
145     {
146         BuildDefinitionTemplate template = new BuildDefinitionTemplate();
147         template.setName( "testTemplate" );
148 
149         template = getBuildDefinitionService().addBuildDefinitionTemplate( template );
150         template = getBuildDefinitionService().getBuildDefinitionTemplate( template.getId() );
151         assertNotNull( template );
152         assertEquals( "testTemplate", template.getName() );
153         List<BuildDefinition> all = getBuildDefinitionService().getAllBuildDefinitions();
154         assertEquals( 5, all.size() );
155         BuildDefinition bd =
156             (BuildDefinition) getBuildDefinitionService().getDefaultMavenTwoBuildDefinitionTemplate().getBuildDefinitions().get(
157                 0 );
158         template = getBuildDefinitionService().addBuildDefinitionInTemplate( template, bd, false );
159         assertEquals( 1, template.getBuildDefinitions().size() );
160         all = getBuildDefinitionService().getAllBuildDefinitions();
161         assertEquals( 5, all.size() );
162 
163         template = getBuildDefinitionService().getBuildDefinitionTemplate( template.getId() );
164         template = getBuildDefinitionService().updateBuildDefinitionTemplate( template );
165         template = getBuildDefinitionService().removeBuildDefinitionFromTemplate( template, bd );
166         assertEquals( 0, template.getBuildDefinitions().size() );
167         all = getBuildDefinitionService().getAllBuildDefinitions();
168         assertEquals( 5, all.size() );
169 
170     }
171     
172     public void testAddDuplicateBuildDefinitionTemplate()
173 	    throws Exception
174 	{
175 	    BuildDefinitionTemplate template = new BuildDefinitionTemplate();
176 	    template.setName( "test" );
177 	    
178 	    template = getBuildDefinitionService().addBuildDefinitionTemplate( template );
179 	    assertNull( template );
180 	}
181 }