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 |
|
@author |
37 |
|
@version |
38 |
|
|
|
|
| 0% |
Uncovered Elements: 61 (61) |
Complexity: 11 |
Complexity Density: 0.22 |
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4
-
|
|
63 |
0
|
public void testSimpleRun()... |
64 |
|
throws Exception |
65 |
|
{ |
66 |
0
|
File pom = new File( getBasedir(), "src/test/projects/project4/pom.xml" ); |
67 |
|
|
68 |
0
|
String output = execute( pom, "java" ); |
69 |
|
|
70 |
0
|
assertEquals( "Hello" + System.getProperty( "line.separator" ), output ); |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4
-
|
|
79 |
0
|
public void testEmptySystemProperty()... |
80 |
|
throws Exception |
81 |
|
{ |
82 |
0
|
File pom = new File( getBasedir(), "src/test/projects/project5/pom.xml" ); |
83 |
|
|
84 |
0
|
assertNull( "System property not yet created", |
85 |
|
System.getProperty( "project5.property.with.no.value" ) ); |
86 |
|
|
87 |
0
|
execute( pom, "java" ); |
88 |
|
|
89 |
0
|
assertEquals( "System property now empty", |
90 |
|
"", |
91 |
|
System.getProperty( "project5.property.with.no.value" ) ); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
4
-
|
|
98 |
0
|
public void testIncorrectMainMethodSignature()... |
99 |
|
throws Exception |
100 |
|
{ |
101 |
0
|
File pom = new File( getBasedir(), "src/test/projects/project12/pom.xml" ); |
102 |
|
|
103 |
0
|
try { |
104 |
0
|
String output = execute( pom, "java" ); |
105 |
|
} catch (MojoExecutionException e) { |
106 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
0
|
private boolean stringContains( String str, String sequence )... |
127 |
|
{ |
128 |
0
|
return str.indexOf( sequence ) != -1; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4
-
|
|
135 |
0
|
public void testWaitNoDaemonThreads()... |
136 |
|
throws Exception |
137 |
|
{ |
138 |
0
|
File pom = new File( getBasedir(), "src/test/projects/project7/pom.xml" ); |
139 |
|
|
140 |
0
|
String output = execute( pom, "java" ); |
141 |
|
|
142 |
0
|
assertEquals( MainWithThreads.ALL_EXITED, output.trim() ); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4
-
|
|
150 |
0
|
public void testWaitNonInterruptibleDaemonThreads()... |
151 |
|
throws Exception |
152 |
|
{ |
153 |
0
|
File pom = new File( getBasedir(), "src/test/projects/project9/pom.xml" ); |
154 |
|
|
155 |
0
|
String output = execute( pom, "java" ); |
156 |
|
|
157 |
0
|
assertEquals( MainWithThreads.TIMER_IGNORED, output.trim() ); |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4
-
|
|
164 |
0
|
public void testUncooperativeThread()... |
165 |
|
throws Exception |
166 |
|
{ |
167 |
0
|
File pom = new File( getBasedir(), "src/test/projects/project10/pom.xml" ); |
168 |
0
|
String output = execute( pom, "java" ); |
169 |
|
|
170 |
|
|
171 |
0
|
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 |
|
@link |
202 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4
-
|
|
203 |
0
|
public void testRunWithArgs() throws Exception... |
204 |
|
{ |
205 |
|
|
206 |
0
|
String resultString = execute( new File( getBasedir(), "src/test/projects/project8/pom.xml" ), "java" ); |
207 |
|
|
208 |
0
|
String LS = System.getProperty("line.separator"); |
209 |
0
|
String expectedResult = "Hello" + LS + "Arg1" + LS +"Arg2a Arg2b" + LS; |
210 |
0
|
assertEquals( expectedResult, resultString ); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
@return |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
|
216 |
0
|
private String execute( File pom, String goal ) throws Exception {... |
217 |
|
|
218 |
0
|
ExecJavaMojo mojo; |
219 |
0
|
mojo = (ExecJavaMojo) lookupMojo( goal, pom ); |
220 |
|
|
221 |
0
|
setUpProject( pom, mojo ); |
222 |
|
|
223 |
0
|
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" ); |
224 |
|
|
225 |
|
|
226 |
0
|
setVariableValueToObject( mojo, "includeProjectDependencies", Boolean.TRUE ); |
227 |
0
|
setVariableValueToObject( mojo, "killAfter", new Long( -1 ) ); |
228 |
0
|
setVariableValueToObject( mojo, "cleanupDaemonThreads", Boolean.TRUE ); |
229 |
0
|
setVariableValueToObject( mojo, "classpathScope", "compile" ); |
230 |
|
|
231 |
0
|
assertNotNull( mojo ); |
232 |
0
|
assertNotNull( project ); |
233 |
|
|
234 |
|
|
235 |
0
|
PrintStream out = System.out; |
236 |
0
|
StringOutputStream stringOutputStream = new StringOutputStream(); |
237 |
0
|
System.setOut( new PrintStream( stringOutputStream ) ); |
238 |
|
|
239 |
0
|
mojo.setLog( new DefaultLog( new ConsoleLogger( Logger.LEVEL_ERROR, "exec:java" ) ) ); |
240 |
|
|
241 |
0
|
try |
242 |
|
{ |
243 |
0
|
mojo.execute(); |
244 |
|
} |
245 |
|
finally |
246 |
|
{ |
247 |
|
|
248 |
0
|
Thread.sleep( 300 ); |
249 |
0
|
System.setOut( out ); |
250 |
|
} |
251 |
|
|
252 |
0
|
return stringOutputStream.toString(); |
253 |
|
} |
254 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
255 |
0
|
private void setUpProject( File pomFile, AbstractMojo mojo )... |
256 |
|
throws Exception |
257 |
|
{ |
258 |
0
|
MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); |
259 |
|
|
260 |
0
|
ArtifactRepositoryLayout localRepositoryLayout = |
261 |
|
(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); |
262 |
|
|
263 |
0
|
String path = "src/test/repository"; |
264 |
|
|
265 |
0
|
ArtifactRepository localRepository = new DefaultArtifactRepository( "local", "file://" + |
266 |
|
new File( path ).getAbsolutePath(), localRepositoryLayout ); |
267 |
|
|
268 |
0
|
MavenProject project = builder.buildWithDependencies( pomFile, localRepository, null ); |
269 |
|
|
270 |
0
|
project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() ); |
271 |
0
|
setVariableValueToObject( mojo, "project", project ); |
272 |
|
} |
273 |
|
} |