Author: bimargulies
Date: Mon Mar 27 14:46:58 2017
New Revision: 1788940

URL: http://svn.apache.org/viewvc?rev=1788940&view=rev
Log:
[MSHADE-253] Re-enable minimization test and add pom project test

Modified:
    
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java

Modified: 
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java?rev=1788940&r1=1788939&r2=1788940&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java
 (original)
+++ 
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java
 Mon Mar 27 14:46:58 2017
@@ -22,9 +22,20 @@ package org.apache.maven.plugins.shade.f
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
 import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+import org.junit.Before;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.mockito.ArgumentCaptor;
 
 import junit.framework.Assert;
@@ -32,37 +43,94 @@ import junit.framework.Assert;
 public class MinijarFilterTest
 {
 
-//    @Test
-//    public void theTestWhichIsTooExpensiveAtTheMoment()
-//        throws IOException
-//    {
-//        ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( 
CharSequence.class );
-//
-//        MavenProject mavenProject = mock( MavenProject.class );
-//        Log log = mock( Log.class );
-//
-//        Artifact artifact = mock( Artifact.class );
-//        when( artifact.getGroupId() ).thenReturn( "com" );
-//        when( artifact.getArtifactId() ).thenReturn( "aid" );
-//        when( artifact.getVersion() ).thenReturn( "1.9" );
-//        when( artifact.getClassifier() ).thenReturn( "classifier1" );
-//        when( artifact.getScope() ).thenReturn( Artifact.SCOPE_COMPILE );
-//
-//        when( mavenProject.getArtifact() ).thenReturn( artifact );
-//
-//        Set<Artifact> artifacts = new TreeSet<Artifact>();
-//        artifacts.add( new DefaultArtifact( "dep.com", "dep.aid", "1.0", 
"compile", "jar", "classifier2", null ) );
-//
-//        when( mavenProject.getArtifacts() ).thenReturn( artifacts );
-//        when( mavenProject.getArtifact().getFile() ).thenReturn( mock( 
File.class ) );
-//
-//        MinijarFilter mf = new MinijarFilter( mavenProject, log );
-//
-//        mf.finished();
-//        verify( log, times( 1 ) ).info( logCaptor.capture() );
-//
-//        Assert.assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
-//    }
+    private File emptyFile;
+
+    @Before
+    public void init() throws IOException {
+        TemporaryFolder tempFolder = new TemporaryFolder();
+        tempFolder.create();
+        this.emptyFile = tempFolder.newFile();
+
+    }
+
+    @Test
+    public void testWithMockProject()
+        throws IOException
+    {
+        ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( 
CharSequence.class );
+
+        MavenProject mavenProject = mockProject(emptyFile);
+
+        Log log = mock( Log.class );
+
+        MinijarFilter mf = new MinijarFilter( mavenProject, log );
+
+        mf.finished();
+
+        verify( log, times( 1 ) ).info( logCaptor.capture() );
+
+        Assert.assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
+
+    }
+
+    @Test
+    public void testWithPomProject()
+        throws IOException
+    {
+        ArgumentCaptor<CharSequence> logCaptor = ArgumentCaptor.forClass( 
CharSequence.class );
+
+        // project with pom packaging and no artifact.
+        MavenProject mavenProject = mockProject(null);
+        mavenProject.setPackaging("pom");
+
+        Log log = mock( Log.class );
+
+        MinijarFilter mf = new MinijarFilter( mavenProject, log );
+
+        mf.finished();
+
+        verify( log, times( 1 ) ).info( logCaptor.capture() );
+
+        // verify no access to project's artifacts
+        verify( mavenProject, times(0) ).getArtifacts();
+
+        Assert.assertEquals( "Minimized 0 -> 0", logCaptor.getValue() );
+
+    }
+
+    private MavenProject mockProject(File file) {
+        MavenProject mavenProject = mock( MavenProject.class );
+
+        Artifact artifact = mock( Artifact.class );
+        when( artifact.getGroupId() ).thenReturn( "com" );
+        when( artifact.getArtifactId() ).thenReturn( "aid" );
+        when( artifact.getVersion() ).thenReturn( "1.9" );
+        when( artifact.getClassifier() ).thenReturn( "classifier1" );
+        when( artifact.getScope() ).thenReturn( Artifact.SCOPE_COMPILE );
+
+        when( mavenProject.getArtifact() ).thenReturn( artifact );
+
+        DefaultArtifact dependencyArtifact =
+            new DefaultArtifact(
+                "dep.com",
+                "dep.aid",
+                "1.0",
+                "compile",
+                "jar",
+                "classifier2",
+                null);
+        dependencyArtifact.setFile(file);
+
+        Set<Artifact> artifacts = new TreeSet<Artifact>();
+        artifacts.add( dependencyArtifact );
+
+        when( mavenProject.getArtifacts() ).thenReturn( artifacts );
+
+        when( mavenProject.getArtifact().getFile() ).thenReturn( file );
+
+        return mavenProject;
+
+    }
 
     @Test
     public void finsishedShouldProduceMessageForClassesTotalNonZero()


Reply via email to