1 package org.apache.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.stubs.CompilerManagerStub;
23 import org.apache.maven.plugin.stubs.DebugEnabledLog;
24 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
25 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33
34 public class CompilerMojoTestCase
35 extends AbstractMojoTestCase
36 {
37
38
39
40
41
42 public void testCompilerBasic()
43 throws Exception
44 {
45 CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-basic-test/plugin-config.xml" );
46
47 compileMojo.execute();
48
49 File testClass = new File( compileMojo.getOutputDirectory(), "TestCompile0.class" );
50
51 assertTrue( testClass.exists() );
52
53 TestCompilerMojo testCompileMojo =
54 getTestCompilerMojo( compileMojo, "target/test-classes/unit/compiler-basic-test/plugin-config.xml" );
55
56 testCompileMojo.execute();
57
58 testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile0Test.class" );
59
60 assertTrue( testClass.exists() );
61 }
62
63
64
65
66
67
68 public void testCompilerEmptySource()
69 throws Exception
70 {
71 CompilerMojo compileMojo =
72 getCompilerMojo( "target/test-classes/unit/compiler-empty-source-test/plugin-config.xml" );
73
74 compileMojo.execute();
75
76 assertFalse( compileMojo.getOutputDirectory().exists() );
77
78 TestCompilerMojo testCompileMojo =
79 getTestCompilerMojo( compileMojo, "target/test-classes/unit/compiler-empty-source-test/plugin-config.xml" );
80
81 testCompileMojo.execute();
82
83 assertFalse( testCompileMojo.getOutputDirectory().exists() );
84 }
85
86
87
88
89
90
91 public void testCompilerIncludesExcludes()
92 throws Exception
93 {
94 CompilerMojo compileMojo =
95 getCompilerMojo( "target/test-classes/unit/compiler-includes-excludes-test/plugin-config.xml" );
96
97 Set includes = new HashSet();
98 includes.add( "**/TestCompile4*.java" );
99 setVariableValueToObject( compileMojo, "includes", includes );
100
101 Set excludes = new HashSet();
102 excludes.add( "**/TestCompile2*.java" );
103 excludes.add( "**/TestCompile3*.java" );
104 setVariableValueToObject( compileMojo, "excludes", excludes );
105
106 compileMojo.execute();
107
108 File testClass = new File( compileMojo.getOutputDirectory(), "TestCompile2.class" );
109 assertFalse( testClass.exists() );
110
111 testClass = new File( compileMojo.getOutputDirectory(), "TestCompile3.class" );
112 assertFalse( testClass.exists() );
113
114 testClass = new File( compileMojo.getOutputDirectory(), "TestCompile4.class" );
115 assertTrue( testClass.exists() );
116
117 TestCompilerMojo testCompileMojo = getTestCompilerMojo( compileMojo,
118 "target/test-classes/unit/compiler-includes-excludes-test/plugin-config.xml" );
119
120 setVariableValueToObject( testCompileMojo, "testIncludes", includes );
121 setVariableValueToObject( testCompileMojo, "testExcludes", excludes );
122
123 testCompileMojo.execute();
124
125 testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile2TestCase.class" );
126 assertFalse( testClass.exists() );
127
128 testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile3TestCase.class" );
129 assertFalse( testClass.exists() );
130
131 testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile4TestCase.class" );
132 assertTrue( testClass.exists() );
133 }
134
135
136
137
138
139
140 public void testCompilerFork()
141 throws Exception
142 {
143 CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-fork-test/plugin-config.xml" );
144
145 compileMojo.execute();
146
147 File testClass = new File( compileMojo.getOutputDirectory(), "TestCompile1.class" );
148 assertTrue( testClass.exists() );
149
150 TestCompilerMojo testCompileMojo =
151 getTestCompilerMojo( compileMojo, "target/test-classes/unit/compiler-fork-test/plugin-config.xml" );
152
153 testCompileMojo.execute();
154
155 testClass = new File( testCompileMojo.getOutputDirectory(), "TestCompile1TestCase.class" );
156 assertTrue( testClass.exists() );
157 }
158
159 public void testOneOutputFileForAllInput()
160 throws Exception
161 {
162 CompilerMojo compileMojo =
163 getCompilerMojo( "target/test-classes/unit/compiler-one-output-file-test/plugin-config.xml" );
164
165 setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub() );
166
167 compileMojo.execute();
168
169 File testClass = new File( compileMojo.getOutputDirectory(), "compiled.class" );
170 assertTrue( testClass.exists() );
171
172 TestCompilerMojo testCompileMojo = getTestCompilerMojo( compileMojo,
173 "target/test-classes/unit/compiler-one-output-file-test/plugin-config.xml" );
174
175 setVariableValueToObject( testCompileMojo, "compilerManager", new CompilerManagerStub() );
176
177 testCompileMojo.execute();
178
179 testClass = new File( testCompileMojo.getOutputDirectory(), "compiled.class" );
180 assertTrue( testClass.exists() );
181 }
182
183 public void testCompilerArgs()
184 throws Exception
185 {
186 CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-args-test/plugin-config.xml" );
187
188 setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub() );
189
190 compileMojo.execute();
191
192 File testClass = new File( compileMojo.getOutputDirectory(), "compiled.class" );
193 assertTrue( testClass.exists() );
194 }
195
196 public void testOneOutputFileForAllInput2()
197 throws Exception
198 {
199 CompilerMojo compileMojo =
200 getCompilerMojo( "target/test-classes/unit/compiler-one-output-file-test2/plugin-config.xml" );
201
202 setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub() );
203
204 Set includes = new HashSet();
205 includes.add( "**/TestCompile4*.java" );
206 setVariableValueToObject( compileMojo, "includes", includes );
207
208 Set excludes = new HashSet();
209 excludes.add( "**/TestCompile2*.java" );
210 excludes.add( "**/TestCompile3*.java" );
211 setVariableValueToObject( compileMojo, "excludes", excludes );
212
213 compileMojo.execute();
214
215 File testClass = new File( compileMojo.getOutputDirectory(), "compiled.class" );
216 assertTrue( testClass.exists() );
217
218 TestCompilerMojo testCompileMojo = getTestCompilerMojo( compileMojo,
219 "target/test-classes/unit/compiler-one-output-file-test2/plugin-config.xml" );
220
221 setVariableValueToObject( testCompileMojo, "compilerManager", new CompilerManagerStub() );
222 setVariableValueToObject( testCompileMojo, "testIncludes", includes );
223 setVariableValueToObject( testCompileMojo, "testExcludes", excludes );
224
225 testCompileMojo.execute();
226
227 testClass = new File( testCompileMojo.getOutputDirectory(), "compiled.class" );
228 assertTrue( testClass.exists() );
229 }
230
231 public void testCompileFailure()
232 throws Exception
233 {
234 CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-fail-test/plugin-config.xml" );
235
236 setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub( true ) );
237
238 try
239 {
240 compileMojo.execute();
241
242 fail( "Should throw an exception" );
243 }
244 catch ( CompilationFailureException e )
245 {
246
247 }
248 }
249
250 public void testCompileFailOnError()
251 throws Exception
252 {
253 CompilerMojo compileMojo =
254 getCompilerMojo( "target/test-classes/unit/compiler-failonerror-test/plugin-config.xml" );
255
256 setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub( true ) );
257
258 try
259 {
260 compileMojo.execute();
261 assertTrue( true );
262 }
263 catch ( CompilationFailureException e )
264 {
265 fail( "The compilation error should have been consumed because failOnError = false" );
266 }
267 }
268
269 private CompilerMojo getCompilerMojo( String pomXml )
270 throws Exception
271 {
272 File testPom = new File( getBasedir(), pomXml );
273
274 CompilerMojo mojo = (CompilerMojo) lookupMojo( "compile", testPom );
275
276 setVariableValueToObject( mojo, "log", new DebugEnabledLog() );
277 setVariableValueToObject( mojo, "projectArtifact", new ArtifactStub() );
278 setVariableValueToObject( mojo, "classpathElements", Collections.EMPTY_LIST );
279
280 assertNotNull( mojo );
281
282 return mojo;
283 }
284
285 private TestCompilerMojo getTestCompilerMojo( CompilerMojo compilerMojo, String pomXml )
286 throws Exception
287 {
288 File testPom = new File( getBasedir(), pomXml );
289
290 TestCompilerMojo mojo = (TestCompilerMojo) lookupMojo( "testCompile", testPom );
291
292 setVariableValueToObject( mojo, "log", new DebugEnabledLog() );
293
294 File buildDir = (File) getVariableValueFromObject( compilerMojo, "buildDirectory" );
295 File testClassesDir = new File( buildDir, "test-classes" );
296 setVariableValueToObject( mojo, "outputDirectory", testClassesDir );
297
298 List testClasspathList = new ArrayList();
299 testClasspathList.add( System.getProperty( "localRepository" ) + "/junit/junit/3.8.1/junit-3.8.1.jar" );
300 testClasspathList.add( compilerMojo.getOutputDirectory().getPath() );
301 setVariableValueToObject( mojo, "classpathElements", testClasspathList );
302
303 String testSourceRoot = testPom.getParent() + "/src/test/java";
304 setVariableValueToObject( mojo, "compileSourceRoots", Collections.singletonList( testSourceRoot ) );
305
306 return mojo;
307 }
308 }