diff --git a/tycho-compiler-plugin/src/test/java/org/eclipse/tycho/osgicompiler/test/OsgiCompilerTest.java b/tycho-compiler-plugin/src/test/java/org/eclipse/tycho/osgicompiler/test/OsgiCompilerTest.java deleted file mode 100644 index 2fcf7d21f0..0000000000 --- a/tycho-compiler-plugin/src/test/java/org/eclipse/tycho/osgicompiler/test/OsgiCompilerTest.java +++ /dev/null @@ -1,623 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008, 2018 Sonatype Inc. and others. - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Sonatype Inc. - initial API and implementation - *******************************************************************************/ -package org.eclipse.tycho.osgicompiler.test; - -import static java.util.Arrays.asList; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.MatcherAssert.assertThat; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Properties; -import java.util.Set; -import java.util.stream.Collectors; - -import org.apache.bcel.classfile.ClassFormatException; -import org.apache.bcel.classfile.ClassParser; -import org.apache.bcel.classfile.JavaClass; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.logging.SystemStreamLog; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.FileUtils; -import org.eclipse.tycho.SourcepathEntry; -import org.eclipse.tycho.compiler.AbstractOsgiCompilerMojo; -import org.eclipse.tycho.core.ee.StandardExecutionEnvironment; -import org.eclipse.tycho.testing.AbstractTychoMojoTestCase; -import org.eclipse.tycho.version.TychoVersion; -import org.hamcrest.Matchers; -import org.hamcrest.core.StringContains; - -import copied.org.apache.maven.plugin.CompilationFailureException; - -public class OsgiCompilerTest extends AbstractTychoMojoTestCase { - - private static final int TARGET_1_4 = 48; - private static final int TARGET_1_8 = 52; - - protected File storage; - private Properties properties; - - @Override - protected void setUp() throws Exception { - super.setUp(); - storage = new File(getBasedir(), "target/storage"); - FileUtils.deleteDirectory(storage); - properties = new Properties(); - properties.setProperty("tycho-version", TychoVersion.getTychoVersion()); - } - - private AbstractOsgiCompilerMojo getMojo(List projects, MavenProject project) throws Exception { - AbstractOsgiCompilerMojo mojo = (AbstractOsgiCompilerMojo) lookupConfiguredMojo(project, "compile"); - for (MavenProject mavenProject : projects) { - new File(mavenProject.getBasedir(), "target/classes").mkdirs(); - } -// setVariableValueToObject(mojo, "project", project); -// setVariableValueToObject(mojo, "session", newMavenSession(project, projects)); - - // tycho-compiler-jdt does not support forked compilation -// setVariableValueToObject(mojo, "fork", fork? Boolean.TRUE: Boolean.FALSE); - return mojo; - } - - public void testAccessRestrictionCompilationError() throws Exception { - File basedir = getBasedir("projects/accessrules"); - List projects = getSortedProjects(basedir); - - try { - for (MavenProject project : projects) { - if (!"pom".equals(project.getPackaging())) { - getMojo(projects, project).execute(); - } - } - fail("Restricted package access"); - } catch (MojoFailureException e) { - assertTrue(e.getLongMessage().contains("The type 'P001Impl' is not API")); - } - } - - public void testAccessRulesClasspath() throws Exception { - File basedir = getBasedir("projects/accessrules"); - List projects = getSortedProjects(basedir); - - getMojo(projects, projects.get(1)).execute(); - getMojo(projects, projects.get(2)).execute(); - getMojo(projects, projects.get(3)).execute(); - - MavenProject project = projects.get(4); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - List cp = mojo.getClasspathElements(); - assertEquals(4, cp.size()); - assertEquals(getClasspathElement(new File(getBasedir()), "target/projects/accessrules/p001/target/classes", - "[+p001/*:?**/*]"), cp.get(0)); - // note that PDE sorts dependencies coming via imported-package by symbolicName_version - assertEquals(getClasspathElement(new File(getBasedir()), "target/projects/accessrules/p003/target/classes", - "[+p003/*:?**/*]"), cp.get(1)); - assertEquals(getClasspathElement(new File(getBasedir()), "target/projects/accessrules/p004/target/classes", - "[+p004/*:?**/*]"), cp.get(2)); - assertEquals(getClasspathElement(project.getBasedir(), "target/classes", ""), cp.get(3)); - - } - - public void testClasspath() throws Exception { - File basedir = getBasedir("projects/classpath"); - List projects = getSortedProjects(basedir, properties); - - MavenProject project; - List cp; - - // simple project - project = projects.get(1); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - cp = mojo.getClasspathElements(); - assertEquals(1, cp.size()); - assertEquals(getClasspathElement(project.getBasedir(), "target/classes", ""), cp.get(0)); - - // project with nested lib - project = projects.get(2); - mojo = getMojo(projects, project); - cp = mojo.getClasspathElements(); - assertEquals(2, cp.size()); - assertEquals(getClasspathElement(project.getBasedir(), "target/classes", ""), cp.get(0)); - assertEquals(getClasspathElement(project.getBasedir(), "lib/lib.jar", ""), cp.get(1)); - - // project with external dependency with nested jar - project = projects.get(3); - mojo = getMojo(projects, project); - cp = mojo.getClasspathElements(); - assertEquals(3, cp.size()); - final String plainJarPath = "target/projects/classpath/platform/plugins/p003_0.0.1.jar"; - final String nestedJarPath = "target/local-repo/.cache/tycho/p003_0.0.1.jar/lib/lib.jar"; - assertEquals(getClasspathElement(new File(getBasedir()), plainJarPath, "[?**/*]"), cp.get(0)); - assertEquals(getClasspathElement(new File(getBasedir()), nestedJarPath, "[?**/*]"), cp.get(1)); - assertEquals(getClasspathElement(project.getBasedir(), "target/classes", ""), cp.get(2)); - - // project with a (not yet) existing nested jar that would be copied later during build - // (wrapper scenario with copy-pom-dependencies) -// project = projects.get(4); -// mojo = getMojo(projects, project); -// mojo.execute(); -// cp = mojo.getClasspathElements(); -// assertEquals(3, cp.size()); -// assertEquals(getClasspathElement(project.getBasedir(), "target/classes", ""), cp.get(0)); -// assertEquals(getClasspathElement(project.getBasedir(), "lib/not_existing_yet.jar", ""), cp.get(1)); -// assertEquals(getClasspathElement(project.getBasedir(), "lib/not_existing_yet_dir/", ""), cp.get(2)); - } - - private String getClasspathElement(File base, String path, String accessRules) throws IOException { - String file = new File(base, path).getAbsolutePath(); - return file + accessRules.replace(":", AbstractOsgiCompilerMojo.RULE_SEPARATOR); - } - - public void test_multisourceP001_viaMojoConfiguration() throws Exception { - File basedir = getBasedir("projects/multisource/p001"); - List projects = getSortedProjects(basedir); - - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - - assertTrue(new File(project.getBasedir(), "target/classes/p001/p1/P1.class").canRead()); - assertTrue(new File(project.getBasedir(), "target/classes/p001/p2/P2.class").canRead()); - } - - public void test_multisourceP002_viaBuildProperties() throws Exception { - File basedir = getBasedir("projects/multisource/p002"); - List projects = getSortedProjects(basedir); - - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - - assertTrue(new File(project.getBasedir(), "target/classes/p002/p1/P1.class").canRead()); - assertTrue(new File(project.getBasedir(), "target/classes/p002/p2/P2.class").canRead()); - } - - public void test_multipleOutputJars() throws Exception { - File basedir = getBasedir("projects/multijar"); - List projects = getSortedProjects(basedir); - - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - - assertTrue(new File(project.getBasedir(), "target/classes/src/Src.class").canRead()); - assertTrue(new File(project.getBasedir(), "target/library.jar-classes/src2/Src2.class").canRead()); - - List sourcepath = getMojo(projects, project).getSourcepath(); - assertEquals(2, sourcepath.size()); - } - - public void test_multipleOutputJars_getSourcepath() throws Exception { - File basedir = getBasedir("projects/multijar"); - List projects = getSortedProjects(basedir); - - MavenProject project = projects.get(0); - - List sourcepath = getMojo(projects, project).getSourcepath(); - assertEquals(2, sourcepath.size()); - assertSameFile(new File(project.getBasedir(), "target/classes"), sourcepath.get(0).getOutputDirectory()); - assertSameFile(new File(project.getBasedir(), "src"), sourcepath.get(0).getSourcesRoot()); - assertSameFile(new File(project.getBasedir(), "target/library.jar-classes"), - sourcepath.get(1).getOutputDirectory()); - assertSameFile(new File(project.getBasedir(), "src2"), sourcepath.get(1).getSourcesRoot()); - } - - private void assertSameFile(File expected, File actual) throws IOException { - assertEquals(expected.getCanonicalFile(), actual.getCanonicalFile()); - } - - public void testCopyResources() throws Exception { - File basedir = getBasedir("projects/resources/p001"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - assertTrue(new File(project.getBasedir(), "target/classes/testresources/Test.class").canRead()); - assertTrue(new File(project.getBasedir(), "target/classes/testresources/test.properties").canRead()); - } - - public void testCopyResourcesWithNestedJar() throws Exception { - File basedir = getBasedir("projects/resources/p004"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - assertTrue(new File(project.getBasedir(), "target/classes/testresources/Test.class").canRead()); - assertTrue(new File(project.getBasedir(), "target/classes/testresources/test.properties").canRead()); - } - - public void testExcludeCopyResources() throws Exception { - File basedir = getBasedir("projects/resources/p002"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - assertTrue(new File(project.getBasedir(), "target/classes/testresources/Test.class").canRead()); - assertFalse(new File(project.getBasedir(), "target/classes/testresources/Test.aj").canRead()); - } - - public void testCopyResourcesWithResourceCopyingSetToOff() throws Exception { - File basedir = getBasedir("projects/resources/p003"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - assertTrue(new File(project.getBasedir(), "target/classes/testresources/Test.class").exists()); - assertFalse(new File(project.getBasedir(), "target/classes/testresources/test.properties").exists()); - } - - public void testSourceCompileLevel() throws Exception { - File basedir = getBasedir("projects/executionEnvironment"); - List projects = getSortedProjects(basedir); - MavenProject project; - // project with neither POM nor MANIFEST configuration => must fallback to - // source/target level == 17 - project = projects.get(1); - getMojo(projects, project).execute(); - assertBytecodeMajorLevel(61 /* Java 17 */, new File(project.getBasedir(), "target/classes/Generic.class")); - - // project with multiple execution envs. - // Minimum source and target level must be taken - project = projects.get(2); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - assertEquals("1.3", mojo.getSourceLevel()); - try { - mojo.execute(); - fail("compilation failure due to assert keyword expected"); - } catch (CompilationFailureException e) { - // expected - } - // project with both explicit compiler configuration in pom.xml and Bundle-RequiredExecutionEnvironment. - // explicit compiler configuration in the pom should win. see https://issues.sonatype.org/browse/TYCHO-476 - project = projects.get(3); - mojo = getMojo(projects, project); - assertEquals("jsr14", mojo.getTargetLevel()); - assertEquals("1.5", mojo.getSourceLevel()); - mojo.execute(); - assertBytecodeMajorLevel(TARGET_1_4, new File(project.getBasedir(), "target/classes/Generic.class")); - // project with both explicit EE configuration in pom.xml and Bundle-RequiredExecutionEnvironment. - // explicit configuration in the pom.xml win - project = projects.get(4); - mojo = getMojo(projects, project); - mojo.execute(); - assertEquals("1.3", mojo.getSourceLevel()); - // project with both explicit compiler configuration in build.properties and Bundle-RequiredExecutionEnvironment. - // build.properties should win. - project = projects.get(5); - mojo = getMojo(projects, project); - mojo.execute(); - assertEquals("jsr14", mojo.getTargetLevel()); - assertEquals("1.5", mojo.getSourceLevel()); - assertBytecodeMajorLevel(TARGET_1_4, new File(project.getBasedir(), "target/classes/Generic.class")); - } - - public void testNewerEEthanBREE() throws Exception { - File basedir = getBasedir("projects/executionEnvironment/p006-newerEEthanBREE"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - mojo.execute(); - assertTrue(Integer.parseInt(mojo.getExecutionEnvironment().substring("JavaSE-".length())) >= 11); - assertEquals("1.8", mojo.getSourceLevel()); - assertEquals("1.8", mojo.getTargetLevel()); - assertEquals("8", mojo.getReleaseLevel()); - assertBytecodeMajorLevel(TARGET_1_8, new File(project.getBasedir(), "target/classes/Noop.class")); - } - - public void testNoBREEButEERequirement() throws Exception { - File basedir = getBasedir("projects/executionEnvironment/eeAsRequirement"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - StandardExecutionEnvironment[] ees = mojo.getBREE(); - assertEquals(Set.of("JavaSE-1.8", "JavaSE/compact1-1.8"), - Arrays.stream(ees).map(StandardExecutionEnvironment::getProfileName).collect(Collectors.toSet())); - } - - public void testAutomaticReleaseCompilerArgumentDeterminationDisabled() throws Exception { - File basedir = getBasedir( - "projects/executionEnvironment/p007-automaticReleaseCommpilerArgumentDeterminationDisabled"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - mojo.execute(); - assertTrue(Integer.parseInt(mojo.getExecutionEnvironment().substring("JavaSE-".length())) >= 11); - assertEquals("1.8", mojo.getSourceLevel()); - assertEquals("1.8", mojo.getTargetLevel()); - assertNull(mojo.getReleaseLevel()); - assertBytecodeMajorLevel(TARGET_1_8, new File(project.getBasedir(), "target/classes/Noop.class")); - } - - private void assertBytecodeMajorLevel(int majorLevel, File classFile) throws ClassFormatException, IOException { - assertTrue(classFile.canRead()); - JavaClass javaClass = new ClassParser(classFile.getAbsolutePath()).parse(); - assertEquals(majorLevel, javaClass.getMajor()); - } - - public void test_TYCHO0400indirectDependencies() throws Exception { - File basedir = getBasedir("projects/indirectDependencies"); - List projects = getSortedProjects(basedir); - - assertEquals("C", projects.get(1).getArtifactId()); - getMojo(projects, projects.get(1)).execute(); - - assertEquals("B", projects.get(2).getArtifactId()); - getMojo(projects, projects.get(2)).execute(); - - assertEquals("A", projects.get(3).getArtifactId()); - AbstractOsgiCompilerMojo mojo = getMojo(projects, projects.get(3)); - List cp = mojo.getClasspathElements(); - assertEquals(getClasspathElement(projects.get(1).getBasedir(), "target/classes", "[?**/*]"), cp.get(1)); - - mojo.execute(); - assertTrue(new File(projects.get(3).getBasedir(), "target/classes/a/A.class").canRead()); - } - - public void test_embeddedNonClasspath() throws Exception { - File basedir = getBasedir("projects/embedednonclasspath"); - List projects = getSortedProjects(basedir); - - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - - assertTrue(new File(project.getBasedir(), "target/classes/src/Src.class").canRead()); - assertTrue(new File(project.getBasedir(), "target/library.jar-classes/src2/Src2.class").canRead()); - - List sourcepath = getMojo(projects, project).getSourcepath(); - assertEquals(2, sourcepath.size()); - } - - public void test_bootclasspathAccessRules() throws Exception { - File basedir = getBasedir("projects/bootclasspath-accessrules"); - List projects = getSortedProjects(basedir); - - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - } - - public void testWarningAndErrorMessages() throws Exception { - File basedir = getBasedir("projects/compilermessages"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - final List warnings = new ArrayList<>(); - mojo.setLog(new SystemStreamLog() { - - @Override - public void warn(CharSequence content) { - warnings.add(content); - } - - }); - try { - mojo.execute(); - fail("compilation failure expected"); - } catch (CompilationFailureException e) { - String message = e.getLongMessage(); - assertThat(message, containsString("3 problems (1 error, 2 warnings)")); - // 1 error - assertThat(message, containsString("Test.java:[25")); - assertThat(message, containsString("System.foo();")); - } - // 2 warnings - List expectedWarnings = asList("Test.java:[21", // - "Test.java:[23"); - assertEquals(expectedWarnings.size(), warnings.size()); - for (int i = 0; i < warnings.size(); i++) { - String warning = (String) warnings.get(i); - String expectedWarning = expectedWarnings.get(i); - assertThat(warning, containsString(expectedWarning)); - } - } - - public void testCompilerArgs() throws Exception { - File basedir = getBasedir("projects/compiler-args"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - - setVariableValueToObject(mojo, "showWarnings", Boolean.TRUE); - - final List warnings = new ArrayList<>(); - mojo.setLog(new SystemStreamLog() { - - @Override - public void warn(CharSequence content) { - warnings.add(content); - } - - }); - try { - mojo.execute(); - fail("compilation failure expected"); - } catch (CompilationFailureException e) { - String message = e.getLongMessage(); - assertThat(message, containsString("2 problems (1 error, 1 warning)")); - // 1 error - assertThat(message, containsString("unused")); - } - // 1 warning - assertThat((String) warnings.iterator().next(), containsString("is boxed")); - } - - public void testUseProjectSettingsSetToTrue() throws Exception { - // the code in the project does use boxing and the settings file - // turns on warning for auto boxing so we expect here a warning - File basedir = getBasedir("projects/projectSettings/p001"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - setVariableValueToObject(mojo, "useProjectSettings", Boolean.TRUE); - setVariableValueToObject(mojo, "showWarnings", Boolean.TRUE); - final List warnings = new ArrayList<>(); - mojo.setLog(new SystemStreamLog() { - - @Override - public void warn(CharSequence content) { - warnings.add(content); - } - - }); - mojo.execute(); - assertThat((String) warnings.iterator().next(), containsString("is boxed")); - } - - public void testUseProjectSettingsSetToFalse() throws Exception { - File basedir = getBasedir("projects/projectSettings/p001"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - setVariableValueToObject(mojo, "useProjectSettings", Boolean.FALSE); - final List warnings = new ArrayList<>(); - mojo.setLog(new SystemStreamLog() { - - @Override - public void warn(CharSequence content) { - warnings.add(content); - } - - }); - mojo.execute(); - assertTrue(warnings.isEmpty()); - } - - public void testUseProjectSettingsSetToTrueWithMissingPrefsFile() throws Exception { - File basedir = getBasedir("projects/projectSettings/p002"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(projects, project); - setVariableValueToObject(mojo, "useProjectSettings", Boolean.TRUE); - final List debug = new ArrayList<>(); - mojo.setLog(new SystemStreamLog() { - - @Override - public void debug(CharSequence content) { - debug.add(content.toString()); - } - - }); - mojo.execute(); - assertTrue(debug.stream() - .anyMatch(msg -> msg.contains("Parameter 'useProjectSettings' is set to true, but preferences file"))); - } - - public void test367431_frameworkExtensionCompileAccessRules() throws Exception { - // This test assumes that the internal class com.sun.crypto.provider.SunJCE exists and its package is not exported. - // This is the case for all supported JDKs to date (1.8, 11, 14). - // Note: The bundle uses BREE 1.8 here, because apparently this kind of framework-extension does not - // correctly work with modular API (Java9+). - File basedir = getBasedir("projects/367431_frameworkExtensionCompileAccessRules/"); - List projects = getSortedProjects(new File(basedir, "bundle"), properties); - - MavenProject project = projects.get(0); - getMojo(projects, project).execute(); - } - - public void testBreeCompilerTargetCompatibilityIsChecked() throws Exception { - File basedir = getBasedir("projects/bree-target-compatibility"); - List projects = getSortedProjects(basedir); - - MavenProject project = projects.get(0); - try { - getMojo(projects, project).execute(); - fail(); - } catch (MojoExecutionException e) { - // assert that the compiler mojo checks the target levels of all BREEs (and not just the first or "minimal" one) - assertThat(e.getMessage(), containsString( - "The effective compiler target level 1.5 is incompatible with the following OSGi execution environments")); - assertThat(e.getMessage(), containsString("J2SE-1.2")); - assertThat(e.getMessage(), containsString("CDC-1.0/Foundation-1.0")); - assertThat(e.getMessage(), containsString("OSGi/Minimum-1.2")); - assertThat(e.getMessage(), not(containsString("JavaSE-1.6"))); - } - } - - public void test386210_compilerConfigurationCrosstalk() throws Exception { - File basedir = getBasedir("projects/crosstalk"); - List projects = getSortedProjects(basedir); - - getMojo(projects, projects.get(1)).execute(); - getMojo(projects, projects.get(2)).execute(); - } - - public void testCompilerLogWithMultiJarInSingleDirectory() throws Exception { - File basedir = getBasedir("projects/logs/multiJarSingleDir"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - lookupConfiguredMojo(project, "compile").execute(); - assertTrue(new File(basedir, "target/log-dir/@dot.log").canRead()); - assertTrue(new File(basedir, "target/log-dir/library.jar.log").canRead()); - } - - public void testCompilerLogWithMultiJarInSubDirectory() throws Exception { - File basedir = getBasedir("projects/logs/multiJarMultiDir"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - lookupConfiguredMojo(project, "compile").execute(); - assertTrue(new File(basedir, "target/log-dir/@dot.log").canRead()); - assertTrue(new File(basedir, "target/log-dir/lib_library.jar.log").canRead()); - } - - public void testCompilerLogWithSingleJar() throws Exception { - File basedir = getBasedir("projects/logs/singleJar"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - lookupConfiguredMojo(project, "compile").execute(); - assertTrue(new File(basedir, "target/log-dir/@dot.xml").canRead()); - } - - public void testCompilerLogWithCustomComilerArgs() throws Exception { - File basedir = getBasedir("projects/logs/customCompilerArgs"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - lookupConfiguredMojo(project, "compile").execute(); - assertTrue(new File(basedir, "target/@dot.xml").canRead()); - } - - public void testCompilerLogWithCustomComilerArgsAndLog() throws Exception { - File basedir = getBasedir("projects/logs/customCompilerArgsAndLog"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - try { - lookupConfiguredMojo(project, "compile").execute(); - fail(); - } catch (MojoFailureException e) { - assertThat(e.getMessage(), containsString("Compiler logging is configured by the 'log' compiler" - + " plugin parameter and the custom compiler argument '-log'. Only either of them is allowed.")); - } - } - - public void testJreCompilationProfile() throws Exception { - File basedir = getBasedir("projects/jreCompilationProfile"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(Collections.singletonList(project), project); - mojo.execute(); - assertEquals("1.8", mojo.getSourceLevel()); - assertEquals("1.8", mojo.getTargetLevel()); - assertBytecodeMajorLevel(TARGET_1_8, new File(project.getBasedir(), "target/classes/Test.class")); - } - - public void testUseJDKBREE() throws Exception { - File basedir = getBasedir("projects/useJDKBREE"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - AbstractOsgiCompilerMojo mojo = getMojo(Collections.singletonList(project), project); - try { - mojo.execute(); - fail("Mojo should fail because of missing toolchains"); - } catch (MojoExecutionException ex) { - assertThat(ex.getMessage(), Matchers.allOf(StringContains.containsStringIgnoringCase("toolchain"), - StringContains.containsString("JavaSE-1.8"))); - } - } -} diff --git a/tycho-core/src/test/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReaderTest.java b/tycho-core/src/test/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReaderTest.java deleted file mode 100644 index 34de153dfa..0000000000 --- a/tycho-core/src/test/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReaderTest.java +++ /dev/null @@ -1,201 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014, 2021 Bachmann electronics GmbH and others. - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Bachmann electronics GmbH - initial API and implementation - * Christoph Läubrich - Adjust to changed API - *******************************************************************************/ -package org.eclipse.tycho.core.resolver; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.File; -import java.util.Arrays; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.xml.Xpp3Dom; -import org.eclipse.tycho.BuildFailureException; -import org.eclipse.tycho.core.TargetPlatformConfiguration; -import org.eclipse.tycho.testing.AbstractTychoMojoTestCase; -import org.junit.Test; - -public class DefaultTargetPlatformConfigurationReaderTest extends AbstractTychoMojoTestCase { - - private DefaultTargetPlatformConfigurationReader configurationReader; - - @Override - protected void setUp() throws Exception { - super.setUp(); - configurationReader = lookup(DefaultTargetPlatformConfigurationReader.class); - } - - @Override - protected void tearDown() throws Exception { - configurationReader = null; - super.tearDown(); - } - - @Test - public void testExtraRequirementMissingVersionRange() throws Exception { - Xpp3Dom dom = createConfigurationDom("type", "id"); - try { - configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom); - fail(); - } catch (BuildFailureException e) { - assertTrue(e.getMessage() - .contains("Element is missing in section.")); - } - } - - @Test - public void testExtraRequirementMissingType() throws Exception { - Xpp3Dom dom = createConfigurationDom("id", "versionRange"); - try { - configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom); - fail(); - } catch (BuildFailureException e) { - assertTrue( - e.getMessage().contains("Element is missing in section.")); - } - } - - @Test - public void testExtraRequirementId() throws Exception { - Xpp3Dom dom = createConfigurationDom("type", "versionRange"); - try { - configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom); - fail(); - } catch (BuildFailureException e) { - assertTrue(e.getMessage().contains("Element is missing in section.")); - } - } - - @Test() - public void testAddTargetWithValidMissingTargetDefinition() { - Xpp3Dom dom = createGavConfiguration("myGroupId", "myArtifactId", "myVersion"); - MavenSession session = setupMockSession(); - TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); - try { - configurationReader.addTargetArtifact(configuration, session, new MavenProject(), dom); - } catch (TargetPlatformConfigurationException e) { - assertMessageContains(e, "No target definition file(s) found in project"); - } - } - - private void assertMessageContains(Throwable throwable, String string) { - if (throwable == null) { - fail("Message " + string + " was not found in the exception stack!"); - } - if (throwable.getMessage().contains(string)) { - return; - } - assertMessageContains(throwable.getCause(), string); - } - - @Test - public void testAddTargetWithMissingVersionInTargetDefinition() throws MojoExecutionException { - Xpp3Dom dom = createGavConfiguration("myGroupId", "myArtifactId", null); - MavenSession session = setupMockSession(); - TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); - try { - configurationReader.addTargetArtifact(configuration, session, null, dom); - fail(); - } catch (TargetPlatformConfigurationException e) { - assertTrue(e.getMessage().contains("The target artifact configuration is invalid")); - } - } - - @Test - public void testAddTargetWithMissingGroupInTargetDefinition() throws MojoExecutionException { - Xpp3Dom dom = createGavConfiguration(null, "myArtifactId", "myVersion"); - MavenSession session = setupMockSession(); - TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); - try { - configurationReader.addTargetArtifact(configuration, session, null, dom); - fail(); - } catch (TargetPlatformConfigurationException e) { - assertTrue(e.getMessage().contains("The target artifact configuration is invalid")); - } - } - - @Test - public void testAddTargetWithMissingArtifactIdInTargetDefinition() throws MojoExecutionException { - Xpp3Dom dom = createGavConfiguration("myGroupId", null, "myVersion"); - MavenSession session = setupMockSession(); - TargetPlatformConfiguration configuration = new TargetPlatformConfiguration(); - try { - configurationReader.addTargetArtifact(configuration, session, null, dom); - fail(); - } catch (TargetPlatformConfigurationException e) { - assertTrue(e.getMessage().contains("The target artifact configuration is invalid")); - } - } - - @Test - public void testOptionalResolution() throws MojoExecutionException { - Xpp3Dom dom = createConfigurationDom(); - Xpp3Dom res = new Xpp3Dom(DefaultTargetPlatformConfigurationReader.DEPENDENCY_RESOLUTION); - Xpp3Dom opt = new Xpp3Dom(DefaultTargetPlatformConfigurationReader.OPTIONAL_DEPENDENCIES); - opt.setValue("optional"); - res.addChild(opt); - dom.addChild(res); - try { - configurationReader.readDependencyResolutionConfiguration(new TargetPlatformConfiguration(), dom, null); - } catch (BuildFailureException e) { - fail(e.getMessage()); - } - } - - private MavenSession setupMockSession() { - MavenSession session = mock(MavenSession.class); - MavenProject project = mock(MavenProject.class); - when(session.getProjects()).thenReturn(Arrays.asList(project)); - when(project.getGroupId()).thenReturn("myGroupId"); - when(project.getArtifactId()).thenReturn("myArtifactId"); - when(project.getVersion()).thenReturn("myVersion"); - when(project.getBasedir()).thenReturn(new File("/basedir/")); - return session; - } - - private Xpp3Dom createGavConfiguration(String groupId, String artifactId, String version) { - Xpp3Dom dom = new Xpp3Dom("artifact"); - if (groupId != null) { - Xpp3Dom group = new Xpp3Dom("groupId"); - group.setValue(groupId); - dom.addChild(group); - } - if (artifactId != null) { - Xpp3Dom artifact = new Xpp3Dom("artifactId"); - artifact.setValue(artifactId); - dom.addChild(artifact); - } - if (version != null) { - Xpp3Dom ver = new Xpp3Dom("version"); - ver.setValue(version); - dom.addChild(ver); - } - return dom; - } - - private Xpp3Dom createConfigurationDom(String... requirementChildren) { - Xpp3Dom dom = new Xpp3Dom("configuration"); - Xpp3Dom extraRequirements = new Xpp3Dom("extraRequirements"); - Xpp3Dom requirement = new Xpp3Dom("requirement"); - extraRequirements.addChild(requirement); - dom.addChild(extraRequirements); - for (String requirementChild : requirementChildren) { - requirement.addChild(new Xpp3Dom(requirementChild)); - } - return dom; - } - -} diff --git a/tycho-core/src/test/java/org/eclipse/tycho/core/test/TychoTest.java b/tycho-core/src/test/java/org/eclipse/tycho/core/test/TychoTest.java deleted file mode 100644 index a5de9c7016..0000000000 --- a/tycho-core/src/test/java/org/eclipse/tycho/core/test/TychoTest.java +++ /dev/null @@ -1,315 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008, 2020 Sonatype Inc. and others. - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Sonatype Inc. - initial API and implementation - *******************************************************************************/ -package org.eclipse.tycho.core.test; - -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Properties; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Dependency; -import org.apache.maven.plugin.testing.SilentLog; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.logging.Logger; -import org.eclipse.tycho.ClasspathEntry; -import org.eclipse.tycho.TargetEnvironment; -import org.eclipse.tycho.core.TargetPlatformConfiguration; -import org.eclipse.tycho.core.TychoProject; -import org.eclipse.tycho.core.osgitools.DefaultReactorProject; -import org.eclipse.tycho.core.osgitools.OsgiBundleProject; -import org.eclipse.tycho.core.resolver.DefaultTargetPlatformConfigurationReader; -import org.eclipse.tycho.core.resolver.TargetPlatformConfigurationException; -import org.eclipse.tycho.p2.resolver.ResolverException; -import org.eclipse.tycho.testing.AbstractTychoMojoTestCase; -import org.eclipse.tycho.version.TychoVersion; - -public class TychoTest extends AbstractTychoMojoTestCase { - - protected Logger logger; - private Properties properties; - - @Override - protected void setUp() throws Exception { - super.setUp(); - logger = new SilentLog(); - properties = new Properties(); - properties.setProperty("tycho-version", TychoVersion.getTychoVersion()); - } - - @Override - protected void tearDown() throws Exception { - logger = null; - super.tearDown(); - } - - public void testModuleOrder() throws Exception { - File basedir = getBasedir("projects/moduleorder"); - - List projects = getSortedProjects(basedir); - assertEquals(5, projects.size()); - - MavenProject p002 = projects.get(1); - MavenProject p001 = projects.get(2); - MavenProject p004 = projects.get(3); // feature - MavenProject p003 = projects.get(4); // site - - assertEquals("moduleorder.p001", p001.getArtifactId()); - assertEquals("moduleorder.p002", p002.getArtifactId()); - assertEquals("moduleorder.p003", p003.getArtifactId()); - assertEquals("moduleorder.p004", p004.getArtifactId()); - } - - public void testResolutionError() throws Exception { - File basedir = getBasedir("projects/resolutionerror/p001"); - - try { - getSortedProjects(basedir); - fail(); - } catch (Exception e) { - assertResolveError(e, - "Missing requirement: moduleorder.p001 0.0.1 requires 'java.package; moduleorder.p002 0.0.0' but it could not be found"); - } - } - - private void assertResolveError(Throwable e, String string) { - if (e instanceof ResolverException re) { - String details = re.getDetails(); - assertTrue(string + " not found in details: " + details, details.contains(string)); - return; - } - if (e.getCause() != null) { - assertResolveError(e.getCause(), string); - return; - } - fail("Resolve error was not found: " + string); - } - - public void testFeatureMissingFeature() throws Exception { - File basedir = getBasedir("projects/resolutionerror/feature_missing_feature"); - try { - getSortedProjects(basedir); - fail(); - } catch (Exception e) { - assertResolveError(e, - "feature_missing_feature.feature.group 1.0.0 requires 'org.eclipse.equinox.p2.iu; feature.not.found.feature.group 0.0.0' but it could not be found"); - } - } - - public void testFeatureMissingPlugin() throws Exception { - File basedir = getBasedir("projects/resolutionerror/feature_missing_plugin"); - try { - getSortedProjects(basedir); - fail(); - } catch (Exception e) { - assertResolveError(e, - "feature_missing_feature.feature.group 1.0.0 requires 'org.eclipse.equinox.p2.iu; plugin.not.found 0.0.0' but it could not be found"); - } - } - - public void testProjectPriority() throws Exception { - File basedir = getBasedir("projects/projectpriority"); - List projects = getSortedProjects(basedir, properties); - - MavenProject p002 = projects.get(2); - - List dependencies = p002.getModel().getDependencies(); - Dependency dependency = dependencies.get(0); - assertEquals("0.0.1", dependency.getVersion()); - } - - public void testMissingClasspathEntries() throws Exception { - File basedir = getBasedir("projects/missingentry"); - MavenProject project = getSortedProjects(basedir, properties).get(0); - - OsgiBundleProject projectType = (OsgiBundleProject) lookup(TychoProject.class, project.getPackaging()); - - List classpath = projectType.getClasspath(DefaultReactorProject.adapt(project)); - - assertEquals(3, classpath.size()); - assertEquals(1, classpath.get(0).getLocations().size()); - assertEquals(canonicalFile("src/test/resources/targetplatforms/missingentry/plugins/dirbundle_0.0.1"), - classpath.get(0).getLocations().get(0).getCanonicalFile()); - assertEquals(1, classpath.get(1).getLocations().size()); - assertEquals(canonicalFile("src/test/resources/targetplatforms/missingentry/plugins/jarbundle_0.0.1.jar"), - classpath.get(1).getLocations().get(0).getCanonicalFile()); - } - - private File canonicalFile(String path) throws IOException { - return new File(path).getCanonicalFile(); - } - - public void testBundleExtraClasspath() throws Exception { - File basedir = getBasedir("projects/extraclasspath"); - List projects = getSortedProjects(basedir, properties); - assertEquals(3, projects.size()); - - MavenProject b02 = projects.get(2); - - OsgiBundleProject projectType = (OsgiBundleProject) lookup(TychoProject.class, b02.getPackaging()); - - List classpath = projectType.getClasspath(DefaultReactorProject.adapt(b02)); - - assertClasspathContains(classpath, canonicalFile( - "target/local-repo/.cache/tycho/org.eclipse.equinox.launcher_1.0.101.R34x_v20081125.jar/launcher.properties")); - assertClasspathContains(classpath, - canonicalFile("target/projects/extraclasspath/b01/target/lib/nested.jar-classes")); - assertClasspathContains(classpath, canonicalFile( - "src/test/resources/targetplatforms/basic/plugins/org.eclipse.equinox.launcher_1.0.101.R34x_v20081125.jar")); - assertClasspathContains(classpath, new File(basedir, "b02/classes")); - assertClasspathContains(classpath, canonicalFile("target/projects/extraclasspath/b02/target/classes")); - } - - private void assertClasspathContains(List classpath, File file) throws IOException { - for (ClasspathEntry cpe : classpath) { - for (File cpfile : cpe.getLocations()) { - if (cpfile.getCanonicalFile().equals(file.getCanonicalFile())) { - return; - } - } - } - - fail("File " + file + " not found on the classpath"); - - } - - public void testImplicitTargetEnvironment() throws Exception { - File basedir = getBasedir("projects/implicitenvironment/simple"); - - List projects = getSortedProjects(basedir); - assertEquals(1, projects.size()); - -// assertEquals("ambiguous", projects.get(0).getArtifactId()); -// assertEquals("none", projects.get(0).getArtifactId()); - assertEquals("simple", projects.get(0).getArtifactId()); - - DefaultTargetPlatformConfigurationReader resolver = lookup(DefaultTargetPlatformConfigurationReader.class); - - MavenSession session; - TargetPlatformConfiguration configuration; - List environments; - - // ambiguous -// session = newMavenSession(projects.get(0), projects); -// configuration = resolver.getTargetPlatformConfiguration(session, session.getCurrentProject()); -// environments = configuration.getEnvironments(); -// assertEquals(0, environments.size()); - - // none -// session = newMavenSession(projects.get(0), projects); -// configuration = resolver.getTargetPlatformConfiguration(session, session.getCurrentProject()); -// environments = configuration.getEnvironments(); -// assertEquals(0, environments.size()); - - // simple - session = newMavenSession(projects.get(0), projects); - configuration = resolver.getTargetPlatformConfiguration(session, session.getCurrentProject()); - environments = configuration.getEnvironments(); - assertEquals(1, environments.size()); - TargetEnvironment env = environments.get(0); - assertEquals("foo", env.getOs()); - assertEquals("bar", env.getWs()); - assertEquals("munchy", env.getArch()); - } - - public void testWithValidExplicitTargetEnvironment() throws Exception { - File basedir = getBasedir("projects/explicitenvironment/valid"); - - List projects = getSortedProjects(basedir); - assertEquals(1, projects.size()); - - assertEquals("valid", projects.get(0).getArtifactId()); - - DefaultTargetPlatformConfigurationReader resolver = lookup(DefaultTargetPlatformConfigurationReader.class); - - MavenSession session = newMavenSession(projects.get(0), projects); - - TargetPlatformConfiguration configuration; - List environments; - - configuration = resolver.getTargetPlatformConfiguration(session, session.getCurrentProject()); - environments = configuration.getEnvironments(); - assertEquals(1, environments.size()); - TargetEnvironment env = environments.get(0); - assertEquals("linux", env.getOs()); - assertEquals("gtk", env.getWs()); - assertEquals("arm", env.getArch()); - } - - public void testWithMissingOsInExplicitTargetEnvironment() throws Exception { - File basedir = getBasedir("projects/explicitenvironment/missingOs"); - try { - getSortedProjects(basedir); - fail("RuntimeException must be thrown when is missing in the target configuration (environment element)"); - } catch (RuntimeException e) { - assertTrue(e.getMessage().contains( - "target-platform-configuration error in project explicitenvironment:missingos:eclipse-plugin")); - Throwable cause = e; - while (cause.getCause() != null) { - cause = cause.getCause(); - } - assertTrue(cause instanceof TargetPlatformConfigurationException); - assertEquals(" element is missing within target-platform-configuration (element )", - cause.getMessage()); - } - } - - public void testWithMissingWsInExplicitTargetEnvironment() throws Exception { - File basedir = getBasedir("projects/explicitenvironment/missingWs"); - try { - getSortedProjects(basedir); - fail("RuntimeException must be thrown when is missing in the target configuration (environment element)"); - } catch (RuntimeException e) { - assertTrue(e.getMessage().contains( - "target-platform-configuration error in project explicitenvironment:missingws:eclipse-plugin")); - Throwable cause = e; - while (cause.getCause() != null) { - cause = cause.getCause(); - } - assertTrue(cause instanceof TargetPlatformConfigurationException); - assertEquals(" element is missing within target-platform-configuration (element )", - cause.getMessage()); - } - } - - public void testWithMissingArchInExplicitTargetEnvironment() throws Exception { - File basedir = getBasedir("projects/explicitenvironment/missingArch"); - try { - getSortedProjects(basedir); - fail("RuntimeException must be thrown when is missing in the target configuration (environment element)"); - } catch (RuntimeException e) { - assertTrue(e.getMessage().contains( - "target-platform-configuration error in project explicitenvironment:missingarch:eclipse-plugin")); - Throwable cause = e; - while (cause.getCause() != null) { - cause = cause.getCause(); - } - assertTrue(cause instanceof TargetPlatformConfigurationException); - assertEquals(" element is missing within target-platform-configuration (element )", - cause.getMessage()); - } - } - - public void testWithProjectReferencesItself() throws Exception { - //Does not work anymore -// File basedir = getBasedir("projects/referencesItself"); -// try { -// getSortedProjects(basedir); -// fail(); -// } catch (Exception e) { -// assertTrue(e.getMessage().contains("Bundle referencesItself cannot be resolved")); -// } - - } - -} diff --git a/tycho-eclipse-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java b/tycho-eclipse-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java deleted file mode 100644 index 17e1fe20cd..0000000000 --- a/tycho-eclipse-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java +++ /dev/null @@ -1,170 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2016 Bachmann electronic GmbH and others. - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Bachmann electronic GmbH - initial API and implementation - ******************************************************************************/ -package org.eclipse.tycho.extras.eclipserun; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.atomic.AtomicReference; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.testing.SilentLog; -import org.apache.maven.project.MavenProject; -import org.apache.maven.toolchain.java.DefaultJavaToolChain; -import org.eclipse.sisu.equinox.launching.EquinoxInstallation; -import org.eclipse.sisu.equinox.launching.EquinoxLauncher; -import org.eclipse.sisu.equinox.launching.LaunchConfiguration; -import org.eclipse.tycho.ExecutionEnvironmentConfiguration; -import org.eclipse.tycho.TargetPlatform; -import org.eclipse.tycho.core.maven.ToolchainProvider; -import org.eclipse.tycho.core.resolver.P2Resolver; -import org.eclipse.tycho.core.resolver.P2ResolverFactory; -import org.eclipse.tycho.eclipserun.EclipseRunMojo; -import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; -import org.eclipse.tycho.testing.AbstractTychoMojoTestCase; -import org.eclipse.tycho.version.TychoVersion; -import org.junit.rules.TemporaryFolder; - -public class EclipseRunMojoTest extends AbstractTychoMojoTestCase { - - private EclipseRunMojo runMojo; - private EquinoxInstallation installation; - private TemporaryFolder temporaryFolder; - private File workFolder; - private ToolchainProvider toolchainProvider; - - @Override - public void setUp() throws Exception { - super.setUp(); - - runMojo = (EclipseRunMojo) lookupMojo("org.eclipse.tycho", "tycho-eclipse-plugin", - TychoVersion.getTychoVersion(), "eclipse-run", null); - runMojo.setLog(new SilentLog()); - MavenSession mavenSession = newMavenSession(mock(MavenProject.class)); - configureMojoWithDefaultConfiguration(runMojo, mavenSession, "eclipse-run"); - - installation = mock(EquinoxInstallation.class); - temporaryFolder = new TemporaryFolder(); - temporaryFolder.create(); - MavenProject project = mock(MavenProject.class); - setVariableValueToObject(runMojo, "project", project); - toolchainProvider = mock(ToolchainProvider.class); - setVariableValueToObject(runMojo, "toolchainProvider", toolchainProvider); - workFolder = new File(temporaryFolder.getRoot(), "work"); - setVariableValueToObject(runMojo, "work", workFolder); - setVariableValueToObject(runMojo, "launcher", mock(EquinoxLauncher.class)); - setVariableValueToObject(runMojo, "repositories", List.of()); - when(installation.getLocation()).thenReturn(new File("installpath")); - } - - @Override - protected void tearDown() throws Exception { - temporaryFolder.delete(); - super.tearDown(); - } - - public void testCreateCommandlineWithJvmArgs() throws IllegalAccessException, MojoExecutionException { - List args = Arrays.asList("-Xdebug", "-DanotherOptionWithValue=theValue", - "-DoptionWith=\"A space in the value\""); - setVariableValueToObject(runMojo, "jvmArgs", args); - setVariableValueToObject(runMojo, "argLine", "-DoldArgLineOption"); - LaunchConfiguration commandLine = runMojo.createCommandLine(installation); - List vmArgs = Arrays.asList(commandLine.getVMArguments()); - assertTrue(vmArgs.contains("-Xdebug")); - assertTrue(vmArgs.contains("-DanotherOptionWithValue=theValue")); - assertTrue(vmArgs.contains("-DoldArgLineOption")); - assertTrue(vmArgs.contains("-DoptionWith=\"A space in the value\"")); - } - - public void testCreateCommandlineWithApplicationArgs() throws IllegalAccessException, MojoExecutionException { - List args = Arrays.asList("arg1", "literal arg with spaces", "argument'with'literalquotes"); - setVariableValueToObject(runMojo, "appArgLine", "appArg1 \"literal appArg with spaces\""); - setVariableValueToObject(runMojo, "applicationArgs", args); - LaunchConfiguration commandLine = runMojo.createCommandLine(installation); - List programArgs = Arrays.asList(commandLine.getProgramArguments()); - assertTrue(programArgs.contains("arg1")); - assertTrue(programArgs.contains("appArg1")); - assertTrue(programArgs.contains("literal arg with spaces")); - assertTrue(programArgs.contains("literal appArg with spaces")); - assertTrue(programArgs.contains("argument'with'literalquotes")); - } - - public void testCreateCommandLineWithNullJvmArgs() throws MojoExecutionException { - LaunchConfiguration commandLine = runMojo.createCommandLine(installation); - assertEquals(0, commandLine.getVMArguments().length); - } - - public void testCreateCommandLineProgramArgs() throws MojoExecutionException { - LaunchConfiguration commandLine = runMojo.createCommandLine(installation); - List programArgs = Arrays.asList(commandLine.getProgramArguments()); - assertTrue(programArgs.containsAll(List.of("-install", installation.getLocation().getAbsolutePath(), // - "-configuration", new File(workFolder, "configuration").getAbsolutePath(), // - "-data", new File(workFolder, "data").getAbsolutePath() // - ))); - } - - public void testDataDirectoryIsClearedBeforeLaunching() throws IOException, MojoExecutionException { - File markerFile = new File(workFolder, "data/markerfile").getAbsoluteFile(); - markerFile.getParentFile().mkdirs(); - markerFile.createNewFile(); - assertTrue(markerFile.exists()); - runMojo.runEclipse(installation); - assertFalse(markerFile.exists()); - } - - public void testExecutionEnvironmentIsRespectedDuringDependencyResolution() throws Exception { - AtomicReference recordedExecutionEnvironmentConfiguration = new AtomicReference<>(); - - TargetPlatform mockTargetPlatform = mock(TargetPlatform.class); - TargetPlatformFactory mockTargetPlatformFactory = mock(TargetPlatformFactory.class); - when(mockTargetPlatformFactory.createTargetPlatform(any(), any(), any())).thenAnswer(invocation -> { - recordedExecutionEnvironmentConfiguration.set(invocation.getArgument(1)); - return mockTargetPlatform; - }); - - P2Resolver mockP2Resolver = mock(P2Resolver.class); - - P2ResolverFactory mockP2ResolverFactory = mock(P2ResolverFactory.class); - when(mockP2ResolverFactory.createResolver(any())).thenReturn(mockP2Resolver); - - setVariableValueToObject(runMojo, "resolverFactory", mockP2ResolverFactory); - setVariableValueToObject(runMojo, "platformFactory", mockTargetPlatformFactory); - - setVariableValueToObject(runMojo, "executionEnvironment", "custom-ee"); - - runMojo.execute(); - - assertEquals("custom-ee", recordedExecutionEnvironmentConfiguration.get().getProfileName()); - } - - public void testExecutionEnvironmentIsRespectedDuringEclipseExecution() throws Exception { - DefaultJavaToolChain mockToolchainForCustomEE = mock(DefaultJavaToolChain.class); - when(mockToolchainForCustomEE.findTool("java")).thenReturn("/path/to/custom-ee-jdk/bin/java"); - when(toolchainProvider.findMatchingJavaToolChain(any(), eq("custom-ee"))) - .thenReturn(mockToolchainForCustomEE); - - setVariableValueToObject(runMojo, "executionEnvironment", "custom-ee"); - - LaunchConfiguration commandLine = runMojo.createCommandLine(installation); - - assertEquals("/path/to/custom-ee-jdk/bin/java", commandLine.getJvmExecutable()); - } - -} diff --git a/tycho-extras/tycho-p2-extras-plugin/src/test/java/org/eclipse/tycho/plugins/p2/extras/MirrorMojoTest.java b/tycho-extras/tycho-p2-extras-plugin/src/test/java/org/eclipse/tycho/plugins/p2/extras/MirrorMojoTest.java deleted file mode 100644 index 658f6a5d5b..0000000000 --- a/tycho-extras/tycho-p2-extras-plugin/src/test/java/org/eclipse/tycho/plugins/p2/extras/MirrorMojoTest.java +++ /dev/null @@ -1,197 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010, 2022 SAP AG and others. - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Jan Sievers - initial API and implementation - *******************************************************************************/ -package org.eclipse.tycho.plugins.p2.extras; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.LegacySupport; -import org.apache.maven.plugin.Mojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; -import org.apache.maven.repository.RepositorySystem; -import org.eclipse.core.runtime.adaptor.EclipseStarter; -import org.eclipse.tycho.testing.AbstractTychoMojoTestCase; -import org.junit.Assert; - -public class MirrorMojoTest extends AbstractTychoMojoTestCase { - - private File mirrorDestinationDir; - private Mojo mirrorMojo; - - @Override - protected void setUp() throws Exception { - super.setUp(); - File basedir = getBasedir("mirroring/testProject"); - List projects = getSortedProjects(basedir); - MavenProject project = projects.get(0); - initLegacySupport(projects, project); - mirrorDestinationDir = new File(project.getFile().getParent(), "target/repository").getCanonicalFile(); - if (mirrorDestinationDir.exists()) { - deleteFolder(mirrorDestinationDir.toPath()); - } - mirrorMojo = lookupMojo("mirror", project.getFile()); - setVariableValueToObject(mirrorMojo, "destination", mirrorDestinationDir); - setVariableValueToObject(mirrorMojo, "project", project); - } - - private static void deleteFolder(final Path path) throws IOException { - Files.walkFileTree(path, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFileFailed(final Path file, final IOException e) { - return FileVisitResult.TERMINATE; - } - - @Override - public FileVisitResult postVisitDirectory(final Path dir, final IOException e) throws IOException { - if (e != null) { - return FileVisitResult.TERMINATE; - } - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } - - @Override - protected void tearDown() throws Exception { - // this is needed because the DefaultEquinoxEmbedder plexus component - // is not disposed - EclipseStarter.shutdown(); - System.clearProperty("org.osgi.framework.vendor"); - } - - public void testMirrorSpecificIUFromP2Repo() throws Exception { - File sourceRepository = new File("src/test/resources/mirroring/sourceP2Repo").getCanonicalFile(); - setVariableValueToObject(mirrorMojo, "source", - Collections.singletonList(new Repository(sourceRepository.toURI()))); - Iu testBundleIu = new Iu(); - testBundleIu.id = "test.bundle1"; - setVariableValueToObject(mirrorMojo, "ius", Collections.singletonList(testBundleIu)); - mirrorMojo.execute(); - assertTrue(mirrorDestinationDir.isDirectory()); - assertEquals(1, new File(mirrorDestinationDir, "plugins").listFiles().length); - assertMirroredBundle(mirrorDestinationDir, "test.bundle1", "1.0.0.201108100850"); - } - - public void testMirrorProduct() throws Exception { - File sourceRepository = new File("src/test/resources/mirroring/sourceP2RepoWithProduct").getCanonicalFile(); - setVariableValueToObject(mirrorMojo, "source", - Collections.singletonList(new Repository(sourceRepository.toURI()))); - Iu testBundleIu = new Iu(); - testBundleIu.id = "dummy"; - setVariableValueToObject(mirrorMojo, "ius", Collections.singletonList(testBundleIu)); - mirrorMojo.execute(); - assertTrue(mirrorDestinationDir.isDirectory()); - assertTrue(Arrays.stream(new File(mirrorDestinationDir, "binary").listFiles()).map(File::getName) - .anyMatch(name -> name.startsWith("dummy"))); - } - - public void testMirrorWithPlatformFilter() throws Exception { - File sourceRepository = new File("src/test/resources/mirroring/sourceP2Repo").getCanonicalFile(); - setVariableValueToObject(mirrorMojo, "source", - Collections.singletonList(new Repository(sourceRepository.toURI()))); - Iu featureIU = new Iu(); - featureIU.id = "test.feature.feature.group"; - setVariableValueToObject(mirrorMojo, "ius", Collections.singletonList(featureIU)); - Map filter = new HashMap<>(); - filter.put("osgi.os", "linux"); - filter.put("osgi.ws", "gtk"); - filter.put("osgi.arch", "x86_64"); - setVariableValueToObject(mirrorMojo, "filter", filter); - mirrorMojo.execute(); - assertTrue(mirrorDestinationDir.isDirectory()); - // win32 fragment must not mirrored because platform filter does not match - assertEquals(2, new File(mirrorDestinationDir, "plugins").listFiles().length); - assertMirroredBundle(mirrorDestinationDir, "test.bundle1", "1.0.0.201108100850"); - assertMirroredBundle(mirrorDestinationDir, "test.bundle2", "1.0.0.201108100850"); - } - - public void testTargetPlatformAsSource() throws Exception { - Iu featureIU = new Iu(); - featureIU.id = "test.feature.feature.group"; - setVariableValueToObject(mirrorMojo, "ius", Collections.singletonList(featureIU)); - setVariableValueToObject(mirrorMojo, "targetPlatformAsSource", Boolean.TRUE); - try { - // Source is allowed to be empty, for example when targetPlatformAsSource is set, but in this test - // project we have no target platform so it should fail gracefully instead of throwing a NPE - mirrorMojo.execute(); - fail(); - } catch (MojoExecutionException e) { - assertEquals(e.getMessage(), "No repository provided as 'source'"); - } - } - - private static void assertMirroredBundle(File publishedContentDir, String bundleID, String version) { - assertMirroredArtifact(publishedContentDir, bundleID, version, "plugins"); - } - - private static void assertMirroredArtifact(File publishedContentDir, String id, String version, String folder) { - String pluginArtifactNamePrefix = id + "_" + version; // without qualifier - for (File bundle : new File(publishedContentDir, folder).listFiles()) { - if (bundle.getName().startsWith(pluginArtifactNamePrefix)) - return; - } - - Assert.fail("Published artifact not found: " + pluginArtifactNamePrefix); - } - - private void initLegacySupport(List projects, MavenProject currentProject) throws Exception { - MavenSession session = newMavenSession(currentProject, projects); - LegacySupport buildContext = lookup(LegacySupport.class); - buildContext.setSession(session); - } - - // use the normal local Maven repository (called by newMavenSession) - @Override - protected ArtifactRepository getLocalRepository() throws Exception { - RepositorySystem repoSystem = lookup(RepositorySystem.class); - File path = getLocalMavenRepository().getCanonicalFile(); - ArtifactRepository r = repoSystem.createLocalRepository(path); - return r; - } - - private File getLocalMavenRepository() { - /* - * The build (more specifically, the maven-properties-plugin) writes the local Maven - * repository location to a file. Here, we read this file. (Approach copied from tycho-its.) - */ - Properties buildProperties = new Properties(); - try (InputStream is = this.getClassLoader().getResourceAsStream("baseTest.properties")) { - buildProperties.load(is); - } catch (IOException e) { - throw new RuntimeException(e); - } - return new File(buildProperties.getProperty("local-repo")); - } -} diff --git a/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildnumber/test/PackageFeatureMojoTest.java b/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildnumber/test/PackageFeatureMojoTest.java deleted file mode 100644 index 9c256f7b5f..0000000000 --- a/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildnumber/test/PackageFeatureMojoTest.java +++ /dev/null @@ -1,130 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008, 2021 Sonatype Inc. and others. - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Sonatype Inc. - initial API and implementation - *******************************************************************************/ -package org.eclipse.tycho.buildnumber.test; - -import java.io.File; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Properties; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -import org.apache.commons.io.IOUtils; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.project.MavenProject; -import org.eclipse.tycho.model.Feature; -import org.eclipse.tycho.packaging.PackageFeatureMojo; -import org.eclipse.tycho.testing.AbstractTychoMojoTestCase; -import org.eclipse.tycho.version.TychoVersion; - -public class PackageFeatureMojoTest extends AbstractTychoMojoTestCase { - - Properties properties = new Properties(); - - public PackageFeatureMojoTest() { - properties.setProperty("tycho-version", TychoVersion.getTychoVersion()); - } - - public void testLicenseFeature() throws Exception { - File basedir = new File(getBasedir("projects/licenseFeature"), "feature"); - File platform = new File("src/test/resources/projects/licenseFeature/eclipse"); - List projects = getSortedProjects(basedir, properties); - - MavenProject project = getProject(projects, "licenseFeature.feature"); - MavenSession session = newMavenSession(project, projects); - // set build qualifier - lookupMojoWithDefaultConfiguration(project, session, "build-qualifier").execute(); - - PackageFeatureMojo mojo = (PackageFeatureMojo) lookupMojo("package-feature", project.getFile()); - setVariableValueToObject(mojo, "project", project); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "finalName", "feature"); - setVariableValueToObject(mojo, "featureFile", new File(project.getBasedir(), "feature.xml")); - - mojo.execute(); - - try (ZipFile zip = new ZipFile(new File(basedir, "target/feature.jar"))) { - // igorf: input streams are closed by zip.close() at the end, sloppy but should work - - // all bin.includes files from license features are included - assertNotNull(zip.getEntry("file-license.txt")); - assertNull(zip.getEntry("file-unlicense.txt")); - - // do not leak build.properties into 'this' feature - assertNull(zip.getEntry("build.properties")); - - // license feature id/version are stripped off - Feature feature = Feature.read(zip.getInputStream(zip.getEntry(Feature.FEATURE_XML))); - assertNull(feature.getLicenseFeature()); - assertNull(feature.getLicenseFeatureVersion()); - assertEquals("http://www.foo.bar", feature.getLicenseURL()); - assertEquals("This is the license", feature.getLicense().trim()); - - // feature.properties merged - Properties p = new Properties(); - p.load(zip.getInputStream(zip.getEntry("feature.properties"))); - assertEquals("test property value", p.getProperty("test")); - assertEquals("license test property value", p.getProperty("license-test")); - - // when present both in 'this' and license feature, files from 'this' feature are included - assertEquals("file.txt contents", - IOUtils.toString(zip.getInputStream(zip.getEntry("file.txt")), Charset.defaultCharset())); - } - } - - public void testAddMavenDescriptorNotAddedPerDefault() throws Exception { - File basedir = getBasedir("projects/addMavenDescriptor/featureDefault/"); - File platform = new File("src/test/resources/eclipse"); - List projects = getSortedProjects(basedir, properties); - - MavenProject project = getProject(projects, "featureDefault"); - MavenSession session = newMavenSession(project, projects); - // set build qualifier - lookupMojoWithDefaultConfiguration(project, session, "build-qualifier").execute(); - - PackageFeatureMojo mojo = (PackageFeatureMojo) lookupMojo("package-feature", project.getFile()); - setVariableValueToObject(mojo, "project", project); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "featureFile", new File(project.getBasedir(), "feature.xml")); - - mojo.execute(); - - try (ZipFile zip = new ZipFile(new File(basedir, "target/featureDefault.jar"))) { - ZipEntry entry = zip.getEntry("META-INF/maven"); - assertNull("No 'META-INF/maven/' entry must be in the feature.jar!", entry); - } - } - - public void testAddMavenDescriptorSetToTrue() throws Exception { - File basedir = getBasedir("projects/addMavenDescriptor/featureForcedToTrue"); - File platform = new File("src/test/resources/eclipse"); - List projects = getSortedProjects(basedir, properties); - - MavenProject project = getProject(projects, "featureForcedToTrue"); - MavenSession session = newMavenSession(project, projects); - // set build qualifier - lookupMojoWithDefaultConfiguration(project, session, "build-qualifier").execute(); - - PackageFeatureMojo mojo = (PackageFeatureMojo) lookupMojo("package-feature", project.getFile()); - setVariableValueToObject(mojo, "project", project); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "featureFile", new File(project.getBasedir(), "feature.xml")); - - mojo.execute(); - - try (ZipFile zip = new ZipFile(new File(basedir, "target/featureForcedToTrue.jar"))) { - ZipEntry entry = zip.getEntry("META-INF/maven"); - assertNotNull("There must be a 'META-INF/maven/' entry in the feature.jar!", entry); - } - } -} diff --git a/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildnumber/test/PackageIUMojoTest.java b/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildnumber/test/PackageIUMojoTest.java deleted file mode 100644 index 83f2340590..0000000000 --- a/tycho-packaging-plugin/src/test/java/org/eclipse/tycho/buildnumber/test/PackageIUMojoTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 Rapicorp, Inc. and others. - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Rapicorp, Inc. - initial API and implementation - *******************************************************************************/ -package org.eclipse.tycho.buildnumber.test; - -import static org.eclipse.tycho.test.util.ArchiveContentUtil.getFilesInZip; - -import java.io.File; -import java.util.List; -import java.util.Properties; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.project.MavenProject; -import org.eclipse.tycho.model.IU; -import org.eclipse.tycho.packaging.PackageIUMojo; -import org.eclipse.tycho.testing.AbstractTychoMojoTestCase; -import org.eclipse.tycho.version.TychoVersion; - -import de.pdark.decentxml.Element; - -public class PackageIUMojoTest extends AbstractTychoMojoTestCase { - - Properties properties = new Properties(); - - public PackageIUMojoTest() { - properties.setProperty("tycho-version", TychoVersion.getTychoVersion()); - } - - public void testThatArtifactPayloadIsCorrect() throws Exception { - File basedir = getBasedir("projects/iuWithPayload/"); - File platform = new File("src/test/resources/eclipse"); - List projects = getSortedProjects(basedir, properties); - - MavenProject project = getProject(projects, "iuWithPayload"); - MavenSession session = newMavenSession(project, projects); - // set build qualifier - lookupMojoWithDefaultConfiguration(project, session, "build-qualifier").execute(); - - PackageIUMojo mojo = (PackageIUMojo) lookupConfiguredMojo(project, "package-iu"); - setVariableValueToObject(mojo, "artifactContentFolder", - new File(basedir, "src/main/resources").getAbsolutePath()); - mojo.execute(); - - assertTrue(getFilesInZip(new File(basedir, "target/iuWithPayload-1.0.0.zip")).contains("file.txt")); - } - - public void testArtifactWithoutPayload() throws Exception { - File basedir = getBasedir("projects/iuWithoutPayload"); - File platform = new File("src/test/resources/eclipse"); - List projects = getSortedProjects(basedir, properties); - - MavenProject project = getProject(projects, "iuWithoutPayload"); - MavenSession session = newMavenSession(project, projects); - // set build qualifier - lookupMojoWithDefaultConfiguration(project, session, "build-qualifier").execute(); - - PackageIUMojo mojo = (PackageIUMojo) lookupConfiguredMojo(project, "package-iu"); - mojo.execute(); - - IU iu = IU.loadIU(new File(basedir, "target")); - Element artifact = iu.getSelfArtifact(); - assertNull(artifact); - - assertTrue(new File(basedir, "target/iuWithoutPayload-1.0.0.zip").exists()); - } - - public void testInjectArtifactReference() throws Exception { - File basedir = getBasedir("projects/iuWithPayloadButNoArtifactReference"); - File platform = new File("src/test/resources/eclipse"); - List projects = getSortedProjects(basedir, properties); - - MavenProject project = getProject(projects, "iuWithPayloadButNoArtifactReference"); - MavenSession session = newMavenSession(project, projects); - // set build qualifier - lookupMojoWithDefaultConfiguration(project, session, "build-qualifier").execute(); - - PackageIUMojo mojo = (PackageIUMojo) lookupConfiguredMojo(project, "package-iu"); - setVariableValueToObject(mojo, "artifactContentFolder", - new File(basedir, "src/main/resources").getAbsolutePath()); - mojo.execute(); - - IU iu = IU.loadIU(new File(basedir, "target")); - Element artifact = iu.getSelfArtifact(); - assertNotNull(artifact); - assertEquals("binary", artifact.getAttributeValue("classifier")); - assertEquals("iuWithPayloadButNoArtifactReference", artifact.getAttributeValue("id")); - assertEquals("1.0.0", artifact.getAttributeValue("version")); - } -}