View Javadoc

1   package org.apache.maven.continuum.release.executors;
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.release.config.ContinuumReleaseDescriptor;
23  import org.apache.maven.continuum.release.ContinuumReleaseManager;
24  import org.apache.maven.continuum.release.tasks.PerformReleaseProjectTask;
25  import org.apache.maven.continuum.release.tasks.PrepareReleaseProjectTask;
26  import org.apache.maven.continuum.release.tasks.RollbackReleaseProjectTask;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmVersion;
29  import org.apache.maven.scm.manager.NoSuchScmProviderException;
30  import org.apache.maven.scm.manager.ScmManager;
31  import org.apache.maven.scm.repository.ScmRepository;
32  import org.apache.maven.scm.repository.ScmRepositoryException;
33  import org.apache.maven.shared.release.ReleaseResult;
34  import org.apache.maven.shared.release.config.ReleaseDescriptor;
35  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
36  import org.codehaus.plexus.taskqueue.Task;
37  import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
38  import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
39  import org.codehaus.plexus.util.FileUtils;
40  import org.codehaus.plexus.util.IOUtil;
41  
42  import java.io.File;
43  import java.io.FileInputStream;
44  import java.io.FileOutputStream;
45  import java.io.InputStream;
46  import java.io.OutputStream;
47  import java.util.Properties;
48  
49  /**
50   * @author Edwin Punzalan
51   */
52  public class ReleaseTaskExecutorTest
53      extends PlexusInSpringTestCase
54  {
55      private ScmManager scmManager;
56  
57      private TaskExecutor prepareExec;
58  
59      private TaskExecutor performExec;
60  
61      private TaskExecutor rollbackExec;
62  
63      private ContinuumReleaseManager releaseManager;
64  
65      protected void setUp()
66          throws Exception
67      {
68          super.setUp();
69  
70          if ( scmManager == null )
71          {
72              scmManager = (ScmManager) lookup( "org.apache.maven.scm.manager.ScmManager" );
73          }
74  
75          if ( prepareExec == null )
76          {
77              prepareExec = (TaskExecutor) lookup( TaskExecutor.class.getName(), "prepare-release" );
78          }
79  
80          if ( performExec == null )
81          {
82              performExec = (TaskExecutor) lookup( TaskExecutor.class.getName(), "perform-release" );
83          }
84  
85          if ( rollbackExec == null )
86          {
87              rollbackExec = (TaskExecutor) lookup( TaskExecutor.class.getName(), "rollback-release" );
88          }
89  
90          if ( releaseManager == null )
91          {
92              releaseManager = (ContinuumReleaseManager) lookup( ContinuumReleaseManager.ROLE );
93          }
94          File scmPath = new File( getBasedir(), "target/scm-src" ).getAbsoluteFile();
95          File scmTargetPath = new File( getBasedir(), "target/scm-test" ).getAbsoluteFile();
96          FileUtils.copyDirectoryStructure( scmPath, scmTargetPath );
97      }
98  
99      public void releaseSimpleProject()
100         throws Exception
101     {
102         String scmPath = new File( getBasedir(), "target/scm-test" ).getAbsolutePath().replace( '\\', '/' );
103         File workDir = new File( getBasedir(), "target/test-classes/work-dir" );
104         FileUtils.deleteDirectory( workDir );
105         File testDir = new File( getBasedir(), "target/test-classes/test-dir" );
106         FileUtils.deleteDirectory( testDir );
107 
108         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
109         descriptor.setInteractive( false );
110         descriptor.setScmSourceUrl( "scm:svn:file://localhost/" + scmPath + "/trunk" );
111         descriptor.setWorkingDirectory( workDir.getAbsolutePath() );
112 
113         ScmRepository repository = getScmRepositorty( descriptor.getScmSourceUrl() );
114         ScmFileSet fileSet = new ScmFileSet( workDir );
115         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
116 
117         String pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
118         assertTrue( "Test dev version", pom.indexOf( "<version>1.0-SNAPSHOT</version>" ) > 0 );
119 
120         doPrepareWithNoError( descriptor );
121 
122         pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
123         assertTrue( "Test version increment", pom.indexOf( "<version>1.1-SNAPSHOT</version>" ) > 0 );
124 
125         repository = getScmRepositorty( "scm:svn:file://localhost/" + scmPath + "/tags/test-artifact-1.0" );
126         fileSet = new ScmFileSet( testDir );
127         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
128 
129         pom = FileUtils.fileRead( new File( testDir, "pom.xml" ) );
130         assertTrue( "Test released version", pom.indexOf( "<version>1.0</version>" ) > 0 );
131     }
132 
133     public void testReleases()
134         throws Exception
135     {
136         releaseSimpleProject();
137         releaseAndRollbackProject();
138         releaseSimpleProjectWithNextVersion();
139         releasePerformWithExecutableInDescriptor();
140         releaseProjectWithDependencyOfCustomPackagingType();
141         releaseProjectWithProfile();
142     }
143 
144     public void releaseSimpleProjectWithNextVersion()
145         throws Exception
146     {
147         String scmPath = new File( getBasedir(), "target/scm-test" ).getAbsolutePath().replace( '\\', '/' );
148         File workDir = new File( getBasedir(), "target/test-classes/work-dir" );
149         FileUtils.deleteDirectory( workDir );
150         File testDir = new File( getBasedir(), "target/test-classes/test-dir" );
151         FileUtils.deleteDirectory( testDir );
152 
153         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
154         descriptor.setInteractive( false );
155         descriptor.setScmSourceUrl( "scm:svn:file://localhost/" + scmPath + "/trunk" );
156         descriptor.setWorkingDirectory( workDir.getAbsolutePath() );
157         descriptor.mapReleaseVersion( "test-group:test-artifact", "2.0" );
158         descriptor.mapDevelopmentVersion( "test-group:test-artifact", "2.1-SNAPSHOT" );
159 
160         ScmRepository repository = getScmRepositorty( descriptor.getScmSourceUrl() );
161         ScmFileSet fileSet = new ScmFileSet( workDir );
162         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
163 
164         String pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
165         assertTrue( "Test dev version", pom.indexOf( "<version>1.1-SNAPSHOT</version>" ) > 0 );
166 
167         doPrepareWithNoError( descriptor );
168 
169         pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
170         assertTrue( "Test version increment", pom.indexOf( "<version>2.1-SNAPSHOT</version>" ) > 0 );
171 
172         repository = getScmRepositorty( "scm:svn:file://localhost/" + scmPath + "/tags/test-artifact-2.0" );
173         fileSet = new ScmFileSet( testDir );
174         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
175 
176         pom = FileUtils.fileRead( new File( testDir, "pom.xml" ) );
177         assertTrue( "Test released version", pom.indexOf( "<version>2.0</version>" ) > 0 );
178 
179 /* CONTINUUM-2559
180         performExec.executeTask(
181             getPerformTask( "testRelease", descriptor, new File( getBasedir(), "target/test-classes/build-dir" ) ) );
182 
183         ReleaseResult result = (ReleaseResult) releaseManager.getReleaseResults().get( "testRelease" );
184         if ( result.getResultCode() != ReleaseResult.SUCCESS )
185         {
186             fail( "Error in release:perform. Release output follows:\n" + result.getOutput() );
187         }
188 */
189     }
190 
191     public void releaseAndRollbackProject()
192         throws Exception
193     {
194         String scmPath = new File( getBasedir(), "target/scm-test" ).getAbsolutePath().replace( '\\', '/' );
195         File workDir = new File( getBasedir(), "target/test-classes/work-dir" );
196         FileUtils.deleteDirectory( workDir );
197         File testDir = new File( getBasedir(), "target/test-classes/test-dir" );
198         FileUtils.deleteDirectory( testDir );
199 
200         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
201         descriptor.setInteractive( false );
202         descriptor.setScmSourceUrl( "scm:svn:file://localhost/" + scmPath + "/trunk" );
203         descriptor.setWorkingDirectory( workDir.getAbsolutePath() );
204 
205         ScmRepository repository = getScmRepositorty( descriptor.getScmSourceUrl() );
206         ScmFileSet fileSet = new ScmFileSet( workDir );
207         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
208 
209         String pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
210         assertTrue( "Test dev version", pom.indexOf( "<version>1.1-SNAPSHOT</version>" ) > 0 );
211 
212         doPrepareWithNoError( descriptor );
213 
214         pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
215         assertTrue( "Test version increment", pom.indexOf( "<version>1.2-SNAPSHOT</version>" ) > 0 );
216 
217         repository = getScmRepositorty( "scm:svn:file://localhost/" + scmPath + "/tags/test-artifact-1.1" );
218         fileSet = new ScmFileSet( testDir );
219         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
220 
221         pom = FileUtils.fileRead( new File( testDir, "pom.xml" ) );
222         assertTrue( "Test released version", pom.indexOf( "<version>1.1</version>" ) > 0 );
223 
224         rollbackExec.executeTask( new RollbackReleaseProjectTask( "testRelease", descriptor, null ) );
225 
226         pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
227         assertTrue( "Test rollback version", pom.indexOf( "<version>1.1-SNAPSHOT</version>" ) > 0 );
228 
229         assertFalse( "Test that release.properties has been cleaned", new File( workDir,
230                                                                                 "release.properties" ).exists() );
231         assertFalse( "Test that backup file has been cleaned", new File( workDir, "pom.xml.releaseBackup" ).exists() );
232 
233         //@todo when implemented already, check if tag was also removed
234     }
235 
236     public void releasePerformWithExecutableInDescriptor()
237         throws Exception
238     {
239         String scmPath = new File( getBasedir(), "target/scm-test" ).getAbsolutePath().replace( '\\', '/' );
240         File workDir = new File( getBasedir(), "target/test-classes/work-dir" );
241         FileUtils.deleteDirectory( workDir );
242         File testDir = new File( getBasedir(), "target/test-classes/test-dir" );
243         FileUtils.deleteDirectory( testDir );
244 
245         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
246         descriptor.setInteractive( false );
247         descriptor.setScmSourceUrl( "scm:svn:file://localhost/" + scmPath + "/trunk" );
248         descriptor.setWorkingDirectory( workDir.getAbsolutePath() );
249 
250         ScmRepository repository = getScmRepositorty( descriptor.getScmSourceUrl() );
251         ScmFileSet fileSet = new ScmFileSet( workDir );
252         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
253 
254         String pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
255         assertTrue( "Test dev version", pom.indexOf( "<version>2.1-SNAPSHOT</version>" ) > 0 );
256 
257         doPrepareWithNoError( descriptor );
258 
259         pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
260         assertTrue( "Test version increment", pom.indexOf( "<version>2.2-SNAPSHOT</version>" ) > 0 );
261 
262         repository = getScmRepositorty( "scm:svn:file://localhost/" + scmPath + "/tags/test-artifact-2.1" );
263         fileSet = new ScmFileSet( testDir );
264         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
265 
266         pom = FileUtils.fileRead( new File( testDir, "pom.xml" ) );
267         assertTrue( "Test released version", pom.indexOf( "<version>2.1</version>" ) > 0 );
268 
269         File file = new File( descriptor.getWorkingDirectory(), "release.properties" );
270         assertTrue( "release.properties file does not exist", file.exists() );
271 
272         Properties properties = new Properties();
273 
274         InputStream inStream = null;
275         OutputStream outStream = null;
276 
277         try
278         {
279             inStream = new FileInputStream( file );
280 
281             properties.load( inStream );
282 
283             properties.setProperty( "build.executable", "test/executable/mvn" );
284 
285             outStream = new FileOutputStream( file );
286 
287             properties.store( outStream, "release configuration" );
288         }
289         finally
290         {
291             IOUtil.close( inStream );
292         }
293 
294         performExec.executeTask( getPerformTask( "testRelease", descriptor, new File( getBasedir(),
295                                                                                       "target/test-classes/build-dir" ) ) );
296 
297         ReleaseResult result = (ReleaseResult) releaseManager.getReleaseResults().get( "testRelease" );
298 
299         if ( !result.getOutput().replace( "\\", "/" ).contains( "test/executable/mvn" ) )
300         {
301             fail( "Error in release:perform. Missing executable" );
302         }
303     }
304 
305     // CONTINUUM-1814
306     public void releaseProjectWithDependencyOfCustomPackagingType()
307         throws Exception
308     {
309         String scmPath = new File( getBasedir(), "target/scm-test/continuum-1814" ).getAbsolutePath().replace( '\\',
310                                                                                                                '/' );
311         File workDir = new File( getBasedir(), "target/test-classes/continuum-1814" );
312         FileUtils.deleteDirectory( workDir );
313         File testDir = new File( getBasedir(), "target/test-classes/test-dir" );
314         FileUtils.deleteDirectory( testDir );
315 
316         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
317         descriptor.setInteractive( false );
318         descriptor.setScmSourceUrl( "scm:svn:file://localhost/" + scmPath + "/trunk" );
319         descriptor.setWorkingDirectory( workDir.getAbsolutePath() );
320 
321         ScmRepository repository = getScmRepositorty( descriptor.getScmSourceUrl() );
322         ScmFileSet fileSet = new ScmFileSet( workDir );
323         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
324 
325         String pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
326         assertTrue( "Test dev version", pom.indexOf( "<version>1.6-SNAPSHOT</version>" ) > 0 );
327 
328         doPrepareWithNoError( descriptor );
329 
330         pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
331         assertTrue( "Test version increment", pom.indexOf( "<version>1.7-SNAPSHOT</version>" ) > 0 );
332 
333         repository = getScmRepositorty( "scm:svn:file://localhost/" + scmPath + "/tags/continuum-1814-1.6" );
334         fileSet = new ScmFileSet( testDir );
335         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
336 
337         pom = FileUtils.fileRead( new File( testDir, "pom.xml" ) );
338         assertTrue( "Test released version", pom.indexOf( "<version>1.6</version>" ) > 0 );
339 
340 /* CONTINUUM-2559
341         performExec.executeTask(
342                 getPerformTask( "testRelease", descriptor, new File( getBasedir(), "target/test-classes/build-dir" ) ) );
343 
344         ReleaseResult result = (ReleaseResult) releaseManager.getReleaseResults().get( "testRelease" );
345         if ( result.getResultCode() != ReleaseResult.SUCCESS )
346         {
347             fail( "Error in release:perform. Release output follows:\n" + result.getOutput() );
348         }
349 */
350     }
351 
352     // CONTINUUM-2610
353     public void releaseProjectWithProfile()
354         throws Exception
355     {
356         String scmPath = new File( getBasedir(), "target/scm-test/continuum-2610" ).getAbsolutePath().replace( '\\',
357                                                                                                                '/' );
358         File workDir = new File( getBasedir(), "target/test-classes/continuum-2610" );
359         FileUtils.deleteDirectory( workDir );
360         File testDir = new File( getBasedir(), "target/test-classes/test-dir" );
361         FileUtils.deleteDirectory( testDir );
362 
363         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
364         descriptor.setInteractive( false );
365         descriptor.setScmSourceUrl( "scm:svn:file://localhost/" + scmPath + "/trunk" );
366         descriptor.setWorkingDirectory( workDir.getAbsolutePath() );
367         descriptor.setAdditionalArguments( "-Pall" );
368 
369         ScmRepository repository = getScmRepositorty( descriptor.getScmSourceUrl() );
370         ScmFileSet fileSet = new ScmFileSet( workDir );
371         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
372 
373         String pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
374         assertTrue( "Test root dev version", pom.indexOf( "<version>1.0-SNAPSHOT</version>" ) > 0 );
375         String moduleAPom = FileUtils.fileRead( new File( workDir, "module-A/pom.xml" ) );
376         assertTrue( "Test module A dev version", moduleAPom.indexOf( "<version>1.0-SNAPSHOT</version>" ) > 0 );
377         String moduleBPom = FileUtils.fileRead( new File( workDir, "module-B/pom.xml" ) );
378         assertTrue( "Test module B dev version", moduleBPom.indexOf( "<version>1.0-SNAPSHOT</version>" ) > 0 );
379 
380         doPrepareWithNoError( descriptor );
381 
382         pom = FileUtils.fileRead( new File( workDir, "pom.xml" ) );
383         assertTrue( "Test root version increment", pom.indexOf( "<version>1.1-SNAPSHOT</version>" ) > 0 );
384         moduleAPom = FileUtils.fileRead( new File( workDir, "module-A/pom.xml" ) );
385         assertTrue( "Test module A version increment", moduleAPom.indexOf( "<version>1.1-SNAPSHOT</version>" ) > 0 );
386         moduleBPom = FileUtils.fileRead( new File( workDir, "module-B/pom.xml" ) );
387         assertTrue( "Test module B version increment", moduleBPom.indexOf( "<version>1.1-SNAPSHOT</version>" ) > 0 );
388 
389         repository = getScmRepositorty( "scm:svn:file://localhost/" + scmPath + "/tags/continuum-2610-1.0" );
390         fileSet = new ScmFileSet( testDir );
391         scmManager.getProviderByRepository( repository ).checkOut( repository, fileSet, (ScmVersion) null );
392 
393         pom = FileUtils.fileRead( new File( testDir, "pom.xml" ) );
394         assertTrue( "Test root released version", pom.indexOf( "<version>1.0</version>" ) > 0 );
395         moduleAPom = FileUtils.fileRead( new File( testDir, "module-A/pom.xml" ) );
396         assertTrue( "Test module A released version", moduleAPom.indexOf( "<version>1.0</version>" ) > 0 );
397         moduleBPom = FileUtils.fileRead( new File( testDir, "module-B/pom.xml" ) );
398         assertTrue( "Test module B released version", moduleBPom.indexOf( "<version>1.0</version>" ) > 0 );
399     }
400 
401     private void doPrepareWithNoError( ReleaseDescriptor descriptor )
402         throws TaskExecutionException
403     {
404         prepareExec.executeTask( getPrepareTask( "testRelease", descriptor ) );
405 
406         ReleaseResult result = (ReleaseResult) releaseManager.getReleaseResults().get( "testRelease" );
407         if ( result.getResultCode() != ReleaseResult.SUCCESS )
408         {
409             fail( "Error in release:prepare. Release output follows:\n" + result.getOutput() );
410         }
411     }
412 
413     private Task getPrepareTask( String releaseId, ReleaseDescriptor descriptor )
414     {
415         return new PrepareReleaseProjectTask( releaseId, descriptor, null );
416     }
417 
418     private Task getPerformTask( String releaseId, ReleaseDescriptor descriptor, File buildDir )
419     {
420         return new PerformReleaseProjectTask( releaseId, descriptor, buildDir, "package", true, null );
421     }
422 
423     private ScmRepository getScmRepositorty( String scmUrl )
424         throws ScmRepositoryException, NoSuchScmProviderException
425     {
426         ScmRepository repository = scmManager.makeScmRepository( scmUrl.trim() );
427 
428         repository.getProviderRepository().setPersistCheckout( true );
429 
430         return repository;
431     }
432 }