diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/AbstractToolsMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/AbstractToolsMojo.java index 0d4f1d8c..d3a572b9 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/AbstractToolsMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/AbstractToolsMojo.java @@ -136,24 +136,7 @@ protected void initializeProperties() { if (projectHelper != null && !properties.containsKey("projectHelper")) { properties.put("projectHelper", projectHelper); } - if (!properties.containsKey("ant")) { - Object antBuilder = null; - try { - antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.ant.AntBuilder"))); - } catch (ClassNotFoundException e1) { - getLog().debug("groovy.ant.AntBuilder not available, trying groovy.util.AntBuilder."); - try { - antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.util.AntBuilder"))); - } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | InvocationTargetException e2) { - logUnableToInitializeAntBuilder(e2); - } - } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { - logUnableToInitializeAntBuilder(e); - } - if (antBuilder != null) { - properties.put("ant", antBuilder); - } - } + initializeAnt(); if (bindSessionUserOverrideProperties && !bindAllProjectProperties) { getLog().warn("bindSessionUserOverrideProperties set without bindAllProjectProperties, ignoring."); } @@ -176,6 +159,40 @@ protected void initializeProperties() { } } + /** + * Initializes the 'ant' property. + */ + protected void initializeAnt() { + if (!properties.containsKey("ant")) { + Object antBuilder = createAntBuilder(); + if (antBuilder != null) { + properties.put("ant", antBuilder); + } + } + } + + /** + * Creates a new AntBuilder object. + * + * @return a new AntBuilder object, or null if it couldn't be created + */ + protected Object createAntBuilder() { + Object antBuilder = null; + try { + antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.ant.AntBuilder"))); + } catch (ClassNotFoundException e1) { + getLog().debug("groovy.ant.AntBuilder not available, trying groovy.util.AntBuilder."); + try { + antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.util.AntBuilder"))); + } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | InvocationTargetException e2) { + logUnableToInitializeAntBuilder(e2); + } + } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { + logUnableToInitializeAntBuilder(e); + } + return antBuilder; + } + /** * Logs errors that caused the 'ant' object to not be populated. * diff --git a/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java b/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java index 50a32b30..d02d3535 100644 --- a/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java +++ b/src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java @@ -100,7 +100,6 @@ public void execute() throws MojoExecutionException, MojoFailureException { // run the console invokeMethod(findMethod(consoleClass, "run"), console); - // TODO: for some reason instantiating AntBuilder before calling run() causes its stdout and stderr streams to not be captured by the Console bindAntBuilder(consoleClass, bindingClass, console); // open script file @@ -194,19 +193,7 @@ protected void bindAntBuilder(Class consoleClass, Class bindingClass, Obje Class groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell"); Object shell = getField(findField(consoleClass, "shell", groovyShellClass), console); Object binding = invokeMethod(findMethod(groovyShellClass, "getContext"), shell); - Object antBuilder = null; - try { - antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.ant.AntBuilder"))); - } catch (ClassNotFoundException e1) { - getLog().debug("groovy.ant.AntBuilder not available, trying groovy.util.AntBuilder."); - try { - antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.util.AntBuilder"))); - } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | InstantiationException e2) { - logUnableToInitializeAntBuilder(e2); - } - } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { - logUnableToInitializeAntBuilder(e); - } + Object antBuilder = createAntBuilder(); if (antBuilder != null) { if (bindPropertiesToSeparateVariables) { invokeMethod(findMethod(bindingClass, "setVariable", String.class, Object.class), binding, "ant", antBuilder); @@ -217,6 +204,16 @@ protected void bindAntBuilder(Class consoleClass, Class bindingClass, Obje } } + /** + * Initializes the 'ant' property with a placeholder. + */ + @Override + protected void initializeAnt() { + if (!properties.containsKey("ant")) { + properties.put("ant", Boolean.TRUE); + } + } + /** * Waits for the console in use to be closed. * diff --git a/src/test/java/org/codehaus/gmavenplus/mojo/ConsoleMojoTest.java b/src/test/java/org/codehaus/gmavenplus/mojo/ConsoleMojoTest.java new file mode 100644 index 00000000..d6dc7f40 --- /dev/null +++ b/src/test/java/org/codehaus/gmavenplus/mojo/ConsoleMojoTest.java @@ -0,0 +1,84 @@ +package org.codehaus.gmavenplus.mojo; + +import org.apache.maven.project.MavenProject; +import org.codehaus.gmavenplus.util.ClassWrangler; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Properties; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +/** + * Unit tests for the ConsoleMojo class. + */ +public class ConsoleMojoTest { + + private ConsoleMojo consoleMojo; + + @Mock + private ClassWrangler classWrangler; + + @Mock + private MavenProject project; + + @Before + public void setup() throws Exception { + MockitoAnnotations.openMocks(this); + consoleMojo = new ConsoleMojo(); + consoleMojo.classWrangler = classWrangler; + consoleMojo.project = project; + when(project.getProperties()).thenReturn(new Properties()); + + Class antBuilderClass; + try { + antBuilderClass = Class.forName("groovy.ant.AntBuilder"); + } catch (ClassNotFoundException e) { + antBuilderClass = Class.forName("groovy.util.AntBuilder"); + } + when(classWrangler.getClass("groovy.ant.AntBuilder")).thenReturn((Class) antBuilderClass); + when(classWrangler.getClass("groovy.util.AntBuilder")).thenReturn((Class) antBuilderClass); + when(classWrangler.getClass("groovy.lang.GroovyShell")).thenReturn((Class) TestShell.class); + } + + @Test + public void testInitializeAnt() { + consoleMojo.initializeAnt(); + assertEquals(Boolean.TRUE, consoleMojo.properties.get("ant")); + } + + @Test + public void testBindAntBuilder() throws Exception { + consoleMojo.initializeAnt(); + assertEquals(Boolean.TRUE, consoleMojo.properties.get("ant")); + + Class consoleClass = TestConsole.class; + Class bindingClass = TestBinding.class; + TestConsole console = new TestConsole(); + + consoleMojo.bindAntBuilder(consoleClass, bindingClass, console); + + Object ant = consoleMojo.properties.get("ant"); + assertNotNull(ant); + assertNotEquals(Boolean.TRUE, ant); + assertTrue(ant.getClass().getName().contains("AntBuilder")); + } + + public static class TestConsole { + public TestShell shell = new TestShell(); + } + + public static class TestShell { + public TestBinding getContext() { + return new TestBinding(); + } + } + + public static class TestBinding { + public void setVariable(String name, Object value) { + } + } +}