You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vm...@apache.org on 2006/08/29 17:51:27 UTC

svn commit: r438105 - in /maven/plugins/trunk/maven-clover-plugin/src: main/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojo.java test/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojoTest.java

Author: vmassol
Date: Tue Aug 29 08:51:26 2006
New Revision: 438105

URL: http://svn.apache.org/viewvc?rev=438105&view=rev
Log:
MCLOVER-51: Clover plugin should use the original dependency if it's newer than the clovered one

Modified:
    maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojo.java
    maven/plugins/trunk/maven-clover-plugin/src/test/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojoTest.java

Modified: maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojo.java?rev=438105&r1=438104&r2=438105&view=diff
==============================================================================
--- maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojo.java (original)
+++ maven/plugins/trunk/maven-clover-plugin/src/main/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojo.java Tue Aug 29 08:51:26 2006
@@ -19,6 +19,8 @@
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -256,7 +258,7 @@
             swizzleCloverDependencies( getProject().getArtifacts() ) );
     }
 
-    private Set swizzleCloverDependencies(Set artifacts)
+    protected Set swizzleCloverDependencies(Set artifacts)
     {
         Set resolvedArtifacts = new HashSet();
         for ( Iterator i = artifacts.iterator(); i.hasNext(); )
@@ -281,9 +283,30 @@
                     // Set the same scope as the main artifact as this is not set by createArtifactWithClassifier.
                     cloveredArtifact.setScope( artifact.getScope() );
 
-                    resolvedArtifacts.add( cloveredArtifact );
+                    // Check the timestamp of the artifact. If the found clovered version is older than the
+                    // non-clovered one we need to use the non-clovered version. This is to handle use case such as:
+                    // - Say you have a module B that depends on a module A
+                    // - You run Clover on A
+                    // - You make modifications on A such that B would fail if not built with the latest version of A
+                    // - You try to run the Clover plugin on B. The build would fail if we didn't pick the latest
+                    //   version between the original A version and the clovered version.
+                    if (cloveredArtifact.getFile().lastModified() < artifact.getFile().lastModified())
+                    {
+                        getLog().warn("Using [" + artifact.getId() + "] even though a Clovered version exists "
+                            + "but it's older and could fail the build. Please consider running Clover again on that "
+                            + "dependency's project.");
+                        resolvedArtifacts.add( artifact );
+                    }
+                    else
+                    {
+                        resolvedArtifacts.add( cloveredArtifact );
+                    }
                 }
-                catch ( Exception e )
+                catch ( ArtifactResolutionException e )
+                {
+                    resolvedArtifacts.add( artifact );
+                }
+                catch ( ArtifactNotFoundException e )
                 {
                     resolvedArtifacts.add( artifact );
                 }
@@ -457,5 +480,15 @@
         }
 
         return (String[]) parameters.toArray(new String[0]);
+    }
+
+    protected void setArtifactFactory(ArtifactFactory artifactFactory)
+    {
+        this.artifactFactory = artifactFactory;
+    }
+
+    protected void setArtifactResolver(ArtifactResolver artifactResolver)
+    {
+        this.artifactResolver = artifactResolver;
     }
 }

Modified: maven/plugins/trunk/maven-clover-plugin/src/test/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-clover-plugin/src/test/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojoTest.java?rev=438105&r1=438104&r2=438105&view=diff
==============================================================================
--- maven/plugins/trunk/maven-clover-plugin/src/test/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojoTest.java (original)
+++ maven/plugins/trunk/maven-clover-plugin/src/test/java/org/apache/maven/plugin/clover/CloverInstrumentInternalMojoTest.java Tue Aug 29 08:51:26 2006
@@ -18,8 +18,13 @@
 import org.jmock.MockObjectTestCase;
 import org.jmock.Mock;
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.plugin.logging.Log;
 
 import java.util.Collections;
+import java.util.Set;
+import java.io.File;
 
 /**
  * Unit tests for {@link org.apache.maven.plugin.clover.CloverInstrumentInternalMojo}.
@@ -29,14 +34,41 @@
  */
 public class CloverInstrumentInternalMojoTest extends MockObjectTestCase
 {
+    private CloverInstrumentInternalMojo mojo;
+
+    /**
+     * Class used to return a given value when lastModified is called. This is because File.getLastModified always
+     * return 0L if the file doesn't exist and our tests below do not point to existing files.
+     */
+    public class MockFile extends File
+    {
+        private long lastModifiedDate;
+
+        public MockFile(String file, long lastModifiedDate)
+        {
+            super(file);
+            this.lastModifiedDate = lastModifiedDate;
+        }
+
+        public long lastModified()
+        {
+            return this.lastModifiedDate;
+        }
+    }
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        this.mojo = new CloverInstrumentInternalMojo();
+    }
+
     public void testFindCloverArtifactWithCorrectArtifactIdButWrongGroupId()
     {
         Mock mockArtifact = mock(Artifact.class);
         mockArtifact.stubs().method( "getArtifactId" ).will( returnValue( "clover" ) );
         mockArtifact.stubs().method( "getGroupId" ).will( returnValue( "notcenquaid" ) );
 
-        CloverInstrumentInternalMojo mojo = new CloverInstrumentInternalMojo();
-        Artifact clover = mojo.findCloverArtifact( Collections.singletonList( mockArtifact.proxy() ) );
+        Artifact clover = this.mojo.findCloverArtifact( Collections.singletonList( mockArtifact.proxy() ) );
 
         assertNull( "Clover artifact should not have been found!", clover );
     }
