Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Schreiber2014-10-10 08:18:22 -0400
committerJan Sievers2014-10-10 08:26:20 -0400
commit9b0c5407d58ceb6593be2418a1c0c4ad93c01b87 (patch)
tree97ab2939d290068a2e70badd437d8f7955246530
parentb699e6c4b09203c4a87c03b8085af441087b2c71 (diff)
downloadorg.eclipse.tycho-9b0c5407d58ceb6593be2418a1c0c4ad93c01b87.tar.gz
org.eclipse.tycho-9b0c5407d58ceb6593be2418a1c0c4ad93c01b87.tar.xz
org.eclipse.tycho-9b0c5407d58ceb6593be2418a1c0c4ad93c01b87.zip
407500 Adding an option to let BREE define the JDK used for the tests.
-adding an parameter 'useJDK' with values 'SYSTEM' (=default) or 'BREE'. For 'BREE' the JDK will be selected from the maven toolchains.xml based on the MANIFEST BREE (MANIFEST BREE name must match the toolchains JDK id). -Moving out the toolchain selection from the OsgiCompilerMojo to provide functionality to select the toolchain the same way for compiler and surefilre mojo (ToolchainProvider Component). -Adding a unit test with easymock to test the different scenarios. Adding the easymock to the dependency management section of the parent pom. Bug: 407500 Change-Id: If14f769a255ba2d25c82a954072e1393f10f38e2 Signed-off-by: Martin Schreiber <m.schreiber@bachmann.info>
-rw-r--r--pom.xml6
-rw-r--r--tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java37
-rw-r--r--tycho-core/pom.xml3
-rw-r--r--tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java64
-rw-r--r--tycho-surefire/tycho-surefire-plugin/pom.xml4
-rw-r--r--tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TestMojo.java66
-rw-r--r--tycho-surefire/tycho-surefire-plugin/src/test/java/org/eclipse/tycho/surefire/TestMojoToolchainTests.java105
7 files changed, 251 insertions, 34 deletions
diff --git a/pom.xml b/pom.xml
index 7e65182a4..e3113f0e5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -299,6 +299,12 @@
<artifactId>maven-verifier</artifactId>
<version>1.3</version>
</dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <version>3.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</dependencyManagement>
diff --git a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java
index 3b53b15b2..cac74eded 100644
--- a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java
+++ b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java
@@ -38,9 +38,7 @@ import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.apache.maven.repository.RepositorySystem;
-import org.apache.maven.toolchain.MisconfiguredToolchainException;
import org.apache.maven.toolchain.ToolchainManagerPrivate;
-import org.apache.maven.toolchain.ToolchainPrivate;
import org.apache.maven.toolchain.java.DefaultJavaToolChain;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import org.codehaus.plexus.compiler.util.scan.InclusionScanException;
@@ -59,6 +57,8 @@ import org.eclipse.tycho.core.TychoConstants;
import org.eclipse.tycho.core.TychoProject;
import org.eclipse.tycho.core.ee.StandardExecutionEnvironment;
import org.eclipse.tycho.core.ee.shared.ExecutionEnvironment;
+import org.eclipse.tycho.core.maven.ToolchainProvider;
+import org.eclipse.tycho.core.maven.ToolchainProvider.JDKUsage;
import org.eclipse.tycho.core.osgitools.BundleReader;
import org.eclipse.tycho.core.osgitools.DefaultClasspathEntry;
import org.eclipse.tycho.core.osgitools.DefaultClasspathEntry.DefaultAccessRule;
@@ -75,10 +75,6 @@ import copied.org.apache.maven.plugin.CompilationFailureException;
public abstract class AbstractOsgiCompilerMojo extends AbstractCompilerMojo implements JavaCompilerConfiguration,
Adaptable {
- private static enum JDKUsage {
- SYSTEM, BREE;
- }
-
public static final String RULE_SEPARATOR = File.pathSeparator;
/**
@@ -238,6 +234,9 @@ public abstract class AbstractOsgiCompilerMojo extends AbstractCompilerMojo impl
@Parameter(defaultValue = "true")
private boolean copyResources;
+ @Component
+ ToolchainProvider toolchainProvider;
+
@Override
public void execute() throws MojoExecutionException, CompilationFailureException {
StandardExecutionEnvironment[] manifestBREEs = bundleReader.loadManifest(project.getBasedir())
@@ -476,7 +475,12 @@ public abstract class AbstractOsgiCompilerMojo extends AbstractCompilerMojo impl
if (useJDK != JDKUsage.BREE) {
return;
}
- DefaultJavaToolChain toolChain = findMatchingJavaToolChain(getTargetExecutionEnvironment());
+ String toolchainId = getTargetExecutionEnvironment().getProfileName();
+ DefaultJavaToolChain toolChain = toolchainProvider.findMatchingJavaToolChain(session, toolchainId);
+ if (toolChain == null) {
+ throw new MojoExecutionException("useJDK = BREE configured, but no toolchain of type 'jdk' with id '"
+ + toolchainId + "' found. See http://maven.apache.org/guides/mini/guide-using-toolchains.html");
+ }
compilerConfiguration.addCompilerCustomArgument("use.java.home", toolChain.getJavaHome());
configureBootClassPath(compilerConfiguration, toolChain);
}
@@ -530,25 +534,6 @@ public abstract class AbstractOsgiCompilerMojo extends AbstractCompilerMojo impl
return values;
}
- private DefaultJavaToolChain findMatchingJavaToolChain(final ExecutionEnvironment environment)
- throws MojoExecutionException {
- try {
- final Map<String, String> requirements = Collections.singletonMap("id", environment.getProfileName());
- for (ToolchainPrivate javaToolChain : toolChainManager.getToolchainsForType("jdk", session)) {
- if (javaToolChain.matchesRequirements(requirements)) {
- if (javaToolChain instanceof DefaultJavaToolChain) {
- return ((DefaultJavaToolChain) javaToolChain);
- }
- }
- }
- throw new MojoExecutionException("useJDK = BREE configured, but no toolchain of type 'jdk' with id '"
- + environment.getProfileName()
- + "' found. See http://maven.apache.org/guides/mini/guide-using-toolchains.html");
- } catch (MisconfiguredToolchainException e) {
- throw new MojoExecutionException(e.getMessage(), e);
- }
- }
-
private void configureSourceAndTargetLevel(CompilerConfiguration compilerConfiguration)
throws MojoExecutionException {
ExecutionEnvironment ee = getTargetExecutionEnvironment();
diff --git a/tycho-core/pom.xml b/tycho-core/pom.xml
index 62c6a92f7..9b92aa5d7 100644
--- a/tycho-core/pom.xml
+++ b/tycho-core/pom.xml
@@ -161,9 +161,6 @@
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
- <version>3.0</version>
- <type>jar</type>
- <scope>test</scope>
</dependency>
</dependencies>
diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java
new file mode 100644
index 000000000..1e1c97454
--- /dev/null
+++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Bachmann electronics GmbH and others.
+ * 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:
+ * Bachmann electronics GmbH - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tycho.core.maven;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.toolchain.MisconfiguredToolchainException;
+import org.apache.maven.toolchain.ToolchainManagerPrivate;
+import org.apache.maven.toolchain.ToolchainPrivate;
+import org.apache.maven.toolchain.java.DefaultJavaToolChain;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+
+@Component(role = ToolchainProvider.class)
+public class ToolchainProvider {
+
+ @Requirement
+ ToolchainManagerPrivate toolChainManager;
+
+ public static enum JDKUsage {
+ SYSTEM, BREE;
+ }
+
+ /**
+ * Finds a matching {@link DefaultJavaToolChain} in the maven toolchains for a given maven
+ * session and toolchain id. Returns the toolchain or null if no toolchain could be found.
+ *
+ * @param session
+ * The maven session
+ * @param toolchainId
+ * The id of the toolchain
+ * @return the toolchain that matches or null if no toolchain could be found
+ * @throws MojoExecutionException
+ * if the toolchains are misconfigured
+ */
+ public DefaultJavaToolChain findMatchingJavaToolChain(final MavenSession session, final String toolchainId)
+ throws MojoExecutionException {
+ try {
+ final Map<String, String> requirements = Collections.singletonMap("id", toolchainId);
+ for (ToolchainPrivate javaToolChain : toolChainManager.getToolchainsForType("jdk", session)) {
+ if (javaToolChain.matchesRequirements(requirements)) {
+ if (javaToolChain instanceof DefaultJavaToolChain) {
+ return ((DefaultJavaToolChain) javaToolChain);
+ }
+ }
+ }
+ return null;
+
+ } catch (MisconfiguredToolchainException e) {
+ throw new MojoExecutionException(e.getMessage(), e);
+ }
+ }
+}
diff --git a/tycho-surefire/tycho-surefire-plugin/pom.xml b/tycho-surefire/tycho-surefire-plugin/pom.xml
index 6e5d55b28..7bcaa24f9 100644
--- a/tycho-surefire/tycho-surefire-plugin/pom.xml
+++ b/tycho-surefire/tycho-surefire-plugin/pom.xml
@@ -111,5 +111,9 @@
<artifactId>maven-surefire-common</artifactId>
<version>${surefire-version}</version>
</dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TestMojo.java b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TestMojo.java
index afd5b661e..6ad3bc612 100644
--- a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TestMojo.java
+++ b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TestMojo.java
@@ -71,6 +71,8 @@ import org.eclipse.tycho.core.TargetPlatformConfiguration;
import org.eclipse.tycho.core.TychoConstants;
import org.eclipse.tycho.core.TychoProject;
import org.eclipse.tycho.core.ee.shared.ExecutionEnvironmentConfiguration;
+import org.eclipse.tycho.core.maven.ToolchainProvider;
+import org.eclipse.tycho.core.maven.ToolchainProvider.JDKUsage;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.osgitools.OsgiBundleProject;
import org.eclipse.tycho.core.osgitools.project.BuildOutputJar;
@@ -588,6 +590,52 @@ public class TestMojo extends AbstractMojo {
@Component
private RepositoryReferenceTool repositoryReferenceTool;
+ @Component
+ private ToolchainProvider toolchainProvider;
+
+ /**
+ * Which JDK to use for executing tests. Possible values are: <code>SYSTEM</code>,
+ * <code>BREE</code> .
+ * <p/>
+ * <ul>
+ * <li>SYSTEM: Use the currently running JVM (or from <a
+ * href="http://maven.apache.org/guides/mini/guide-using-toolchains.html">toolchain</a> if
+ * configured in pom.xml)</li>
+ * <li>BREE: use MANIFEST header <code>Bundle-RequiredExecutionEnvironment</code> to lookup the
+ * JDK from <a
+ * href="http://maven.apache.org/guides/mini/guide-using-toolchains.html">toolchains.xml</a>.
+ * The value of BREE will be matched against the id of the toolchain elements in toolchains.xml.
+ * </li>
+ * </ul>
+ * <p>
+ * Example for BREE: <br>
+ * In <code>META-INF/MANIFEST.MF</code>:
+ *
+ * <pre>
+ * Bundle-RequiredExecutionEnvironment: JavaSE-1.7
+ * </pre>
+ *
+ * In toolchains.xml:
+ *
+ * <pre>
+ * &lt;toolchains&gt;
+ * &lt;toolchain&gt;
+ * &lt;type&gt;jdk&lt;/type&gt;
+ * &lt;provides&gt;
+ * &lt;id&gt;JavaSE-1.7&lt;/id&gt;
+ * &lt;/provides&gt;
+ * &lt;configuration&gt;
+ * &lt;jdkHome&gt;/path/to/jdk/1.7&lt;/jdkHome&gt;
+ * &lt;/configuration&gt;
+ * &lt;/toolchain&gt;
+ * &lt;/toolchains&gt;
+ * </pre>
+ *
+ * </p>
+ */
+ @Parameter(defaultValue = "SYSTEM")
+ private JDKUsage useJDK;
+
public void execute() throws MojoExecutionException, MojoFailureException {
if (shouldSkip()) {
getLog().info("Skipping tests");
@@ -957,12 +1005,20 @@ public class TestMojo extends AbstractMojo {
}
}
- private Toolchain getToolchain() {
- Toolchain tc = null;
- if (toolchainManager != null) {
- tc = toolchainManager.getToolchainFromBuildContext("jdk", session);
+ protected Toolchain getToolchain() throws MojoExecutionException {
+ if (JDKUsage.SYSTEM.equals(useJDK)) {
+ if (toolchainManager != null) {
+ return toolchainManager.getToolchainFromBuildContext("jdk", session);
+ }
+ return null;
+ }
+ String profileName = TychoProjectUtils.getExecutionEnvironmentConfiguration(project).getProfileName();
+ Toolchain toolChain = toolchainProvider.findMatchingJavaToolChain(session, profileName);
+ if (toolChain == null) {
+ throw new MojoExecutionException("useJDK = BREE configured, but no toolchain of type 'jdk' with id '"
+ + profileName + "' found. See http://maven.apache.org/guides/mini/guide-using-toolchains.html");
}
- return tc;
+ return toolChain;
}
LaunchConfiguration createCommandLine(EquinoxInstallation testRuntime) throws MalformedURLException,
diff --git a/tycho-surefire/tycho-surefire-plugin/src/test/java/org/eclipse/tycho/surefire/TestMojoToolchainTests.java b/tycho-surefire/tycho-surefire-plugin/src/test/java/org/eclipse/tycho/surefire/TestMojoToolchainTests.java
new file mode 100644
index 000000000..7c533126f
--- /dev/null
+++ b/tycho-surefire/tycho-surefire-plugin/src/test/java/org/eclipse/tycho/surefire/TestMojoToolchainTests.java
@@ -0,0 +1,105 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Bachmann electronics GmbH and others.
+ * 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:
+ * Bachmann electronics GmbH - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tycho.surefire;
+
+import java.lang.reflect.Field;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.toolchain.Toolchain;
+import org.apache.maven.toolchain.ToolchainManager;
+import org.apache.maven.toolchain.java.DefaultJavaToolChain;
+import org.codehaus.plexus.util.ReflectionUtils;
+import org.easymock.EasyMock;
+import org.eclipse.tycho.core.TychoConstants;
+import org.eclipse.tycho.core.ee.shared.ExecutionEnvironmentConfiguration;
+import org.eclipse.tycho.core.maven.ToolchainProvider;
+import org.eclipse.tycho.core.maven.ToolchainProvider.JDKUsage;
+import org.junit.Assert;
+
+public class TestMojoToolchainTests extends TestCase {
+
+ private ToolchainManager toolchainManager;
+ private TestMojo testMojo;
+ private MavenSession session;
+ private MavenProject project;
+ private DefaultJavaToolChain breeToolchain;
+ private Toolchain systemToolchain;
+ private ToolchainProvider toolchainProvider;
+
+ @Override
+ public void setUp() throws Exception {
+ toolchainManager = EasyMock.createMock(ToolchainManager.class);
+ session = EasyMock.createMock(MavenSession.class);
+ breeToolchain = EasyMock.createMock(DefaultJavaToolChain.class);
+ systemToolchain = EasyMock.createMock(Toolchain.class);
+ toolchainProvider = EasyMock.createMock(ToolchainProvider.class);
+ project = new MavenProject();
+ testMojo = new TestMojo();
+ setParameter(testMojo, "useJDK", JDKUsage.SYSTEM);
+ setParameter(testMojo, "toolchainManager", toolchainManager);
+ setParameter(testMojo, "toolchainProvider", toolchainProvider);
+ setParameter(testMojo, "project", project);
+ setParameter(testMojo, "session", session);
+ }
+
+ public void testGetToolchainWithUseJDKSetToSystemNoToolchainManager() throws Exception {
+ setParameter(testMojo, "toolchainManager", null);
+ Assert.assertNull(testMojo.getToolchain());
+ }
+
+ public void testGetToolchainWithUseJDKSetToSystemWithToolchainManager() throws Exception {
+ EasyMock.expect(toolchainManager.getToolchainFromBuildContext("jdk", session)).andReturn(systemToolchain);
+ EasyMock.replay(toolchainManager);
+ Toolchain tc = testMojo.getToolchain();
+ Assert.assertEquals(systemToolchain, tc);
+ }
+
+ public void testGetToolchainWithUseJDKSetToBREE() throws Exception {
+ setupWithBree();
+ EasyMock.expect(toolchainProvider.findMatchingJavaToolChain(session, "myId")).andReturn(breeToolchain);
+ EasyMock.replay(toolchainProvider);
+ Toolchain tc = testMojo.getToolchain();
+ Assert.assertEquals(breeToolchain, tc);
+ }
+
+ public void testGetToolchainWithUseJDKSetToBREEToolchainNotFound() throws Exception {
+ setupWithBree();
+ EasyMock.expect(toolchainProvider.findMatchingJavaToolChain(session, "myId")).andReturn(null);
+ EasyMock.replay(toolchainProvider);
+ try {
+ testMojo.getToolchain();
+ fail("MojoExcecutionException expected since Toolchain could not be found!");
+ } catch (MojoExecutionException e) {
+ assertTrue(e.getMessage().startsWith(
+ "useJDK = BREE configured, but no toolchain of type 'jdk' with id 'myId' found"));
+ }
+ }
+
+ public void setupWithBree() throws Exception {
+ setParameter(testMojo, "useJDK", JDKUsage.BREE);
+ ExecutionEnvironmentConfiguration envConf = EasyMock.createMock(ExecutionEnvironmentConfiguration.class);
+ EasyMock.expect(envConf.getProfileName()).andReturn("myId");
+ EasyMock.replay(envConf);
+ project.setContextValue(TychoConstants.CTX_EXECUTION_ENVIRONMENT_CONFIGURATION, envConf);
+ }
+
+ private void setParameter(Object object, String variable, Object value) throws IllegalArgumentException,
+ IllegalAccessException {
+ Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses(variable, object.getClass());
+ field.setAccessible(true);
+ field.set(object, value);
+ }
+
+}

Back to the top