Coverage Report - org.apache.maven.shared.test.plugin.ProjectTool
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectTool
75%
91/120
44%
8/18
3.154
ProjectTool$PomInfo
100%
14/14
N/A
3.154
 
 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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.io.Reader;
 25  
 import java.io.Writer;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 import java.util.Properties;
 30  
 
 31  
 import org.apache.maven.artifact.Artifact;
 32  
 import org.apache.maven.artifact.UnknownRepositoryLayoutException;
 33  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 34  
 import org.apache.maven.artifact.handler.ArtifactHandler;
 35  
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 36  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 37  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 38  
 import org.apache.maven.model.Build;
 39  
 import org.apache.maven.model.DeploymentRepository;
 40  
 import org.apache.maven.model.DistributionManagement;
 41  
 import org.apache.maven.model.Model;
 42  
 import org.apache.maven.model.Plugin;
 43  
 import org.apache.maven.model.Repository;
 44  
 import org.apache.maven.model.Site;
 45  
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 46  
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 47  
 import org.apache.maven.project.DefaultProjectBuildingRequest;
 48  
 import org.apache.maven.project.MavenProject;
 49  
 import org.apache.maven.project.ProjectBuilder;
 50  
 import org.apache.maven.project.ProjectBuildingException;
 51  
 import org.apache.maven.project.ProjectBuildingRequest;
 52  
 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 53  
 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
 54  
 import org.codehaus.plexus.component.annotations.Component;
 55  
 import org.codehaus.plexus.component.annotations.Requirement;
 56  
 import org.codehaus.plexus.util.FileUtils;
 57  
 import org.codehaus.plexus.util.IOUtil;
 58  
 import org.codehaus.plexus.util.ReaderFactory;
 59  
 import org.codehaus.plexus.util.WriterFactory;
 60  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 61  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 62  
 
 63  
 /**
 64  
  * Testing tool used to read MavenProject instances from pom.xml files, and to create plugin jar
 65  
  * files (package phase of the normal build process) for distribution to a test local repository
 66  
  * directory.
 67  
  *
 68  
  * @author jdcasey
 69  
  * @version $Id: ProjectTool.java 1185951 2011-10-19 02:43:50Z ifedorenko $
 70  
  */
 71  
 @Component( role = ProjectTool.class )
 72  6
 public class ProjectTool
 73  
 {
 74  
     /** Plexus role */
 75  2
     public static final String ROLE = ProjectTool.class.getName();
 76  
 
 77  
     public static final String INTEGRATION_TEST_DEPLOYMENT_REPO_URL = "integration-test.deployment.repo.url";
 78  
 
 79  
     @Requirement
 80  
     private BuildTool buildTool;
 81  
 
 82  
     @Requirement
 83  
     private RepositoryTool repositoryTool;
 84  
 
 85  
     @Requirement
 86  
     private ProjectBuilder projectBuilder;
 87  
 
 88  
     @Requirement
 89  
     private ArtifactHandlerManager artifactHandlerManager;
 90  
 
 91  
     @Requirement
 92  
     private ArtifactFactory artifactFactory;
 93  
 
 94  
     @Requirement
 95  
     private ArtifactRepositoryFactory artifactRepositoryFactory;
 96  
 
 97  
     /**
 98  
      * Construct a MavenProject instance from the specified POM file.
 99  
      *
 100  
      * @param pomFile current POM file
 101  
      * @return the Maven project from a file
 102  
      * @throws TestToolsException if any
 103  
      */
 104  
     public MavenProject readProject( File pomFile )
 105  
         throws TestToolsException
 106  
     {
 107  0
         return readProject( pomFile, repositoryTool.findLocalRepositoryDirectory() );
 108  
     }
 109  
 
 110  
     /**
 111  
      * Construct a MavenProject instance from the specified POM file, using the specified local
 112  
      * repository directory to resolve ancestor POMs as needed.
 113  
      *
 114  
      * @param pomFile current POM file
 115  
      * @param localRepositoryBasedir
 116  
      * @return the Maven project from a file and a local repo
 117  
      * @throws TestToolsException if any
 118  
      */
 119  
     public MavenProject readProject( File pomFile, File localRepositoryBasedir )
 120  
         throws TestToolsException
 121  
     {
 122  
         try
 123  
         {
 124  0
             ArtifactRepository localRepository = repositoryTool
 125  
                 .createLocalArtifactRepositoryInstance( localRepositoryBasedir );
 126  
 
 127  0
             ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
 128  0
             return projectBuilder.build( pomFile, request ).getProject();
 129  
         }
 130  0
         catch ( ProjectBuildingException e )
 131  
         {
 132  0
             throw new TestToolsException( "Error building MavenProject instance from test pom: " + pomFile, e );
 133  
         }
 134  
     }
 135  
 
 136  
     /**
 137  
      * Construct a MavenProject instance from the specified POM file with dependencies.
 138  
      *
 139  
      * @param pomFile current POM file
 140  
      * @return the Maven project with dependencies from a file
 141  
      * @throws TestToolsException if any
 142  
      */
 143  
     public MavenProject readProjectWithDependencies( File pomFile )
 144  
         throws TestToolsException
 145  
     {
 146  0
         return readProjectWithDependencies( pomFile, repositoryTool.findLocalRepositoryDirectory() );
 147  
     }
 148  
 
 149  
     /**
 150  
      * Construct a MavenProject instance from the specified POM file with dependencies, using the specified local
 151  
      * repository directory to resolve ancestor POMs as needed.
 152  
      *
 153  
      * @param pomFile current POM file
 154  
      * @param localRepositoryBasedir
 155  
      * @return the Maven project with dependencies from a file and a local repo
 156  
      * @throws TestToolsException if any
 157  
      */
 158  
     public MavenProject readProjectWithDependencies( File pomFile, File localRepositoryBasedir )
 159  
         throws TestToolsException
 160  
     {
 161  
         try
 162  
         {
 163  0
             ArtifactRepository localRepository = repositoryTool
 164  
                 .createLocalArtifactRepositoryInstance( localRepositoryBasedir );
 165  
 
 166  0
             ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
 167  0
             return projectBuilder.build( pomFile, request ).getProject();
 168  
         }
 169  0
         catch ( ProjectBuildingException e )
 170  
         {
 171  0
             throw new TestToolsException( "Error building MavenProject instance from test pom: " + pomFile, e );
 172  
         }
 173  
     }
 174  
 
 175  
     /**
 176  
      * Run the plugin's Maven build up to the package phase, in order to produce a jar file for
 177  
      * distribution to a test-time local repository. The testVersion parameter specifies the version
 178  
      * to be used in the <version/> element of the plugin configuration, and also in fully
 179  
      * qualified, unambiguous goal invocations (as in
 180  
      * org.apache.maven.plugins:maven-eclipse-plugin:test:eclipse).
 181  
      *
 182  
      * @param pomFile The plugin's POM
 183  
      * @param testVersion The version to use for testing this plugin. To promote test resiliency,
 184  
      *   this version should remain unchanged, regardless of what plugin version is under
 185  
      *   development.
 186  
      * @param skipUnitTests In cases where test builds occur during the unit-testing phase (usually
 187  
      *   a bad testing smell), the plugin jar must be produced <b>without</b> running unit tests.
 188  
      *   Otherwise, the testing process will result in a recursive loop of building a plugin jar and
 189  
      *   trying to unit test it during the build. In these cases, set this flag to <code>true</code>.
 190  
      * @return The resulting MavenProject, after the test version and skip flag (for unit tests)
 191  
      *   have been appropriately configured.
 192  
      * @throws TestToolsException if any
 193  
      */
 194  
     public MavenProject packageProjectArtifact( File pomFile, String testVersion, boolean skipUnitTests )
 195  
         throws TestToolsException
 196  
     {
 197  4
         return packageProjectArtifact( pomFile, testVersion, skipUnitTests, null );
 198  
     }
 199  
 
 200  
     /**
 201  
      * Run the plugin's Maven build up to the package phase, in order to produce a jar file for
 202  
      * distribution to a test-time local repository. The testVersion parameter specifies the version
 203  
      * to be used in the &lt;version/&gt; element of the plugin configuration, and also in fully
 204  
      * qualified, unambiguous goal invocations (as in
 205  
      * org.apache.maven.plugins:maven-eclipse-plugin:test:eclipse).
 206  
      *
 207  
      * @param pomFile The plugin's POM
 208  
      * @param testVersion The version to use for testing this plugin. To promote test resiliency,
 209  
      *   this version should remain unchanged, regardless of what plugin version is under
 210  
      *   development.
 211  
      * @param skipUnitTests In cases where test builds occur during the unit-testing phase (usually
 212  
      *   a bad testing smell), the plugin jar must be produced <b>without</b> running unit tests.
 213  
      *   Otherwise, the testing process will result in a recursive loop of building a plugin jar and
 214  
      *   trying to unit test it during the build. In these cases, set this flag to <code>true</code>.
 215  
      * @param logFile The file to which build output should be logged, in order to allow later
 216  
      *   inspection in case this build fails.
 217  
      * @return The resulting MavenProject, after the test version and skip flag (for unit tests)
 218  
      *   have been appropriately configured.
 219  
      * @throws TestToolsException if any
 220  
      */
 221  
     public MavenProject packageProjectArtifact( File pomFile, String testVersion, boolean skipUnitTests, File logFile )
 222  
         throws TestToolsException
 223  
     {
 224  4
         PomInfo pomInfo = manglePomForTesting( pomFile, testVersion, skipUnitTests );
 225  
 
 226  4
         Properties properties = new Properties();
 227  
 
 228  4
         List goals = new ArrayList();
 229  4
         goals.add( "package" );
 230  
 
 231  4
         File buildLog = logFile == null ? pomInfo.getBuildLogFile() : logFile;
 232  4
         System.out.println( "Now Building test version of the plugin...\nUsing staged plugin-pom: "
 233  
             + pomInfo.getPomFile().getAbsolutePath() );
 234  
 
 235  4
         buildTool.executeMaven( pomInfo.getPomFile(), properties, goals, buildLog );
 236  
 
 237  4
         File artifactFile = new File( pomInfo.getPomFile().getParentFile(),
 238  
                                       pomInfo.getBuildDirectory() + "/" + pomInfo.getFinalName() );
 239  4
         System.out.println( "Using IT Plugin Jar: " + artifactFile.getAbsolutePath() );
 240  
         try
 241  
         {
 242  4
             ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
 243  4
             request.setLocalRepository( artifactRepositoryFactory.createArtifactRepository( "local", new File( "target/localrepo" ).getCanonicalFile().toURL().toExternalForm(), "default", null, null ) );
 244  4
             request.setRepositorySession( new MavenRepositorySystemSession() );
 245  4
             MavenProject project = projectBuilder.build( pomInfo.getPomFile(), request ).getProject();
 246  
 
 247  4
             Artifact artifact = artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), project
 248  
                 .getVersion(), null, project.getPackaging() );
 249  
 
 250  4
             artifact.setFile( artifactFile );
 251  4
             artifact.addMetadata( new ProjectArtifactMetadata( artifact, project.getFile() ) );
 252  
 
 253  4
             project.setArtifact( artifact );
 254  
 
 255  4
             return project;
 256  
         }
 257  0
         catch ( ProjectBuildingException e )
 258  
         {
 259  0
             throw new TestToolsException(
 260  
                                           "Error building MavenProject instance from test pom: " + pomInfo.getPomFile(),
 261  
                                           e );
 262  
         }
 263  0
         catch ( UnknownRepositoryLayoutException e )
 264  
         {
 265  0
             throw new TestToolsException(
 266  
                                          "Error building ArtifactRepository instance from test pom: " + pomInfo.getPomFile(),
 267  
                                          e );
 268  
         }
 269  0
         catch ( IOException e )
 270  
         {
 271  0
             throw new TestToolsException(
 272  
                                          "Error building ArtifactRepository instance from test pom: " + pomInfo.getPomFile(),
 273  
                                          e );
 274  
         }
 275  
     }
 276  
 
 277  
     /**
 278  
      * Inject a special version for testing, to allow tests to unambiguously reference the plugin
 279  
      * currently under test. If test builds will be executed from the unit-testing phase, also inject
 280  
      * &lt;skip&gt;true&lt;/skip&gt; into the configuration of the <code>maven-surefire-plugin</code>
 281  
      * to allow production of a test-only version of the plugin jar without running unit tests.
 282  
      *
 283  
      * @param pomFile The plugin POM
 284  
      * @param testVersion The version that allows test builds to reference the plugin under test
 285  
      * @param skipUnitTests If true, configure the surefire plugin to skip unit tests
 286  
      * @return Information about mangled POM, including the temporary file to which it was written.
 287  
      * @throws TestToolsException if any
 288  
      */
 289  
     protected PomInfo manglePomForTesting( File pomFile, String testVersion, boolean skipUnitTests )
 290  
         throws TestToolsException
 291  
     {
 292  6
         File input = pomFile;
 293  
 
 294  6
         File output = new File( pomFile.getParentFile(), "pom-" + testVersion + ".xml" );
 295  6
         output.deleteOnExit();
 296  
 
 297  6
         Reader reader = null;
 298  6
         Writer writer = null;
 299  
 
 300  6
         Model model = null;
 301  6
         String finalName = null;
 302  6
         String buildDirectory = null;
 303  
 
 304  
         try
 305  
         {
 306  6
             reader = ReaderFactory.newXmlReader( input );
 307  
 
 308  6
             model = new MavenXpp3Reader().read( reader );
 309  
         }
 310  0
         catch ( IOException e )
 311  
         {
 312  0
             throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
 313  
         }
 314  0
         catch ( XmlPullParserException e )
 315  
         {
 316  0
             throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
 317  
         }
 318  
         finally
 319  
         {
 320  6
             IOUtil.close( reader );
 321  6
         }
 322  
 
 323  
         try
 324  
         {
 325  6
             model.setVersion( testVersion );
 326  
 
 327  6
             Build build = model.getBuild();
 328  6
             if ( build == null )
 329  
             {
 330  6
                 build = new Build();
 331  
             }
 332  6
             buildDirectory = build.getDirectory();
 333  
 
 334  6
             if ( buildDirectory == null )
 335  
             {
 336  6
                 buildDirectory = "target";
 337  
             }
 338  
 
 339  6
             buildDirectory = ( buildDirectory + File.separatorChar + "it-build-target" );
 340  6
             build.setDirectory( buildDirectory );
 341  6
             build.setOutputDirectory( buildDirectory + File.separatorChar + "classes" );
 342  6
             System.out.println( "Using " + build.getDirectory() + " and " + build.getOutputDirectory()
 343  
                 + " to build IT version of plugin" );
 344  6
             model.setBuild( build );
 345  
 
 346  6
             finalName = build.getFinalName();
 347  
 
 348  6
             if ( finalName == null )
 349  
             {
 350  6
                 ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( model.getPackaging() );
 351  
 
 352  6
                 String ext = handler.getExtension();
 353  
 
 354  6
                 finalName = model.getArtifactId() + "-" + model.getVersion() + "." + ext;
 355  
             }
 356  
 
 357  6
             DistributionManagement distMgmt = new DistributionManagement();
 358  
 
 359  6
             DeploymentRepository deployRepo = new DeploymentRepository();
 360  
 
 361  6
             deployRepo.setId( "integration-test.output" );
 362  
 
 363  6
             File tmpDir = FileUtils.createTempFile( "integration-test-repo", "", null );
 364  6
             String tmpUrl = tmpDir.toURL().toExternalForm();
 365  
 
 366  6
             deployRepo.setUrl( tmpUrl );
 367  
 
 368  6
             distMgmt.setRepository( deployRepo );
 369  6
             distMgmt.setSnapshotRepository( deployRepo );
 370  
 
 371  6
             Repository localAsRemote = new Repository();
 372  6
             localAsRemote.setId( "testing.mainLocalAsRemote" );
 373  
 
 374  6
             File localRepoDir = repositoryTool.findLocalRepositoryDirectory();
 375  6
             localAsRemote.setUrl( localRepoDir.toURL().toExternalForm() );
 376  
 
 377  6
             model.addRepository( localAsRemote );
 378  6
             model.addPluginRepository( localAsRemote );
 379  
 
 380  6
             Site site = new Site();
 381  
 
 382  6
             site.setId( "integration-test.output" );
 383  6
             site.setUrl( tmpUrl );
 384  
 
 385  6
             distMgmt.setSite( site );
 386  
 
 387  6
             model.setDistributionManagement( distMgmt );
 388  
 
 389  6
             model.addProperty( INTEGRATION_TEST_DEPLOYMENT_REPO_URL, tmpUrl );
 390  
 
 391  6
             if ( skipUnitTests )
 392  
             {
 393  6
                 List plugins = build.getPlugins();
 394  6
                 Plugin plugin = null;
 395  6
                 for ( Iterator iter = plugins.iterator(); iter.hasNext(); )
 396  
                 {
 397  0
                     Plugin plug = (Plugin) iter.next();
 398  
 
 399  0
                     if ( "maven-surefire-plugin".equals( plug.getArtifactId() ) )
 400  
                     {
 401  0
                         plugin = plug;
 402  0
                         break;
 403  
                     }
 404  0
                 }
 405  
 
 406  6
                 if ( plugin == null )
 407  
                 {
 408  6
                     plugin = new Plugin();
 409  6
                     plugin.setArtifactId( "maven-surefire-plugin" );
 410  6
                     build.addPlugin( plugin );
 411  
                 }
 412  
 
 413  6
                 Xpp3Dom configDom = (Xpp3Dom) plugin.getConfiguration();
 414  6
                 if ( configDom == null )
 415  
                 {
 416  6
                     configDom = new Xpp3Dom( "configuration" );
 417  6
                     plugin.setConfiguration( configDom );
 418  
                 }
 419  
 
 420  6
                 Xpp3Dom skipDom = new Xpp3Dom( "skip" );
 421  6
                 skipDom.setValue( "true" );
 422  
 
 423  6
                 configDom.addChild( skipDom );
 424  
             }
 425  
 
 426  6
             writer = WriterFactory.newXmlWriter( output );
 427  
 
 428  6
             new MavenXpp3Writer().write( writer, model );
 429  
         }
 430  0
         catch ( IOException e )
 431  
         {
 432  0
             throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
 433  
         }
 434  
         finally
 435  
         {
 436  6
             IOUtil.close( writer );
 437  6
         }
 438  
 
 439  6
         return new PomInfo( output, model.getGroupId(), model.getArtifactId(), model.getVersion(),
 440  
                             model.getBuild().getDirectory(), model.getBuild().getOutputDirectory(), finalName );
 441  
     }
 442  
 
 443  6
     static final class PomInfo
 444  
     {
 445  
         private final File pomFile;
 446  
 
 447  
         private final String groupId;
 448  
 
 449  
         private final String artifactId;
 450  
 
 451  
         private final String version;
 452  
 
 453  
         private final String finalName;
 454  
 
 455  
         private final String buildDirectory;
 456  
 
 457  
         private final String buildOutputDirectory;
 458  
 
 459  
         PomInfo( File pomFile, String groupId, String artifactId, String version, String buildDirectory,
 460  
                  String buildOutputDirectory, String finalName )
 461  6
         {
 462  6
             this.pomFile = pomFile;
 463  6
             this.groupId = groupId;
 464  6
             this.artifactId = artifactId;
 465  6
             this.version = version;
 466  6
             this.buildDirectory = buildDirectory;
 467  6
             this.buildOutputDirectory = buildOutputDirectory;
 468  6
             this.finalName = finalName;
 469  6
         }
 470  
 
 471  
         File getPomFile()
 472  
         {
 473  16
             return pomFile;
 474  
         }
 475  
 
 476  
         String getBuildDirectory()
 477  
         {
 478  6
             return buildDirectory;
 479  
         }
 480  
 
 481  
         String getBuildOutputDirectory()
 482  
         {
 483  2
             return buildOutputDirectory;
 484  
         }
 485  
 
 486  
         String getFinalName()
 487  
         {
 488  6
             return finalName;
 489  
         }
 490  
 
 491  
         File getBuildLogFile()
 492  
         {
 493  4
             return new File( buildDirectory + "/test-build-logs/" + groupId + "_" + artifactId + "_" + version
 494  
                 + ".build.log" );
 495  
         }
 496  
     }
 497  
 }