View Javadoc

1   package org.apache.maven.shared.release.phase;
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.maven.artifact.ArtifactUtils;
23  import org.apache.maven.model.Scm;
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.shared.release.ReleaseExecutionException;
26  import org.apache.maven.shared.release.config.ReleaseDescriptor;
27  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
28  import org.apache.maven.shared.release.util.ReleaseUtil;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.util.Iterator;
33  import java.util.LinkedList;
34  import java.util.List;
35  
36  /**
37   * Test the SCM modification check phase.
38   *
39   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40   */
41  public class RewritePomsForDevelopmentPhaseTest
42      extends AbstractEditModeRewritingReleasePhaseTestCase
43  {
44      private static final String NEXT_VERSION = "1.1-SNAPSHOT";
45  
46      private static final String ALTERNATIVE_NEXT_VERSION = "2.1-SNAPSHOT";
47  
48      private static final String RELEASE_VERSION = "1.0";
49  
50      private static final String ALTERNATIVE_RELEASE_VERSION = "2.0";
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56  
57          phase = (ReleasePhase) lookup( ReleasePhase.ROLE, "rewrite-poms-for-development" );
58      }
59  
60      public void testSimulateRewrite()
61          throws Exception
62      {
63          List<MavenProject> reactorProjects = createReactorProjectsWhenSimulated( "basic-pom" );
64          ReleaseDescriptor config = createDescriptorFromBasicPom( reactorProjects );
65          config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
66          config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
67  
68          String expected = readTestProjectFile( "basic-pom/pom.xml" );
69  
70          phase.simulate( config, new DefaultReleaseEnvironment(), reactorProjects );
71  
72          String actual = readTestProjectFile( "basic-pom/pom.xml" );
73          assertEquals( "Check the original POM untouched", expected, actual );
74  
75          expected = readTestProjectFile( "basic-pom/expected-pom.xml" );
76          actual = readTestProjectFile( "basic-pom/pom.xml.next" );
77          assertEquals( "Check the transformed POM", expected, actual );
78      }
79  
80      private List<MavenProject> createReactorProjectsWhenSimulated( String name )
81          throws Exception
82      {
83          return createReactorProjects( "rewrite-for-release/", "rewrite-for-development/", name );
84      }
85  
86      public void testSimulateRewriteEjbClientDeps()
87          throws Exception
88      {
89          List<MavenProject> reactorProjects = new LinkedList<MavenProject>( createReactorProjects( "basic-pom-ejb-client-dep/project" ) );
90          reactorProjects.addAll( createReactorProjects( "basic-pom-ejb-client-dep/ejb" ) );
91          ReleaseDescriptor config = createDescriptorFromBasicPom( reactorProjects );
92          config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
93          config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
94          config.addDevelopmentVersion( ArtifactUtils.versionlessKey( "groupId", "artifactId1" ), NEXT_VERSION );
95          config.addReleaseVersion( ArtifactUtils.versionlessKey( "groupId", "artifactId1" ), RELEASE_VERSION );
96  
97          String expected = readTestProjectFile( "basic-pom-ejb-client-dep/project/pom.xml" );
98  
99          phase.simulate( config, new DefaultReleaseEnvironment(), reactorProjects );
100 
101         String actual = readTestProjectFile( "basic-pom-ejb-client-dep/project/pom.xml" );
102         assertEquals( "Check the original POM untouched", expected, actual );
103 
104         expected = readTestProjectFile( "basic-pom-ejb-client-dep/project/expected-pom.xml" );
105         actual = readTestProjectFile( "basic-pom-ejb-client-dep/project/pom.xml.next" );
106         assertEquals( "Check the transformed POM", expected, actual );
107     }
108 
109     public void testClean()
110         throws Exception
111     {
112         List<MavenProject> reactorProjects = createReactorProjectsWhenSimulated( "basic-pom" );
113         ReleaseDescriptor config = createDescriptorFromBasicPom( reactorProjects );
114         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
115         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
116 
117         File testFile = getTestFile( "target/test-classes/projects/rewrite-for-development/basic-pom/pom.xml.next" );
118         testFile.delete();
119         assertFalse( testFile.exists() );
120 
121         phase.simulate( config, new DefaultReleaseEnvironment(), reactorProjects );
122 
123         assertTrue( testFile.exists() );
124 
125         phase.clean( reactorProjects );
126 
127         assertFalse( testFile.exists() );
128     }
129 
130     public void testCleanNotExists()
131         throws Exception
132     {
133         List<MavenProject> reactorProjects = createReactorProjectsFromBasicPom();
134         ReleaseDescriptor config = createDescriptorFromBasicPom( reactorProjects );
135         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
136         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
137 
138         File testFile = getTestFile( "target/test-classes/projects/rewrite-for-development/basic-pom/pom.xml.next" );
139         testFile.delete();
140         assertFalse( testFile.exists() );
141 
142         phase.clean( reactorProjects );
143 
144         assertFalse( testFile.exists() );
145     }
146 
147     public void testRewriteBasicPomUnmappedScm()
148         throws Exception
149     {
150         List<MavenProject> reactorProjects = prepareReactorProjects( "basic-pom", true );
151 
152         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
153 
154         mapNextVersion( config, "groupId:artifactId" );
155 
156         try
157         {
158             phase.execute( config, new DefaultReleaseEnvironment(), reactorProjects );
159 
160             fail( "Expected failure" );
161         }
162         catch ( ReleaseExecutionException e )
163         {
164             verifyReactorProjects( "basic-pom", true );
165         }
166     }
167 
168     protected String readTestProjectFile( String fileName )
169         throws IOException
170     {
171         return readTestProjectFile( fileName, "rewrite-for-development/" );
172     }
173 
174     protected String readTestProjectFile( String fileName, String subpath )
175         throws IOException
176     {
177         return ReleaseUtil.readXmlFile( getTestFile( "target/test-classes/projects/"+ subpath + fileName ) );
178     }
179 
180     protected List<MavenProject> prepareReactorProjects( String path, boolean copyFiles )
181         throws Exception
182     {
183         return createReactorProjects( "rewrite-for-development/", path );
184     }
185 
186     protected ReleaseDescriptor createDescriptorFromBasicPom( List<MavenProject> reactorProjects )
187         throws Exception
188     {
189         ReleaseDescriptor config = super.createDescriptorFromBasicPom( reactorProjects );
190 
191         mapScm( config );
192 
193         return config;
194     }
195 
196     private void mapScm( ReleaseDescriptor config )
197     {
198         Scm scm = new Scm();
199         scm.setConnection( "scm:svn:file://localhost/tmp/scm-repo/trunk" );
200         scm.setDeveloperConnection( "scm:svn:file://localhost/tmp/scm-repo/trunk" );
201         scm.setUrl( "file://localhost/tmp/scm-repo/trunk" );
202         config.mapOriginalScmInfo( "groupId:artifactId", scm );
203     }
204 
205     protected void mapAlternateNextVersion( ReleaseDescriptor config, String projectId )
206     {
207         config.mapReleaseVersion( projectId, ALTERNATIVE_RELEASE_VERSION );
208         config.mapDevelopmentVersion( projectId, ALTERNATIVE_NEXT_VERSION );
209     }
210 
211     protected void mapNextVersion( ReleaseDescriptor config, String projectId )
212     {
213         config.mapReleaseVersion( projectId, RELEASE_VERSION );
214         config.mapDevelopmentVersion( projectId, NEXT_VERSION );
215     }
216 
217     protected void unmapNextVersion( ReleaseDescriptor config, String projectId )
218     {
219         config.mapReleaseVersion( projectId, RELEASE_VERSION );
220     }
221 
222     protected ReleaseDescriptor createConfigurationForPomWithParentAlternateNextVersion( List<MavenProject> reactorProjects )
223         throws Exception
224     {
225         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
226 
227         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
228         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
229         config.mapReleaseVersion( "groupId:subproject1", ALTERNATIVE_RELEASE_VERSION );
230         config.mapDevelopmentVersion( "groupId:subproject1", ALTERNATIVE_NEXT_VERSION );
231         mapScm( config );
232 
233         return config;
234     }
235 
236     protected ReleaseDescriptor createConfigurationForWithParentNextVersion( List<MavenProject> reactorProjects )
237         throws Exception
238     {
239         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
240 
241         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
242         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
243         config.mapReleaseVersion( "groupId:subproject1", RELEASE_VERSION );
244         config.mapDevelopmentVersion( "groupId:subproject1", NEXT_VERSION );
245         mapScm( config );
246 
247         return config;
248     }
249 
250     public void testRewriteBasicPomWithCvs()
251         throws Exception
252     {
253 
254         List<MavenProject> reactorProjects = createReactorProjects( "basic-pom-with-cvs" );
255         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
256         mapNextVersion( config, "groupId:artifactId" );
257 
258         Scm scm = new Scm();
259         scm.setConnection( "${scm.base}:pserver:anoncvs@localhost:/tmp/scm-repo:module" );
260         scm.setDeveloperConnection( "${scm.base}:ext:${username}@localhost:/tmp/scm-repo:module" );
261         scm.setUrl( "${baseUrl}/module" );
262         config.mapOriginalScmInfo( "groupId:artifactId", scm );
263 
264         phase.execute( config, new DefaultReleaseEnvironment(), reactorProjects );
265 
266         assertTrue( comparePomFiles( reactorProjects ) );
267     }
268 
269     public void testRewriteBasicPomWithCvsFromTag()
270         throws Exception
271     {
272 
273         List<MavenProject> reactorProjects = createReactorProjects( "basic-pom-with-cvs-from-tag" );
274         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
275         mapNextVersion( config, "groupId:artifactId" );
276 
277         Scm scm = new Scm();
278         scm.setConnection( "scm:cvs:pserver:anoncvs@localhost:/tmp/scm-repo:module" );
279         scm.setDeveloperConnection( "scm:cvs:ext:${username}@localhost:/tmp/scm-repo:module" );
280         scm.setUrl( "http://localhost/viewcvs.cgi/module" );
281         scm.setTag( "original-label" );
282         config.mapOriginalScmInfo( "groupId:artifactId", scm );
283 
284         phase.execute( config, new DefaultReleaseEnvironment(), reactorProjects );
285 
286         assertTrue( comparePomFiles( reactorProjects ) );
287     }
288 
289     public void testRewriteBasicPomWithInheritedScm()
290         throws Exception
291     {
292 
293         List<MavenProject> reactorProjects = createReactorProjects( "basic-pom-inherited-scm" );
294         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
295 
296         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
297         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
298         config.mapReleaseVersion( "groupId:subproject1", RELEASE_VERSION );
299         config.mapDevelopmentVersion( "groupId:subproject1", NEXT_VERSION );
300         config.mapReleaseVersion( "groupId:subsubproject", RELEASE_VERSION );
301         config.mapDevelopmentVersion( "groupId:subsubproject", NEXT_VERSION );
302         config.mapOriginalScmInfo( "groupId:artifactId", null );
303         Scm scm = new Scm();
304         scm.setConnection( "scm:svn:file://localhost/tmp/scm-repo/trunk/subproject1" );
305         scm.setDeveloperConnection( "scm:svn:file://localhost/tmp/scm-repo/trunk/subproject1" );
306         //MRELEASE-107
307         scm.setUrl( "http://localhost/viewvc/mypath/trunk/subproject1" );
308         config.mapOriginalScmInfo( "groupId:subproject1", scm );
309         config.mapOriginalScmInfo( "groupId:subsubproject", null );
310 
311         phase.execute( config, new DefaultReleaseEnvironment(), reactorProjects );
312 
313         assertTrue( comparePomFiles( reactorProjects ) );
314     }
315 
316     public void testRewritePomWithParentAndProperties()
317         throws Exception
318     {
319         performTestRewritePomWithParentAndProperties( "pom-with-parent-and-properties" );
320     }
321 
322     //MRELEASE-454
323     public void testRewritePomWithParentAndPropertiesInDependencyManagement()
324         throws Exception
325     {
326         performTestRewritePomWithParentAndProperties( "pom-with-parent-and-properties-in-dependency-management" );
327     }
328 
329     //MRELEASE-454
330     public void testRewritePomWithParentAndPropertiesInDependencyManagementImport()
331         throws Exception
332     {
333         performTestRewritePomWithParentAndProperties( "pom-with-parent-and-properties-in-dependency-management-import" );
334     }
335 
336     private void performTestRewritePomWithParentAndProperties( String path )
337         throws Exception
338     {
339         List<MavenProject> reactorProjects = createReactorProjects( path );
340   
341         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
342         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
343         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
344         config.mapReleaseVersion( "groupId:subproject1", ALTERNATIVE_RELEASE_VERSION );
345         config.mapDevelopmentVersion( "groupId:subproject1", ALTERNATIVE_NEXT_VERSION );
346         config.mapReleaseVersion( "groupId:subproject2", ALTERNATIVE_RELEASE_VERSION );
347         config.mapDevelopmentVersion( "groupId:subproject2", ALTERNATIVE_NEXT_VERSION );
348 
349         mapScm( config );
350 
351         phase.execute( config, new DefaultReleaseEnvironment(), reactorProjects );
352 
353         assertTrue( comparePomFiles( reactorProjects ) );
354     }
355 
356     public void testSimulateRewritePomWithParentAndProperties()
357         throws Exception
358     {
359         // use the original ones since simulation didn't modify them
360         List<MavenProject> reactorProjects = createReactorProjects( "pom-with-parent-and-properties-sim" );
361 
362         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
363         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
364         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
365         config.mapReleaseVersion( "groupId:subproject1", ALTERNATIVE_RELEASE_VERSION );
366         config.mapDevelopmentVersion( "groupId:subproject1", ALTERNATIVE_NEXT_VERSION );
367         config.mapReleaseVersion( "groupId:subproject2", ALTERNATIVE_RELEASE_VERSION );
368         config.mapDevelopmentVersion( "groupId:subproject2", ALTERNATIVE_NEXT_VERSION );
369 
370         mapScm( config );
371 
372         phase.simulate( config, new DefaultReleaseEnvironment(), reactorProjects );
373 
374         for ( Iterator<MavenProject> i = reactorProjects.iterator(); i.hasNext(); )
375         {
376             MavenProject project = i.next();
377 
378             File pomFile = project.getFile();
379             File actualFile = new File( pomFile.getParentFile(), pomFile.getName() + ".next" );
380             File expectedFile = new File( actualFile.getParentFile(), "expected-pom.xml" );
381 
382             comparePomFiles( expectedFile, actualFile, true );
383         }
384     }
385 
386     // MRELEASE-311
387     public void testRewritePomWithDependencyPropertyCoordinate()
388         throws Exception
389     {
390         List<MavenProject> reactorProjects = createReactorProjects( "pom-with-property-dependency-coordinate" );
391 
392         ReleaseDescriptor config = createDescriptorFromProjects( reactorProjects );
393         config.mapReleaseVersion( "groupId:artifactId", RELEASE_VERSION );
394         config.mapDevelopmentVersion( "groupId:artifactId", NEXT_VERSION );
395         config.mapReleaseVersion( "groupId:subproject1-3.4", ALTERNATIVE_RELEASE_VERSION );
396         config.mapDevelopmentVersion( "groupId:subproject1-3.4", ALTERNATIVE_NEXT_VERSION );
397         config.mapReleaseVersion( "groupId:subproject2", ALTERNATIVE_RELEASE_VERSION );
398         config.mapDevelopmentVersion( "groupId:subproject2", ALTERNATIVE_NEXT_VERSION );
399 
400         mapScm( config );
401 
402         phase.execute( config, new DefaultReleaseEnvironment(), reactorProjects );
403 
404         assertTrue( comparePomFiles( reactorProjects ) );
405     }
406 
407     public void testRewritePomDependenciesWithoutDependenciesVersionUpdate()
408         throws Exception
409     {
410         List<MavenProject> reactorProjects =
411             createReactorProjects( "internal-snapshot-dependencies-without-dependencies-version-update" );
412         ReleaseDescriptor config = createDefaultConfiguration( reactorProjects );
413         config.setUpdateDependencies( false );
414         mapNextVersion( config, "groupId:subsubproject" );
415 
416         phase.execute( config, new DefaultReleaseEnvironment(), reactorProjects );
417 
418         assertTrue( comparePomFiles( reactorProjects ) );
419     }
420 }