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

[maven-dependency-plugin] branch improve-tests-for-tree-goal created (now 3edb35d)

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

slachiewicz pushed a change to branch improve-tests-for-tree-goal
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git.


      at 3edb35d  First two test cases re-implemented as unit test

This branch includes the following new commits:

     new 686135d  Use full Hamcrest library
     new 3edb35d  First two test cases re-implemented as unit test

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-dependency-plugin] 01/02: Use full Hamcrest library

Posted by sl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch improve-tests-for-tree-goal
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git

commit 686135db84c427c285bc1e95f16949cd8ed2e0c3
Author: Maarten Mulders <m....@xs4all.nl>
AuthorDate: Thu Jan 2 15:07:23 2020 +0100

    Use full Hamcrest library
---
 pom.xml | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/pom.xml b/pom.xml
index 4169862..67dcef5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -284,6 +284,12 @@ under the License.
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-all</artifactId>
+      <version>1.3</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>org.apache.maven.plugin-testing</groupId>
       <artifactId>maven-plugin-testing-tools</artifactId>
       <version>${pluginTestingVersion}</version>


[maven-dependency-plugin] 02/02: First two test cases re-implemented as unit test

Posted by sl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch improve-tests-for-tree-goal
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git

commit 3edb35db06d56d1451bc39bfc50c1036ced08f66
Author: Maarten Mulders <m....@xs4all.nl>
AuthorDate: Thu Jan 2 15:07:54 2020 +0100

    First two test cases re-implemented as unit test
---
 .../dependency/testUtils/stubs/CapturingLog.java   | 171 +++++++++++++++++++++
 .../plugins/dependency/tree/TestTreeMojo.java      | 155 ++++++++++++++++---
 2 files changed, 303 insertions(+), 23 deletions(-)

diff --git a/src/test/java/org/apache/maven/plugins/dependency/testUtils/stubs/CapturingLog.java b/src/test/java/org/apache/maven/plugins/dependency/testUtils/stubs/CapturingLog.java
new file mode 100644
index 0000000..2c62273
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugins/dependency/testUtils/stubs/CapturingLog.java
@@ -0,0 +1,171 @@
+package org.apache.maven.plugins.dependency.testUtils.stubs;
+
+/*
+ * 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.plugin.logging.Log;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+public class CapturingLog implements Log
+{
+    private static final String NEWLINE = System.lineSeparator();
+
+    private StringBuffer buffer = new StringBuffer();
+
+    @Override
+    public boolean isDebugEnabled()
+    {
+        return true;
+    }
+
+    @Override
+    public void debug( CharSequence content )
+    {
+        buffer.append("DEBUG ");
+        buffer.append(content);
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void debug( CharSequence content, Throwable error )
+    {
+        buffer.append("DEBUG ");
+        buffer.append(content);
+        buffer.append(' ');
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void debug( Throwable error )
+    {
+        buffer.append("DEBUG ");
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public boolean isInfoEnabled()
+    {
+        return true;
+    }
+
+    @Override
+    public void info( CharSequence content )
+    {
+        buffer.append("INFO ");
+        buffer.append(content);
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void info( CharSequence content, Throwable error )
+    {
+        buffer.append("INFO ");
+        buffer.append(content);
+        buffer.append(' ');
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void info( Throwable error )
+    {
+        buffer.append("INFO ");
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public boolean isWarnEnabled() {
+        return true;
+    }
+
+    @Override
+    public void warn( CharSequence content )
+    {
+        buffer.append("WARNING ");
+        buffer.append(content);
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void warn( CharSequence content, Throwable error )
+    {
+        buffer.append("WARNING ");
+        buffer.append(content);
+        buffer.append(' ');
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void warn( Throwable error )
+    {
+        buffer.append("WARNING ");
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public boolean isErrorEnabled()
+    {
+        return true;
+    }
+
+    @Override
+    public void error( CharSequence content )
+    {
+        buffer.append("ERROR ");
+        buffer.append(content);
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void error( CharSequence content, Throwable error )
+    {
+        buffer.append("ERROR ");
+        buffer.append(content);
+        buffer.append(' ');
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    @Override
+    public void error( Throwable error )
+    {
+        buffer.append("ERROR ");
+        buffer.append( printThrowable( error ) );
+        buffer.append(NEWLINE);
+    }
+
+    private String printThrowable( Throwable error )
+    {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        error.printStackTrace( new PrintStream( output ) );
+        return output.toString();
+    }
+
+    public String getContent()
+    {
+        return this.buffer.toString();
+    }
+}
diff --git a/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java b/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java
index 5a640dd..666d435 100644
--- a/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java
+++ b/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java
@@ -19,17 +19,43 @@ package org.apache.maven.plugins.dependency.tree;
  * under the License.
  */
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
-import java.util.ArrayList;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
-import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
-import org.apache.maven.project.MavenProject;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.dependency.testUtils.stubs.CapturingLog;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
 import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyNode;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;
