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.config.ReleaseDescriptor;
24  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
25  import org.apache.maven.shared.release.util.ReleaseUtil;
26  import org.codehaus.plexus.util.FileUtils;
27  
28  import java.io.File;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * @author Edwin Punzalan
34   */
35  public class RestoreBackupPomsPhaseTest
36      extends AbstractBackupPomsPhaseTest
37  {
38      private String expectedPomFilename = "expected-pom.xml";
39  
40      ReleasePhase getReleasePhase()
41          throws Exception
42      {
43          return (ReleasePhase) lookup( ReleasePhase.ROLE, "restore-backup-poms" );
44      }
45  
46      public void testBasicPom()
47          throws Exception
48      {
49          String projectPath = "/projects/restore-backup-poms/basic-pom";
50  
51          //copy poms so tests are valid without clean
52          File sourceDir = getTestFile( "src/test/resources" + projectPath );
53          File testDir = getTestFile( "target/test-classes" + projectPath );
54          FileUtils.copyDirectoryStructure( sourceDir, testDir );
55  
56          String testPath = "target/test-classes" + projectPath;
57  
58          runExecuteOnProjects( testPath );
59      }
60  
61      public void testMultiModulePom()
62          throws Exception
63      {
64          String projectPath = "/projects/restore-backup-poms/pom-with-modules";
65  
66          //copy poms so tests are valid without clean
67          File sourceDir = getTestFile( "src/test/resources" + projectPath );
68          File testDir = getTestFile( "target/test-classes" + projectPath );
69          FileUtils.copyDirectoryStructure( sourceDir, testDir );
70  
71          String testPath = "target/test-classes" + projectPath;
72  
73          runExecuteOnProjects( testPath );
74      }
75  
76      private void runExecuteOnProjects( String path )
77          throws Exception
78      {
79          List<MavenProject> projects = getReactorProjects( getTestPath( path ) );
80  
81          ReleaseDescriptor desc = new ReleaseDescriptor();
82          desc.setScmSourceUrl( "scm:svn:http://myhost/myrepo" );
83          phase.execute( desc, new DefaultReleaseEnvironment(), projects );
84  
85          testProjectIsRestored( projects );
86      }
87  
88      private void testProjectIsRestored( List<MavenProject> reactorProjects )
89          throws Exception
90      {
91          for ( Iterator<MavenProject> projects = reactorProjects.iterator(); projects.hasNext(); )
92          {
93              MavenProject project = projects.next();
94  
95              File pomFile = project.getFile();
96  
97              File expectedFile = new File( pomFile.getParentFile(), expectedPomFilename );
98  
99              assertTrue( "Check if expected file exists.", expectedFile.exists() );
100 
101             String pomContents = ReleaseUtil.readXmlFile( pomFile );
102 
103             String expectedContents = ReleaseUtil.readXmlFile( expectedFile );
104 
105             assertTrue( "Check if pom and backup files are identical", pomContents.equals( expectedContents ) );
106         }
107     }
108 }