Skip to content

Commit

Permalink
Remove JAVA_HOME from rule violation message
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Jan 12, 2025
1 parent 9062c05 commit 242451e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 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 @@ -27,7 +27,8 @@
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* The Class TestRequireJavaVendor.
Expand Down Expand Up @@ -57,27 +58,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 +90,34 @@ 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());

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

@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 @@ -110,12 +110,9 @@ void shouldIncludeJavaHomeLocationInTheErrorMessage() {
String requiredVersion = "10000";
rule.setVersion(requiredVersion);

EnforcerRuleException exception = assertThrows(
EnforcerRuleException.class,
() -> rule.execute());
EnforcerRuleException exception = assertThrows(EnforcerRuleException.class, () -> rule.execute());

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

Expand Down

0 comments on commit 242451e

Please sign in to comment.