+import static org.codehaus.plexus.util.ReflectionUtils.setVariableValueInObject;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.number.OrderingComparison.greaterThan;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 /**
  * Tests <code>TreeMojo</code>.
@@ -39,33 +65,108 @@ import org.apache.maven.shared.dependency.graph.DependencyNode;
  * @since 2.0
  */
 public class TestTreeMojo
-    extends AbstractDependencyMojoTestCase
 {
-    // TestCase methods -------------------------------------------------------
+    private static final String GROUP_ID = "org.apache.maven.plugins";
+    private static final String ARTIFACT_ID = "example-artifact";
+    private static final String VERSION = "0.1-SNAPSHOT";
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private DependencyGraphBuilder dependencyGraphBuilder = mock( DependencyGraphBuilder.class );
+    private CapturingLog log = new CapturingLog();
+    private MavenSession session = mock( MavenSession.class );
+
+    private TreeMojo mojo = new TreeMojo();
+
+    @Before
+    public void prepareAndInjectMocks() throws IllegalAccessException, DependencyGraphBuilderException
+    {
+        Artifact root = new DefaultArtifact( GROUP_ID,  ARTIFACT_ID, VERSION, SCOPE_COMPILE, "jar", "", null );
+        DefaultDependencyNode dependencyGraph = new DefaultDependencyNode(null, root, null, null, null);
+        dependencyGraph.setChildren( Collections.<DependencyNode>emptyList() );
+
+        when( session.getProjectBuildingRequest() ).thenReturn( mock( ProjectBuildingRequest.class ) );
+        when( dependencyGraphBuilder.buildDependencyGraph(
+                nullable( ProjectBuildingRequest.class ),
+                nullable( ArtifactFilter.class ),
+                nullable( Collection.class )
+            )
+        ).thenReturn(dependencyGraph);
+
+        mojo.setLog(log);
+        setVariableValueInObject( mojo, "dependencyGraphBuilder", dependencyGraphBuilder );
+        setVariableValueInObject( mojo, "session", session );
+    }
+
+    @Test
+    public void withSkipParameter_shouldSkipExecution()
+            throws MojoFailureException, MojoExecutionException, DependencyGraphBuilderException
+    {
+        // Arrange
+        mojo.setSkip( true );
+
+        // Act
+        mojo.execute();
+
+        // Assert
+        verify( dependencyGraphBuilder, never() ).buildDependencyGraph( any( ProjectBuildingRequest.class ),
+                                                                        any( ArtifactFilter.class ),
+                                                                        any( Collection.class )
+        );
+    }
+
+    @Test
+    public void withoutOutputFile_shouldWriteToLog()
+            throws MojoFailureException, MojoExecutionException
+    {
+        // Arrange
+
+        // Act
+        mojo.execute();
+
+        // Assert
+        String expectedLine = String.format("%s:%s:jar:%s:%s", GROUP_ID, ARTIFACT_ID, VERSION, SCOPE_COMPILE);
+        assertThat( log.getContent(), containsString( "INFO " + expectedLine ) );
+    }
+
+    @Test
+    public void withOutputFile_shouldWriteToFile()
+            throws MojoFailureException, MojoExecutionException, IOException, IllegalAccessException
+    {
+        // Arrange
+        final File outputFile = temporaryFolder.newFile();
+        setVariableValueInObject( mojo, "outputFile", outputFile );
+
+        // Act
+        mojo.execute();
+
+        // Assert
+        assertThat( outputFile.exists() , is ( true ) );
+        assertThat( outputFile.length() , is ( greaterThan (0L ) ) );
+
+        final List<String> lines = Files.readAllLines( outputFile.toPath(), Charset.defaultCharset() );
+        assertThat( lines.size(), is ( 1 ) );
+        String expectedLine = String.format("%s:%s:jar:%s:%s", GROUP_ID, ARTIFACT_ID, VERSION, SCOPE_COMPILE);
+        assertThat( lines.get( 0 ), is ( expectedLine ) );
+    }
 
     /*
      * @see org.apache.maven.plugin.testing.AbstractMojoTestCase#setUp()
-     */
+     *
     protected void setUp()
         throws Exception
     {
         // required for mojo lookups to work
-        super.setUp( "tree", false );
+        super.setUp( "tree", true );
     }
 
     // tests ------------------------------------------------------------------
 
-    public void testVoid()
-    {
-        // TODO: tests disabled during MDEP-339 work, to be reactivated
-    }
-
     /**
      * Tests the proper discovery and configuration of the mojo.
      *
-     * @throws Exception in case of an error.
-     */
-    public void _testTreeTestEnvironment()
+    public void testTreeTestEnvironment()
         throws Exception
     {
         File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
@@ -83,6 +184,13 @@ public class TestTreeMojo
         project.setArtifacts( artifacts );
         project.setDependencyArtifacts( directArtifacts );
 
+        MavenSession session = newMavenSession( new MavenProjectStub() );
+
+        DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) session.getRepositorySession();
+        repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( stubFactory.getWorkingDir() ) );
+
+        setVariableValueToObject( mojo, "session", session );
+
         mojo.execute();
 
         DependencyNode rootNode = mojo.getDependencyGraph();
