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 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 goals = new ArrayList();
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 goals = new ArrayList();
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 goals = new ArrayList();
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 goals = new ArrayList();
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 goals = new ArrayList();
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 goals = new ArrayList();
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 invoker.setMavenHome( findMavenHome() );
198 InvokerLogger logger = new SystemOutLogger();
199 logger.setThreshold( InvokerLogger.DEBUG );
200 invoker.setLogger( logger );
201 return invoker;
202 }
203
204 private File findMavenHome()
205 throws IOException
206 {
207 String mavenHome = System.getProperty( "maven.home" );
208
209 if ( mavenHome == null )
210 {
211 mavenHome = CommandLineUtils.getSystemEnvVars().getProperty( "M2_HOME" );
212 }
213
214 if ( mavenHome == null )
215 {
216 throw new IllegalStateException( "Cannot find Maven application "
217 + "directory. Either specify \'maven.home\' system property, or M2_HOME environment variable." );
218 }
219
220 return new File( mavenHome );
221 }
222
223 private File getBasedirForBuild()
224 throws URISyntaxException
225 {
226 StackTraceElement element = new NullPointerException().getStackTrace()[1];
227 String methodName = element.getMethodName();
228
229 String dirName = StringUtils.addAndDeHump( methodName );
230
231 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
232 URL dirResource = cloader.getResource( dirName );
233
234 if ( dirResource == null )
235 {
236 throw new IllegalStateException( "Project: " + dirName + " for test method: " + methodName + " is missing." );
237 }
238
239 return new File( new URI( dirResource.toString() ).getPath() );
240 }
241
242
243 private void logTestStart()
244 {
245 NullPointerException npe = new NullPointerException();
246 StackTraceElement element = npe.getStackTrace()[1];
247
248 System.out.println( "Starting: " + element.getMethodName() );
249 }
250
251 }