View Javadoc

1   package org.apache.maven.continuum.buildcontroller;
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.BuildDefinitionDao;
23  import org.apache.continuum.dao.BuildResultDao;
24  import org.apache.continuum.model.project.ProjectScmRoot;
25  import org.apache.continuum.utils.build.BuildTrigger;
26  import org.apache.maven.continuum.AbstractContinuumTest;
27  import org.apache.maven.continuum.core.action.AbstractContinuumAction;
28  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
29  import org.apache.maven.continuum.model.project.BuildDefinition;
30  import org.apache.maven.continuum.model.project.BuildResult;
31  import org.apache.maven.continuum.model.project.Project;
32  import org.apache.maven.continuum.model.project.ProjectDependency;
33  import org.apache.maven.continuum.model.project.Schedule;
34  import org.apache.maven.continuum.model.scm.ScmResult;
35  import org.apache.maven.continuum.project.ContinuumProjectState;
36  
37  import java.io.BufferedWriter;
38  import java.io.File;
39  import java.io.FileWriter;
40  import java.util.Calendar;
41  import java.util.List;
42  import java.util.Map;
43  
44  public class DefaultBuildControllerTest
45      extends AbstractContinuumTest
46  {
47      private DefaultBuildController controller;
48  
49      private static String FORCED_BUILD_USER = "TestUsername";
50  
51      private static String SCHEDULE_NAME = "TEST_SCHEDULE";
52  
53      int projectId1;
54  
55      int projectId2;
56  
57      int buildDefinitionId1;
58  
59      int buildDefinitionId2;
60  
61      public void setUp()
62          throws Exception
63      {
64          super.setUp();
65  
66          BuildDefinitionDao buildDefinitionDao = (BuildDefinitionDao) lookup( BuildDefinitionDao.class.getName() );
67  
68          BuildResultDao buildResultDao = (BuildResultDao) lookup( BuildResultDao.class.getName() );
69  
70          Project project1 = createProject( "project1" );
71          BuildDefinition bd1 = createBuildDefinition();
72          project1.addBuildDefinition( bd1 );
73          project1.setState( ContinuumProjectState.OK );
74          projectId1 = addProject( project1 ).getId();
75          buildDefinitionId1 = buildDefinitionDao.getDefaultBuildDefinition( projectId1 ).getId();
76          project1 = getProjectDao().getProject( projectId1 );
77          BuildResult buildResult1 = new BuildResult();
78          buildResult1.setStartTime( Calendar.getInstance().getTimeInMillis() );
79          buildResult1.setEndTime( Calendar.getInstance().getTimeInMillis() );
80          buildResult1.setState( ContinuumProjectState.OK );
81          buildResult1.setSuccess( true );
82          buildResult1.setBuildDefinition( bd1 );
83          buildResultDao.addBuildResult( project1, buildResult1 );
84          BuildResult buildResult2 = new BuildResult();
85          buildResult2.setStartTime( Calendar.getInstance().getTimeInMillis() - 7200000 );
86          buildResult2.setEndTime( Calendar.getInstance().getTimeInMillis() - 7200000 );
87          buildResult2.setSuccess( true );
88          buildResult2.setState( ContinuumProjectState.OK );
89          buildResult2.setBuildDefinition( bd1 );
90          buildResultDao.addBuildResult( project1, buildResult2 );
91          createPomFile( getProjectDao().getProjectWithAllDetails( projectId1 ) );
92  
93          Project project2 = createProject( "project2" );
94          ProjectDependency dep1 = new ProjectDependency();
95          dep1.setGroupId( "org.apache.maven.testproject" );
96          dep1.setArtifactId( "project1" );
97          dep1.setVersion( "1.0-SNAPSHOT" );
98          project2.addDependency( dep1 );
99          ProjectDependency dep2 = new ProjectDependency();
100         dep2.setGroupId( "junit" );
101         dep2.setArtifactId( "junit" );
102         dep2.setVersion( "3.8.1" );
103         project2.addDependency( dep2 );
104         BuildDefinition bd2 = createBuildDefinition();
105         project2.addBuildDefinition( bd2 );
106         project2.setState( ContinuumProjectState.OK );
107         projectId2 = addProject( project2 ).getId();
108         buildDefinitionId2 = buildDefinitionDao.getDefaultBuildDefinition( projectId2 ).getId();
109         createPomFile( getProjectDao().getProjectWithAllDetails( projectId2 ) );
110 
111         controller = (DefaultBuildController) lookup( BuildController.ROLE );
112     }
113 
114     private Project createProject( String artifactId )
115     {
116         Project project = new Project();
117         project.setExecutorId( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
118         project.setName( artifactId );
119         project.setGroupId( "org.apache.maven.testproject" );
120         project.setArtifactId( artifactId );
121         project.setVersion( "1.0-SNAPSHOT" );
122         return project;
123     }
124 
125     private BuildDefinition createBuildDefinition()
126     {
127         BuildDefinition builddef = new BuildDefinition();
128         Schedule schedule = new Schedule();
129         schedule.setName( SCHEDULE_NAME );
130         builddef.setSchedule( schedule );
131         builddef.setBuildFile( "pom.xml" );
132         builddef.setGoals( "clean" );
133         builddef.setDefaultForProject( true );
134         return builddef;
135     }
136 
137     private BuildContext getScheduledBuildContext()
138         throws Exception
139     {
140         return controller.initializeBuildContext( projectId2, buildDefinitionId2, new BuildTrigger(
141             ContinuumProjectState.TRIGGER_SCHEDULED ), new ScmResult() );
142     }
143 
144     private BuildContext getForcedBuildContext()
145         throws Exception
146     {
147         return controller.initializeBuildContext( projectId2, buildDefinitionId2, new BuildTrigger(
148             ContinuumProjectState.TRIGGER_FORCED, FORCED_BUILD_USER ), new ScmResult() );
149     }
150 
151     private BuildContext getContext( int hourOfLastExecution )
152         throws Exception
153     {
154         BuildContext context = getScheduledBuildContext();
155         BuildResult oldBuildResult = new BuildResult();
156         oldBuildResult.setEndTime( Calendar.getInstance().getTimeInMillis() + ( hourOfLastExecution * 3600000 ) );
157         context.setOldBuildResult( oldBuildResult );
158         context.setScmResult( new ScmResult() );
159 
160         Map<String, Object> actionContext = context.getActionContext();
161         ProjectScmRoot projectScmRoot = new ProjectScmRoot();
162         projectScmRoot.setId( 1 );
163         projectScmRoot.setScmRootAddress( "scm:local:src/test-projects:flat-multi-module" );
164         AbstractContinuumAction.setProjectScmRoot( actionContext, projectScmRoot );
165 
166         return context;
167     }
168 
169     public void testWithoutDependencyChanges()
170         throws Exception
171     {
172         BuildContext context = getContext( +1 );
173         controller.checkProjectDependencies( context );
174         assertEquals( 0, context.getModifiedDependencies().size() );
175         assertFalse( controller.shouldBuild( context ) );
176     }
177 
178     public void testWithNewProjects()
179         throws Exception
180     {
181         Project p1 = getProjectDao().getProject( projectId1 );
182         p1.setState( ContinuumProjectState.NEW );
183         getProjectDao().updateProject( p1 );
184 
185         Project p2 = getProjectDao().getProject( projectId2 );
186         p2.setState( ContinuumProjectState.NEW );
187         getProjectDao().updateProject( p2 );
188 
189         BuildContext context = getScheduledBuildContext();
190         controller.checkProjectDependencies( context );
191         assertEquals( 0, context.getModifiedDependencies().size() );
192         assertTrue( controller.shouldBuild( context ) );
193     }
194 
195     public void testWithNewBuildDefinition()
196         throws Exception
197     {
198         BuildContext context = getScheduledBuildContext();
199         assertNull( context.getOldBuildResult() );
200         assertTrue( controller.shouldBuild( context ) );
201     }
202 
203     public void testWithDependencyChanges()
204         throws Exception
205     {
206         BuildContext context = getContext( -1 );
207         controller.checkProjectDependencies( context );
208         assertEquals( 1, context.getModifiedDependencies().size() );
209         assertTrue( controller.shouldBuild( context ) );
210     }
211 
212     public void testWithNullScmResult()
213         throws Exception
214     {
215         BuildContext context = getContext( +1 );
216         context.setScmResult( null );
217         controller.checkProjectDependencies( context );
218         assertEquals( 0, context.getModifiedDependencies().size() );
219         assertFalse( controller.shouldBuild( context ) );
220     }
221 
222     public void testForcedBuildTriggeredByField()
223         throws Exception
224     {
225         BuildContext context = getForcedBuildContext();
226         assertEquals( FORCED_BUILD_USER, context.getBuildTrigger().getTriggeredBy() );
227     }
228 
229     public void testScheduledBuildTriggeredByField()
230         throws Exception
231     {
232         BuildContext context = getScheduledBuildContext();
233         assertEquals( SCHEDULE_NAME, context.getBuildTrigger().getTriggeredBy() );
234     }
235 
236     public void testScheduledBuildTriggeredByField_UsernameProvided()
237         throws Exception
238     {
239         BuildTrigger buildTrigger = new BuildTrigger( ContinuumProjectState.TRIGGER_SCHEDULED, "test-user" );
240 
241         BuildContext context = controller.initializeBuildContext( projectId2, buildDefinitionId2, buildTrigger,
242                                                                   new ScmResult() );
243 
244         String contextTriggeredBy = context.getBuildTrigger().getTriggeredBy();
245         assertFalse( "test-user".equals( contextTriggeredBy ) );
246         assertEquals( SCHEDULE_NAME, contextTriggeredBy );
247     }
248 
249     private File getWorkingDirectory()
250         throws Exception
251     {
252         File workingDirectory = getTestFile( "target/working-directory" );
253 
254         if ( !workingDirectory.exists() )
255         {
256             workingDirectory.mkdir();
257         }
258 
259         return workingDirectory;
260     }
261 
262     private File getWorkingDirectory( Project project )
263         throws Exception
264     {
265         File projectDir = new File( getWorkingDirectory(), Integer.toString( project.getId() ) );
266 
267         if ( !projectDir.exists() )
268         {
269             projectDir.mkdirs();
270             System.out.println( "projectdirectory created" + projectDir.getAbsolutePath() );
271         }
272 
273         return projectDir;
274     }
275 
276     private void createPomFile( Project project )
277         throws Exception
278     {
279         File pomFile = new File( getWorkingDirectory( project ), "pom.xml" );
280 
281         BufferedWriter out = new BufferedWriter( new FileWriter( pomFile ) );
282         out.write( "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" " +
283                        "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
284                        "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" );
285         out.write( "<modelVersion>4.0.0</modelVersion>\n" );
286         out.write( "<groupId>" + project.getGroupId() + "</groupId>\n" );
287         out.write( "<artifactId>" + project.getArtifactId() + "</artifactId>\n" );
288         out.write( "<version>" + project.getVersion() + "</version>\n" );
289         out.write( "<scm>\n" );
290         out.write( "<connection>" + "scm:local|" + getWorkingDirectory().getAbsolutePath() +
291                        "|" + project.getId() + "</connection>\n" );
292         out.write( "</scm>" );
293 
294         if ( project.getDependencies().size() > 0 )
295         {
296             out.write( "<dependencies>\n" );
297 
298             List<ProjectDependency> dependencies = project.getDependencies();
299 
300             for ( ProjectDependency dependency : dependencies )
301             {
302                 out.write( "<dependency>\n" );
303                 out.write( "<groupId>" + dependency.getGroupId() + "</groupId>\n" );
304                 out.write( "<artifactId>" + dependency.getArtifactId() + "</artifactId>\n" );
305                 out.write( "<version>" + dependency.getVersion() + "</version>\n" );
306                 out.write( "</dependency>\n" );
307             }
308             out.write( "</dependencies>\n" );
309         }
310 
311         out.write( "</project>" );
312         out.close();
313 
314         System.out.println( "pom file created" );
315     }
316 }