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