Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions src/main/java/org/codehaus/gmavenplus/mojo/AbstractToolsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand All @@ -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.
*
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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.
*
Expand Down
84 changes: 84 additions & 0 deletions src/test/java/org/codehaus/gmavenplus/mojo/ConsoleMojoTest.java
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}
Loading