1 package org.codehaus.mojo.exec;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.maven.artifact.repository.ArtifactRepository;
20 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
21 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.AbstractMojo;
24 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
25 import org.apache.maven.project.MavenProject;
26 import org.apache.maven.project.MavenProjectBuilder;
27 import org.apache.maven.monitor.logging.DefaultLog;
28 import org.codehaus.plexus.util.StringOutputStream;
29 import org.codehaus.plexus.logging.Logger;
30 import org.codehaus.plexus.logging.console.ConsoleLogger;
31
32 import java.io.File;
33 import java.io.PrintStream;
34
35
36
37
38
39 public class ExecJavaMojoTest
40 extends AbstractMojoTestCase
41 {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public void testSimpleRun()
64 throws Exception
65 {
66 File pom = new File( getBasedir(), "src/test/projects/project4/pom.xml" );
67
68 String output = execute( pom, "java" );
69
70 assertEquals( "Hello" + System.getProperty( "line.separator" ), output );
71 }
72
73
74
75
76
77
78
79 public void testEmptySystemProperty()
80 throws Exception
81 {
82 File pom = new File( getBasedir(), "src/test/projects/project5/pom.xml" );
83
84 assertNull( "System property not yet created",
85 System.getProperty( "project5.property.with.no.value" ) );
86
87 execute( pom, "java" );
88
89 assertEquals( "System property now empty",
90 "",
91 System.getProperty( "project5.property.with.no.value" ) );
92 }
93
94
95
96
97
98 public void testIncorrectMainMethodSignature()
99 throws Exception
100 {
101 File pom = new File( getBasedir(), "src/test/projects/project12/pom.xml" );
102
103 try {
104 String output = execute( pom, "java" );
105 } catch (MojoExecutionException e) {
106 assertTrue( stringContains( e.getMessage(), "The specified mainClass doesn't contain a main method with appropriate signature." ) );
107 }
108
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 private boolean stringContains( String str, String sequence )
127 {
128 return str.indexOf( sequence ) != -1;
129 }
130
131
132
133
134
135 public void testWaitNoDaemonThreads()
136 throws Exception
137 {
138 File pom = new File( getBasedir(), "src/test/projects/project7/pom.xml" );
139
140 String output = execute( pom, "java" );
141
142 assertEquals( MainWithThreads.ALL_EXITED, output.trim() );
143 }
144
145
146
147
148
149
150 public void testWaitNonInterruptibleDaemonThreads()
151 throws Exception
152 {
153 File pom = new File( getBasedir(), "src/test/projects/project9/pom.xml" );
154
155 String output = execute( pom, "java" );
156
157 assertEquals( MainWithThreads.TIMER_IGNORED, output.trim() );
158 }
159
160
161
162
163
164 public void testUncooperativeThread()
165 throws Exception
166 {
167 File pom = new File( getBasedir(), "src/test/projects/project10/pom.xml" );
168 String output = execute( pom, "java" );
169
170
171 assertEquals( MainUncooperative.SUCCESS, output.trim() );
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 public void testRunWithArgs() throws Exception
204 {
205
206 String resultString = execute( new File( getBasedir(), "src/test/projects/project8/pom.xml" ), "java" );
207
208 String LS = System.getProperty("line.separator");
209 String expectedResult = "Hello" + LS + "Arg1" + LS +"Arg2a Arg2b" + LS;
210 assertEquals( expectedResult, resultString );
211 }
212
213
214
215
216 private String execute( File pom, String goal ) throws Exception {
217
218 ExecJavaMojo mojo;
219 mojo = (ExecJavaMojo) lookupMojo( goal, pom );
220
221 setUpProject( pom, mojo );
222
223 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
224
225
226 setVariableValueToObject( mojo, "includeProjectDependencies", Boolean.TRUE );
227 setVariableValueToObject( mojo, "killAfter", new Long( -1 ) );
228 setVariableValueToObject( mojo, "cleanupDaemonThreads", Boolean.TRUE );
229 setVariableValueToObject( mojo, "classpathScope", "compile" );
230
231 assertNotNull( mojo );
232 assertNotNull( project );
233
234
235 PrintStream out = System.out;
236 StringOutputStream stringOutputStream = new StringOutputStream();
237 System.setOut( new PrintStream( stringOutputStream ) );
238
239 mojo.setLog( new DefaultLog( new ConsoleLogger( Logger.LEVEL_ERROR, "exec:java" ) ) );
240
241 try
242 {
243 mojo.execute();
244 }
245 finally
246 {
247
248 Thread.sleep( 300 );
249 System.setOut( out );
250 }
251
252 return stringOutputStream.toString();
253 }
254
255 private void setUpProject( File pomFile, AbstractMojo mojo )
256 throws Exception
257 {
258 MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
259
260 ArtifactRepositoryLayout localRepositoryLayout =
261 (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
262
263 String path = "src/test/repository";
264
265 ArtifactRepository localRepository = new DefaultArtifactRepository( "local", "file://" +
266 new File( path ).getAbsolutePath(), localRepositoryLayout );
267
268 MavenProject project = builder.buildWithDependencies( pomFile, localRepository, null );
269
270 project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() );
271 setVariableValueToObject( mojo, "project", project );
272 }
273 }