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 CompileTestsMojoTest {
30 private static final String INTENTIONAL_EXCEPTION_MESSAGE = "Intentionally blowing up.";
31
32 @Spy
33 private CompileTestsMojo compileTestsMojo;
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(compileTestsMojo).getTestFiles(any(FileSet[].class), eq(false));
41 compileTestsMojo.testOutputDirectory = mock(File.class);
42 compileTestsMojo.project = mock(MavenProject.class);
43 doReturn(mock(Build.class)).when(compileTestsMojo.project).getBuild();
44 doReturn(true).when(compileTestsMojo).groovyVersionSupportsAction();
45 compileTestsMojo.classWrangler = mock(ClassWrangler.class);
46 doReturn(new Version(1, 5, 0)).when(compileTestsMojo.classWrangler).getGroovyVersion();
47 }
48
49 @Test
50 public void testCallsExpectedMethods() throws Exception {
51 doNothing().when(compileTestsMojo).doCompile(anySet(), anyList(), any(File.class));
52 compileTestsMojo.execute();
53 verify(compileTestsMojo, times(1)).doCompile(anySet(), anyList(), any(File.class));
54 }
55
56 @Test
57 public void testSkipped() throws Exception {
58 compileTestsMojo.skipTests = true;
59 compileTestsMojo.execute();
60 verify(compileTestsMojo, never()).doCompile(anySet(), anyList(), any(File.class));
61 }
62
63 @Test(expected = MojoExecutionException.class)
64 public void testClassNotFoundExceptionThrowsMojoExecutionException() throws Exception {
65 doThrow(new ClassNotFoundException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileTestsMojo).doCompile(anySet(), anyList(), any(File.class));
66 compileTestsMojo.execute();
67 }
68
69 @Test(expected = MojoExecutionException.class)
70 public void testInvocationTargetExceptionThrowsMojoExecutionException() throws Exception {
71 doThrow(new InvocationTargetException(mock(Exception.class), INTENTIONAL_EXCEPTION_MESSAGE)).when(compileTestsMojo).doCompile(anySet(), anyList(), any(File.class));
72 compileTestsMojo.execute();
73 }
74
75 @Test(expected = MojoExecutionException.class)
76 public void testInstantiationExceptionThrowsMojoExecutionException() throws Exception {
77 doThrow(new InstantiationException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileTestsMojo).doCompile(anySet(), anyList(), any(File.class));
78 compileTestsMojo.execute();
79 }
80
81 @Test(expected = MojoExecutionException.class)
82 public void testIllegalAccessExceptionThrowsMojoExecutionException() throws Exception {
83 doThrow(new IllegalAccessException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileTestsMojo).doCompile(anySet(), anyList(), any(File.class));
84 compileTestsMojo.execute();
85 }
86
87 @Test(expected = MojoExecutionException.class)
88 public void testDependencyResolutionRequiredExceptionThrowsMojoExecutionException() throws Exception {
89 doThrow(mock(DependencyResolutionRequiredException.class)).when(compileTestsMojo.project).getTestClasspathElements();
90 compileTestsMojo.execute();
91 }
92
93 @Test(expected = MojoExecutionException.class)
94 public void testMalformedURLExceptionThrowsMojoExecutionException() throws Exception {
95 doThrow(new MalformedURLException(INTENTIONAL_EXCEPTION_MESSAGE)).when(compileTestsMojo).doCompile(anySet(), anyList(), any(File.class));
96 compileTestsMojo.execute();
97 }
98
99 @Test
100 public void testGroovyVersionSupportsActionTrue() {
101 compileTestsMojo = new CompileTestsMojo();
102 compileTestsMojo.classWrangler = mock(ClassWrangler.class);
103 doReturn(new Version(1, 5, 0)).when(compileTestsMojo.classWrangler).getGroovyVersion();
104 assertTrue(compileTestsMojo.groovyVersionSupportsAction());
105 }
106
107 @Test
108 public void testGroovyVersionSupportsActionFalse() {
109 compileTestsMojo = new CompileTestsMojo();
110 compileTestsMojo.classWrangler = mock(ClassWrangler.class);
111 doReturn(new Version(1, 0)).when(compileTestsMojo.classWrangler).getGroovyVersion();
112 assertFalse(compileTestsMojo.groovyVersionSupportsAction());
113 }
114
115 }