View Javadoc
1   package org.apache.maven.shared.test.plugin;
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.codehaus.plexus.component.annotations.Component;
24  import org.codehaus.plexus.component.annotations.Requirement;
25  import org.codehaus.plexus.util.FileUtils;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  /**
31   * Test tool that provides a single point of access for staging a plugin artifact - along with its
32   * POM lineage - into a clean test-time local repository. This involves modifying the plugin POM to
33   * provide a stable test-time version for test-build POMs to reference, then installing the plugin
34   * jar and associated POMs (including those ancestors that are reachable using <relativePath>)
35   * into the test local repository.
36   *
37   * <p>
38   * <b>WARNING:</b> Currently, the <code>RepositoryTool</code> will not
39   * resolve parent POMs that exist <b>only</b> in your normal local repository, and are not reachable
40   * using the relativePath element. This may result in failed test builds, as one or more of the
41   * plugin's ancestor POMs cannot be resolved.
42   * </p>
43   *
44   * @author jdcasey
45   * @version $Id$
46   */
47  @Deprecated
48  @Component( role = PluginTestTool.class )
49  public class PluginTestTool
50  {
51      /** Plexus role */
52      public static final String ROLE = PluginTestTool.class.getName();
53  
54      @Requirement
55      private ProjectTool projectTool;
56  
57      @Requirement
58      private RepositoryTool repositoryTool;
59  
60      /**
61       * Stage the plugin, using a stable version, into a temporary local-repository directory that is
62       * generated by this method. When the plugin is staged, return the local repository base directory
63       * for use in test builds.
64       *
65       * @param pomFile current POM file
66       * @param testVersion The test version for the plugin, used for reference in test-build POMs and
67       *   fully-qualified goals
68       * @return The base-directory location of the generated local repository
69       * @throws TestToolsException if any
70       */
71      public File preparePluginForIntegrationTesting( File pomFile, String testVersion )
72          throws TestToolsException
73      {
74          return prepareForTesting( pomFile, testVersion, false, null );
75      }
76  
77      /**
78       * Stage the plugin, using a stable version, into a temporary local-repository directory that is
79       * generated by this method. When the plugin is staged, return the local repository base directory
80       * for use in test builds. This method also skips unit testing during plugin jar production,
81       * since it is assumed that executing these tests would lead to a recursive test-and-build loop.
82       *
83       * @param pomFile current POM file
84       * @param testVersion The test version for the plugin, used for reference in test-build POMs and
85       *   fully-qualified goals
86       * @return The base-directory location of the generated local repository
87       * @throws TestToolsException if any
88       */
89      public File preparePluginForUnitTestingWithMavenBuilds( File pomFile, String testVersion )
90          throws TestToolsException
91      {
92          return prepareForTesting( pomFile, testVersion, true, null );
93      }
94  
95      /**
96       * Stage the plugin, using a stable version, into the specified local-repository directory.
97       * When the plugin is staged, return the local repository base directory for verification.
98       *
99       * @param pomFile current POM file
100      * @param testVersion The test version for the plugin, used for reference in test-build POMs and
101      *   fully-qualified goals
102      * @param localRepositoryDir The base-directory location of the test local repository, into which
103      *   the plugin's test version should be staged.
104      * @return The base-directory location of the generated local repository
105      * @throws TestToolsException if any
106      */
107     public File preparePluginForIntegrationTesting( File pomFile, String testVersion, File localRepositoryDir )
108         throws TestToolsException
109     {
110         return prepareForTesting( pomFile, testVersion, false, localRepositoryDir );
111     }
112 
113     /**
114      * Stage the plugin, using a stable version, into the specified local-repository directory.
115      * When the plugin is staged, return the local repository base directory for verification. This
116      * method also skips unit testing during plugin jar production, since it is assumed that
117      * executing these tests would lead to a recursive test-and-build loop.
118      *
119      * @param pomFile current POM file
120      * @param testVersion The test version for the plugin, used for reference in test-build POMs and
121      *   fully-qualified goals
122      * @param localRepositoryDir The base-directory location of the test local repository, into which
123      *   the plugin's test version should be staged.
124      * @return The base-directory location of the generated local repository
125      * @throws TestToolsException if any
126      */
127     public File preparePluginForUnitTestingWithMavenBuilds( File pomFile, String testVersion, File localRepositoryDir )
128         throws TestToolsException
129     {
130         return prepareForTesting( pomFile, testVersion, true, localRepositoryDir );
131     }
132 
133     private File prepareForTesting( File pomFile, String testVersion, boolean skipUnitTests, File localRepositoryDir )
134         throws TestToolsException
135     {
136         File realProjectDir = pomFile.getParentFile();
137 
138         try
139         {
140             realProjectDir = realProjectDir.getCanonicalFile();
141         }
142         catch ( IOException e )
143         {
144             throw new TestToolsException( "Failed to stage plugin for testing.", e );
145         }
146 
147         try
148         {
149             final File tmpDir = File.createTempFile( "plugin-IT-staging-project", "" );
150 
151             tmpDir.delete();
152 
153             tmpDir.mkdirs();
154 
155             Runtime.getRuntime().addShutdownHook( new Thread( new Runnable()
156             {
157 
158                 public void run()
159                 {
160                     try
161                     {
162                         FileUtils.deleteDirectory( tmpDir );
163                     }
164                     catch ( IOException e )
165                     {
166                         // it'll get cleaned up when the temp dir is purged next...
167                     }
168                 }
169 
170             } ) );
171 
172             FileUtils.copyDirectoryStructure( realProjectDir, tmpDir );
173         }
174         catch ( IOException e )
175         {
176             throw new TestToolsException( "Failed to create temporary staging directory for plugin project.", e );
177         }
178 
179         File buildLog = new File( "target/test-build-logs/setup.build.log" );
180 
181         buildLog.getParentFile().mkdirs();
182 
183         File localRepoDir = localRepositoryDir;
184 
185         if ( localRepoDir == null )
186         {
187             localRepoDir = new File( "target/test-local-repository" );
188         }
189 
190         MavenProject project = projectTool.packageProjectArtifact( pomFile, testVersion, skipUnitTests, buildLog );
191 
192         repositoryTool.createLocalRepositoryFromComponentProject( project, new File( realProjectDir, "pom.xml" ),
193                                                                   localRepoDir );
194 
195         return localRepoDir;
196     }
197 
198 }