[MNG-7772] Test for extensions.xml defined in an ext directory in the ${user.home}/.m2 folder (#311)

This commit is contained in:
juulhobert 2023-10-18 20:06:47 +02:00 committed by GitHub
parent 5c88324237
commit 6a83ac6c29
14 changed files with 439 additions and 24 deletions

View File

@ -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 + "\"");
}
}
}

View File

@ -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 + "\"");
}
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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 <a href="https://issues.apache.org/jira/browse/MNG-7772">MNG-7772</a>:
* check that Maven fails if it cannot load core extensions contributed by <code>${user.home}/.m2/extensions.xml</code>.
*/
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));
}
}
}
}

View File

@ -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);

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.7772-core-extensions-scopes</groupId>
<artifactId>maven-it-core-extensions</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Maven Integration Test :: maven-it-core-extensions</name>
<description>Maven core extensions components that use @SessionScope and @MojoExecutionScope Guice scopes</description>
<!--
mvn clean deploy -DaltDeploymentRepository=default::default::file://
-->
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.8.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.3.5</version>
<executions>
<execution>
<id>generate-index</id>
<goals>
<goal>main-index</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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!");
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<extension>
<exportedPackages>
<exportedPackage>org.apache.maven.its.extensions</exportedPackage>
</exportedPackages>
<exportedArtifacts>
<exportedArtifact>org.apache.maven.its.7772-core-extensions-scopes:maven-it-core-extensions</exportedArtifact>
</exportedArtifacts>
</extension>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<extensions>
<extension>
<groupId>org.apache.maven.its.7772-core-extensions-scopes</groupId>
<artifactId>maven-it-core-extensions</artifactId>
<version>0.1</version>
</extension>
</extensions>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.it-core-extensions</groupId>
<artifactId>test</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Maven Integration Test :: it-core-extensions</name>
<description>Verify that Maven loads core extensions and components contributed by the extensions are available to regular
plugins.</description>
</project>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<extensions>
<extension>
<groupId>org.apache.maven.its.it-core-extensions</groupId>
<artifactId>maven-it-unknown-extensions</artifactId>
<version>0.1</version>
</extension>
</extensions>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.it-core-extensions</groupId>
<artifactId>test</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Maven Integration Test :: it-core-extensions</name>
<description>Verify that Maven loads core extensions and components contributed by the extensions are available to regular
plugins.</description>
</project>