View Javadoc
1   package org.codehaus.gmavenplus.util;
2   
3   import org.apache.maven.plugin.logging.Log;
4   import org.codehaus.gmavenplus.model.internal.Version;
5   import org.junit.Before;
6   import org.junit.Test;
7   import org.mockito.Mock;
8   import org.mockito.MockitoAnnotations;
9   
10  import java.util.LinkedHashMap;
11  import java.util.Map;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.mockito.Mockito.doReturn;
15  
16  /**
17   * Unit tests for the GroovyCompiler class.
18   */
19  public class GroovyCompilerTest {
20  
21      @Mock
22      private Log log;
23  
24      @Mock
25      private ClassWrangler classWrangler;
26  
27      private TestGroovyCompiler compiler;
28  
29      @Before
30      public void setup() {
31          MockitoAnnotations.openMocks(this);
32      }
33  
34      private void setupCompiler(String groovyVersion) {
35          doReturn(Version.parseFromString(groovyVersion)).when(classWrangler).getGroovyVersion();
36          doReturn(false).when(classWrangler).isGroovyIndy();
37          compiler = new TestGroovyCompiler(classWrangler, log);
38      }
39  
40      private void setupCompiler(String groovyVersion, boolean indy) {
41          doReturn(Version.parseFromString(groovyVersion)).when(classWrangler).getGroovyVersion();
42          doReturn(indy).when(classWrangler).isGroovyIndy();
43          compiler = new TestGroovyCompiler(classWrangler, log);
44      }
45  
46      @Test(expected = IllegalArgumentException.class)
47      public void testJava6WithUnsupportedGroovy() {
48          setupCompiler("2.1.2");
49          compiler.verifyGroovyVersionSupportsTargetBytecode("1.6");
50      }
51  
52      @Test
53      public void testJava6WithSupportedGroovy() {
54          setupCompiler("2.1.3");
55          compiler.verifyGroovyVersionSupportsTargetBytecode("1.6");
56          compiler.verifyGroovyVersionSupportsTargetBytecode("6");
57      }
58  
59      @Test(expected = IllegalArgumentException.class)
60      public void testJava7WithUnsupportedGroovy() {
61          setupCompiler("2.1.2");
62          compiler.verifyGroovyVersionSupportsTargetBytecode("1.7");
63      }
64  
65      @Test
66      public void testJava7WithSupportedGroovy() {
67          setupCompiler("2.1.3");
68          compiler.verifyGroovyVersionSupportsTargetBytecode("1.7");
69          compiler.verifyGroovyVersionSupportsTargetBytecode("7");
70      }
71  
72      @Test(expected = IllegalArgumentException.class)
73      public void testJava8WithUnsupportedGroovy() {
74          setupCompiler("2.3.2");
75          compiler.verifyGroovyVersionSupportsTargetBytecode("1.8");
76      }
77  
78      @Test
79      public void testJava8WithSupportedGroovy() {
80          setupCompiler("2.3.3");
81          compiler.verifyGroovyVersionSupportsTargetBytecode("1.8");
82          compiler.verifyGroovyVersionSupportsTargetBytecode("8");
83      }
84  
85      @Test(expected = IllegalArgumentException.class)
86      public void testJava9WithUnsupportedGroovy2_5() {
87          setupCompiler("2.5.2");
88          compiler.verifyGroovyVersionSupportsTargetBytecode("9");
89      }
90  
91      @Test
92      public void testJava9WithSupportedGroovy2_5() {
93          setupCompiler("2.5.3");
94          compiler.verifyGroovyVersionSupportsTargetBytecode("9");
95          compiler.verifyGroovyVersionSupportsTargetBytecode("1.9");
96      }
97  
98      @Test(expected = IllegalArgumentException.class)
99      public void testJava9WithUnsupportedGroovy2_6() {
100         setupCompiler("2.6.0-alpha-3");
101         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
102     }
103 
104     @Test
105     public void testJava9WithSupportedGroovy2_6() {
106         setupCompiler("2.6.0-alpha-4");
107         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
108     }
109 
110     @Test(expected = IllegalArgumentException.class)
111     public void testJava9WithUnsupportedGroovy3() {
112         setupCompiler("3.0.0-alpha-1");
113         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
114     }
115 
116     @Test
117     public void testJava9WithSupportedGroovy3() {
118         setupCompiler("3.0.0-alpha-2");
119         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
120     }
121 
122     @Test(expected = IllegalArgumentException.class)
123     public void testJava9WithUnsupportedGroovyIndy() {
124         setupCompiler("2.5.2", true);
125         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
126     }
127 
128     @Test
129     public void testJava9WithSupportedGroovyIndy() {
130         setupCompiler("2.5.3", true);
131         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
132     }
133 
134     @Test(expected = IllegalArgumentException.class)
135     public void testJava9WithUnsupportedGroovy3Indy() {
136         setupCompiler("3.0.0-alpha-3", true);
137         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
138     }
139 
140     @Test
141     public void testJava9WithSupportedGroovy3Indy() {
142         setupCompiler("3.0.0-alpha-4", true);
143         compiler.verifyGroovyVersionSupportsTargetBytecode("9");
144     }
145 
146     @Test(expected = IllegalArgumentException.class)
147     public void testJava10WithUnsupportedGroovy() {
148         setupCompiler("2.5.2");
149         compiler.verifyGroovyVersionSupportsTargetBytecode("10");
150     }
151 
152     @Test
153     public void testJava10WithSupportedGroovy() {
154         setupCompiler("2.5.3");
155         compiler.verifyGroovyVersionSupportsTargetBytecode("10");
156     }
157 
158     @Test(expected = IllegalArgumentException.class)
159     public void testJava10WithUnsupportedGroovy3() {
160         setupCompiler("3.0.0-alpha-3");
161         compiler.verifyGroovyVersionSupportsTargetBytecode("10");
162     }
163 
164     @Test
165     public void testJava10WithSupportedGroovy3() {
166         setupCompiler("3.0.0-alpha-4");
167         compiler.verifyGroovyVersionSupportsTargetBytecode("10");
168     }
169 
170     @Test(expected = IllegalArgumentException.class)
171     public void testJava11WithUnsupportedGroovy() {
172         setupCompiler("2.5.2");
173         compiler.verifyGroovyVersionSupportsTargetBytecode("11");
174     }
175 
176     @Test
177     public void testJava11WithSupportedGroovy() {
178         setupCompiler("2.5.3");
179         compiler.verifyGroovyVersionSupportsTargetBytecode("11");
180     }
181 
182     @Test(expected = IllegalArgumentException.class)
183     public void testJava11WithUnsupportedGroovy3() {
184         setupCompiler("3.0.0-alpha-3");
185         compiler.verifyGroovyVersionSupportsTargetBytecode("11");
186     }
187 
188     @Test
189     public void testJava11WithSupportedGroovy3() {
190         setupCompiler("3.0.0-alpha-4");
191         compiler.verifyGroovyVersionSupportsTargetBytecode("11");
192     }
193 
194     @Test(expected = IllegalArgumentException.class)
195     public void testJava12WithUnsupportedGroovy() {
196         setupCompiler("2.5.2");
197         compiler.verifyGroovyVersionSupportsTargetBytecode("12");
198     }
199 
200     @Test
201     public void testJava12WithSupportedGroovy() {
202         setupCompiler("2.5.3");
203         compiler.verifyGroovyVersionSupportsTargetBytecode("12");
204     }
205 
206     @Test(expected = IllegalArgumentException.class)
207     public void testJava12WithUnsupportedGroovy3() {
208         setupCompiler("3.0.0-alpha-3");
209         compiler.verifyGroovyVersionSupportsTargetBytecode("12");
210     }
211 
212     @Test
213     public void testJava12WithSupportedGroovy3() {
214         setupCompiler("3.0.0-alpha-4");
215         compiler.verifyGroovyVersionSupportsTargetBytecode("12");
216     }
217 
218     @Test(expected = IllegalArgumentException.class)
219     public void testJava13WithUnsupportedGroovy() {
220         setupCompiler("2.5.6");
221         compiler.verifyGroovyVersionSupportsTargetBytecode("13");
222     }
223 
224     @Test
225     public void testJava13WithSupportedGroovy() {
226         setupCompiler("2.5.7");
227         compiler.verifyGroovyVersionSupportsTargetBytecode("13");
228     }
229 
230     @Test(expected = IllegalArgumentException.class)
231     public void testJava13WithUnsupportedGroovy3() {
232         setupCompiler("3.0.0-alpha-4");
233         compiler.verifyGroovyVersionSupportsTargetBytecode("13");
234     }
235 
236     @Test
237     public void testJava13WithSupportedGroovy3() {
238         setupCompiler("3.0.0-beta-1");
239         compiler.verifyGroovyVersionSupportsTargetBytecode("13");
240     }
241 
242     @Test(expected = IllegalArgumentException.class)
243     public void testJava14WithUnsupportedGroovy() {
244         setupCompiler("3.0.0-beta-1");
245         compiler.verifyGroovyVersionSupportsTargetBytecode("14");
246     }
247 
248     @Test
249     public void testJava14WithSupportedGroovy() {
250         setupCompiler("3.0.0-beta-2");
251         compiler.verifyGroovyVersionSupportsTargetBytecode("14");
252     }
253 
254     @Test(expected = IllegalArgumentException.class)
255     public void testJava15WithUnsupportedGroovy() {
256         setupCompiler("3.0.2");
257         compiler.verifyGroovyVersionSupportsTargetBytecode("15");
258     }
259 
260     @Test
261     public void testJava15WithSupportedGroovy() {
262         setupCompiler("3.0.3");
263         compiler.verifyGroovyVersionSupportsTargetBytecode("15");
264     }
265 
266     @Test(expected = IllegalArgumentException.class)
267     public void testJava16WithUnsupportedGroovy() {
268         setupCompiler("3.0.5");
269         compiler.verifyGroovyVersionSupportsTargetBytecode("16");
270     }
271 
272     @Test
273     public void testJava16WithSupportedGroovy() {
274         setupCompiler("3.0.6");
275         compiler.verifyGroovyVersionSupportsTargetBytecode("16");
276     }
277 
278     @Test(expected = IllegalArgumentException.class)
279     public void testJava17WithUnsupportedGroovy() {
280         setupCompiler("3.0.7");
281         compiler.verifyGroovyVersionSupportsTargetBytecode("17");
282     }
283 
284     @Test
285     public void testJava17WithSupportedGroovy() {
286         setupCompiler("3.0.8");
287         compiler.verifyGroovyVersionSupportsTargetBytecode("17");
288     }
289 
290     @Test(expected = IllegalArgumentException.class)
291     public void testJava17WithUnsupportedGroovy4() {
292         setupCompiler("4.0.0-alpha-2");
293         compiler.verifyGroovyVersionSupportsTargetBytecode("17");
294     }
295 
296     @Test
297     public void testJava17WithSupportedGroovy4() {
298         setupCompiler("4.0.0-alpha-3");
299         compiler.verifyGroovyVersionSupportsTargetBytecode("17");
300     }
301 
302     @Test(expected = IllegalArgumentException.class)
303     public void testJava18WithUnsupportedGroovy() {
304         setupCompiler("4.0.0-alpha-3");
305         compiler.verifyGroovyVersionSupportsTargetBytecode("18");
306     }
307 
308     @Test
309     public void testJava18WithSupportedGroovy() {
310         setupCompiler("4.0.0-beta-1");
311         compiler.verifyGroovyVersionSupportsTargetBytecode("18");
312     }
313 
314     @Test(expected = IllegalArgumentException.class)
315     public void testJava19WithUnsupportedGroovy() {
316         setupCompiler("4.0.1");
317         compiler.verifyGroovyVersionSupportsTargetBytecode("19");
318     }
319 
320     @Test
321     public void testJava19WithSupportedGroovy() {
322         setupCompiler("4.0.2");
323         compiler.verifyGroovyVersionSupportsTargetBytecode("19");
324     }
325 
326     @Test(expected = IllegalArgumentException.class)
327     public void testJava20WithUnsupportedGroovy() {
328         setupCompiler("4.0.5");
329         compiler.verifyGroovyVersionSupportsTargetBytecode("20");
330     }
331 
332     @Test
333     public void testJava20WithSupportedGroovy() {
334         setupCompiler("4.0.6");
335         compiler.verifyGroovyVersionSupportsTargetBytecode("20");
336     }
337 
338     @Test(expected = IllegalArgumentException.class)
339     public void testJava21WithUnsupportedGroovy() {
340         setupCompiler("4.0.10");
341         compiler.verifyGroovyVersionSupportsTargetBytecode("21");
342     }
343 
344     @Test
345     public void testJava21WithSupportedGroovy() {
346         setupCompiler("4.0.11");
347         compiler.verifyGroovyVersionSupportsTargetBytecode("21");
348     }
349 
350     @Test(expected = IllegalArgumentException.class)
351     public void testJava22WithUnsupportedGroovy() {
352         setupCompiler("4.0.15");
353         compiler.verifyGroovyVersionSupportsTargetBytecode("22");
354     }
355 
356     @Test
357     public void testJava22WithSupportedGroovy() {
358         setupCompiler("4.0.16");
359         compiler.verifyGroovyVersionSupportsTargetBytecode("22");
360     }
361 
362     @Test(expected = IllegalArgumentException.class)
363     public void testJava22WithUnsupportedGroovy5() {
364         setupCompiler("5.0.0-alpha-2");
365         compiler.verifyGroovyVersionSupportsTargetBytecode("22");
366     }
367 
368     @Test
369     public void testJava22WithSupportedGroovy5() {
370         setupCompiler("5.0.0-alpha-3");
371         compiler.verifyGroovyVersionSupportsTargetBytecode("22");
372     }
373 
374     @Test(expected = IllegalArgumentException.class)
375     public void testJava23WithUnsupportedGroovy() {
376         setupCompiler("4.0.20");
377         compiler.verifyGroovyVersionSupportsTargetBytecode("23");
378     }
379 
380     @Test
381     public void testJava23WithSupportedGroovy() {
382         setupCompiler("4.0.21");
383         compiler.verifyGroovyVersionSupportsTargetBytecode("23");
384     }
385 
386     @Test(expected = IllegalArgumentException.class)
387     public void testJava23WithUnsupportedGroovy5() {
388         setupCompiler("5.0.0-alpha-7");
389         compiler.verifyGroovyVersionSupportsTargetBytecode("23");
390     }
391 
392     @Test
393     public void testJava23WithSupportedGroovy5() {
394         setupCompiler("5.0.0-alpha-8");
395         compiler.verifyGroovyVersionSupportsTargetBytecode("23");
396     }
397 
398     @Test(expected = IllegalArgumentException.class)
399     public void testJava24WithUnsupportedGroovy() {
400         setupCompiler("4.0.23");
401         compiler.verifyGroovyVersionSupportsTargetBytecode("24");
402     }
403 
404     @Test
405     public void testJava24WithSupportedGroovy() {
406         setupCompiler("4.0.24");
407         compiler.verifyGroovyVersionSupportsTargetBytecode("24");
408     }
409 
410     @Test(expected = IllegalArgumentException.class)
411     public void testJava24WithUnsupportedGroovy5() {
412         setupCompiler("5.0.0-alpha-10");
413         compiler.verifyGroovyVersionSupportsTargetBytecode("24");
414     }
415 
416     @Test
417     public void testJava24WithSupportedGroovy5() {
418         setupCompiler("5.0.0-alpha-11");
419         compiler.verifyGroovyVersionSupportsTargetBytecode("24");
420     }
421 
422     @Test(expected = IllegalArgumentException.class)
423     public void testJava25WithUnsupportedGroovy() {
424         setupCompiler("4.0.26");
425         compiler.verifyGroovyVersionSupportsTargetBytecode("25");
426     }
427 
428     @Test
429     public void testJava25WithSupportedGroovy() {
430         setupCompiler("4.0.27");
431         compiler.verifyGroovyVersionSupportsTargetBytecode("25");
432     }
433 
434     @Test(expected = IllegalArgumentException.class)
435     public void testJava25WithUnsupportedGroovy5() {
436         setupCompiler("5.0.0-alpha-12");
437         compiler.verifyGroovyVersionSupportsTargetBytecode("25");
438     }
439 
440     @Test
441     public void testJava25WithSupportedGroovy5() {
442         setupCompiler("5.0.0-alpha-13");
443         compiler.verifyGroovyVersionSupportsTargetBytecode("25");
444     }
445 
446     @Test(expected = IllegalArgumentException.class)
447     public void testUnrecognizedJava() {
448         setupCompiler("2.1.2");
449         compiler.verifyGroovyVersionSupportsTargetBytecode("unknown");
450     }
451 
452     @Test
453     public void testBytecodeTranslation() {
454         Map<String, String> expectedTranslations = new LinkedHashMap<>();
455         expectedTranslations.put("5", "1.5");
456         expectedTranslations.put("6", "1.6");
457         expectedTranslations.put("7", "1.7");
458         expectedTranslations.put("8", "1.8");
459         expectedTranslations.put("1.9", "9");
460         for (Map.Entry<String, String> entry : expectedTranslations.entrySet()) {
461             String javacVersion = entry.getKey();
462             String expectedGroovycVersion = entry.getValue();
463             assertEquals(expectedGroovycVersion, GroovyCompiler.translateJavacTargetToTargetBytecode(javacVersion));
464         }
465     }
466 
467     protected static class TestGroovyCompiler extends GroovyCompiler {
468         public TestGroovyCompiler(ClassWrangler classWrangler, Log log) {
469             super(classWrangler, log);
470         }
471 
472         @Override
473         public void verifyGroovyVersionSupportsTargetBytecode(String targetBytecode) {
474            super.verifyGroovyVersionSupportsTargetBytecode(targetBytecode);
475         }
476     }
477 }