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