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.project.MavenProject;
23  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
24  import org.apache.maven.shared.release.util.ReleaseUtil;
25  
26  import java.io.File;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /**
31   * @author Edwin Punzalan
32   */
33  public class CreateBackupPomsPhaseTest
34      extends AbstractBackupPomsPhaseTest
35  {
36      ReleasePhase getReleasePhase()
37          throws Exception
38      {
39          return (ReleasePhase) lookup( ReleasePhase.ROLE, "create-backup-poms" );
40      }
41  
42      public void testBasicPom()
43          throws Exception
44      {
45          String projectPath = "target/test-classes/projects/create-backup-poms/basic-pom";
46  
47          //should create backup files
48          runExecuteOnProjects( projectPath );
49  
50          //should delete backup files
51          runCleanOnProjects( projectPath );
52  
53          //should re-create backup files
54          runSimulateOnProjects( projectPath );
55      }
56  
57      public void testMultiModulePom()
58          throws Exception
59      {
60          String projectPath = "target/test-classes/projects/create-backup-poms/pom-with-modules";
61  
62          //should create backup files
63          runExecuteOnProjects( projectPath );
64  
65          //should delete backup files
66          runCleanOnProjects( projectPath );
67  
68          //should re-create backup files
69          runSimulateOnProjects( projectPath );
70      }
71  
72      private void runExecuteOnProjects( String path )
73          throws Exception
74      {
75          List<MavenProject> projects = getReactorProjects( getTestPath( path ) );
76  
77          phase.execute( null, new DefaultReleaseEnvironment(), projects );
78  
79          testProjectBackups( projects, true );
80      }
81  
82      private void runSimulateOnProjects( String path )
83          throws Exception
84      {
85          List<MavenProject> projects = getReactorProjects( getTestPath( path ) );
86  
87          phase.simulate( null, new DefaultReleaseEnvironment(), projects );
88  
89          testProjectBackups( projects, true );
90      }
91  
92      private void runCleanOnProjects( String path )
93          throws Exception
94      {
95          List<MavenProject> projects = getReactorProjects( getTestPath( path ) );
96  
97          phase.clean( projects );
98  
99          testProjectBackups( projects, false );
100     }
101 
102     protected void testProjectBackups( List<MavenProject> reactorProjects, boolean created )
103         throws Exception
104     {
105         for( Iterator<MavenProject> projects = reactorProjects.iterator(); projects.hasNext(); )
106         {
107             MavenProject project = projects.next();
108 
109             File pomFile = project.getFile();
110 
111             File backupFile = new File( pomFile.getAbsolutePath() + releaseBackupSuffix );
112 
113             if ( created )
114             {
115                 assertTrue( "Check if backup file was created.", backupFile.exists() );
116 
117                 String pomContents = ReleaseUtil.readXmlFile( pomFile );
118 
119                 String backupContents = ReleaseUtil.readXmlFile( backupFile );
120 
121                 assertTrue( "Check if pom and backup files are identical", pomContents.equals( backupContents ) );
122             }
123             else
124             {
125                 assertFalse( "Check if backup file is not present", backupFile.exists() );
126             }
127         }
128     }
129 }