aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc R. Hoffmann <hoffmann@mountainminds.com>2013-10-11 18:37:05 +0200
committerMarc R. Hoffmann <hoffmann@mountainminds.com>2013-10-11 19:16:25 +0200
commitbd886f0a891bf68489bfa81a8fc3164fe7e1486f (patch)
treec07678d790ee38afee0c8ae76a9d0e39862eb750
parent1bbf1debc32022aa1a680adc78487756fb6755fd (diff)
downloadjacoco-bd886f0a891bf68489bfa81a8fc3164fe7e1486f.tar.gz
Multiple executions of Maven goal "prepare-agent" should not lead to
incorrect configuration (GitHub #40).
-rw-r--r--jacoco-maven-plugin.test/it/it-multiple-executions/pom.xml66
-rw-r--r--jacoco-maven-plugin.test/it/it-multiple-executions/src/main/java/Example.java18
-rw-r--r--jacoco-maven-plugin.test/it/it-multiple-executions/src/test/java/ExampleTest.java21
-rw-r--r--jacoco-maven-plugin.test/it/it-multiple-executions/verify.bsh38
-rw-r--r--jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java84
-rw-r--r--org.jacoco.doc/docroot/doc/changes.html6
6 files changed, 212 insertions, 21 deletions
diff --git a/jacoco-maven-plugin.test/it/it-multiple-executions/pom.xml b/jacoco-maven-plugin.test/it/it-multiple-executions/pom.xml
new file mode 100644
index 00000000..10fc263f
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-multiple-executions/pom.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Eclipse Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/epl-v10.html
+
+ Contributors:
+ Evgeny Mandrikov - initial API and implementation
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>jacoco</groupId>
+ <artifactId>setup-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>it-multiple-executions</artifactId>
+
+ <properties>
+ <!-- Make sure we actually prepend existing args-->
+ <argLine>-Da=1 -Da=2</argLine>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>@project.groupId@</groupId>
+ <artifactId>jacoco-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>first</id>
+ <goals>
+ <goal>prepare-agent</goal>
+ </goals>
+ <configuration>
+ <destFile>${project.build.directory}/nospace.exec</destFile>
+ </configuration>
+ </execution>
+ <execution>
+ <id>second</id>
+ <goals>
+ <goal>prepare-agent</goal>
+ </goals>
+ <configuration>
+ <destFile>${project.build.directory}/with space.exec</destFile>
+ </configuration>
+ </execution>
+ <execution>
+ <id>third</id>
+ <goals>
+ <goal>prepare-agent</goal>
+ </goals>
+ <configuration>
+ <destFile>${project.build.directory}/third.exec</destFile>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/jacoco-maven-plugin.test/it/it-multiple-executions/src/main/java/Example.java b/jacoco-maven-plugin.test/it/it-multiple-executions/src/main/java/Example.java
new file mode 100644
index 00000000..630a62f2
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-multiple-executions/src/main/java/Example.java
@@ -0,0 +1,18 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Evgeny Mandrikov - initial API and implementation
+ *
+ *******************************************************************************/
+public class Example {
+
+ public void sayHello() {
+ System.out.println("Hello world");
+ }
+
+}
diff --git a/jacoco-maven-plugin.test/it/it-multiple-executions/src/test/java/ExampleTest.java b/jacoco-maven-plugin.test/it/it-multiple-executions/src/test/java/ExampleTest.java
new file mode 100644
index 00000000..d9b78ab9
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-multiple-executions/src/test/java/ExampleTest.java
@@ -0,0 +1,21 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Evgeny Mandrikov - initial API and implementation
+ *
+ *******************************************************************************/
+import org.junit.Test;
+
+public class ExampleTest {
+
+ @Test
+ public void test() {
+ new Example().sayHello();
+ }
+
+}
diff --git a/jacoco-maven-plugin.test/it/it-multiple-executions/verify.bsh b/jacoco-maven-plugin.test/it/it-multiple-executions/verify.bsh
new file mode 100644
index 00000000..263ce022
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-multiple-executions/verify.bsh
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Evgeny Mandrikov - initial API and implementation
+ *
+ *******************************************************************************/
+import java.io.*;
+import org.codehaus.plexus.util.*;
+
+File file = new File( basedir, "target/nospace.exec" );
+if ( file.isFile() )
+{
+ throw new FileNotFoundException( "Found unwanted dump: " + file );
+}
+
+File file = new File( basedir, "target/with space.exec" );
+if ( file.isFile() )
+{
+ throw new FileNotFoundException( "Found unwanted dump: " + file );
+}
+
+file = new File( basedir, "target/third.exec" );
+if ( !file.isFile() )
+{
+ throw new FileNotFoundException( "Could not find generated dump: " + file );
+}
+
+String argLine = "-Da=1 -Da=2";
+String buildLog = FileUtils.fileRead( new File( basedir, "build.log" ) );
+if ( buildLog.indexOf( argLine ) < 0 ) {
+ throw new RuntimeException( "Original argLine was overwritten" );
+}
+
diff --git a/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java b/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java
index 2913da3d..de0c162c 100644
--- a/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java
+++ b/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java
@@ -171,24 +171,12 @@ public class AgentMojo extends AbstractJacocoMojo {
@Override
public void executeMojo() {
- final String vmArgument = StringUtils.quoteAndEscape(
- createAgentOptions().getVMArgument(getAgentJarFile()), '"');
- prependProperty(vmArgument);
+ modifyProperty(true);
}
@Override
protected void skipMojo() {
- prependProperty("");
- }
-
- private void prependProperty(final String vmArgument) {
- if (isPropertyNameSpecified()) {
- prependProperty(propertyName, vmArgument);
- } else if (isEclipseTestPluginPackaging()) {
- prependProperty(TYCHO_ARG_LINE, vmArgument);
- } else {
- prependProperty(SUREFIRE_ARG_LINE, vmArgument);
- }
+ modifyProperty(false);
}
private File getAgentJarFile() {
@@ -240,6 +228,32 @@ public class AgentMojo extends AbstractJacocoMojo {
return agentOptions;
}
+ private void modifyProperty(final boolean prepend) {
+ final String name = getEffectivePropertyName();
+ final Properties projectProperties = getProject().getProperties();
+ String value = projectProperties.getProperty(name);
+ if (value == null) {
+ value = "";
+ } else {
+ value = removeAgent(value);
+ }
+ if (prepend) {
+ value = prependAgent(value);
+ }
+ getLog().info(name + " set to " + value);
+ projectProperties.setProperty(name, value);
+ }
+
+ private String getEffectivePropertyName() {
+ if (isPropertyNameSpecified()) {
+ return propertyName;
+ }
+ if (isEclipseTestPluginPackaging()) {
+ return TYCHO_ARG_LINE;
+ }
+ return SUREFIRE_ARG_LINE;
+ }
+
private boolean isPropertyNameSpecified() {
return propertyName != null && !"".equals(propertyName);
}
@@ -248,13 +262,41 @@ public class AgentMojo extends AbstractJacocoMojo {
return "eclipse-test-plugin".equals(getProject().getPackaging());
}
- private void prependProperty(final String name, final String value) {
- final Properties projectProperties = getProject().getProperties();
- final String oldValue = projectProperties.getProperty(name);
- final String newValue = oldValue == null ? value : value + ' '
- + oldValue;
- getLog().info(name + " set to " + newValue);
- projectProperties.put(name, newValue);
+ private String prependAgent(final String value) {
+ final String agent = StringUtils.quoteAndEscape(createAgentOptions()
+ .getVMArgument(getAgentJarFile()), '"');
+ final StringBuilder result = new StringBuilder(agent);
+ if (value.length() > 0) {
+ result.append(' ').append(value);
+ }
+ return result.toString();
+ }
+
+ private String removeAgent(final String value) {
+ // Plain agent string to search for:
+ final String plainAgent = StringUtils.escape(new AgentOptions()
+ .getVMArgument(getAgentJarFile()));
+ final int startIdx = value.indexOf(plainAgent);
+ if (startIdx == -1) {
+ return value;
+ }
+ final StringBuilder result = new StringBuilder(value);
+ if (startIdx > 0 && value.charAt(startIdx - 1) == '"') {
+ removePart(result, startIdx - 1, "\" ");
+ } else {
+ removePart(result, startIdx, " ");
+ }
+ return result.toString();
+ }
+
+ private void removePart(final StringBuilder s, final int startIdx,
+ final String endMarker) {
+ final int endIdx = s.indexOf(endMarker, startIdx);
+ if (endIdx == -1) {
+ s.delete(startIdx, s.length());
+ } else {
+ s.delete(startIdx, endIdx + endMarker.length());
+ }
}
}
diff --git a/org.jacoco.doc/docroot/doc/changes.html b/org.jacoco.doc/docroot/doc/changes.html
index 29f0f128..7a65c443 100644
--- a/org.jacoco.doc/docroot/doc/changes.html
+++ b/org.jacoco.doc/docroot/doc/changes.html
@@ -27,6 +27,12 @@
(GitHub #126).</li>
</ul>
+<h3>Fixed Bugs</h3>
+<ul>
+ <li>Multiple executions of Maven goal "prepare-agent" should not lead to
+ incorrect configuration (GitHub #40).</li>
+</ul>
+
<h2>Release 0.6.3 (2013/06/03)</h2>
<h3>New Features</h3>