[MNG-7750] Ensure no unwanted interpolation in plugins from profiles. (#1076)

This is the port of https://github.com/apache/maven/pull/1075 to the master branch.

The good thing is that this is ONLY the tests because the maven 4 does not have the problem of MNG-7750.

A few small changes in the tests were needed to handle the internal differences between maven 3.x and maven 4.x.

---

https://issues.apache.org/jira/browse/MNG-7750
This commit is contained in:
Niels Basjes 2023-04-12 12:26:40 +02:00 committed by GitHub
parent 53b6473237
commit 328ea1be31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 448 additions and 0 deletions

View File

@ -28,8 +28,12 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution; import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.Profile;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.ReportSet;
import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.project.harness.PomTestWrapper; import org.apache.maven.project.harness.PomTestWrapper;
import org.apache.maven.repository.RepositorySystem; import org.apache.maven.repository.RepositorySystem;
@ -47,11 +51,13 @@ import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@PlexusTest @PlexusTest
public class PomConstructionTest { public class PomConstructionTest {
@ -135,6 +141,202 @@ public class PomConstructionTest {
assertEquals("PASSED", pom.getValue("properties[1]/property")); assertEquals("PASSED", pom.getValue("properties[1]/property"));
} }
/*MNG-7750*/
private void checkBuildPluginWithArtifactId(
List<Plugin> plugins, String artifactId, String expectedId, String expectedConfig) {
Plugin plugin = plugins.stream()
.filter(p -> p.getArtifactId().equals(artifactId))
.findFirst()
.orElse(null);
assertNotNull(plugin, "Unable to find plugin with artifactId: " + artifactId);
List<PluginExecution> pluginExecutions = plugin.getExecutions();
PluginExecution pluginExecution = pluginExecutions.stream()
.filter(pe -> pe.getId().equals(expectedId))
.findFirst()
.orElse(null);
assertNotNull(pluginExecution, "Wrong id for \"" + artifactId + "\"");
String config = pluginExecution.getConfiguration().toString();
assertTrue(
config.contains(expectedConfig),
"Wrong config for \"" + artifactId + "\": (" + config + ") does not contain :" + expectedConfig);
}
private boolean isActiveProfile(MavenProject project, Profile activeProfile) {
return project.getActiveProfiles().stream().anyMatch(p -> p.getId().equals(activeProfile.getId()));
}
@Test
void testBuildPluginInterpolation() throws Exception {
PomTestWrapper pom = buildPom("plugin-interpolation-build", "activeProfile");
Model originalModel = pom.getMavenProject().getOriginalModel();
// =============================================
assertEquals("||${project.basedir}||", originalModel.getProperties().get("prop-outside"));
List<Plugin> outsidePlugins = originalModel.getBuild().getPlugins();
assertEquals(2, outsidePlugins.size());
checkBuildPluginWithArtifactId(
outsidePlugins,
"plugin-all-profiles",
"Outside ||${project.basedir}||",
"<plugin-all-profiles-out>Outside ||${project.basedir}||</plugin-all-profiles-out>");
// =============================================
Profile activeProfile = originalModel.getProfiles().stream()
.filter(profile -> profile.getId().equals("activeProfile"))
.findFirst()
.orElse(null);
assertNotNull(activeProfile, "Unable to find the activeProfile");
assertTrue(
isActiveProfile(pom.getMavenProject(), activeProfile),
"The activeProfile should be active in the maven project");
assertEquals("||${project.basedir}||", activeProfile.getProperties().get("prop-active"));
List<Plugin> activeProfilePlugins = activeProfile.getBuild().getPlugins();
assertEquals(2, activeProfilePlugins.size(), "Number of active profile plugins");
checkBuildPluginWithArtifactId(
activeProfilePlugins,
"plugin-all-profiles",
"Active all ||${project.basedir}||",
"<plugin-all-profiles-in>Active all ||${project.basedir}||</plugin-all-profiles-in>");
checkBuildPluginWithArtifactId(
activeProfilePlugins,
"only-active-profile",
"Active only ||${project.basedir}||",
"<plugin-in-active-profile-only>Active only ||${project.basedir}||</plugin-in-active-profile-only>");
// =============================================
Profile inactiveProfile = originalModel.getProfiles().stream()
.filter(profile -> profile.getId().equals("inactiveProfile"))
.findFirst()
.orElse(null);
assertNotNull(inactiveProfile, "Unable to find the inactiveProfile");
assertFalse(
isActiveProfile(pom.getMavenProject(), inactiveProfile),
"The inactiveProfile should NOT be active in the maven project");
assertEquals("||${project.basedir}||", inactiveProfile.getProperties().get("prop-inactive"));
List<Plugin> inactiveProfilePlugins = inactiveProfile.getBuild().getPlugins();
assertEquals(2, inactiveProfilePlugins.size(), "Number of active profile plugins");
checkBuildPluginWithArtifactId(
inactiveProfilePlugins,
"plugin-all-profiles",
"Inactive all ||${project.basedir}||",
"<plugin-all-profiles-ina>Inactive all ||${project.basedir}||</plugin-all-profiles-ina>");
checkBuildPluginWithArtifactId(
inactiveProfilePlugins,
"only-inactive-profile",
"Inactive only ||${project.basedir}||",
"<plugin-in-inactive-only>Inactive only ||${project.basedir}||</plugin-in-inactive-only>");
}
private void checkReportPluginWithArtifactId(
List<ReportPlugin> plugins, String artifactId, String expectedId, String expectedConfig) {
ReportPlugin plugin = plugins.stream()
.filter(p -> p.getArtifactId().equals(artifactId))
.findFirst()
.orElse(null);
assertNotNull(plugin, "Unable to find plugin with artifactId: " + artifactId);
List<ReportSet> pluginReportSets = plugin.getReportSets();
ReportSet reportSet = pluginReportSets.stream()
.filter(rs -> rs.getId().equals(expectedId))
.findFirst()
.orElse(null);
assertNotNull(reportSet, "Wrong id for \"" + artifactId + "\"");
String config = reportSet.getConfiguration().toString();
assertTrue(
config.contains(expectedConfig),
"Wrong config for \"" + artifactId + "\": (" + config + ") does not contain :" + expectedConfig);
}
@Test
void testReportingPluginInterpolation() throws Exception {
PomTestWrapper pom = buildPom("plugin-interpolation-reporting", "activeProfile");
Model originalModel = pom.getMavenProject().getOriginalModel();
// =============================================
assertEquals("||${project.basedir}||", originalModel.getProperties().get("prop-outside"));
List<ReportPlugin> outsidePlugins = originalModel.getReporting().getPlugins();
assertEquals(2, outsidePlugins.size(), "Wrong number of plugins found");
checkReportPluginWithArtifactId(
outsidePlugins,
"plugin-all-profiles",
"Outside ||${project.basedir}||",
"<plugin-all-profiles-out>Outside ||${project.basedir}||</plugin-all-profiles-out>");
// =============================================
Profile activeProfile = originalModel.getProfiles().stream()
.filter(profile -> profile.getId().equals("activeProfile"))
.findFirst()
.orElse(null);
assertNotNull(activeProfile, "Unable to find the activeProfile");
assertTrue(
isActiveProfile(pom.getMavenProject(), activeProfile),
"The activeProfile should be active in the maven project");
assertEquals("||${project.basedir}||", activeProfile.getProperties().get("prop-active"));
List<ReportPlugin> activeProfilePlugins = activeProfile.getReporting().getPlugins();
assertEquals(2, activeProfilePlugins.size(), "The activeProfile should be active in the maven project");
checkReportPluginWithArtifactId(
activeProfilePlugins,
"plugin-all-profiles",
"Active all ||${project.basedir}||",
"<plugin-all-profiles-in>Active all ||${project.basedir}||</plugin-all-profiles-in>");
checkReportPluginWithArtifactId(
activeProfilePlugins,
"only-active-profile",
"Active only ||${project.basedir}||",
"<plugin-in-active-profile-only>Active only ||${project.basedir}||</plugin-in-active-profile-only>");
// =============================================
Profile inactiveProfile = originalModel.getProfiles().stream()
.filter(profile -> profile.getId().equals("inactiveProfile"))
.findFirst()
.orElse(null);
assertNotNull(inactiveProfile, "Unable to find the inactiveProfile");
assertFalse(
isActiveProfile(pom.getMavenProject(), inactiveProfile),
"The inactiveProfile should NOT be active in the maven project");
assertEquals("||${project.basedir}||", inactiveProfile.getProperties().get("prop-inactive"));
List<ReportPlugin> inactiveProfilePlugins =
inactiveProfile.getReporting().getPlugins();
assertEquals(2, inactiveProfilePlugins.size(), "Number of active profile plugins");
checkReportPluginWithArtifactId(
inactiveProfilePlugins,
"plugin-all-profiles",
"Inactive all ||${project.basedir}||",
"<plugin-all-profiles-ina>Inactive all ||${project.basedir}||</plugin-all-profiles-ina>");
checkReportPluginWithArtifactId(
inactiveProfilePlugins,
"only-inactive-profile",
"Inactive only ||${project.basedir}||",
"<plugin-in-inactive-only>Inactive only ||${project.basedir}||</plugin-in-inactive-only>");
}
// Some better conventions for the test poms needs to be created and each of these tests // Some better conventions for the test poms needs to be created and each of these tests
// that represent a verification of a specification item needs to be a couple lines at most. // that represent a verification of a specification item needs to be a couple lines at most.
// The expressions help a lot, but we need a clean to pick up a directory of POMs, automatically load // The expressions help a lot, but we need a clean to pick up a directory of POMs, automatically load

View File

@ -0,0 +1,123 @@
<?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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.build.plugins</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<name>MNG-7750</name>
<description>
Test build plugin and execution interpolation.
</description>
<properties>
<prop-outside>||${project.basedir}||</prop-outside>
</properties>
<build>
<plugins>
<plugin>
<artifactId>plugin-all-profiles</artifactId>
<executions>
<execution>
<id>Outside ||${project.basedir}||</id>
<configuration>
<plugin-all-profiles-out>Outside ||${project.basedir}||</plugin-all-profiles-out>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>activeProfile</id>
<properties>
<prop-active>||${project.basedir}||</prop-active>
</properties>
<build>
<plugins>
<plugin>
<artifactId>plugin-all-profiles</artifactId>
<executions>
<execution>
<id>Active all ||${project.basedir}||</id>
<configuration>
<plugin-all-profiles-in>Active all ||${project.basedir}||</plugin-all-profiles-in>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>only-active-profile</artifactId>
<executions>
<execution>
<id>Active only ||${project.basedir}||</id>
<configuration>
<plugin-in-active-profile-only>Active only ||${project.basedir}||</plugin-in-active-profile-only>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>inactiveProfile</id>
<properties>
<prop-inactive>||${project.basedir}||</prop-inactive>
</properties>
<build>
<plugins>
<plugin>
<artifactId>plugin-all-profiles</artifactId>
<executions>
<execution>
<id>Inactive all ||${project.basedir}||</id>
<configuration>
<plugin-all-profiles-ina>Inactive all ||${project.basedir}||</plugin-all-profiles-ina>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>only-inactive-profile</artifactId>
<executions>
<execution>
<id>Inactive only ||${project.basedir}||</id>
<configuration>
<plugin-in-inactive-only>Inactive only ||${project.basedir}||</plugin-in-inactive-only>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -0,0 +1,123 @@
<?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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.reporting.plugins</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<name>MNG-7750</name>
<description>
Test reporting plugin and reportSet interpolation.
</description>
<properties>
<prop-outside>||${project.basedir}||</prop-outside>
</properties>
<reporting>
<plugins>
<plugin>
<artifactId>plugin-all-profiles</artifactId>
<reportSets>
<reportSet>
<id>Outside ||${project.basedir}||</id>
<configuration>
<plugin-all-profiles-out>Outside ||${project.basedir}||</plugin-all-profiles-out>
</configuration>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<profiles>
<profile>
<id>activeProfile</id>
<properties>
<prop-active>||${project.basedir}||</prop-active>
</properties>
<reporting>
<plugins>
<plugin>
<artifactId>plugin-all-profiles</artifactId>
<reportSets>
<reportSet>
<id>Active all ||${project.basedir}||</id>
<configuration>
<plugin-all-profiles-in>Active all ||${project.basedir}||</plugin-all-profiles-in>
</configuration>
</reportSet>
</reportSets>
</plugin>
<plugin>
<artifactId>only-active-profile</artifactId>
<reportSets>
<reportSet>
<id>Active only ||${project.basedir}||</id>
<configuration>
<plugin-in-active-profile-only>Active only ||${project.basedir}||</plugin-in-active-profile-only>
</configuration>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</profile>
<profile>
<id>inactiveProfile</id>
<properties>
<prop-inactive>||${project.basedir}||</prop-inactive>
</properties>
<reporting>
<plugins>
<plugin>
<artifactId>plugin-all-profiles</artifactId>
<reportSets>
<reportSet>
<id>Inactive all ||${project.basedir}||</id>
<configuration>
<plugin-all-profiles-ina>Inactive all ||${project.basedir}||</plugin-all-profiles-ina>
</configuration>
</reportSet>
</reportSets>
</plugin>
<plugin>
<artifactId>only-inactive-profile</artifactId>
<reportSets>
<reportSet>
<id>Inactive only ||${project.basedir}||</id>
<configuration>
<plugin-in-inactive-only>Inactive only ||${project.basedir}||</plugin-in-inactive-only>
</configuration>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</profile>
</profiles>
</project>