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 2019/08/06 06:20:39 UTC

[maven] 01/02: Fix ExclusionArtifactFilter to respect wildcard exclusions.

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

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

commit 6175b36523703305fc14464c2b3654d551d18278
Author: Ray Tsang <sa...@gmail.com>
AuthorDate: Thu Jul 18 18:25:19 2019 +0300

    Fix ExclusionArtifactFilter to respect wildcard exclusions.
---
 .../resolver/filter/ExclusionArtifactFilter.java   |  22 +++-
 .../filter/ExclusionArtifactFilterTest.java        | 123 +++++++++++++++++++++
 2 files changed, 140 insertions(+), 5 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java b/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java
index 42390d6..fae069b 100644
--- a/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java
+++ b/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java
@@ -19,13 +19,13 @@ package org.apache.maven.artifact.resolver.filter;
  * under the License.
  */
 
-import java.util.List;
-
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Exclusion;
 
+import java.util.List;
+
 /**
- *  Filter to exclude from a list of artifact patterns.
+ * Filter to exclude from a list of artifact patterns.
  */
 public class ExclusionArtifactFilter implements ArtifactFilter
 {
@@ -41,8 +41,20 @@ public class ExclusionArtifactFilter implements ArtifactFilter
     {
         for ( Exclusion exclusion : exclusions )
         {
-            if ( exclusion.getGroupId().equals( artifact.getGroupId() )
-                    && exclusion.getArtifactId().equals( artifact.getArtifactId() ) )
+            if ( "*".equals( exclusion.getGroupId() ) && "*".equals( exclusion.getArtifactId() ) )
+            {
+                return false;
+            }
+            if ( "*".equals( exclusion.getGroupId() ) )
+            {
+                return !exclusion.getArtifactId().equals( artifact.getArtifactId() );
+            }
+            if ( "*".equals( exclusion.getArtifactId() ) )
+            {
+                return !exclusion.getGroupId().equals( artifact.getGroupId() );
+            }
+            if ( exclusion.getGroupId().equals( artifact.getGroupId() ) && exclusion.getArtifactId().equals(
+                    artifact.getArtifactId() ) )
             {
                 return false;
             }
diff --git a/maven-core/src/test/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilterTest.java b/maven-core/src/test/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilterTest.java
new file mode 100644
index 0000000..62a4911
--- /dev/null
+++ b/maven-core/src/test/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilterTest.java
@@ -0,0 +1,123 @@
+package org.apache.maven.artifact.resolver.filter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.model.Exclusion;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ExclusionArtifactFilterTest
+{
+    private Artifact artifact;
+
+    @Before
+    public void setup()
+    {
+        artifact = mock( Artifact.class );
+        when( artifact.getGroupId() ).thenReturn( "org.apache.maven" );
+        when( artifact.getArtifactId() ).thenReturn( "maven-core" );
+    }
+
+    @Test
+    public void testExcludeExact()
+    {
+        Exclusion exclusion = new Exclusion();
+        exclusion.setGroupId( "org.apache.maven" );
+        exclusion.setArtifactId( "maven-core" );
+        ExclusionArtifactFilter filter = new ExclusionArtifactFilter( Collections.singletonList( exclusion ) );
+
+        assertThat( filter.include( artifact ), is( false ) );
+    }
+
+    @Test
+    public void testExcludeNoMatch()
+    {
+        Exclusion exclusion = new Exclusion();
+        exclusion.setGroupId( "org.apache.maven" );
+        exclusion.setArtifactId( "maven-model" );
+        ExclusionArtifactFilter filter = new ExclusionArtifactFilter( Collections.singletonList( exclusion ) );
+
+        assertThat( filter.include( artifact ), is( true ) );
+    }
+
+    @Test
+    public void testExcludeGroupIdWildcard()
+    {
+        Exclusion exclusion = new Exclusion();
+        exclusion.setGroupId( "*" );
+        exclusion.setArtifactId( "maven-core" );
+        ExclusionArtifactFilter filter = new ExclusionArtifactFilter( Collections.singletonList( exclusion ) );
+
+        assertThat( filter.include( artifact ), is( false ) );
+    }
+
+
+    @Test
+    public void testExcludeGroupIdWildcardNoMatch()
+    {
+        Exclusion exclusion = new Exclusion();
+        exclusion.setGroupId( "*" );
+        exclusion.setArtifactId( "maven-compat" );
+        ExclusionArtifactFilter filter = new ExclusionArtifactFilter( Collections.singletonList( exclusion ) );
+
+        assertThat( filter.include( artifact ), is( true ) );
+    }
+
+    @Test
+    public void testExcludeArtifactIdWildcard()
+    {
+        Exclusion exclusion = new Exclusion();
+        exclusion.setGroupId( "org.apache.maven" );
+        exclusion.setArtifactId( "*" );
+        ExclusionArtifactFilter filter = new ExclusionArtifactFilter( Collections.singletonList( exclusion ) );
+
+        assertThat( filter.include( artifact ), is( false ) );
+    }
+
+    @Test
+    public void testExcludeArtifactIdWildcardNoMatch()
+    {
+        Exclusion exclusion = new Exclusion();
+        exclusion.setGroupId( "org.apache.groovy" );
+        exclusion.setArtifactId( "*" );
+        ExclusionArtifactFilter filter = new ExclusionArtifactFilter( Collections.singletonList( exclusion ) );
+
+        assertThat( filter.include( artifact ), is( true ) );
+    }
+
+    @Test
+    public void testExcludeAllWildcard()
+    {
+        Exclusion exclusion = new Exclusion();
+        exclusion.setGroupId( "*" );
+        exclusion.setArtifactId( "*" );
+        ExclusionArtifactFilter filter = new ExclusionArtifactFilter( Collections.singletonList( exclusion ) );
+
+        assertThat( filter.include( artifact ), is( false ) );
+    }
+}
\ No newline at end of file