View Javadoc
1   /*
2    * Copyright (C) 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 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 }