View Javadoc
1   /*
2    * Copyright (C) 2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.gmavenplus.mojo;
18  
19  import org.codehaus.gmavenplus.model.internal.Version;
20  import org.codehaus.gmavenplus.util.ClassWrangler;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.mockito.MockitoAnnotations;
24  
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  import static org.mockito.Mockito.doReturn;
32  import static org.mockito.Mockito.mock;
33  
34  
35  /**
36   * Unit tests for the AbstractCompileMojo class.
37   *
38   * @author Keegan Witt
39   */
40  public class AbstractCompileMojoTest {
41      private TestMojo testMojo;
42  
43      @Before
44      public void setup() {
45          MockitoAnnotations.openMocks(this);
46          testMojo = new TestMojo();
47      }
48  
49      @Test
50      public void testGroovyVersionSupportsActionTrue() {
51          testMojo = new TestMojo("1.5.0");
52          assertTrue(testMojo.groovyVersionSupportsAction());
53      }
54  
55      @Test
56      public void testGroovyVersionSupportsActionFalse() {
57          testMojo = new TestMojo("1.1-rc-3");
58          assertFalse(testMojo.groovyVersionSupportsAction());
59      }
60  
61      @Test(expected = IllegalArgumentException.class)
62      public void testJava6WithUnsupportedGroovy() {
63          testMojo = new TestMojo("2.1.2");
64          testMojo.targetBytecode = "1.6";
65          testMojo.verifyGroovyVersionSupportsTargetBytecode();
66      }
67  
68      @Test
69      public void testJava6WithSupportedGroovy() {
70          testMojo = new TestMojo("2.1.3");
71          testMojo.targetBytecode = "1.6";
72          testMojo.verifyGroovyVersionSupportsTargetBytecode();
73          testMojo.targetBytecode = "6";
74          testMojo.verifyGroovyVersionSupportsTargetBytecode();
75      }
76  
77      @Test(expected = IllegalArgumentException.class)
78      public void testJava7WithUnsupportedGroovy() {
79          testMojo = new TestMojo("2.1.2");
80          testMojo.targetBytecode = "1.7";
81          testMojo.verifyGroovyVersionSupportsTargetBytecode();
82      }
83  
84      @Test
85      public void testJava7WithSupportedGroovy() {
86          testMojo = new TestMojo("2.1.3");
87          testMojo.targetBytecode = "1.7";
88          testMojo.verifyGroovyVersionSupportsTargetBytecode();
89          testMojo.targetBytecode = "7";
90          testMojo.verifyGroovyVersionSupportsTargetBytecode();
91      }
92  
93      @Test(expected = IllegalArgumentException.class)
94      public void testJava8WithUnsupportedGroovy() {
95          testMojo = new TestMojo("2.3.2");
96          testMojo.targetBytecode = "1.8";
97          testMojo.verifyGroovyVersionSupportsTargetBytecode();
98      }
99  
100     @Test
101     public void testJava8WithSupportedGroovy() {
102         testMojo = new TestMojo("2.3.3");
103         testMojo.targetBytecode = "1.8";
104         testMojo.verifyGroovyVersionSupportsTargetBytecode();
105         testMojo.targetBytecode = "8";
106         testMojo.verifyGroovyVersionSupportsTargetBytecode();
107     }
108 
109     @Test(expected = IllegalArgumentException.class)
110     public void testJava9WithUnsupportedGroovy2_5() {
111         testMojo = new TestMojo("2.5.2");
112         testMojo.targetBytecode = "9";
113         testMojo.verifyGroovyVersionSupportsTargetBytecode();
114     }
115 
116     @Test
117     public void testJava9WithSupportedGroovy2_5() {
118         testMojo = new TestMojo("2.5.3");
119         testMojo.targetBytecode = "9";
120         testMojo.verifyGroovyVersionSupportsTargetBytecode();
121         testMojo.targetBytecode = "1.9";
122         testMojo.verifyGroovyVersionSupportsTargetBytecode();
123     }
124 
125     @Test(expected = IllegalArgumentException.class)
126     public void testJava9WithUnsupportedGroovy2_6() {
127         testMojo = new TestMojo("2.6.0-alpha-3");
128         testMojo.targetBytecode = "9";
129         testMojo.verifyGroovyVersionSupportsTargetBytecode();
130     }
131 
132     @Test
133     public void testJava9WithSupportedGroovy2_6() {
134         testMojo = new TestMojo("2.6.0-alpha-4");
135         testMojo.targetBytecode = "9";
136         testMojo.verifyGroovyVersionSupportsTargetBytecode();
137     }
138 
139     @Test(expected = IllegalArgumentException.class)
140     public void testJava9WithUnsupportedGroovy3() {
141         testMojo = new TestMojo("3.0.0-alpha-1");
142         testMojo.targetBytecode = "9";
143         testMojo.verifyGroovyVersionSupportsTargetBytecode();
144     }
145 
146     @Test
147     public void testJava9WithSupportedGroovy3() {
148         testMojo = new TestMojo("3.0.0-alpha-2");
149         testMojo.targetBytecode = "9";
150         testMojo.verifyGroovyVersionSupportsTargetBytecode();
151     }
152 
153     @Test(expected = IllegalArgumentException.class)
154     public void testJava9WithUnsupportedGroovyIndy() {
155         testMojo = new TestMojo("2.5.2", true);
156         testMojo.targetBytecode = "9";
157         testMojo.verifyGroovyVersionSupportsTargetBytecode();
158     }
159 
160     @Test
161     public void testJava9WithSupportedGroovyIndy() {
162         testMojo = new TestMojo("2.5.3", true);
163         testMojo.targetBytecode = "9";
164         testMojo.verifyGroovyVersionSupportsTargetBytecode();
165     }
166 
167     @Test(expected = IllegalArgumentException.class)
168     public void testJava9WithUnsupportedGroovy3Indy() {
169         testMojo = new TestMojo("3.0.0-alpha-3", true);
170         testMojo.targetBytecode = "9";
171         testMojo.verifyGroovyVersionSupportsTargetBytecode();
172     }
173 
174     @Test
175     public void testJava9WithSupportedGroovy3Indy() {
176         testMojo = new TestMojo("3.0.0-alpha-4", true);
177         testMojo.targetBytecode = "9";
178         testMojo.verifyGroovyVersionSupportsTargetBytecode();
179     }
180 
181     @Test(expected = IllegalArgumentException.class)
182     public void testJava10WithUnsupportedGroovy() {
183         testMojo = new TestMojo("2.5.2");
184         testMojo.targetBytecode = "10";
185         testMojo.verifyGroovyVersionSupportsTargetBytecode();
186     }
187 
188     @Test
189     public void testJava10WithSupportedGroovy() {
190         testMojo = new TestMojo("2.5.3");
191         testMojo.targetBytecode = "10";
192         testMojo.verifyGroovyVersionSupportsTargetBytecode();
193     }
194 
195     @Test(expected = IllegalArgumentException.class)
196     public void testJava10WithUnsupportedGroovy3() {
197         testMojo = new TestMojo("3.0.0-alpha-3");
198         testMojo.targetBytecode = "10";
199         testMojo.verifyGroovyVersionSupportsTargetBytecode();
200     }
201 
202     @Test
203     public void testJava10WithSupportedGroovy3() {
204         testMojo = new TestMojo("3.0.0-alpha-4");
205         testMojo.targetBytecode = "10";
206         testMojo.verifyGroovyVersionSupportsTargetBytecode();
207     }
208 
209     @Test(expected = IllegalArgumentException.class)
210     public void testJava11WithUnsupportedGroovy() {
211         testMojo = new TestMojo("2.5.2");
212         testMojo.targetBytecode = "11";
213         testMojo.verifyGroovyVersionSupportsTargetBytecode();
214     }
215 
216     @Test
217     public void testJava11WithSupportedGroovy() {
218         testMojo = new TestMojo("2.5.3");
219         testMojo.targetBytecode = "11";
220         testMojo.verifyGroovyVersionSupportsTargetBytecode();
221     }
222 
223     @Test(expected = IllegalArgumentException.class)
224     public void testJava11WithUnsupportedGroovy3() {
225         testMojo = new TestMojo("3.0.0-alpha-3");
226         testMojo.targetBytecode = "11";
227         testMojo.verifyGroovyVersionSupportsTargetBytecode();
228     }
229 
230     @Test
231     public void testJava11WithSupportedGroovy3() {
232         testMojo = new TestMojo("3.0.0-alpha-4");
233         testMojo.targetBytecode = "11";
234         testMojo.verifyGroovyVersionSupportsTargetBytecode();
235     }
236 
237     @Test(expected = IllegalArgumentException.class)
238     public void testJava12WithUnsupportedGroovy() {
239         testMojo = new TestMojo("2.5.2");
240         testMojo.targetBytecode = "12";
241         testMojo.verifyGroovyVersionSupportsTargetBytecode();
242     }
243 
244     @Test
245     public void testJava12WithSupportedGroovy() {
246         testMojo = new TestMojo("2.5.3");
247         testMojo.targetBytecode = "12";
248         testMojo.verifyGroovyVersionSupportsTargetBytecode();
249     }
250 
251     @Test(expected = IllegalArgumentException.class)
252     public void testJava12WithUnsupportedGroovy3() {
253         testMojo = new TestMojo("3.0.0-alpha-3");
254         testMojo.targetBytecode = "12";
255         testMojo.verifyGroovyVersionSupportsTargetBytecode();
256     }
257 
258     @Test
259     public void testJava12WithSupportedGroovy3() {
260         testMojo = new TestMojo("3.0.0-alpha-4");
261         testMojo.targetBytecode = "12";
262         testMojo.verifyGroovyVersionSupportsTargetBytecode();
263     }
264 
265     @Test(expected = IllegalArgumentException.class)
266     public void testJava13WithUnsupportedGroovy() {
267         testMojo = new TestMojo("2.5.6");
268         testMojo.targetBytecode = "13";
269         testMojo.verifyGroovyVersionSupportsTargetBytecode();
270     }
271 
272     @Test
273     public void testJava13WithSupportedGroovy() {
274         testMojo = new TestMojo("2.5.7");
275         testMojo.targetBytecode = "13";
276         testMojo.verifyGroovyVersionSupportsTargetBytecode();
277     }
278 
279     @Test(expected = IllegalArgumentException.class)
280     public void testJava13WithUnsupportedGroovy3() {
281         testMojo = new TestMojo("3.0.0-alpha-4");
282         testMojo.targetBytecode = "13";
283         testMojo.verifyGroovyVersionSupportsTargetBytecode();
284     }
285 
286     @Test
287     public void testJava13WithSupportedGroovy3() {
288         testMojo = new TestMojo("3.0.0-beta-1");
289         testMojo.targetBytecode = "13";
290         testMojo.verifyGroovyVersionSupportsTargetBytecode();
291     }
292 
293     @Test(expected = IllegalArgumentException.class)
294     public void testJava14WithUnsupportedGroovy() {
295         testMojo = new TestMojo("3.0.0-beta-1");
296         testMojo.targetBytecode = "14";
297         testMojo.verifyGroovyVersionSupportsTargetBytecode();
298     }
299 
300     @Test
301     public void testJava14WithSupportedGroovy() {
302         testMojo = new TestMojo("3.0.0-beta-2");
303         testMojo.targetBytecode = "14";
304         testMojo.verifyGroovyVersionSupportsTargetBytecode();
305     }
306 
307     @Test(expected = IllegalArgumentException.class)
308     public void testJava15WithUnsupportedGroovy() {
309         testMojo = new TestMojo("3.0.2");
310         testMojo.targetBytecode = "15";
311         testMojo.verifyGroovyVersionSupportsTargetBytecode();
312     }
313 
314     @Test
315     public void testJava15WithSupportedGroovy() {
316         testMojo = new TestMojo("3.0.3");
317         testMojo.targetBytecode = "15";
318         testMojo.verifyGroovyVersionSupportsTargetBytecode();
319     }
320 
321     @Test(expected = IllegalArgumentException.class)
322     public void testJava16WithUnsupportedGroovy() {
323         testMojo = new TestMojo("3.0.5");
324         testMojo.targetBytecode = "16";
325         testMojo.verifyGroovyVersionSupportsTargetBytecode();
326     }
327 
328     @Test
329     public void testJava16WithSupportedGroovy() {
330         testMojo = new TestMojo("3.0.6");
331         testMojo.targetBytecode = "16";
332         testMojo.verifyGroovyVersionSupportsTargetBytecode();
333     }
334 
335     @Test(expected = IllegalArgumentException.class)
336     public void testJava17WithUnsupportedGroovy() {
337         testMojo = new TestMojo("3.0.7");
338         testMojo.targetBytecode = "17";
339         testMojo.verifyGroovyVersionSupportsTargetBytecode();
340     }
341 
342     @Test
343     public void testJava17WithSupportedGroovy() {
344         testMojo = new TestMojo("3.0.8");
345         testMojo.targetBytecode = "17";
346         testMojo.verifyGroovyVersionSupportsTargetBytecode();
347     }
348 
349     @Test(expected = IllegalArgumentException.class)
350     public void testJava17WithUnsupportedGroovy4() {
351         testMojo = new TestMojo("4.0.0-alpha-2");
352         testMojo.targetBytecode = "17";
353         testMojo.verifyGroovyVersionSupportsTargetBytecode();
354     }
355 
356     @Test
357     public void testJava17WithSupportedGroovy4() {
358         testMojo = new TestMojo("4.0.0-alpha-3");
359         testMojo.targetBytecode = "17";
360         testMojo.verifyGroovyVersionSupportsTargetBytecode();
361     }
362 
363     @Test(expected = IllegalArgumentException.class)
364     public void testJava18WithUnsupportedGroovy() {
365         testMojo = new TestMojo("4.0.0-alpha-3");
366         testMojo.targetBytecode = "18";
367         testMojo.verifyGroovyVersionSupportsTargetBytecode();
368     }
369 
370     @Test
371     public void testJava18WithSupportedGroovy() {
372         testMojo = new TestMojo("4.0.0-beta-1");
373         testMojo.targetBytecode = "18";
374         testMojo.verifyGroovyVersionSupportsTargetBytecode();
375     }
376 
377     @Test(expected = IllegalArgumentException.class)
378     public void testJava19WithUnsupportedGroovy() {
379         testMojo = new TestMojo("4.0.1");
380         testMojo.targetBytecode = "19";
381         testMojo.verifyGroovyVersionSupportsTargetBytecode();
382     }
383 
384     @Test
385     public void testJava19WithSupportedGroovy() {
386         testMojo = new TestMojo("4.0.2");
387         testMojo.targetBytecode = "19";
388         testMojo.verifyGroovyVersionSupportsTargetBytecode();
389     }
390 
391     @Test(expected = IllegalArgumentException.class)
392     public void testJava20WithUnsupportedGroovy() {
393         testMojo = new TestMojo("4.0.5");
394         testMojo.targetBytecode = "20";
395         testMojo.verifyGroovyVersionSupportsTargetBytecode();
396     }
397 
398     @Test
399     public void testJava20WithSupportedGroovy() {
400         testMojo = new TestMojo("4.0.6");
401         testMojo.targetBytecode = "20";
402         testMojo.verifyGroovyVersionSupportsTargetBytecode();
403     }
404 
405     @Test(expected = IllegalArgumentException.class)
406     public void testJava21WithUnsupportedGroovy() {
407         testMojo = new TestMojo("4.0.10");
408         testMojo.targetBytecode = "21";
409         testMojo.verifyGroovyVersionSupportsTargetBytecode();
410     }
411 
412     @Test
413     public void testJava21WithSupportedGroovy() {
414         testMojo = new TestMojo("4.0.11");
415         testMojo.targetBytecode = "21";
416         testMojo.verifyGroovyVersionSupportsTargetBytecode();
417     }
418 
419     @Test(expected = IllegalArgumentException.class)
420     public void testJava22WithUnsupportedGroovy() {
421         testMojo = new TestMojo("4.0.15");
422         testMojo.targetBytecode = "22";
423         testMojo.verifyGroovyVersionSupportsTargetBytecode();
424     }
425 
426     @Test
427     public void testJava22WithSupportedGroovy() {
428         testMojo = new TestMojo("4.0.16");
429         testMojo.targetBytecode = "22";
430         testMojo.verifyGroovyVersionSupportsTargetBytecode();
431     }
432 
433     @Test(expected = IllegalArgumentException.class)
434     public void testJava22WithUnsupportedGroovy5() {
435         testMojo = new TestMojo("5.0.0-alpha-2");
436         testMojo.targetBytecode = "22";
437         testMojo.verifyGroovyVersionSupportsTargetBytecode();
438     }
439 
440     @Test
441     public void testJava22WithSupportedGroovy5() {
442         testMojo = new TestMojo("5.0.0-alpha-3");
443         testMojo.targetBytecode = "22";
444         testMojo.verifyGroovyVersionSupportsTargetBytecode();
445     }
446 
447     @Test(expected = IllegalArgumentException.class)
448     public void testJava23WithUnsupportedGroovy() {
449         testMojo = new TestMojo("4.0.20");
450         testMojo.targetBytecode = "23";
451         testMojo.verifyGroovyVersionSupportsTargetBytecode();
452     }
453 
454     @Test
455     public void testJava23WithSupportedGroovy() {
456         testMojo = new TestMojo("4.0.21");
457         testMojo.targetBytecode = "23";
458         testMojo.verifyGroovyVersionSupportsTargetBytecode();
459     }
460 
461     @Test(expected = IllegalArgumentException.class)
462     public void testJava23WithUnsupportedGroovy5() {
463         testMojo = new TestMojo("5.0.0-alpha-7");
464         testMojo.targetBytecode = "23";
465         testMojo.verifyGroovyVersionSupportsTargetBytecode();
466     }
467 
468     @Test
469     public void testJava23WithSupportedGroovy5() {
470         testMojo = new TestMojo("5.0.0-alpha-8");
471         testMojo.targetBytecode = "23";
472         testMojo.verifyGroovyVersionSupportsTargetBytecode();
473     }
474 
475     @Test(expected = IllegalArgumentException.class)
476     public void testJava24WithUnsupportedGroovy() {
477         testMojo = new TestMojo("4.0.23");
478         testMojo.targetBytecode = "24";
479         testMojo.verifyGroovyVersionSupportsTargetBytecode();
480     }
481 
482     @Test
483     public void testJava24WithSupportedGroovy() {
484         testMojo = new TestMojo("4.0.24");
485         testMojo.targetBytecode = "24";
486         testMojo.verifyGroovyVersionSupportsTargetBytecode();
487     }
488 
489     @Test(expected = IllegalArgumentException.class)
490     public void testJava24WithUnsupportedGroovy5() {
491         testMojo = new TestMojo("5.0.0-alpha-10");
492         testMojo.targetBytecode = "24";
493         testMojo.verifyGroovyVersionSupportsTargetBytecode();
494     }
495 
496     @Test
497     public void testJava24WithSupportedGroovy5() {
498         testMojo = new TestMojo("5.0.0-alpha-11");
499         testMojo.targetBytecode = "24";
500         testMojo.verifyGroovyVersionSupportsTargetBytecode();
501     }
502 
503     @Test(expected = IllegalArgumentException.class)
504     public void testJava25WithUnsupportedGroovy() {
505         testMojo = new TestMojo("4.0.26");
506         testMojo.targetBytecode = "25";
507         testMojo.verifyGroovyVersionSupportsTargetBytecode();
508     }
509 
510     @Test
511     public void testJava25WithSupportedGroovy() {
512         testMojo = new TestMojo("4.0.27");
513         testMojo.targetBytecode = "25";
514         testMojo.verifyGroovyVersionSupportsTargetBytecode();
515     }
516 
517     @Test(expected = IllegalArgumentException.class)
518     public void testJava25WithUnsupportedGroovy5() {
519         testMojo = new TestMojo("5.0.0-alpha-12");
520         testMojo.targetBytecode = "25";
521         testMojo.verifyGroovyVersionSupportsTargetBytecode();
522     }
523 
524     @Test
525     public void testJava25WithSupportedGroovy5() {
526         testMojo = new TestMojo("5.0.0-alpha-13");
527         testMojo.targetBytecode = "25";
528         testMojo.verifyGroovyVersionSupportsTargetBytecode();
529     }
530 
531     @Test(expected = IllegalArgumentException.class)
532     public void testUnrecognizedJava() {
533         testMojo = new TestMojo("2.1.2");
534         testMojo.targetBytecode = "unknown";
535         testMojo.verifyGroovyVersionSupportsTargetBytecode();
536     }
537 
538     @Test
539     public void testBytecodeTranslation() {
540         Map<String, String> expectedTranslations = new LinkedHashMap<>();
541         expectedTranslations.put("5", "1.5");
542         expectedTranslations.put("6", "1.6");
543         expectedTranslations.put("7", "1.7");
544         expectedTranslations.put("8", "1.8");
545         expectedTranslations.put("1.9", "9");
546         for (Map.Entry<String, String> entry : expectedTranslations.entrySet()) {
547             String javacVersion = entry.getKey();
548             String expectedGroovycVersion = entry.getValue();
549             assertEquals(expectedGroovycVersion, AbstractCompileMojo.translateJavacTargetToTargetBytecode(javacVersion));
550         }
551     }
552 
553     protected static class TestMojo extends AbstractCompileMojo {
554         protected TestMojo() {
555             this(GROOVY_1_5_0.toString(), false);
556         }
557 
558         protected TestMojo(String groovyVersion) {
559             this(groovyVersion, false);
560         }
561 
562         protected TestMojo(String groovyVersion, boolean indy) {
563             classWrangler = mock(ClassWrangler.class);
564             doReturn(Version.parseFromString(groovyVersion)).when(classWrangler).getGroovyVersion();
565             doReturn(indy).when(classWrangler).isGroovyIndy();
566         }
567 
568         @Override
569         public void execute() {
570         }
571     }
572 
573 }