Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-503] Pass context to ProfileActivator - fix NPE in Maven 3.9.7 #315

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +46,8 @@
public final class RequireOS extends AbstractStandardEnforcerRule {
private final ProfileActivator activator;

private final ProfileActivationContext profileActivationContext;

/**
* The OS family type desired<br />
* Possible values:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 -> {}));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String> iter = Os.getValidFamilies().iterator();
String validFamily;
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}
Expand Down