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