View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.release.phase;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.file.Files;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.shared.release.PlexusJUnit4TestCase;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  
34  /**
35   * @author Edwin Punzalan
36   */
37  public abstract class AbstractBackupPomsPhaseTest extends PlexusJUnit4TestCase {
38      private final String pomFilename = "pom.xml";
39  
40      protected final String releaseBackupSuffix = ".releaseBackup";
41  
42      protected ReleasePhase phase;
43  
44      @Override
45      public void setUp() throws Exception {
46          super.setUp();
47  
48          phase = getReleasePhase();
49      }
50  
51      abstract ReleasePhase getReleasePhase() throws Exception;
52  
53      protected List<MavenProject> getReactorProjects(String projectPath) throws Exception {
54          List<MavenProject> reactorProjects = new ArrayList<>();
55  
56          File pomFile = new File(projectPath, pomFilename);
57  
58          MavenProject mainProject = createMavenProject(pomFile);
59  
60          reactorProjects.add(mainProject);
61  
62          for (String module : mainProject.getModel().getModules()) {
63              File modulePom = new File(projectPath + "/" + module, pomFilename);
64  
65              MavenProject subproject = createMavenProject(modulePom);
66  
67              reactorProjects.add(subproject);
68          }
69  
70          return reactorProjects;
71      }
72  
73      private MavenProject createMavenProject(File pomFile) throws IOException, XmlPullParserException {
74          MavenXpp3Reader reader = new MavenXpp3Reader();
75          try (InputStream in = Files.newInputStream(pomFile.toPath())) {
76              Model model = reader.read(in);
77              MavenProject project = new MavenProject(model);
78              project.setFile(pomFile);
79              return project;
80          }
81      }
82  }