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