You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2021/07/13 15:05:07 UTC

[maven-enforcer] branch master updated: [MENFORCER-376] Add support for excludes/includes in requireJavaVendor rule code based on PR from KroArtem

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new 3b47fa9  [MENFORCER-376] Add support for excludes/includes in requireJavaVendor rule code based on PR from KroArtem <ar...@gmail.com>
3b47fa9 is described below

commit 3b47fa9627bdf96a9fb45aac31c655bc6cae5060
Author: rfscholte <rf...@apache.org>
AuthorDate: Tue Jul 13 17:04:52 2021 +0200

    [MENFORCER-376] Add support for excludes/includes in requireJavaVendor rule
    code based on PR from KroArtem <ar...@gmail.com>
---
 .../maven/plugins/enforcer/RequireJavaVendor.java  |  63 ++++++++----
 enforcer-rules/src/site/apt/bannedPlugins.apt.vm   |   2 +-
 .../src/site/apt/requireJavaVendor.apt.vm          |  15 ++-
 .../plugins/enforcer/TestRequireJavaVendor.java    | 108 +++++++++++++++++----
 4 files changed, 148 insertions(+), 40 deletions(-)

diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVendor.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVendor.java
index 5d2444e..64c7802 100644
--- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVendor.java
+++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVendor.java
@@ -23,48 +23,77 @@ import org.apache.commons.lang3.SystemUtils;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
 
+import java.util.List;
+
 /**
  * This rule checks that the Java vendor is allowed.
+ * Rule will fail is it matches any of the excludes or doesn't match any include in case it was set. 
  *
  * @author Tim Sijstermans
  * @since 3.0.0
  */
 public class RequireJavaVendor extends AbstractNonCacheableEnforcerRule
 {
-    private String name;
+    /**
+     * Java vendors to include. If none is defined, all are included.
+     * 
+     */
+    private List<String> includes;
+
+    /**
+     * Java vendors to exclude.
+     */
+    private List<String> excludes;
 
     @Override
     public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException
     {
-        if ( !SystemUtils.JAVA_VENDOR.equals( name ) )
+        if ( excludes != null && excludes.contains( SystemUtils.JAVA_VENDOR ) )
         {
             String message = getMessage();
-            String error = "Vendor " + SystemUtils.JAVA_VENDOR + " did not match required vendor " + name;
-            StringBuilder sb = new StringBuilder();
-            if ( message != null )
+            if ( message == null ) 
             {
-                sb.append( message ).append( System.lineSeparator() );
+                message = String.format( "%s is an excluded Required Java Vendor", SystemUtils.JAVA_VENDOR );
             }
-
-            sb.append( error );
-
-            throw new EnforcerRuleException( sb.toString() );
+            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 );
+            }
+            throw new EnforcerRuleException( message );
         }
     }
 
     /**
-     * Specify the required name. Some examples are:
-     * Name should be an exact match of the System Property java.vendor, which you can also see with mvn --version
+     * Specify the banned vendors. This should be an exact match of the System Property
+     * java.vendor, which you can also see with mvn --version. <br>
+     * Excludes override the include rules.
      *
+     * Some examples are:
      * <ul>
-     * <li><code>AdoptOpenJDK</code> enforces vendor name AdoptOpenJDK </li>
-     * <li><code>Amazon</code> enforces vendor name Amazon </li>
+     * <li><code>AdoptOpenJDK</code> prohibits vendor name AdoptOpenJDK </li>
+     * <li><code>Amazon</code> prohibits vendor name Amazon </li>
      * </ul>
+     */
+    public void setExcludes( List<String> theExcludes )
+    {
+        this.excludes = theExcludes;
+    }
+
+    /**
+     * Specify the allowed vendor names. This should be an exact match of the System Property
+     * java.vendor, which you can also see with mvn --version. <br>
+     * The rule will fail if vendor name matches any exclude, unless it also matches an
+     * include rule.
      *
-     * @param name the required name to set
+     * @see #setIncludes(List)
      */
-    public final void setName( String name )
+    public void setIncludes( List<String> theIncludes )
     {
-        this.name = name;
+        this.includes = theIncludes;
     }
 }
diff --git a/enforcer-rules/src/site/apt/bannedPlugins.apt.vm b/enforcer-rules/src/site/apt/bannedPlugins.apt.vm
index 8c91ca6..dc0e23a 100644
--- a/enforcer-rules/src/site/apt/bannedPlugins.apt.vm
+++ b/enforcer-rules/src/site/apt/bannedPlugins.apt.vm
@@ -43,7 +43,7 @@ Banned Plugins
         []
 
    * includes - a list of plugin artifacts to include. These are exceptions to the excludes. It is meant to allow wide exclusion rules with wildcards and fine tune using includes. 
