View Javadoc

1   package org.apache.maven.shared.invoker;
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.net.URI;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import junit.framework.TestCase;
32  
33  import org.codehaus.plexus.util.StringUtils;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  
36  public class DefaultInvokerTest
37      extends TestCase
38  {
39  
40      public void testBuildShouldSucceed()
41          throws IOException, MavenInvocationException, URISyntaxException
42      {
43          File basedir = getBasedirForBuild();
44  
45          Invoker invoker = newInvoker();
46  
47          InvocationRequest request = new DefaultInvocationRequest();
48          request.setBaseDirectory( basedir );
49  
50          request.setDebug( true );
51  
52          List<String> goals = new ArrayList<String>();
53          goals.add( "clean" );
54          goals.add( "package" );
55  
56          request.setGoals( goals );
57  
58          InvocationResult result = invoker.execute( request );
59  
60          assertEquals( 0, result.getExitCode() );
61      }
62  
63      public void testBuildShouldFail()
64          throws IOException, MavenInvocationException, URISyntaxException
65      {
66          File basedir = getBasedirForBuild();
67  
68          Invoker invoker = newInvoker();
69  
70          InvocationRequest request = new DefaultInvocationRequest();
71          request.setBaseDirectory( basedir );
72  
73          request.setDebug( true );
74  
75          List<String> goals = new ArrayList<String>();
76          goals.add( "clean" );
77          goals.add( "package" );
78  
79          request.setGoals( goals );
80  
81          InvocationResult result = invoker.execute( request );
82  
83          assertEquals( 1, result.getExitCode() );
84      }
85  
86      public void testSpacePom()
87          throws Exception
88      {
89          logTestStart();
90  
91          File basedir = getBasedirForBuild();
92  
93          Invoker invoker = newInvoker();
94  
95          InvocationRequest request = new DefaultInvocationRequest();
96          request.setBaseDirectory( basedir );
97  
98          request.setPomFileName( "pom with spaces.xml" );
99  
100         request.setDebug( true );
101 
102         List<String> goals = new ArrayList<String>();
103         goals.add( "clean" );
104 
105         request.setGoals( goals );
106 
107         InvocationResult result = invoker.execute( request );
108 
109         assertEquals( 0, result.getExitCode() );
110     }
111 
112     public void testSpaceSettings()
113         throws Exception
114     {
115         logTestStart();
116 
117         File basedir = getBasedirForBuild();
118 
119         Invoker invoker = newInvoker();
120 
121         InvocationRequest request = new DefaultInvocationRequest();
122         request.setBaseDirectory( basedir );
123 
124         request.setUserSettingsFile( new File( basedir, "settings with spaces.xml" ) );
125 
126         request.setDebug( true );
127 
128         List<String> goals = new ArrayList<String>();
129         goals.add( "validate" );
130 
131         request.setGoals( goals );
132 
133         InvocationResult result = invoker.execute( request );
134 
135         assertEquals( 0, result.getExitCode() );
136     }
137 
138     public void testSpaceLocalRepo()
139         throws Exception
140     {
141         logTestStart();
142 
143         File basedir = getBasedirForBuild();
144 
145         Invoker invoker = newInvoker();
146 
147         InvocationRequest request = new DefaultInvocationRequest();
148         request.setBaseDirectory( basedir );
149 
150         request.setLocalRepositoryDirectory( new File( basedir, "repo with spaces" ) );
151 
152         request.setDebug( true );
153 
154         List<String> goals = new ArrayList<String>();
155         goals.add( "validate" );
156 
157         request.setGoals( goals );
158 
159         InvocationResult result = invoker.execute( request );
160 
161         assertEquals( 0, result.getExitCode() );
162     }
163 
164     public void testSpaceProperties()
165         throws Exception
166     {
167         logTestStart();
168 
169         File basedir = getBasedirForBuild();
170 
171         Invoker invoker = newInvoker();
172 
173         InvocationRequest request = new DefaultInvocationRequest();
174         request.setBaseDirectory( basedir );
175 
176         Properties props = new Properties();
177         props.setProperty( "key", "value with spaces" );
178         props.setProperty( "key with spaces", "value" );
179         request.setProperties( props );
180 
181         request.setDebug( true );
182 
183         List<String> goals = new ArrayList<String>();
184         goals.add( "validate" );
185 
186         request.setGoals( goals );
187 
188         InvocationResult result = invoker.execute( request );
189 
190         assertEquals( 0, result.getExitCode() );
191     }
192 
193     private Invoker newInvoker()
194         throws IOException
195     {
196         Invoker invoker = new DefaultInvoker();
197 
198         invoker.setMavenHome( findMavenHome() );
199 
200         InvokerLogger logger = new SystemOutLogger();
201         logger.setThreshold( InvokerLogger.DEBUG );
202         invoker.setLogger( logger );
203 
204         invoker.setLocalRepositoryDirectory( findLocalRepo() );
205 
206         return invoker;
207     }
208 
209     private File findMavenHome()
210         throws IOException
211     {
212         String mavenHome = System.getProperty( "maven.home" );
213 
214         if ( mavenHome == null )
215         {
216             mavenHome = CommandLineUtils.getSystemEnvVars().getProperty( "M2_HOME" );
217         }
218 
219         if ( mavenHome == null )
220         {
221             throw new IllegalStateException( "Cannot find Maven application "
222                 + "directory. Either specify \'maven.home\' system property, or M2_HOME environment variable." );
223         }
224 
225         return new File( mavenHome );
226     }
227 
228     private File findLocalRepo()
229     {
230         String basedir = System.getProperty( "maven.repo.local", "" );
231 
232         if ( StringUtils.isNotEmpty( basedir ) )
233         {
234             return new File( basedir );
235         }
236 
237         return null;
238     }
239 
240     private File getBasedirForBuild()
241         throws URISyntaxException
242     {
243         StackTraceElement element = new NullPointerException().getStackTrace()[1];
244         String methodName = element.getMethodName();
245 
246         String dirName = StringUtils.addAndDeHump( methodName );
247 
248         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
249         URL dirResource = cloader.getResource( dirName );
250 
251         if ( dirResource == null )
252         {
253             throw new IllegalStateException( "Project: " + dirName + " for test method: " + methodName + " is missing." );
254         }
255 
256         return new File( new URI( dirResource.toString() ).getPath() );
257     }
258 
259     // this is just a debugging helper for separating unit test output...
260     private void logTestStart()
261     {
262         NullPointerException npe = new NullPointerException();
263         StackTraceElement element = npe.getStackTrace()[1];
264 
265         System.out.println( "Starting: " + element.getMethodName() );
266     }
267 
268 }