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