From f21d040c01707139a005afd927b4b1194926622f Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 26 May 2024 13:24:01 +0200 Subject: [PATCH] [MENFORCER-503] Pass context to ProfileActivator - fix NPE in Maven 3.9.7 --- .../maven/enforcer/rules/RequireOS.java | 22 ++++++++++++++-- .../maven/enforcer/rules/TestRequireOS.java | 26 ++++++++++++++----- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java index e6051436..a8f7bd4a 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java @@ -26,9 +26,12 @@ import org.apache.maven.enforcer.rule.api.EnforcerRuleError; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rules.utils.OSUtil; +import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Activation; import org.apache.maven.model.ActivationOS; import org.apache.maven.model.Profile; +import org.apache.maven.model.profile.DefaultProfileActivationContext; +import org.apache.maven.model.profile.ProfileActivationContext; import org.apache.maven.model.profile.activation.ProfileActivator; import org.codehaus.plexus.util.Os; import org.codehaus.plexus.util.StringUtils; @@ -43,6 +46,8 @@ public final class RequireOS extends AbstractStandardEnforcerRule { private final ProfileActivator activator; + private final ProfileActivationContext profileActivationContext; + /** * The OS family type desired
* Possible values: @@ -85,8 +90,20 @@ public final class RequireOS extends AbstractStandardEnforcerRule { * Instantiates a new RequireOS. */ @Inject - RequireOS(@Named("os") ProfileActivator activator) { + RequireOS(@Named("os") ProfileActivator activator, MavenSession session) { this.activator = Objects.requireNonNull(activator); + this.profileActivationContext = createProfileActivationContext(session); + } + + private ProfileActivationContext createProfileActivationContext(MavenSession session) { + DefaultProfileActivationContext context = new DefaultProfileActivationContext(); + context.setActiveProfileIds(session.getRequest().getActiveProfiles()); + context.setInactiveProfileIds(session.getRequest().getInactiveProfiles()); + context.setProjectDirectory(session.getCurrentProject().getBasedir()); + context.setProjectProperties(session.getCurrentProject().getProperties()); + context.setSystemProperties(System.getProperties()); + context.setUserProperties(session.getUserProperties()); + return context; } @Override @@ -143,7 +160,8 @@ private void displayOSInfo() { * @return true if the version is allowed. */ public boolean isAllowed() { - return activator.isActive(createProfile(), null, null); + // empty lambda as problems collector + return activator.isActive(createProfile(), profileActivationContext, (req -> {})); } /** diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireOS.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireOS.java index e7cfdb74..d18a62bd 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireOS.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireOS.java @@ -22,15 +22,20 @@ import org.apache.maven.enforcer.rule.api.EnforcerLogger; import org.apache.maven.enforcer.rule.api.EnforcerRuleError; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenSession; import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.SystemStreamLog; +import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.Os; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Exhaustively check the OS mojo. @@ -39,14 +44,23 @@ */ class TestRequireOS { + private MavenSession mavenSession; + + @BeforeEach + void setup() { + mavenSession = mock(MavenSession.class); + when(mavenSession.getRequest()).thenReturn(mock(MavenExecutionRequest.class)); + when(mavenSession.getCurrentProject()).thenReturn(mock(MavenProject.class)); + } + /** * Test os. */ @Test - public void testOS() { + void testOS() { Log log = new SystemStreamLog(); - RequireOS rule = new RequireOS(new OperatingSystemProfileActivator()); + RequireOS rule = new RequireOS(new OperatingSystemProfileActivator(), mavenSession); Iterator iter = Os.getValidFamilies().iterator(); String validFamily; @@ -107,8 +121,8 @@ public void testOS() { @Test void testInvalidFamily() { - RequireOS rule = new RequireOS(new OperatingSystemProfileActivator()); - rule.setLog(Mockito.mock(EnforcerLogger.class)); + RequireOS rule = new RequireOS(new OperatingSystemProfileActivator(), mavenSession); + rule.setLog(mock(EnforcerLogger.class)); rule.setFamily("junk"); assertThatCode(rule::execute) @@ -118,7 +132,7 @@ void testInvalidFamily() { @Test void testId() { - RequireOS rule = new RequireOS(new OperatingSystemProfileActivator()); + RequireOS rule = new RequireOS(new OperatingSystemProfileActivator(), mavenSession); rule.setVersion("1.2"); assertThat(rule.getCacheId()).isNotEmpty(); }