View Javadoc

1   package org.apache.maven.continuum.builddefinition;
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.maven.continuum.AbstractContinuumTest;
24  import org.apache.maven.continuum.model.project.BuildDefinition;
25  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
26  import org.apache.maven.continuum.model.project.Project;
27  import org.apache.maven.continuum.model.project.ProjectGroup;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.util.List;
32  
33  /**
34   * @author <a href="mailto:olamy@apache.org">olamy</a>
35   * @version $Id: DefaultBuildDefinitionServiceTest.java 1372267 2012-08-13 05:47:30Z brett $
36   * @since 15 sept. 07
37   */
38  public class DefaultBuildDefinitionServiceTest
39      extends AbstractContinuumTest
40  {
41      private static final Logger logger = LoggerFactory.getLogger( DefaultBuildDefinitionServiceTest.class );
42  
43      private Project project;
44  
45      private BuildDefinitionTemplate buildDefinitionTemplate;
46  
47      protected void setUp()
48          throws Exception
49      {
50          super.setUp();
51          DaoUtils daoUtils = (DaoUtils) lookup( DaoUtils.class.getName() );
52          daoUtils.eraseDatabase();
53  
54          ProjectGroup projectGroup = new ProjectGroup();
55          projectGroup.setName( "test" );
56          projectGroup = getProjectGroupDao().addProjectGroup( projectGroup );
57  
58          project = new Project();
59          project.setGroupId( "foo" );
60          project.setArtifactId( "bar" );
61          project.setVersion( "0.1-alpha-1-SNAPSHOT" );
62          projectGroup.addProject( project );
63          getProjectGroupDao().updateProjectGroup( projectGroup );
64  
65          BuildDefinition buildDefinition = new BuildDefinition();
66          buildDefinition.setTemplate( true );
67          buildDefinition.setArguments( "-N" );
68          buildDefinition.setGoals( "clean test-compile" );
69          buildDefinition.setBuildFile( "pom.xml" );
70          buildDefinition.setDescription( "desc template" );
71          buildDefinition = getBuildDefinitionService().addBuildDefinition( buildDefinition );
72  
73          buildDefinitionTemplate = new BuildDefinitionTemplate();
74          buildDefinitionTemplate.setName( "test" );
75          buildDefinitionTemplate = getBuildDefinitionService().addBuildDefinitionTemplate( buildDefinitionTemplate );
76          buildDefinitionTemplate = getBuildDefinitionService().addBuildDefinitionInTemplate( buildDefinitionTemplate,
77                                                                                              buildDefinition, false );
78  
79  
80      }
81  
82      protected BuildDefinitionService getBuildDefinitionService()
83          throws Exception
84      {
85          return (BuildDefinitionService) lookup( BuildDefinitionService.class );
86      }
87  
88      public void testaddTemplateInProject()
89          throws Exception
90      {
91          try
92          {
93              List<BuildDefinitionTemplate> templates = getBuildDefinitionService().getAllBuildDefinitionTemplate();
94              assertEquals( 5, templates.size() );
95              assertEquals( 5, getBuildDefinitionService().getAllBuildDefinitions().size() );
96  
97              getBuildDefinitionService().addTemplateInProject( buildDefinitionTemplate.getId(), project );
98              project = getProjectDao().getProjectWithAllDetails( project.getId() );
99              templates = getBuildDefinitionService().getAllBuildDefinitionTemplate();
100             assertEquals( 1, project.getBuildDefinitions().size() );
101             assertEquals( 5, templates.size() );
102             List<BuildDefinition> all = getBuildDefinitionService().getAllBuildDefinitions();
103             assertEquals( 6, all.size() );
104 
105             getBuildDefinitionService().addTemplateInProject( buildDefinitionTemplate.getId(), project );
106 
107             project = getProjectDao().getProjectWithAllDetails( project.getId() );
108             templates = getBuildDefinitionService().getAllBuildDefinitionTemplate();
109             assertEquals( 2, project.getBuildDefinitions().size() );
110             assertEquals( 5, templates.size() );
111             all = getBuildDefinitionService().getAllBuildDefinitions();
112             assertEquals( 7, all.size() );
113 
114         }
115         catch ( Exception e )
116         {
117             logger.error( e.getMessage(), e );
118             throw e;
119         }
120     }
121 
122 
123     public void testGetDefaultBuildDef()
124         throws Exception
125     {
126         BuildDefinition bd =
127             (BuildDefinition) getBuildDefinitionService().getDefaultAntBuildDefinitionTemplate().getBuildDefinitions().get(
128                 0 );
129         assertNotNull( bd );
130         assertEquals( "build.xml", bd.getBuildFile() );
131 
132         bd =
133             (BuildDefinition) getBuildDefinitionService().getDefaultMavenTwoBuildDefinitionTemplate().getBuildDefinitions().get(
134                 0 );
135         BuildDefinitionService buildDefinitionService = (BuildDefinitionService) lookup( BuildDefinitionService.class );
136 
137         assertEquals( 5, buildDefinitionService.getAllBuildDefinitionTemplate().size() );
138         assertNotNull( bd );
139         assertEquals( "pom.xml", bd.getBuildFile() );
140         assertEquals( "clean install", bd.getGoals() );
141     }
142 
143 
144     public void testAddBuildDefinitionTemplate()
145         throws Exception
146     {
147         BuildDefinitionTemplate template = new BuildDefinitionTemplate();
148         template.setName( "testTemplate" );
149 
150         template = getBuildDefinitionService().addBuildDefinitionTemplate( template );
151         template = getBuildDefinitionService().getBuildDefinitionTemplate( template.getId() );
152         assertNotNull( template );
153         assertEquals( "testTemplate", template.getName() );
154         List<BuildDefinition> all = getBuildDefinitionService().getAllBuildDefinitions();
155         assertEquals( 5, all.size() );
156         BuildDefinition bd =
157             (BuildDefinition) getBuildDefinitionService().getDefaultMavenTwoBuildDefinitionTemplate().getBuildDefinitions().get(
158                 0 );
159         template = getBuildDefinitionService().addBuildDefinitionInTemplate( template, bd, false );
160         assertEquals( 1, template.getBuildDefinitions().size() );
161         all = getBuildDefinitionService().getAllBuildDefinitions();
162         assertEquals( 5, all.size() );
163 
164         template = getBuildDefinitionService().getBuildDefinitionTemplate( template.getId() );
165         template = getBuildDefinitionService().updateBuildDefinitionTemplate( template );
166         template = getBuildDefinitionService().removeBuildDefinitionFromTemplate( template, bd );
167         assertEquals( 0, template.getBuildDefinitions().size() );
168         all = getBuildDefinitionService().getAllBuildDefinitions();
169         assertEquals( 5, all.size() );
170 
171     }
172 
173     public void testAddDuplicateBuildDefinitionTemplate()
174         throws Exception
175     {
176         BuildDefinitionTemplate template = new BuildDefinitionTemplate();
177         template.setName( "test" );
178 
179         template = getBuildDefinitionService().addBuildDefinitionTemplate( template );
180         assertNull( template );
181     }
182 }