Skip to content

Commit

Permalink
[MENFORCER-495] Remove "JAVA_HOME" from rule violation message (#346)
Browse files Browse the repository at this point in the history
* Remove JAVA_HOME from rule violation message
  • Loading branch information
elharo authored Jan 13, 2025
1 parent 3bf1aa0 commit 46f61df
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public void execute() throws EnforcerRuleException {
String message = getMessage();
if (message == null) {
message = String.format(
"%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
"%s is an excluded Required Java Vendor (detected JDK: %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 (JAVA_HOME=%s)",
"%s is not an included Required Java Vendor (detected JDK: %s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
}
throw new EnforcerRuleException(message);
Expand All @@ -96,7 +96,7 @@ public void execute() throws EnforcerRuleException {
* java.vendor, which you can also see with mvn --version. <br>
* Excludes override the include rules.
*
* @param theExcludes the vendor to to exclude from the include list.
* @param theExcludes the vendors to exclude from the include list
*/
public void setExcludes(List<String> theExcludes) {
this.excludes = theExcludes;
Expand All @@ -114,7 +114,7 @@ public void setExcludes(List<String> theExcludes) {
* <li><code>Amazon</code> prohibits vendor name Amazon </li>
* </ul>
*
* @param theIncludes the list of required vendors.
* @param theIncludes the list of required vendors
*
* @see #setExcludes(List)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ private void setCustomMessageIfNoneConfigured(ArtifactVersion detectedJdkVersion
getLog().debug("Could not parse allowed version range " + allowedVersionRange + " " + e.getMessage());
version = allowedVersionRange;
}
String message = String.format(
"Detected JDK version %s (JAVA_HOME=%s) is not in the allowed range %s.",
detectedJdkVersion, SystemUtils.JAVA_HOME, version);
String message = "Detected JDK " + SystemUtils.JAVA_HOME
+ " is version " + detectedJdkVersion
+ " which is not in the allowed range " + version + ".";
super.setMessage(message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;

/**
* The Class TestRequireJavaVendor.
Expand Down Expand Up @@ -57,27 +57,27 @@ void nonMatchingInclude() {
// Set the included vendor to something irrelevant
underTest.setIncludes(Collections.singletonList(NON_MATCHING_VENDOR));

assertThatThrownBy(() -> underTest.execute())
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(
"%s is not an included Required Java Vendor (JAVA_HOME=%s)",
SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
EnforcerRuleException exception = assertThrows(EnforcerRuleException.class, () -> underTest.execute());

assertTrue(exception
.getMessage()
.endsWith(" is not an included Required Java Vendor (detected JDK: " + SystemUtils.JAVA_HOME + ")"));
}

@Test
void matchingExclude() {
// Set the excluded vendor to current vendor name
underTest.setExcludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));

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

assertTrue(exception
.getMessage()
.endsWith(" is an excluded Required Java Vendor (detected JDK: " + SystemUtils.JAVA_HOME + ")"));
}

@Test
void nonMatchingExclude() throws EnforcerRuleException {
void nonMatchingExclude() {
// Set the excluded vendor to something nonsensical
underTest.setExcludes(Collections.singletonList(NON_MATCHING_VENDOR));

Expand All @@ -89,36 +89,35 @@ void matchingIncludeAndMatchingExclude() {
underTest.setExcludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));
underTest.setIncludes(Collections.singletonList(SystemUtils.JAVA_VENDOR));

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

assertTrue(exception.getMessage().contains(" is an excluded Required Java Vendor (detected JDK: "));
assertTrue(exception.getMessage().contains(SystemUtils.JAVA_HOME));
}

@Test
void matchAnyExclude() {
// Set a bunch of excluded vendors
underTest.setExcludes(Arrays.asList(SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_VENDOR + "modified"));

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

assertEquals(
SystemUtils.JAVA_VENDOR + " is an excluded Required Java Vendor (detected JDK: " + SystemUtils.JAVA_HOME
+ ")",
exception.getMessage());
}

@Test
void matchAnyInclude() throws EnforcerRuleException {
void matchAnyInclude() {
// Set a bunch of included vendors
underTest.setIncludes(Arrays.asList(SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_VENDOR + "modified"));

assertThatCode(() -> underTest.execute()).doesNotThrowAnyException();
}

@Test
void defaultRule() throws EnforcerRuleException {

void defaultRule() {
assertThatCode(() -> underTest.execute()).doesNotThrowAnyException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -105,15 +107,13 @@ void excludingTheCurrentJavaVersionViaRangeThisShouldFailWithException() {

@Test
void shouldIncludeJavaHomeLocationInTheErrorMessage() {
String thisVersion = RequireJavaVersion.normalizeJDKVersion(SystemUtils.JAVA_VERSION);
String requiredVersion = "10000";
rule.setVersion(requiredVersion);

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

assertTrue(exception.getMessage().startsWith("Detected JDK " + SystemUtils.JAVA_HOME + " is version "));
assertTrue(exception.getMessage().endsWith(" which is not in the allowed range [10000,)."));
}

@Test
Expand Down

0 comments on commit 46f61df

Please sign in to comment.