@@ -96,7 +204,7 @@ public class TestTreeMojo
      * Test the DOT format serialization
      *
      * @throws Exception in case of an error.
-     */
+     *
     public void _testTreeDotSerializing()
         throws Exception
     {
@@ -112,7 +220,7 @@ public class TestTreeMojo
      * Test the GraphML format serialization
      * 
      * @throws Exception in case of an error.
-     */
+     *
     public void _testTreeGraphMLSerializing()
         throws Exception
     {
@@ -131,7 +239,7 @@ public class TestTreeMojo
      * Test the TGF format serialization
      * 
      * @throws Exception in case of an error.
-     */
+     *
     public void _testTreeTGFSerializing()
         throws Exception
     {
@@ -148,7 +256,7 @@ public class TestTreeMojo
      * @param format The format.
      * @throws Exception in case of an error.
      * @return list of strings in the output file
-     */
+     *
     private List<String> runTreeMojo( String outputFile, String format )
         throws Exception
     {
@@ -190,7 +298,7 @@ public class TestTreeMojo
      * 
      * @param contents The contents.
      * @param str The content which should be checked for.
-     */
+     *
     private boolean findString( List<String> contents, String str )
     {
         for ( String line : contents )
@@ -233,4 +341,5 @@ public class TestTreeMojo
         assertEquals( "version", expectedVersion, actualArtifact.getVersion() );
         assertEquals( "scope", expectedScope, actualArtifact.getScope() );
     }
+    */
 }