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 maven component artifact - along with its
32   * POM lineage - into a clean test-time local repository. This involves modifying the component POM to
33   * provide a stable test-time version for test-build POMs to reference, then installing the component
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   * component's ancestor POMs cannot be resolved.
42   * </p>
43   *
44   * @author jdcasey
45   * @version $Id$
46   */
47  @Deprecated
48  @Component( role = ComponentTestTool.class )
49  public class ComponentTestTool
50  {
51      /** Plexus role */
52      public static final String ROLE = ComponentTestTool.class.getName();
53  
54      @Requirement
55      private ProjectTool projectTool;
56  
57      @Requirement
58      private RepositoryTool repositoryTool;
59  
60      /**
61       * Stage the component, using a stable version, into a temporary local-repository directory that is
62       * generated by this method. When the component 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 component, 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 prepareComponentForIntegrationTesting( File pomFile, String testVersion )
72          throws TestToolsException
73      {
74          return prepareForTesting( pomFile, testVersion, false, null );
75      }
76  
77      /**
78       * Stage the component, using a stable version, into a temporary local-repository directory that is
79       * generated by this method. When the component is staged, return the local repository base directory
80       * for use in test builds. This method also skips unit testing during component 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 component, 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 prepareComponentForUnitTestingWithMavenBuilds( File pomFile, String testVersion )
90          throws TestToolsException
91      {
92          return prepareForTesting( pomFile, testVersion, true, null );
93      }
94  
95      /**
96       * Stage the component, using a stable version, into the specified local-repository directory.
97       * When the component 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 component, 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 component'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 prepareComponentForIntegrationTesting( File pomFile, String testVersion, File localRepositoryDir )
108         throws TestToolsException
109     {
110         return prepareForTesting( pomFile, testVersion, false, localRepositoryDir );
111     }
112 
113     /**
114      * Stage the component, using a stable version, into the specified local-repository directory.
115      * When the component is staged, return the local repository base directory for verification. This
116      * method also skips unit testing during component 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 component, 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 component'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 prepareComponentForUnitTestingWithMavenBuilds( File pomFile, String testVersion,
128                                                                File localRepositoryDir )
129         throws TestToolsException
130     {
131         return prepareForTesting( pomFile, testVersion, true, localRepositoryDir );
132     }
133 
134     private File prepareForTesting( File pomFile, String testVersion, boolean skipUnitTests, File localRepositoryDir )
135         throws TestToolsException
136     {
137         File realProjectDir = pomFile.getParentFile();
138         try
139         {
140             realProjectDir = realProjectDir.getCanonicalFile();
141         }
142         catch ( IOException e )
143         {
144             throw new TestToolsException( "Failed to stage component for testing.", e );
145         }
146 
147         try
148         {
149             final File tmpDir = File.createTempFile( "component-IT-staging-project", "" );
150 
151             tmpDir.delete();
152 
153             tmpDir.mkdirs();
154 
155             Runtime.getRuntime().addShutdownHook( new Thread( new Runnable()
156             {
157                 public void run()
158                 {
159                     try
160                     {
161                         FileUtils.deleteDirectory( tmpDir );
162                     }
163                     catch ( IOException e )
164                     {
165                         // it'll get cleaned up when the temp dir is purged next...
166                     }
167                 }
168 
169             } ) );
170 
171             FileUtils.copyDirectoryStructure( realProjectDir, tmpDir );
172         }
173         catch ( IOException e )
174         {
175             throw new TestToolsException( "Failed to create temporary staging directory for component project.", e );
176         }
177 
178         File buildLog = new File( "target/test-build-logs/setup.build.log" );
179 
180         buildLog.getParentFile().mkdirs();
181 
182         File localRepoDir = localRepositoryDir;
183 
184         if ( localRepoDir == null )
185         {
186             localRepoDir = new File( "target/test-local-repository" );
187         }
188 
189         MavenProject project = projectTool.packageProjectArtifact( pomFile, testVersion, skipUnitTests, buildLog );
190 
191         repositoryTool.createLocalRepositoryFromComponentProject( project, new File( realProjectDir, "pom.xml" ),
192                                                                   localRepoDir );
193 
194         return localRepoDir;
195     }
196 }