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.plugin.MojoExecution;
20  import org.apache.maven.plugin.descriptor.MojoDescriptor;
21  import org.apache.maven.project.MavenProject;
22  import org.codehaus.gmavenplus.model.internal.Version;
23  import org.codehaus.gmavenplus.util.ClassWrangler;
24  import org.codehaus.gmavenplus.util.FileUtils;
25  import org.junit.Before;
26  import org.junit.Rule;
27  import org.junit.Test;
28  import org.junit.rules.TemporaryFolder;
29  import org.mockito.MockitoAnnotations;
30  
31  import java.io.BufferedReader;
32  import java.io.File;
33  import java.io.FileReader;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertFalse;
37  import static org.junit.Assert.assertTrue;
38  import static org.mockito.Mockito.doReturn;
39  import static org.mockito.Mockito.mock;
40  
41  
42  /**
43   * Unit tests for the ExecuteMojo class.
44   *
45   * @author Keegan Witt
46   */
47  public class ExecuteMojoTest {
48      private ExecuteMojo executeMojo;
49  
50      @Rule
51      public TemporaryFolder tmpDir = new TemporaryFolder();
52  
53      @Before
54      public void setup() {
55          MockitoAnnotations.openMocks(this);
56          executeMojo = new ExecuteMojo();
57          executeMojo.bindPropertiesToSeparateVariables = true;
58          executeMojo.mojoExecution = mock(MojoExecution.class);
59          executeMojo.project = mock(MavenProject.class);
60          MojoDescriptor mockMojoDescriptor = mock(MojoDescriptor.class);
61          doReturn(mockMojoDescriptor).when(executeMojo.mojoExecution).getMojoDescriptor();
62          doReturn("execute").when(mockMojoDescriptor).getGoal();
63      }
64  
65      @Test
66      public void testScriptString() throws Exception {
67          File file = tmpDir.newFile();
68          String line = "hello world";
69          executeMojo.scripts = new String[]{"new File('" + file.getAbsolutePath().replaceAll("\\\\", "/") + "').withWriter { w -> w << '" + line + "' }"};
70  
71          executeMojo.execute();
72          BufferedReader reader = new BufferedReader(new FileReader(file));
73          String actualLine = reader.readLine();
74          FileUtils.closeQuietly(reader);
75  
76          assertEquals(line, actualLine);
77      }
78  
79      @Test
80      public void testScriptPath() throws Exception {
81          executeMojo.sourceEncoding = "UTF-8";
82          File file = new File("target/testFile.txt");
83          String line = "Hello world!";
84          executeMojo.scripts = new String[]{new File("src/test/resources/testScript.groovy").getCanonicalPath()};
85  
86          String actualLine;
87          try {
88              executeMojo.execute();
89          } finally {
90              BufferedReader reader = new BufferedReader(new FileReader(file));
91              actualLine = reader.readLine();
92              FileUtils.closeQuietly(reader);
93              if (!file.delete()) {
94                  System.err.println("Unable to delete " + file.getAbsolutePath());
95              }
96          }
97  
98          assertEquals(line, actualLine);
99      }
100 
101     @Test
102     public void testScriptURL() throws Exception {
103         executeMojo.sourceEncoding = "UTF-8";
104         File file = new File("target/testFile.txt");
105         String line = "Hello world!";
106         executeMojo.scripts = new String[]{new File("src/test/resources/testScript.groovy").toURI().toURL().toString()};
107 
108         String actualLine;
109         try {
110             executeMojo.execute();
111         } finally {
112             BufferedReader reader = new BufferedReader(new FileReader(file));
113             actualLine = reader.readLine();
114             FileUtils.closeQuietly(reader);
115             if (!file.delete()) {
116                 System.err.println("Unable to delete " + file.getAbsolutePath());
117             }
118         }
119 
120         assertEquals(line, actualLine);
121     }
122 
123     @Test
124     public void testGroovyVersionSupportsActionTrue() {
125         executeMojo.classWrangler = mock(ClassWrangler.class);
126         doReturn(Version.parseFromString("1.5.0")).when(executeMojo.classWrangler).getGroovyVersion();
127         assertTrue(executeMojo.groovyVersionSupportsAction());
128     }
129 
130     @Test
131     public void testGroovyVersionSupportsActionFalse() {
132         executeMojo.classWrangler = mock(ClassWrangler.class);
133         doReturn(Version.parseFromString("1.0")).when(executeMojo.classWrangler).getGroovyVersion();
134         assertFalse(executeMojo.groovyVersionSupportsAction());
135     }
136 
137 }