-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathExecJavaMojoTest.java
More file actions
332 lines (275 loc) · 13.3 KB
/
ExecJavaMojoTest.java
File metadata and controls
332 lines (275 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package org.codehaus.mojo.exec;
/*
* Copyright 2005-2006 The Codehaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.monitor.logging.DefaultLog;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.StringOutputStream;
/**
* @author Jerome Lacoste <jerome@coffeebreaks.org>
* @version $Id$
*/
public class ExecJavaMojoTest
extends AbstractMojoTestCase
{
/*
* This one won't work yet public void xxtestSimpleRunPropertiesAndArguments() throws MojoExecutionException,
* Exception { File pom = new File( getBasedir(), "src/test/projects/project2/pom.xml" ); String output = execute(
* pom, "java" ); System.out.println(output); assertEquals( -1, output.trim().indexOf( "ERROR" ) ); }
*/
/**
* Check that a simple execution with no arguments and no system properties produces the expected result
* <p/>
* we load the config from a pom file and fill up the MavenProject property ourselves
*/
public void testSimpleRun()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project4/pom.xml" );
String output = execute( pom, "java" );
assertEquals( "Hello" + System.getProperty( "line.separator" ), output );
}
/**
* MEXEC-10 Check that an execution with no arguments and an system property with no value produces the expected
* result
* <p/>
* we load the config from a pom file and fill up the MavenProject property ourselves
*/
public void testEmptySystemProperty()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project5/pom.xml" );
assertNull( "System property not yet created", System.getProperty( "project5.property.with.no.value" ) );
execute( pom, "java" );
assertEquals( "System property now empty", "", System.getProperty( "project5.property.with.no.value" ) );
}
/**
* MEXEC-29 exec:java throws NPE if the mainClass main method has not a correct signature
* <p/>
*/
// Moved this test to src/it/mexec-29 (integration test)
// cause it will fail. This is based of trying to
// using dependencies (commons-logging:1.0.4:jar; commons-io:commons-is:1.1) which will be resolved
// against maven central which will not work always in particular
// when maven central is proxied via a repository manager.
// This could be solved by using the local settings.xml
// file of the user, but the tests don't use it.
// Apart from that if the class does not exist as in this test
// the real execution of the plugin will result in a ClassNotFoundException
// like this:
// [DEBUG] Adding project dependency artifact: commons-io to classpath
// [DEBUG] Adding project dependency artifact: commons-logging to classpath
// [DEBUG] joining on thread Thread[org.codehaus.mojo.exec.NoMain.main(),5,org.codehaus.mojo.exec.NoMain]
// [WARNING]
// java.lang.ClassNotFoundException: org.codehaus.mojo.exec.NoMain
// at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
// at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
// at java.security.AccessController.doPrivileged(Native Method)
// at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
// at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
// at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
// at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
// at java.lang.Thread.run(Thread.java:722)
/*
* public void testIncorrectMainMethodSignature() throws Exception { File pom = new File( getBasedir(),
* "src/test/projects/project12/pom.xml" ); try { String output = execute( pom, "java" ); } catch
* (MojoExecutionException e) { assertTrue( stringContains( e.getMessage(),
* "The specified mainClass doesn't contain a main method with appropriate signature." ) ); } }
*/
// this test doesn't work as the classpath passed to the project when executing the POM isn't the same as when maven
// is executed from within the project dir
// Should be moved as an integration-test
/*
* public void testSetClasspathScopeToTest() throws Exception { File pom = new File( getBasedir(),
* "src/test/projects/project14/pom.xml" ); String output = execute( pom, "java" ); assertEquals( "Hello" +
* System.getProperty( "line.separator" ), output ); }
*/
/**
* For cases where the Java code spawns Threads and main returns soon. See
* <a href="http://jira.codehaus.org/browse/MEXEC-6">MEXEC-6</a>.
*/
public void testWaitNoDaemonThreads()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project7/pom.xml" );
String output = execute( pom, "java" );
assertEquals( MainWithThreads.ALL_EXITED, output.trim() );
}
public void testClasspathDependencyExcludes()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project15/pom.xml" );
String output = executeDebug( pom, "java" );
String expectedExcludeDependency = "commons-logging:commons-logging";
Pattern pattern = Pattern.compile(".*?Collected project artifacts \\[(.*?)\\].*");
Matcher matcher = pattern.matcher(output.trim());
String result = expectedExcludeDependency;
//check it present in project dependency
if(matcher.find()){
pattern = Pattern.compile(".*?Collected project artifacts after filter \\[(.*?)\\].*");
matcher = pattern.matcher(output.trim());
//check it present after filter
if(matcher.find()){
result = matcher.group(0);
}
}
assertFalse(result.contains(expectedExcludeDependency) );
}
/**
* For cases where the Java code spawns Threads and main returns soon, but code contains non interruptible threads.
* User is required to timeout the execution, otherwise it will hang. See
* <a href="http://jira.codehaus.org/browse/MEXEC-15">MEXEC-15</a>.
*/
public void testWaitNonInterruptibleDaemonThreads()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project9/pom.xml" );
String output = execute( pom, "java" );
assertEquals( MainWithThreads.TIMER_IGNORED, output.trim() );
}
/**
* See <a href="http://jira.codehaus.org/browse/MEXEC-15">MEXEC-15</a>. FIXME: this sometimes fail with
* unit.framework.ComparisonFailure: expected:<...> but was:<...3(f)>
*/
public void testUncooperativeThread()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project10/pom.xml" );
String output = execute( pom, "java" );
// note: execute() will wait a little bit before returning the output,
// thereby allowing the stop()'ed thread to output the final "(f)".
assertEquals( MainUncooperative.SUCCESS, output.trim() );
}
/**
* See <a href="http://jira.codehaus.org/browse/MEXEC-17">MEXEC-17</a>.
*/
/**
* This test doesn't work because the sun.tools.javac.Main class referenced in the project pom is found even if the
* system scope dependencies are not added by the plugin. The class was probably loaded by another plugin ?! To fix
* the test we have to: - maybe use a different system scope dependency/class to load. - find a different way to
* test. When ran manually, the test works though (i.e. removing the code that manually adds the system scope
* dependencies make the test fail). public void testSystemDependencyFound() throws Exception { File pom = new File(
* getBasedir(), "src/test/projects/project11/pom.xml" ); String output = execute( pom, "java" ); assertEquals(
* FindClassInClasspath.FOUND_ALL, output.trim() ); }
*/
/**
* Test the commandline parsing facilities of the {@link AbstractExecMojo} class
*/
public void testRunWithArgs()
throws Exception
{
String resultString = execute( new File( getBasedir(), "src/test/projects/project8/pom.xml" ), "java" );
String LS = System.getProperty( "line.separator" );
String expectedResult = "Hello" + LS + "Arg1" + LS + "Arg2a Arg2b" + LS;
assertEquals( expectedResult, resultString );
}
/**
* @return output from System.out during mojo execution
*/
private String execute( File pom, String goal )
throws Exception
{
ExecJavaMojo mojo;
mojo = (ExecJavaMojo) lookupMojo( goal, pom );
setUpProject( pom, mojo );
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
// why isn't this set up by the harness based on the default-value? TODO get to bottom of this!
setVariableValueToObject( mojo, "includeProjectDependencies", Boolean.TRUE );
setVariableValueToObject( mojo, "killAfter", (long) -1 );
setVariableValueToObject( mojo, "cleanupDaemonThreads", Boolean.TRUE );
setVariableValueToObject( mojo, "classpathScope", "compile" );
assertNotNull( mojo );
assertNotNull( project );
// trap System.out
PrintStream out = System.out;
StringOutputStream stringOutputStream = new StringOutputStream();
System.setOut( new PrintStream( stringOutputStream ) );
// ensure we don't log unnecessary stuff which would interfere with assessing success of tests
mojo.setLog( new DefaultLog( new ConsoleLogger( Logger.LEVEL_ERROR, "exec:java" ) ) );
try
{
mojo.execute();
}
finally
{
// see testUncooperativeThread() for explaination
Thread.sleep( 300 ); // time seems about right
System.setOut( out );
}
return stringOutputStream.toString();
}
/**
* @return output from System.out during mojo execution
*/
private String executeDebug( File pom, String goal )
throws Exception
{
ExecJavaMojo mojo;
mojo = (ExecJavaMojo) lookupMojo( goal, pom );
setUpProject( pom, mojo );
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
// why isn't this set up by the harness based on the default-value? TODO get to bottom of this!
setVariableValueToObject( mojo, "includeProjectDependencies", Boolean.TRUE );
setVariableValueToObject( mojo, "killAfter", (long) -1 );
setVariableValueToObject( mojo, "cleanupDaemonThreads", Boolean.TRUE );
setVariableValueToObject( mojo, "classpathScope", "compile" );
assertNotNull( mojo );
assertNotNull( project );
// trap System.out
PrintStream out = System.out;
StringOutputStream stringOutputStream = new StringOutputStream();
System.setOut( new PrintStream( stringOutputStream ) );
// ensure we don't log unnecessary stuff which would interfere with assessing success of tests
mojo.setLog( new DefaultLog( new ConsoleLogger( Logger.LEVEL_DEBUG, "exec:java" ) ) );
try
{
mojo.execute();
}
finally
{
// see testUncooperativeThread() for explaination
Thread.sleep( 300 ); // time seems about right
System.setOut( out );
}
return stringOutputStream.toString();
}
private void setUpProject( File pomFile, AbstractMojo mojo )
throws Exception
{
MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
ArtifactRepositoryLayout localRepositoryLayout =
(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
String path = "src/test/repository";
ArtifactRepository localRepository =
new DefaultArtifactRepository( "local", "file://" + new File( path ).getAbsolutePath(),
localRepositoryLayout );
MavenProject project = builder.buildWithDependencies( pomFile, localRepository, null );
// this gets the classes for these tests of this mojo (exec plugin) onto the project classpath for the test
project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() );
setVariableValueToObject( mojo, "project", project );
}
}