View Javadoc
1   
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18  *
19   * This software is copyright (c) 2015 by Alex Heneveld and Cloudsoft Corporation.
20   * 
21   * code lifted from https://github.com/ahgittin/license-audit-maven-plugin
22   */
23  package org.apache.rat.mp;
24  
25  import java.io.File;
26  import java.util.Arrays;
27  
28  import org.apache.maven.DefaultMaven;
29  import org.apache.maven.Maven;
30  import org.apache.maven.execution.DefaultMavenExecutionRequest;
31  import org.apache.maven.execution.DefaultMavenExecutionResult;
32  import org.apache.maven.execution.MavenExecutionRequest;
33  import org.apache.maven.execution.MavenExecutionRequestPopulator;
34  import org.apache.maven.execution.MavenExecutionResult;
35  import org.apache.maven.execution.MavenSession;
36  import org.apache.maven.plugin.Mojo;
37  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
38  import org.apache.maven.project.MavenProject;
39  import org.apache.maven.project.ProjectBuilder;
40  import org.apache.maven.project.ProjectBuildingRequest;
41  import org.eclipse.aether.DefaultRepositorySystemSession;
42  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
43  import org.eclipse.aether.repository.LocalRepository;
44  
45  /** Use this as you would {@link AbstractMojoTestCase},
46   * where you want more of the standard maven defaults to be set
47   * (and where the {@link AbstractMojoTestCase} leaves them as null or empty).
48   * This includes:
49   * <li> local repo, repo sessions and managers configured
50   * <li> maven default remote repos installed (NB: this does not use your ~/.m2 local settings)
51   * <li> system properties are copies
52   * <p>
53   * No changes to subclass code is needed; this simply intercepts the {@link #newMavenSession(MavenProject)} method
54   * used by the various {@link #lookupMojo(String, File)} methods.
55   * <p>
56   * This also provides new methods, {@link #newMavenSession()} to conveniently create a maven session,
57   * and {@link #lookupConfiguredMojo(File, String)} so you don't have to always build the project yourself.
58   */ 
59  public abstract class BetterAbstractMojoTestCase extends AbstractMojoTestCase {
60  
61      protected MavenSession newMavenSession() {
62          try {
63              MavenExecutionRequest request = new DefaultMavenExecutionRequest();
64              MavenExecutionResult result = new DefaultMavenExecutionResult();
65  
66              // populate sensible defaults, including repository basedir and remote repos
67              MavenExecutionRequestPopulator populator;
68              populator = getContainer().lookup( MavenExecutionRequestPopulator.class );
69              populator.populateDefaults( request );
70  
71              // this is needed to allow java profiles to get resolved; i.e. avoid during project builds:
72              // [ERROR] Failed to determine Java version for profile java-1.5-detected @ org.apache.commons:commons-parent:22, /Users/alex/.m2/repository/org/apache/commons/commons-parent/22/commons-parent-22.pom, line 909, column 14
73              request.setSystemProperties( System.getProperties() );
74              
75              // and this is needed so that the repo session in the maven session 
76              // has a repo manager, and it points at the local repo
77              // (cf MavenRepositorySystemUtils.newSession() which is what is otherwise done)
78              DefaultMaven maven = (DefaultMaven) getContainer().lookup( Maven.class );
79              DefaultRepositorySystemSession repoSession =
80                  (DefaultRepositorySystemSession) maven.newRepositorySession( request );
81              repoSession.setLocalRepositoryManager(
82                  new SimpleLocalRepositoryManagerFactory().newInstance(repoSession, 
83                      new LocalRepository( request.getLocalRepository().getBasedir() ) ));
84  
85              MavenSession session = new MavenSession( getContainer(), 
86                  repoSession,
87                  request, result );
88              return session;
89          } catch (Exception e) {
90              throw new RuntimeException(e);
91          }
92      }
93      
94      /** Extends the super to use the new {@link #newMavenSession()} introduced here 
95       * which sets the defaults one expects from maven; the standard test case leaves a lot of things blank */
96      @Override
97      protected MavenSession newMavenSession(MavenProject project) {
98          MavenSession session = newMavenSession();
99          session.setCurrentProject( project );
100         session.setProjects( Arrays.asList( project ) );
101         return session;        
102     }
103 
104     /** As {@link #lookupConfiguredMojo(MavenProject, String)} but taking the pom file 
105      * and creating the {@link MavenProject}. */
106     protected Mojo lookupConfiguredMojo(File pom, String goal) throws Exception {
107         assertNotNull( pom );
108         assertTrue( pom.exists() );
109 
110         ProjectBuildingRequest buildingRequest = newMavenSession().getProjectBuildingRequest();
111         ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
112         MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
113 
114         return lookupConfiguredMojo(project, goal);
115     }
116 
117 
118 }