From 6a83ac6c2923f21e59666e8cd49434b257691dba Mon Sep 17 00:00:00 2001 From: juulhobert Date: Wed, 18 Oct 2023 20:06:47 +0200 Subject: [PATCH] [MNG-7772] Test for extensions.xml defined in an ext directory in the ${user.home}/.m2 folder (#311) --- .../java/org/apache/maven/it/ItUtils.java | 34 ++++++++ ...nITmng0553SettingsAuthzEncryptionTest.java | 16 +--- ...enITmng6326CoreExtensionsNotFoundTest.java | 12 +-- .../MavenITmng7772CoreExtensionFoundTest.java | 82 +++++++++++++++++++ ...enITmng7772CoreExtensionsNotFoundTest.java | 60 ++++++++++++++ .../apache/maven/it/TestSuiteOrdering.java | 2 + .../extension/pom.xml | 68 +++++++++++++++ .../extensions/MavenLifecycleParticipant.java | 40 +++++++++ .../resources/META-INF/maven/extension.xml | 29 +++++++ .../home-extensions-xml/.m2/extensions.xml | 28 +++++++ .../home-lib-ext/.m2/ext/.gitkeep | 0 .../mng-7772-core-extensions-found/pom.xml | 32 ++++++++ .../home/.m2/extensions.xml | 28 +++++++ .../pom.xml | 32 ++++++++ 14 files changed, 439 insertions(+), 24 deletions(-) create mode 100644 its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionFoundTest.java create mode 100644 its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionsNotFoundTest.java create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/pom.xml create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/java/org/apache/maven/its/extensions/MavenLifecycleParticipant.java create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/resources/META-INF/maven/extension.xml create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/home-extensions-xml/.m2/extensions.xml create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/home-lib-ext/.m2/ext/.gitkeep create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/pom.xml create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/home/.m2/extensions.xml create mode 100644 its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/pom.xml diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/ItUtils.java b/its/core-it-suite/src/test/java/org/apache/maven/it/ItUtils.java index 786d501ea2..d6b3812dff 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/ItUtils.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/ItUtils.java @@ -18,11 +18,18 @@ */ package org.apache.maven.it; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.DigestInputStream; import java.security.MessageDigest; +import org.apache.maven.shared.verifier.Verifier; + /** * @author Benjamin Bentmann */ @@ -56,4 +63,31 @@ class ItUtils { return hash.toString(); } + + public static String getLogContent(Verifier verifier) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Files.copy(Paths.get(verifier.getBasedir(), verifier.getLogFileName()), baos); + return baos.toString(); + } + + /** + * @see ItUtils#setUserHome(Verifier, Path) + */ + public static void setUserHome(Verifier verifier, File file) { + setUserHome(verifier, file.toPath()); + } + + /** + * Note that this only has effect when fork mode is set to true. + * Please make sure to call {@link Verifier#setForkJvm(boolean)} and set it to true + */ + public static void setUserHome(Verifier verifier, Path home) { + // NOTE: We set the user.home directory instead of say settings.security to reflect Maven's normal behavior + String path = home.toAbsolutePath().toString(); + if (path.indexOf(' ') < 0) { + verifier.setEnvironmentVariable("MAVEN_OPTS", "-Duser.home=" + path); + } else { + verifier.setEnvironmentVariable("MAVEN_OPTS", "\"-Duser.home=" + path + "\""); + } + } } diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng0553SettingsAuthzEncryptionTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng0553SettingsAuthzEncryptionTest.java index 9508923a8d..b360619d4c 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng0553SettingsAuthzEncryptionTest.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng0553SettingsAuthzEncryptionTest.java @@ -123,7 +123,7 @@ public class MavenITmng0553SettingsAuthzEncryptionTest extends AbstractMavenInte verifier.deleteArtifacts("org.apache.maven.its.mng0553"); verifier.verifyArtifactNotPresent("org.apache.maven.its.mng0553", "a", "0.1-SNAPSHOT", "jar"); verifier.filterFile("settings-template.xml", "settings.xml", "UTF-8", filterProps); - setUserHome(verifier, new File(testDir, "userhome")); + ItUtils.setUserHome(verifier, new File(testDir, "userhome")); verifier.addCliArgument("--settings"); verifier.addCliArgument("settings.xml"); verifier.addCliArgument("validate"); @@ -186,7 +186,7 @@ public class MavenITmng0553SettingsAuthzEncryptionTest extends AbstractMavenInte Verifier verifier = newVerifier(testDir.getAbsolutePath()); verifier.setAutoclean(false); - setUserHome(verifier, new File(testDir, "userhome")); + ItUtils.setUserHome(verifier, new File(testDir, "userhome")); verifier.addCliArgument("--encrypt-master-password"); verifier.addCliArgument("test"); verifier.setLogFileName("log-emp.txt"); @@ -199,7 +199,7 @@ public class MavenITmng0553SettingsAuthzEncryptionTest extends AbstractMavenInte verifier = newVerifier(testDir.getAbsolutePath()); verifier.setAutoclean(false); - setUserHome(verifier, new File(testDir, "userhome")); + ItUtils.setUserHome(verifier, new File(testDir, "userhome")); verifier.addCliArgument("--encrypt-password"); verifier.addCliArgument("testpass"); verifier.setLogFileName("log-ep.txt"); @@ -220,14 +220,4 @@ public class MavenITmng0553SettingsAuthzEncryptionTest extends AbstractMavenInte return null; } - - private void setUserHome(Verifier verifier, File home) { - // NOTE: We set the user.home directory instead of say settings.security to reflect Maven's normal behavior - String path = home.getAbsolutePath(); - if (path.indexOf(' ') < 0) { - verifier.setEnvironmentVariable("MAVEN_OPTS", "-Duser.home=" + path); - } else { - verifier.setEnvironmentVariable("MAVEN_OPTS", "\"-Duser.home=" + path + "\""); - } - } } diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6326CoreExtensionsNotFoundTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6326CoreExtensionsNotFoundTest.java index b686a285fc..de0596c617 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6326CoreExtensionsNotFoundTest.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6326CoreExtensionsNotFoundTest.java @@ -18,11 +18,7 @@ */ package org.apache.maven.it; -import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; import org.apache.maven.shared.verifier.VerificationException; import org.apache.maven.shared.verifier.Verifier; @@ -53,14 +49,8 @@ public class MavenITmng6326CoreExtensionsNotFoundTest extends AbstractMavenInteg verifier.verifyTextInLog( "Extension org.apache.maven.its.it-core-extensions:maven-it-unknown-extensions:0.1 or one of its dependencies could not be resolved"); } catch (VerificationException e2) { - throw new VerificationException(e2.getMessage() + "\nLog:" + getLogContent(verifier)); + throw new VerificationException(e2.getMessage() + "\nLog:" + ItUtils.getLogContent(verifier)); } } } - - private String getLogContent(Verifier verifier) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Files.copy(Paths.get(verifier.getBasedir(), verifier.getLogFileName()), baos); - return baos.toString(); - } } diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionFoundTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionFoundTest.java new file mode 100644 index 0000000000..2d103b4cd4 --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionFoundTest.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.it; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.maven.shared.verifier.Verifier; +import org.apache.maven.shared.verifier.util.ResourceExtractor; +import org.junit.jupiter.api.Test; + +public class MavenITmng7772CoreExtensionFoundTest extends AbstractMavenIntegrationTestCase { + public MavenITmng7772CoreExtensionFoundTest() { + super("(4.0.0-alpha-7,)"); + } + + @Test + public void testWithExtensionsXmlCoreExtensionsFound() throws Exception { + File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-7772-core-extensions-found"); + + Verifier verifier = newVerifier(new File(testDir, "extension").getAbsolutePath()); + verifier.addCliArgument("install"); + verifier.execute(); + verifier.verifyErrorFreeLog(); + + verifier = newVerifier(testDir.getAbsolutePath()); + ItUtils.setUserHome(verifier, Paths.get(testDir.toPath().toString(), "home-extensions-xml")); + verifier.setForkJvm(true); + + verifier.addCliArgument("validate"); + verifier.execute(); + + verifier.verifyTextInLog("[INFO] Extension loaded!"); + } + + @Test + public void testWithLibExtCoreExtensionsFound() throws Exception { + File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-7772-core-extensions-found"); + + Verifier verifier = newVerifier(new File(testDir, "extension").getAbsolutePath()); + verifier.addCliArgument("package"); + verifier.execute(); + verifier.verifyErrorFreeLog(); + + Path jarPath = Paths.get(verifier.getArtifactPath( + "org.apache.maven.its.7772-core-extensions-scopes", "maven-it-core-extensions", "0.1", "jar", "")); + + assertNotNull("Jar output path was not found in the log", jarPath); + + Path jarToPath = Paths.get(testDir.toString(), "home-lib-ext", ".m2", "ext", "extension.jar"); + try { + Files.copy(jarPath, jarToPath); + + verifier = newVerifier(testDir.getAbsolutePath()); + ItUtils.setUserHome(verifier, Paths.get(testDir.toPath().toString(), "home-lib-ext")); + verifier.setForkJvm(true); + verifier.addCliArgument("validate"); + verifier.execute(); + verifier.verifyTextInLog("[INFO] Extension loaded!"); + } finally { + Files.deleteIfExists(jarToPath); + } + } +} diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionsNotFoundTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionsNotFoundTest.java new file mode 100644 index 0000000000..3177b8fe6d --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7772CoreExtensionsNotFoundTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.it; + +import java.io.File; +import java.nio.file.Paths; + +import org.apache.maven.shared.verifier.VerificationException; +import org.apache.maven.shared.verifier.Verifier; +import org.apache.maven.shared.verifier.util.ResourceExtractor; +import org.junit.jupiter.api.Test; + +/** + * This is a test set for MNG-7772: + * check that Maven fails if it cannot load core extensions contributed by ${user.home}/.m2/extensions.xml. + */ +public class MavenITmng7772CoreExtensionsNotFoundTest extends AbstractMavenIntegrationTestCase { + + public MavenITmng7772CoreExtensionsNotFoundTest() { + super("(4.0.0-alpha-7,)"); + } + + @Test + public void testCoreExtensionsNotFound() throws Exception { + File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/mng-7772-core-extensions-not-found"); + + Verifier verifier = newVerifier(testDir.getAbsolutePath()); + ItUtils.setUserHome(verifier, Paths.get(testDir.toPath().toString(), "home")); + + try { + verifier.addCliArgument("validate"); + verifier.execute(); + fail("should have failed "); + } catch (VerificationException e) { + try { + verifier.verifyTextInLog("[ERROR] Error executing Maven."); + verifier.verifyTextInLog( + "Extension org.apache.maven.its.it-core-extensions:maven-it-unknown-extensions:0.1 or one of its dependencies could not be resolved"); + } catch (VerificationException e2) { + throw new VerificationException(e2.getMessage() + "\nLog:" + ItUtils.getLogContent(verifier)); + } + } + } +} diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java index f544ef4c42..ebcefa9748 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java @@ -129,6 +129,8 @@ public class TestSuiteOrdering implements ClassOrderer { suite.addTestSuite(MavenITmng7819FileLockingWithSnapshotsTest.class); suite.addTestSuite(MavenITmng5659ProjectSettingsTest.class); suite.addTestSuite(MavenITmng5600DependencyManagementImportExclusionsTest.class); + suite.addTestSuite(MavenITmng7772CoreExtensionFoundTest.class); + suite.addTestSuite(MavenITmng7772CoreExtensionsNotFoundTest.class); suite.addTestSuite(MavenITmng7740ConsumerBuildShouldCleanUpOldFilesTest.class); suite.addTestSuite(MavenITmng7587Jsr330.class); suite.addTestSuite(MavenITmng7038RootdirTest.class); diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/pom.xml b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/pom.xml new file mode 100644 index 0000000000..74baa7bfa4 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/pom.xml @@ -0,0 +1,68 @@ + + + + 4.0.0 + + org.apache.maven.its.7772-core-extensions-scopes + maven-it-core-extensions + 0.1 + jar + + Maven Integration Test :: maven-it-core-extensions + Maven core extensions components that use @SessionScope and @MojoExecutionScope Guice scopes + + + + + + org.apache.maven + maven-core + 3.8.6 + provided + + + + javax.inject + javax.inject + 1 + provided + + + + + + + org.eclipse.sisu + sisu-maven-plugin + 0.3.5 + + + generate-index + + main-index + + + + + + + diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/java/org/apache/maven/its/extensions/MavenLifecycleParticipant.java b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/java/org/apache/maven/its/extensions/MavenLifecycleParticipant.java new file mode 100644 index 0000000000..d216b075b7 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/java/org/apache/maven/its/extensions/MavenLifecycleParticipant.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.its.extensions; + +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.AbstractMavenLifecycleParticipant; +import org.apache.maven.MavenExecutionException; +import org.apache.maven.execution.MavenSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Named("integrationTestExtension") +@Singleton +public class MavenLifecycleParticipant extends AbstractMavenLifecycleParticipant { + + private static final Logger log = LoggerFactory.getLogger(MavenLifecycleParticipant.class); + + @Override + public void afterSessionStart(MavenSession session) throws MavenExecutionException { + log.info("Extension loaded!"); + } +} diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/resources/META-INF/maven/extension.xml b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/resources/META-INF/maven/extension.xml new file mode 100644 index 0000000000..d7c88e463a --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/extension/src/main/resources/META-INF/maven/extension.xml @@ -0,0 +1,29 @@ + + + + + + + org.apache.maven.its.extensions + + + org.apache.maven.its.7772-core-extensions-scopes:maven-it-core-extensions + + \ No newline at end of file diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/home-extensions-xml/.m2/extensions.xml b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/home-extensions-xml/.m2/extensions.xml new file mode 100644 index 0000000000..eda7f0f1e7 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/home-extensions-xml/.m2/extensions.xml @@ -0,0 +1,28 @@ + + + + + + + org.apache.maven.its.7772-core-extensions-scopes + maven-it-core-extensions + 0.1 + + diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/home-lib-ext/.m2/ext/.gitkeep b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/home-lib-ext/.m2/ext/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/pom.xml b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/pom.xml new file mode 100644 index 0000000000..5807bd3063 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-found/pom.xml @@ -0,0 +1,32 @@ + + + + 4.0.0 + + org.apache.maven.its.it-core-extensions + test + 0.1 + jar + + Maven Integration Test :: it-core-extensions + Verify that Maven loads core extensions and components contributed by the extensions are available to regular + plugins. + + diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/home/.m2/extensions.xml b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/home/.m2/extensions.xml new file mode 100644 index 0000000000..2c7f5b0f04 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/home/.m2/extensions.xml @@ -0,0 +1,28 @@ + + + + + + + org.apache.maven.its.it-core-extensions + maven-it-unknown-extensions + 0.1 + + diff --git a/its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/pom.xml b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/pom.xml new file mode 100644 index 0000000000..5807bd3063 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-7772-core-extensions-not-found/pom.xml @@ -0,0 +1,32 @@ + + + + 4.0.0 + + org.apache.maven.its.it-core-extensions + test + 0.1 + jar + + Maven Integration Test :: it-core-extensions + Verify that Maven loads core extensions and components contributed by the extensions are available to regular + plugins. + +