1 package org.codehaus.gmavenplus.mojo;
2
3 import org.apache.maven.artifact.DependencyResolutionRequiredException;
4 import org.apache.maven.model.Build;
5 import org.apache.maven.plugin.MojoExecutionException;
6 import org.apache.maven.project.MavenProject;
7 import org.apache.maven.shared.model.fileset.FileSet;
8 import org.codehaus.gmavenplus.model.internal.Version;
9 import org.codehaus.gmavenplus.util.ClassWrangler;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.mockito.MockitoAnnotations;
13 import org.mockito.Spy;
14
15 import java.io.File;
16 import java.lang.reflect.InvocationTargetException;
17 import java.net.MalformedURLException;
18 import java.util.Set;
19 import java.util.TreeSet;
20
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.*;
24
25
26
27
28
29 public class CompileMojoTest {
30 private static final String INTENTIONAL_EXCEPTION_MESSAGE = "Intentionally blowing up.";
31
32 @Spy
33 private CompileMojo compileMojo;
34
35 @Before
36 public void setup() {
37 MockitoAnnotations.openMocks(this);
38 Set<File> sources = new TreeSet<>();
39 sources.add(mock(File.class));
40 doReturn(sources).when(compileMojo).getTestFiles(any(FileSet[].class), eq(false));
41 compileMojo.outputDirectory = mock(File.class);
42 compileMojo.project = mock(MavenProject.class);
43 doReturn(mock(Build.class)).when(compileMojo.project).getBuild();
44 doReturn(true).when(compileMojo).groovyVersionSupportsAction();
45 compileMojo.classWrangler = mock(ClassWrangler.class);
46 doReturn(new Version(1, 5, 0)).when(compileMojo.classWrangler).getGroovyVersion();
47 }
48
49 @Test
50 public void testCallsExpectedMethods() throws Exception {
51 doNothing().when(compileMojo).doCompile(anySet(), anyList(), any(File.class));
52 compileMojo.execute();
53 verify(compileMojo, never()).logPluginClasspath();
54 }
55
56 @Test(expected = MojoExecutionException.class)
57 public void testClassNotFoundExceptionThrowsMojoExecutionException() throws Exception {
58 doThrow(new ClassNotFoundException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileMojo).doCompile(anySet(), anyList(), any(File.class));
59 compileMojo.execute();
60 }
61
62 @Test(expected = MojoExecutionException.class)
63 public void testInvocationTargetExceptionThrowsMojoExecutionException() throws Exception {
64 doThrow(new InvocationTargetException(mock(Exception.class), INTENTIONAL_EXCEPTION_MESSAGE)).when(compileMojo).doCompile(anySet(), anyList(), any(File.class));
65 compileMojo.execute();
66 }
67
68 @Test(expected = MojoExecutionException.class)
69 public void testInstantiationExceptionThrowsMojoExecutionException() throws Exception {
70 doThrow(new InstantiationException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileMojo).doCompile(anySet(), anyList(), any(File.class));
71 compileMojo.execute();
72 }
73
74 @Test(expected = MojoExecutionException.class)
75 public void testIllegalAccessExceptionThrowsMojoExecutionException() throws Exception {
76 doThrow(new IllegalAccessException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileMojo).doCompile(anySet(), anyList(), any(File.class));
77 compileMojo.execute();
78 }
79
80 @Test(expected = MojoExecutionException.class)
81 public void testDependencyResolutionRequiredExceptionThrowsMojoExecutionException() throws Exception {
82 doThrow(mock(DependencyResolutionRequiredException.class)).when(compileMojo.project).getCompileClasspathElements();
83 compileMojo.execute();
84 }
85
86 @Test(expected = MojoExecutionException.class)
87 public void testMalformedURLExceptionThrowsMojoExecutionException() throws Exception {
88 doThrow(new MalformedURLException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileMojo).doCompile(anySet(), anyList(), any(File.class));
89 compileMojo.execute();
90 }
91
92 @Test
93 public void testGroovyVersionSupportsActionTrue() {
94 compileMojo = new CompileMojo();
95 compileMojo.classWrangler = mock(ClassWrangler.class);
96 doReturn(new Version(1, 5, 0)).when(compileMojo.classWrangler).getGroovyVersion();
97 assertTrue(compileMojo.groovyVersionSupportsAction());
98 }
99
100 @Test
101 public void testGroovyVersionSupportsActionFalse() {
102 compileMojo = new CompileMojo();
103 compileMojo.classWrangler = mock(ClassWrangler.class);
104 doReturn(new Version(1, 0)).when(compileMojo.classWrangler).getGroovyVersion();
105 assertFalse(compileMojo.groovyVersionSupportsAction());
106 }
107
108 }