You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2008/09/24 20:05:08 UTC

svn commit: r698679 - in /maven/shared/trunk/maven-common-artifact-filters/src: main/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilter.java test/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilterTest.java

Author: jdcasey
Date: Wed Sep 24 11:05:08 2008
New Revision: 698679

URL: http://svn.apache.org/viewvc?rev=698679&view=rev
Log:
Improvements to fine-grained scope filtering, along with tests.

Modified:
    maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilter.java
    maven/shared/trunk/maven-common-artifact-filters/src/test/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilterTest.java

Modified: maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilter.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilter.java?rev=698679&r1=698678&r2=698679&view=diff
==============================================================================
--- maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilter.java (original)
+++ maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilter.java Wed Sep 24 11:05:08 2008
@@ -28,20 +28,30 @@
 import org.codehaus.plexus.logging.Logger;
 
 /**
- * TODO: include in maven-artifact in future
+ * {@link ArtifactFilter} implementation that selects artifacts based on their
+ * scopes.
+ * <br/>
+ * <b>NOTE:</b> None of the fine-grained scopes imply other scopes when enabled;
+ * when fine-grained scope control is used, each scope must be enabled separately,
+ * UNLESS the corresponding XXXWithImplications() method is used to enable that
+ * scope.
  */
 public class ScopeArtifactFilter
     implements ArtifactFilter, StatisticsReportingArtifactFilter
 {
-    private final boolean compileScope;
+    private boolean includeCompileScope;
 
-    private final boolean runtimeScope;
+    private boolean includeRuntimeScope;
 
-    private final boolean testScope;
+    private boolean includeTestScope;
 
-    private final boolean providedScope;
+    private boolean includeProvidedScope;
 
-    private final boolean systemScope;
+    private boolean includeSystemScope;
+    
+    private boolean includeNullScope = true;
+    
+    private boolean nullScopeHit = false;
 
     private boolean compileScopeHit = false;
 
@@ -54,93 +64,86 @@
     private boolean systemScopeHit = false;
 
     private List filteredArtifactIds = new ArrayList();
+    
+    /**
+     * Constructor that is meant to be used with fine-grained manipulation to 
+     * enable/disable specific scopes using the associated mutator methods.
+     */
+    public ScopeArtifactFilter()
+    {
+        // don't enable anything by default.
+        this( null );
+    }
 
+    /**
+     * Constructor that uses the implied nature of Maven scopes to determine which
+     * artifacts to include. For instance, 'test' scope implies compile, provided, and runtime,
+     * while 'runtime' scope implies only compile.
+     */
     public ScopeArtifactFilter( String scope )
     {
         if ( DefaultArtifact.SCOPE_COMPILE.equals( scope ) )
         {
-            systemScope = true;
-            providedScope = true;
-            compileScope = true;
-            runtimeScope = false;
-            testScope = false;
+            setIncludeCompileScopeWithImplications( true );
         }
         else if ( DefaultArtifact.SCOPE_RUNTIME.equals( scope ) )
         {
-            systemScope = false;
-            providedScope = false;
-            compileScope = true;
-            runtimeScope = true;
-            testScope = false;
+            setIncludeRuntimeScopeWithImplications( true );
         }
         else if ( DefaultArtifact.SCOPE_TEST.equals( scope ) )
         {
-            systemScope = true;
-            providedScope = true;
-            compileScope = true;
-            runtimeScope = true;
-            testScope = true;
+            setIncludeTestScopeWithImplications( true );
         }
         else if ( DefaultArtifact.SCOPE_PROVIDED.equals( scope ) )
         {
-            systemScope = false;
-            providedScope = true;
-            compileScope = false;
-            runtimeScope = false;
-            testScope = false;
+            setIncludeProvidedScope( true );
         }
         else if ( DefaultArtifact.SCOPE_SYSTEM.equals( scope ) )
         {
-            systemScope = true;
-            providedScope = false;
-            compileScope = false;
-            runtimeScope = false;
-            testScope = false;
-        }
-        else
-        {
-            systemScope = false;
-            providedScope = false;
-            compileScope = false;
-            runtimeScope = false;
-            testScope = false;
+            setIncludeSystemScope( true );
         }
     }
 
     public boolean include( Artifact artifact )
     {
         boolean result = true;
-
-        if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) )
+        
+        if ( artifact.getScope() == null )
+        {
+            nullScopeHit = true;
+            result = includeNullScope;
+        }
+        else if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) )
         {
             compileScopeHit = true;
-            result = compileScope;
+            result = includeCompileScope;
         }
         else if ( Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) )
         {
             runtimeScopeHit = true;
-            result = runtimeScope;
+            result = includeRuntimeScope;
         }
         else if ( Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
         {
             testScopeHit = true;
-            result = testScope;
+            result = includeTestScope;
         }
         else if ( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) )
         {
             providedScopeHit = true;
-            result = providedScope;
+            result = includeProvidedScope;
         }
         else if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
         {
             systemScopeHit = true;
-            result = systemScope;
+            result = includeSystemScope;
         }
 
         if ( !result )
         {
             // We have to be very careful with artifacts that have ranges, 
-            // because artifact.getId() will throw a NPE if a range is specified.
+            // because DefaultArtifact.getId() as of <= 2.1.0-M1 will throw a NPE 
+            // if a range is specified.
             String id;
             if ( artifact.getVersionRange() != null )
             {
@@ -159,8 +162,8 @@
 
     public String toString()
     {
-        return "Scope filter [compile=" + compileScope + ", runtime=" + runtimeScope + ", test=" + testScope
-            + ", provided=" + providedScope + ", system=" + systemScope + "]";
+        return "Scope filter [null-scope=" + includeNullScope + ", compile=" + includeCompileScope + ", runtime=" + includeRuntimeScope + ", test=" + includeTestScope
+            + ", provided=" + includeProvidedScope + ", system=" + includeSystemScope + "]";
     }
 
     public void reportFilteredArtifacts( Logger logger )
@@ -187,6 +190,11 @@
             StringBuffer buffer = new StringBuffer();
 
             boolean report = false;
+            if ( !nullScopeHit )
+            {
+                buffer.append( "\no [Null Scope]" );
+                report = true;
+            }
             if ( !compileScopeHit )
             {
                 buffer.append( "\no Compile" );
@@ -224,6 +232,10 @@
     {
         boolean report = false;
 
+        if ( !nullScopeHit )
+        {
+            report = true;
+        }
         if ( !compileScopeHit )
         {
             report = true;
@@ -247,4 +259,145 @@
 
         return report;
     }
+    
+    public boolean isIncludeCompileScope()
+    {
+        return includeCompileScope;
+    }
+
+    public ScopeArtifactFilter setIncludeCompileScope( boolean includeCompileScope )
+    {
+        this.includeCompileScope = includeCompileScope;
+        
+        return this;
+    }
+
+    public boolean isIncludeRuntimeScope()
+    {
+        return includeRuntimeScope;
+    }
+
+    public ScopeArtifactFilter setIncludeRuntimeScope( boolean includeRuntimeScope )
+    {
+        this.includeRuntimeScope = includeRuntimeScope;
+        
+        return this;
+    }
+
+    public boolean isIncludeTestScope()
+    {
+        return includeTestScope;
+    }
+
+    public ScopeArtifactFilter setIncludeTestScope( boolean includeTestScope )
+    {
+        this.includeTestScope = includeTestScope;
+        
+        return this;
+    }
+
+    public boolean isIncludeProvidedScope()
+    {
+        return includeProvidedScope;
+    }
+
+    public ScopeArtifactFilter setIncludeProvidedScope( boolean includeProvidedScope )
+    {
+        this.includeProvidedScope = includeProvidedScope;
+        
+        return this;
+    }
+
+    public boolean isIncludeSystemScope()
+    {
+        return includeSystemScope;
+    }
+
+    public ScopeArtifactFilter setIncludeSystemScope( boolean includeSystemScope )
+    {
+        this.includeSystemScope = includeSystemScope;
+        
+        return this;
+    }
+    
+    /**
+     * Manages the following scopes:
+     * 
+     * <ul>
+     *   <li>system</li>
+     *   <li>provided</li>
+     *   <li>compile</li>
+     * </ul>
+     */
+    public ScopeArtifactFilter setIncludeCompileScopeWithImplications( boolean enabled )
+    {
+        includeSystemScope = enabled;
+        includeProvidedScope = enabled;
+        includeCompileScope = enabled;
+        
+        return this;
+    }
+    
+    /**
+     * Manages the following scopes:
+     * 
+     * <ul>
+     *   <li>compile</li>
+     *   <li>runtime</li>
+     * </ul>
+     */
+    public ScopeArtifactFilter setIncludeRuntimeScopeWithImplications( boolean enabled )
+    {
+        includeCompileScope = enabled;
+        includeRuntimeScope = enabled;
+        
+        return this;
+    }
+
+    /**
+     * Manages the following scopes:
+     * 
+     * <ul>
+     *   <li>system</li>
+     *   <li>provided</li>
+     *   <li>compile</li>
+     *   <li>runtime</li>
+     *   <li>test</li>
+     * </ul>
+     */
+    public ScopeArtifactFilter setIncludeTestScopeWithImplications( boolean enabled )
+    {
+        includeSystemScope = enabled;
+        includeProvidedScope = enabled;
+        includeCompileScope = enabled;
+        includeRuntimeScope = enabled;
+        includeTestScope = enabled;
+        
+        return this;
+    }
+    
+    /**
+     * Determine whether artifacts that have a null scope are included or excluded.
+     */
+    public ScopeArtifactFilter setIncludeNullScope( boolean enable )
+    {
+        includeNullScope = enable;
+        
+        return this;
+    }
+    
+    /**
+     * Reset hit counts and tracking of filtered artifacts, BUT NOT ENABLED SCOPES.
+     */
+    public ScopeArtifactFilter reset()
+    {
+        compileScopeHit = false;
+        runtimeScopeHit = false;
+        testScopeHit = false;
+        providedScopeHit = false;
+        systemScopeHit = false;
+        filteredArtifactIds.clear();
+        
+        return this;
+    }
 }

Modified: maven/shared/trunk/maven-common-artifact-filters/src/test/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilterTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-common-artifact-filters/src/test/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilterTest.java?rev=698679&r1=698678&r2=698679&view=diff
==============================================================================
--- maven/shared/trunk/maven-common-artifact-filters/src/test/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilterTest.java (original)
+++ maven/shared/trunk/maven-common-artifact-filters/src/test/java/org/apache/maven/shared/artifact/filter/ScopeArtifactFilterTest.java Wed Sep 24 11:05:08 2008
@@ -44,6 +44,149 @@
 
     private MockManager mockManager = new MockManager();
 
+    public void testNullScopeDisabled()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeNullScope( false );
+        
+        verifyExcluded( filter, null );
+    }
+
+    public void testFineGrained_IncludeOnlyScopesThatWereEnabled_TestScope()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeTestScope( true );
+        
+        verifyExcluded( filter, Artifact.SCOPE_COMPILE );
+        verifyExcluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyExcluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyExcluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyIncluded( filter, Artifact.SCOPE_TEST );
+        verifyIncluded( filter, null );
+    }
+
+    public void testFineGrained_IncludeOnlyScopesThatWereEnabled_CompileScope()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeCompileScope( true );
+        
+        verifyIncluded( filter, Artifact.SCOPE_COMPILE );
+        verifyExcluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyExcluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyExcluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+        verifyIncluded( filter, null );
+    }
+
+    public void testFineGrained_IncludeOnlyScopesThatWereEnabled_RuntimeScope()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeRuntimeScope( true );
+        
+        verifyExcluded( filter, Artifact.SCOPE_COMPILE );
+        verifyExcluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyIncluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyExcluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+        verifyIncluded( filter, null );
+    }
+
+    public void testFineGrained_IncludeOnlyScopesThatWereEnabled_ProvidedScope()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeProvidedScope( true );
+        
+        verifyExcluded( filter, Artifact.SCOPE_COMPILE );
+        verifyIncluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyExcluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyExcluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+        verifyIncluded( filter, null );
+    }
+
+    public void testFineGrained_IncludeOnlyScopesThatWereEnabled_SystemScope()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeSystemScope( true );
+        
+        verifyExcluded( filter, Artifact.SCOPE_COMPILE );
+        verifyExcluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyExcluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyIncluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+        verifyIncluded( filter, null );
+    }
+
+    public void testFineGrained_IncludeOnlyScopesThatWereEnabled_ProvidedAndRuntimeScopes()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeRuntimeScope( true );
+        filter.setIncludeProvidedScope( true );
+        
+        verifyExcluded( filter, Artifact.SCOPE_COMPILE );
+        verifyIncluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyIncluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyExcluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+        verifyIncluded( filter, null );
+    }
+
+    public void testFineGrained_IncludeOnlyScopesThatWereEnabled_SystemAndRuntimeScopes()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeRuntimeScope( true );
+        filter.setIncludeSystemScope( true );
+        
+        verifyExcluded( filter, Artifact.SCOPE_COMPILE );
+        verifyExcluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyIncluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyIncluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+        verifyIncluded( filter, null );
+    }
+
+    public void testFineGrainedWithImplications_CompileScopeShouldIncludeOnlyArtifactsWithNullSystemProvidedOrCompileScopes()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeCompileScopeWithImplications( true );
+
+        verifyIncluded( filter, null );
+        verifyIncluded( filter, Artifact.SCOPE_COMPILE );
+        verifyIncluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyIncluded( filter, Artifact.SCOPE_SYSTEM );
+
+        verifyExcluded( filter, Artifact.SCOPE_RUNTIME );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+    }
+
+    public void testFineGrainedWithImplications_RuntimeScopeShouldIncludeOnlyArtifactsWithNullRuntimeOrCompileScopes()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeRuntimeScopeWithImplications( true );
+
+        verifyIncluded( filter, null );
+        verifyIncluded( filter, Artifact.SCOPE_COMPILE );
+        verifyIncluded( filter, Artifact.SCOPE_RUNTIME );
+
+        verifyExcluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyExcluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyExcluded( filter, Artifact.SCOPE_TEST );
+    }
+
+    public void testFineGrainedWithImplications_TestScopeShouldIncludeAllScopes()
+    {
+        ScopeArtifactFilter filter = new ScopeArtifactFilter();
+        filter.setIncludeTestScopeWithImplications( true );
+
+        verifyIncluded( filter, null );
+        verifyIncluded( filter, Artifact.SCOPE_COMPILE );
+        verifyIncluded( filter, Artifact.SCOPE_RUNTIME );
+
+        verifyIncluded( filter, Artifact.SCOPE_PROVIDED );
+        verifyIncluded( filter, Artifact.SCOPE_SYSTEM );
+        verifyIncluded( filter, Artifact.SCOPE_TEST );
+    }
+    
     public void testScopesShouldIncludeArtifactWithSameScope()
     {
         verifyIncluded( Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE );
@@ -51,7 +194,7 @@
         verifyIncluded( Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME );
         verifyIncluded( Artifact.SCOPE_SYSTEM, Artifact.SCOPE_SYSTEM );
         verifyIncluded( Artifact.SCOPE_TEST, Artifact.SCOPE_TEST );
-        verifyIncluded( null, null );
+        verifyIncluded( (String) null, null );
     }
 
     public void testCompileScopeShouldIncludeOnlyArtifactsWithNullSystemProvidedOrCompileScopes()
@@ -155,6 +298,36 @@
         mockManager.clear();
     }
 
+    private void verifyIncluded( ScopeArtifactFilter filter, String artifactScope )
+    {
+        ArtifactMockAndControl mac = new ArtifactMockAndControl( artifactScope );
+
+        mockManager.replayAll();
+
+        assertTrue( "Artifact scope: " + artifactScope + " SHOULD BE included", filter
+            .include( mac.artifact ) );
+
+        mockManager.verifyAll();
+
+        // enable multiple calls to this method within a single test.
+        mockManager.clear();
+    }
+
+    private void verifyExcluded( ScopeArtifactFilter filter, String artifactScope )
+    {
+        ArtifactMockAndControl mac = new ArtifactMockAndControl( artifactScope );
+
+        mockManager.replayAll();
+
+        assertFalse( "Artifact scope: " + artifactScope + " SHOULD BE excluded", filter
+            .include( mac.artifact ) );
+
+        mockManager.verifyAll();
+
+        // enable multiple calls to this method within a single test.
+        mockManager.clear();
+    }
+
     private final class ArtifactMockAndControl
     {
         Artifact artifact;