@@ -47,9 +79,92 @@
         mockArtifact.stubs().method( "getArtifactId" ).will( returnValue( "clover" ) );
         mockArtifact.stubs().method( "getGroupId" ).will( returnValue( "com.cenqua.clover" ) );
 
-        CloverInstrumentInternalMojo mojo = new CloverInstrumentInternalMojo();
-        Artifact clover = mojo.findCloverArtifact( Collections.singletonList( mockArtifact.proxy() ) );
+        Artifact clover = this.mojo.findCloverArtifact( Collections.singletonList( mockArtifact.proxy() ) );
 
         assertNotNull( "Clover artifact should have been found!", clover );
+    }
+
+    public void testSwizzleCloverDependenciesWhenDependencyHasClassifier()
+    {
+        Artifact artifact = setUpMockArtifact( "some.groupId", "someArtifactId", "1.0", "jar", "compile", "whatever",
+            null );
+
+        Set resultSet = this.mojo.swizzleCloverDependencies( Collections.singleton( artifact ) );
+        assertEquals( 1, resultSet.size() );
+        assertTrue( "Resulting artifact should have been the original one", resultSet.contains( artifact ) );
+    }
+
+    public void testSwizzleCloverDependenciesWhenCloveredVersionOfDependencyIsNewerThanOriginal()
+    {
+        // Ensure that the original artifact is older than the clovered artifact so that the clovered artifact
+        // is picked. Note that that we use -500/-1000 to ensure not to set the time in the future as maybe
+        // this could cause some problems on some OS.
+        long now = System.currentTimeMillis();
+        File artifactFile = new MockFile( "some/file/artifact", now - 1000L );
+        File cloveredArtifactFile = new MockFile( "some/file/cloveredArtifact", now - 500L );
+
+        Artifact artifact = setUpMockArtifact( "some.groupId", "someArtifactId", "1.0", "jar", "compile", null,
+            artifactFile );
+        Artifact cloveredArtifact = setUpMockArtifact( null, null, null, null, null, null, cloveredArtifactFile );
+
+        setUpCommonMocksForSwizzleCloverDependenciesTests(cloveredArtifact);
+
+        Set resultSet = this.mojo.swizzleCloverDependencies( Collections.singleton( artifact ) );
+        assertEquals( 1, resultSet.size() );
+        assertTrue( "Resulting artifact should have been the clovered one", resultSet.contains( cloveredArtifact ) );
+    }
+
+    public void testSwizzleCloverDependenciesWhenOriginalVersionOfDependencyIsNewerThanCloveredOne()
+    {
+        // Ensure that the clovered artifact is older than the original artifact so that the original artifact
+        // is picked. Note that that we use -500/-1000 to ensure not to set the time in the future as maybe
+        // this could cause some problems on some OS.
+        long now = System.currentTimeMillis();
+        File artifactFile = new MockFile( "some/file/artifact", now - 500L );
+        File cloveredArtifactFile = new MockFile( "some/file/cloveredArtifact", now - 1000L );
+
+        Artifact artifact = setUpMockArtifact( "some.groupId", "someArtifactId", "1.0", "jar", "compile", null,
+            artifactFile );
+        Artifact cloveredArtifact = setUpMockArtifact( null, null, null, null, null, null, cloveredArtifactFile );
+
+        setUpCommonMocksForSwizzleCloverDependenciesTests(cloveredArtifact);
+
+        Set resultSet = this.mojo.swizzleCloverDependencies( Collections.singleton( artifact ) );
+        assertEquals( 1, resultSet.size() );
+        assertTrue( "Resulting artifact should have been the original one", resultSet.contains( artifact ) );
+    }
+
+    private void setUpCommonMocksForSwizzleCloverDependenciesTests(Artifact cloveredArtifact)
+    {
+        Mock mockArtifactFactory = mock( ArtifactFactory.class );
+        mockArtifactFactory.stubs().method( "createArtifactWithClassifier" ).will(returnValue( cloveredArtifact ) );
+
+        Mock mockArtifactResolver = mock( ArtifactResolver.class );
+        mockArtifactResolver.stubs().method( "resolve" );
+
+        Mock mockLog = mock( Log.class );
+        mockLog.stubs().method( "warn" );
+
+        this.mojo.setArtifactFactory( ( ArtifactFactory ) mockArtifactFactory.proxy() );
+        this.mojo.setArtifactResolver( ( ArtifactResolver ) mockArtifactResolver.proxy() );
+        this.mojo.setLog( (Log) mockLog.proxy() );
+    }
+
+    private Artifact setUpMockArtifact(String groupId, String artifactId, String version, String type, String scope,
+        String classifier, File file)
+    {
+        Mock mockArtifact = mock( Artifact.class );
+        mockArtifact.stubs().method( "getClassifier" ).will( returnValue( classifier ) );
+        mockArtifact.stubs().method( "getGroupId" ).will( returnValue( groupId ) );
+        mockArtifact.stubs().method( "getArtifactId" ).will( returnValue( artifactId ) );
+        mockArtifact.stubs().method( "getVersion" ).will( returnValue( version ) );
+        mockArtifact.stubs().method( "getType" ).will( returnValue( type ) );
+        mockArtifact.stubs().method( "getScope" ).will( returnValue( scope ) );
+        mockArtifact.stubs().method( "getFile" ).will( returnValue ( file ) );
+        mockArtifact.stubs().method( "getId" ).will( returnValue (
+            groupId + ":" + artifactId + ":" + version + ":" + classifier ) );
+        mockArtifact.stubs().method( "setScope" );
+
+        return (Artifact) mockArtifact.proxy(); 
     }
 }