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-445] Include JAVA_HOME location in the Java rule failure messages #213

Merged
merged 1 commit into from
Dec 30, 2022
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 @@ -48,13 +48,17 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
if (excludes != null && excludes.contains(SystemUtils.JAVA_VENDOR)) {
String message = getMessage();
if (message == null) {
message = String.format("%s is an excluded Required Java Vendor", SystemUtils.JAVA_VENDOR);
message = String.format(
"%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
}
throw new EnforcerRuleException(message);
} else if (includes != null && !includes.contains(SystemUtils.JAVA_VENDOR)) {
String message = getMessage();
if (message == null) {
message = String.format("%s is not an included Required Java Vendor", SystemUtils.JAVA_VENDOR);
message = String.format(
"%s is not an included Required Java Vendor (JAVA_HOME=%s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
}
throw new EnforcerRuleException(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
+ " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: "
+ detectedJdkVersion.getQualifier());

setCustomMessageIfNoneConfigured(detectedJdkVersion, getVersion());

enforceVersion(helper.getLog(), "JDK", getVersion(), detectedJdkVersion);
}

Expand Down Expand Up @@ -112,4 +114,13 @@ public static String normalizeJDKVersion(String theJdkVersion) {
version = StringUtils.stripEnd(version, "-");
return StringUtils.stripEnd(version, ".");
}

private void setCustomMessageIfNoneConfigured(ArtifactVersion detectedJdkVersion, String allowedVersionRange) {
if (getMessage() == null) {
String message = String.format(
"Detected JDK version %s (JAVA_HOME=%s) is not in the allowed range %s.",
detectedJdkVersion, SystemUtils.JAVA_HOME, allowedVersionRange);
super.setMessage(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* The Class TestRequireJavaVendor.
*
* @author Tim Sijstermans
*/
public class TestRequireJavaVendor {
class TestRequireJavaVendor {
private static final String NON_MATCHING_VENDOR = "non-matching-vendor";

private RequireJavaVendor underTest;
Expand All @@ -47,7 +45,7 @@ public void prepareTest() {
}

@Test
public void matchingInclude() throws EnforcerRuleException {
void matchingInclude() throws EnforcerRuleException {
// Set the required vendor to the current system vendor
underTest.setIncludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
Expand All @@ -56,25 +54,33 @@ public void matchingInclude() throws EnforcerRuleException {
}

@Test
public void nonMatchingInclude() {
void nonMatchingInclude() {
// Set the included vendor to something irrelevant
underTest.setIncludes(Collections.singletonList(NON_MATCHING_VENDOR));
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> underTest.execute(helper));
assertThat(e.getMessage(), is(SystemUtils.JAVA_VENDOR + " is not an included Required Java Vendor"));

assertThatThrownBy(() -> underTest.execute(helper))
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(
"%s is not an included Required Java Vendor (JAVA_HOME=%s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void matchingExclude() {
void matchingExclude() {
// Set the excluded vendor to current vendor name
underTest.setExcludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> underTest.execute(helper));
assertThat(e.getMessage(), is(SystemUtils.JAVA_VENDOR + " is an excluded Required Java Vendor"));

assertThatThrownBy(() -> underTest.execute(helper))
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(
"%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
}

@Test
public void nonMatchingExclude() throws EnforcerRuleException {
void nonMatchingExclude() throws EnforcerRuleException {
// Set the excluded vendor to something nonsensical
underTest.setExcludes(Collections.singletonList(NON_MATCHING_VENDOR));
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
Expand All @@ -83,25 +89,33 @@ public void nonMatchingExclude() throws EnforcerRuleException {
}

@Test
public void matchingIncludeAndMatchingExclude() {
void matchingIncludeAndMatchingExclude() {
underTest.setExcludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
underTest.setIncludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> underTest.execute(helper));
assertThat(e.getMessage(), is(SystemUtils.JAVA_VENDOR + " is an excluded Required Java Vendor"));

assertThatThrownBy(() -> underTest.execute(helper))
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(
"%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
}

@Test
public void matchAnyExclude() {
void matchAnyExclude() {
// Set a bunch of excluded vendors
underTest.setExcludes(Arrays.asList(SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_VENDOR + "modified"));
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> underTest.execute(helper));
assertThat(e.getMessage(), is(SystemUtils.JAVA_VENDOR + " is an excluded Required Java Vendor"));

assertThatThrownBy(() -> underTest.execute(helper))
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(
"%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
}

@Test
public void matchAnyInclude() throws EnforcerRuleException {
void matchAnyInclude() throws EnforcerRuleException {
// Set a bunch of included vendors
underTest.setIncludes(Arrays.asList(SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_VENDOR + "modified"));
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
Expand All @@ -110,7 +124,7 @@ public void matchAnyInclude() throws EnforcerRuleException {
}

@Test
public void defaultRule() throws EnforcerRuleException {
void defaultRule() throws EnforcerRuleException {
final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
underTest.execute(helper);
// No assert and no expected exception because this test should not fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* The Class TestRequireJavaVersion.
Expand Down Expand Up @@ -89,30 +88,44 @@ void settingsTheJavaVersionAsNormalizedVersionShouldNotFail() throws EnforcerRul

@Test
void excludingTheCurrentJavaVersionViaRangeThisShouldFailWithException() {
assertThrows(EnforcerRuleException.class, () -> {
String thisVersion = RequireJavaVersion.normalizeJDKVersion(SystemUtils.JAVA_VERSION);

RequireJavaVersion rule = new RequireJavaVersion();
// exclude this version
rule.setVersion("(" + thisVersion);

EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
rule.execute(helper);
// intentionally no assertThat(...) because we expect and exception.
});
// intentionally no assertThat(...) because we expect and exception.
String thisVersion = RequireJavaVersion.normalizeJDKVersion(SystemUtils.JAVA_VERSION);
String requiredVersion = "(" + thisVersion;
RequireJavaVersion rule = new RequireJavaVersion();
rule.setVersion(requiredVersion);
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();

assertThatThrownBy(() -> rule.execute(helper))
.isInstanceOf(EnforcerRuleException.class)
.hasMessage("The requested JDK version %s is invalid.", requiredVersion);
}

@Test
@Disabled
// TODO: Think about the intention of this test? What should it prove?
void thisShouldNotCrash() throws EnforcerRuleException {
void shouldIncludeJavaHomeLocationInTheErrorMessage() {
String thisVersion = RequireJavaVersion.normalizeJDKVersion(SystemUtils.JAVA_VERSION);
String requiredVersion = "10000";
RequireJavaVersion rule = new RequireJavaVersion();
rule.setVersion(SystemUtils.JAVA_VERSION);
rule.setVersion(requiredVersion);
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();

assertThatThrownBy(() -> rule.execute(helper))
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(
"Detected JDK version %s (JAVA_HOME=%s) is not in the allowed range %s.",
thisVersion, SystemUtils.JAVA_HOME, requiredVersion);
}

@Test
void shouldUseCustomErrorMessage() {
String requiredVersion = "10000";
String message = "My custom error message";
RequireJavaVersion rule = new RequireJavaVersion();
rule.setVersion(requiredVersion);
rule.setMessage(message);
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
rule.execute(helper);
// intentionally no assertThat(...) because we don't expect and exception.
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved

assertThatThrownBy(() -> rule.execute(helper))
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(message);
}

/**
Expand Down