View Javadoc

1   package org.apache.maven.plugin.dependency.its;
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.net.MalformedURLException;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Properties;
28  
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.shared.invoker.InvocationRequest;
31  import org.apache.maven.shared.invoker.InvocationResult;
32  import org.apache.maven.shared.test.plugin.BuildTool;
33  import org.apache.maven.shared.test.plugin.PluginTestTool;
34  import org.apache.maven.shared.test.plugin.ProjectTool;
35  import org.apache.maven.shared.test.plugin.TestToolsException;
36  import org.codehaus.classworlds.ClassRealm;
37  import org.codehaus.plexus.PlexusContainer;
38  import org.codehaus.plexus.PlexusTestCase;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
43   *         Copied from the Eclipse
44   *         AbstractEclipsePluginTestCase v2.4
45   * @version $Id: AbstractDependencyPluginITCase.java 556442
46   *          2007-07-15 20:20:23Z dantran $
47   */
48  public abstract class AbstractDependencyPluginITCase
49      extends PlexusTestCase
50  {
51  
52      private BuildTool buildTool;
53  
54      private ProjectTool projectTool;
55  
56      /**
57       * Test repository directory.
58       */
59      protected static File localRepositoryDirectory = getTestFile( "target/test-classes/m2repo" );
60  
61      /**
62       * Pom File
63       */
64      protected static File PomFile = new File( getBasedir(), "pom.xml" );
65  
66      /**
67       * Group-Id for running test builds.
68       */
69      protected static final String GROUP_ID = "org.apache.maven.plugins";
70  
71      /**
72       * Artifact-Id for running test builds.
73       */
74      protected static final String ARTIFACT_ID = "maven-dependency-plugin";
75  
76      /**
77       * Version under which the plugin was installed to the
78       * test-time local repository for running test builds.
79       */
80      protected static final String VERSION = "test";
81  
82      private static final String BUILD_OUTPUT_DIRECTORY = "target/surefire-reports/build-output";
83  
84      private static boolean installed = false;
85  
86      /**
87       * @see org.codehaus.plexus.PlexusTestCase#setUp()
88       */
89      protected void setUp()
90          throws Exception
91      {
92          if ( !installed )
93          {
94              System.out
95                  .println( "*** Running test builds; output will be directed to: " + BUILD_OUTPUT_DIRECTORY + "\n" );
96          }
97  
98          super.setUp();
99  
100         buildTool = (BuildTool) lookup( BuildTool.ROLE, "default" );
101 
102         projectTool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
103 
104         String mavenHome = System.getProperty( "maven.home" );
105 
106         // maven.home is set by surefire when the test is
107         // run with maven, but better make the test run in
108         // IDEs without
109         // the need of additional properties
110         if ( mavenHome == null )
111         {
112             String path = System.getProperty( "java.library.path" );
113             String[] paths = StringUtils.split( path, System.getProperty( "path.separator" ) );
114             for ( int j = 0; j < paths.length; j++ )
115             {
116                 String pt = paths[j];
117                 if ( new File( pt, "mvn" ).exists() )
118                 {
119                     System.setProperty( "maven.home", new File( pt ).getAbsoluteFile().getParent() );
120                     break;
121                 }
122 
123             }
124         }
125 
126         System.setProperty( "MAVEN_TERMINATE_CMD", "on" );
127 
128         synchronized ( AbstractDependencyPluginITCase.class )
129         {
130             if ( !installed )
131             {
132                 PluginTestTool pluginTestTool = (PluginTestTool) lookup( PluginTestTool.ROLE, "default" );
133 
134                 localRepositoryDirectory = pluginTestTool
135                     .preparePluginForUnitTestingWithMavenBuilds( PomFile, "test", localRepositoryDirectory );
136 
137                 System.out.println( "*** Installed test-version of the Dependency plugin to: "
138                     + localRepositoryDirectory + "\n" );
139 
140                 installed = true;
141             }
142         }
143 
144     }
145 
146     /**
147      * @see org.codehaus.plexus.PlexusTestCase#tearDown()
148      */
149     protected void tearDown()
150         throws Exception
151     {
152         super.tearDown();
153 
154         List<PlexusContainer> containers = new ArrayList<PlexusContainer>();
155 
156         containers.add( getContainer() );
157 
158         for ( PlexusContainer container : containers )
159         {
160             if ( container != null )
161             {
162                 container.dispose();
163 
164                 ClassRealm realm = container.getContainerRealm();
165 
166                 if ( realm != null )
167                 {
168                     realm.getWorld().disposeRealm( realm.getId() );
169                 }
170             }
171         }
172     }
173 
174     /**
175      * Execute the plugin with no properties
176      * 
177      * @param projectName project directory
178      * @param goalList comma separated list of goals to
179      *            execute
180      * @throws Exception any exception generated during test
181      */
182     protected void testProject( String projectName, String goalList )
183         throws Exception
184     {
185         Properties props = new Properties();
186         testProject( projectName, props, goalList );
187     }
188 
189     /**
190      * Execute the plugin.
191      * 
192      * @param projectName project directory
193      * @param properties additional properties
194      * @param goalList comma separated list of goals to
195      *            execute
196      * @throws Exception any exception generated during test
197      */
198     protected void testProject( String projectName, Properties properties, String goalList )
199         throws Exception
200     {
201         File theBasedir = getTestFile( "target/test-classes/its/" + projectName );
202 
203         File pom = new File( theBasedir, "pom.xml" );
204 
205         List<String> goals = Arrays.asList( goalList.split( "," ) );
206 
207         executeMaven( pom, properties, goals );
208 
209         // MavenProject project = readProject( pom );
210 
211         /*
212          * String outputDirPath = IdeUtils.getPluginSetting(
213          * project, "maven-dependency-plugin", "outputDir",
214          * null ); File outputDir; File projectOutputDir =
215          * basedir;
216          * 
217          * if ( outputDirPath == null ) { outputDir =
218          * basedir; } else { outputDir = new File( basedir,
219          * outputDirPath ); outputDir.mkdirs();
220          * projectOutputDir = new File( outputDir,
221          * project.getArtifactId() ); }
222          */
223     }
224 
225     protected File getOutputDirectory( String projectName )
226     {
227         return getTestFile( "target/test-classes/projects/" + projectName );
228     }
229 
230     protected void executeMaven( File pom, Properties properties, List<String> goals )
231         throws TestToolsException, ExecutionFailedException
232     {
233         executeMaven( pom, properties, goals, true );
234     }
235 
236     protected void executeMaven( File pom, Properties properties, List<String> goals, boolean switchLocalRepo )
237         throws TestToolsException, ExecutionFailedException
238     {
239         // insert the test property to activate the test
240         // profile
241         properties.setProperty( "test", "true" );
242         new File( BUILD_OUTPUT_DIRECTORY ).mkdirs();
243 
244         File buildLog = null;
245 
246         for ( StackTraceElement element : new NullPointerException().getStackTrace() )
247         {
248             String methodName = element.getMethodName();
249 
250             if ( methodName.startsWith( "test" ) && !methodName.equals( "testProject" ) )
251             {
252                 String classname = element.getClassName();
253 
254                 buildLog = new File( BUILD_OUTPUT_DIRECTORY, classname + "_" + element.getMethodName() + ".build.log" );
255 
256                 break;
257             }
258         }
259 
260         if ( buildLog == null )
261         {
262             buildLog = new File( BUILD_OUTPUT_DIRECTORY, "unknown.build.log" );
263         }
264 
265         InvocationRequest request = buildTool.createBasicInvocationRequest( pom, properties, goals, buildLog );
266         request.setUpdateSnapshots( false );
267         request.setShowErrors( true );
268 
269         request.setDebug( true );
270 
271         if ( switchLocalRepo )
272         {
273             request.setLocalRepositoryDirectory( localRepositoryDirectory );
274         }
275 
276         InvocationResult result = buildTool.executeMaven( request );
277 
278         if ( result.getExitCode() != 0 )
279         {
280             String buildLogUrl = buildLog.getAbsolutePath();
281 
282             try
283             {
284                 
285                 buildLogUrl = buildLog.toURI().toURL().toExternalForm();
286             }
287             catch ( MalformedURLException e )
288             {
289             }
290 
291             throw new ExecutionFailedException( "Failed to execute build.\nPOM: " + pom + "\nGoals: "
292                 + StringUtils.join( goals.iterator(), ", " ) + "\nExit Code: " + result.getExitCode() + "\nError: "
293                 + result.getExecutionException() + "\nBuild Log: " + buildLogUrl + "\n", result );
294         }
295     }
296 
297     protected MavenProject readProject( File pom )
298         throws TestToolsException
299     {
300         return projectTool.readProject( pom, localRepositoryDirectory );
301     }
302 
303     protected String getPluginCLISpecification()
304     {
305         String pluginSpec = GROUP_ID + ":" + ARTIFACT_ID + ":";
306 
307         // String pluginVersion = System.getProperty(
308         // "pluginVersion" );
309         //        
310         // if ( pluginVersion != null )
311         // {
312         // pluginSpec += pluginVersion + ":";
313         // }
314         //
315         // System.out.println( "\n\nUsing Eclipse plugin
316         // version: " + pluginVersion + "\n\n" );
317 
318         // try using the test-version installed during
319         // setUp()
320         pluginSpec += VERSION + ":";
321 
322         return pluginSpec;
323     }
324 }