-     If nothing has been excluded, then the includes have no effect. In otherwords, includes only subtract from artifacts that matched an exclude rule.
+     If nothing has been excluded, then the includes have no effect. In other words, includes only subtract from artifacts that matched an exclude rule.
       
    * message - an optional message to the user if the rule fails.
    
diff --git a/enforcer-rules/src/site/apt/requireJavaVendor.apt.vm b/enforcer-rules/src/site/apt/requireJavaVendor.apt.vm
index b10bdd2..d7bf3bd 100644
--- a/enforcer-rules/src/site/apt/requireJavaVendor.apt.vm
+++ b/enforcer-rules/src/site/apt/requireJavaVendor.apt.vm
@@ -30,8 +30,11 @@ Require Java Vendor
    The following parameters are supported by this rule:
    
    * message - an optional message to the user if the rule fails.
-   
-   * name - The name of the allowed JDK vendor.
+
+   * includes - a list of allowed JDK vendors to include, by default all is included.
+
+   * excludes - a list of banned JDK vendors. These are exceptions to the includes.
+
  
    Sample Plugin Configuration:
    
@@ -53,7 +56,13 @@ Require Java Vendor
             <configuration>
               <rules>
                 <requireJavaVendor>
-                  <name>AdoptOpenJDK</name>
+                  <excludes>
+                    <exclude>Pivotal</exclude>
+                    <exclude>Oracle JDK</exclude>
+                  </excludes>
+                  <includes>
+                    <include>AdoptOpenJDK</include>
+                  </includes>
                 </requireJavaVendor>
               </rules>    
             </configuration>
diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVendor.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVendor.java
index a3f0927..e4d896b 100644
--- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVendor.java
+++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVendor.java
@@ -22,8 +22,16 @@ package org.apache.maven.plugins.enforcer;
 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.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.assertThrows;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import java.util.Arrays;
+import java.util.Collections;
+
 /**
  * The Class TestRequireJavaVendor.
  *
@@ -31,37 +39,99 @@ import org.junit.Test;
  */
 public class TestRequireJavaVendor
 {
+    private static final String NON_MATCHING_VENDOR = "non-matching-vendor";
+
+    private RequireJavaVendor underTest;
+
+    @Before
+    public void prepareTest()
+    {
+        underTest = new RequireJavaVendor();
+    }
 
-    /**
-     * Test Rule: Success case
-     *
-     * @throws EnforcerRuleException the enforcer rule exception
-     */
     @Test
-    public void settingTheRequiredJavaVendorToSystemVendorShouldNotFail() throws EnforcerRuleException
+    public void matchingInclude()
+        throws EnforcerRuleException
     {
-        RequireJavaVendor underTest = new RequireJavaVendor();
         // Set the required vendor to the current system vendor
-        underTest.setName( SystemUtils.JAVA_VENDOR );
+        underTest.setIncludes( Collections.singletonList( SystemUtils.JAVA_VENDOR ) );
         final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
         underTest.execute( helper );
         // No assert and no expected exception because this test should not fail
     }
 
-    /**
-     * Test rule: Failing case
-     *
-     * @throws EnforcerRuleException the enforcer rule exception
-     */
-    @Test( expected = EnforcerRuleException.class )
-    public void excludingTheCurrentVendorShouldFail() throws EnforcerRuleException
+    @Test
+    public void nonMatchingInclude()
+        throws EnforcerRuleException
     {
+        // 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" ) );
+    }
 
-        RequireJavaVendor underTest = new RequireJavaVendor();
-        // Set the required vendor to something nonsensical
-        underTest.setName( "..." + SystemUtils.JAVA_VENDOR );
+    @Test
+    public void matchingExclude()
+        throws EnforcerRuleException
+    {
+        // 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" ) );
+    }
+
+    @Test
+    public void nonMatchingExclude()
+        throws EnforcerRuleException
+    {
+        // Set the excluded vendor to something nonsensical
+        underTest.setExcludes( Collections.singletonList( NON_MATCHING_VENDOR ) );
         final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
         underTest.execute( helper );
-        // expect EnforcerRuleException to happen
+        // No assert and no expected exception because this test should not fail
+    }
+
+    @Test
+    public void matchingIncludeAndMatchingExclude()
+        throws EnforcerRuleException
+    {
+        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" ) );
+    }
+
+    @Test
+    public void matchAnyExclude()
+        throws EnforcerRuleException
+    {
+        // 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" ) );
+    }
+
+    @Test
+    public 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();
+        underTest.execute( helper );
+        // No assert and no expected exception because this test should not fail
+    }
+
+    @Test
+    public void defaultRule()
+        throws EnforcerRuleException
+    {
+        final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
+        underTest.execute( helper );
+        // No assert and no expected exception because this test should not fail
     }
 }
\ No newline at end of file