From 4884e50e825507c500b41c9b32fd1ab3e7ad3997 Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Thu, 2 Jan 2020 11:59:17 +0100 Subject: [PATCH 1/3] Upgrade JUnit to latest version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index cfbdf0c61..3d61abb66 100644 --- a/pom.xml +++ b/pom.xml @@ -283,7 +283,7 @@ under the License. junit junit - 4.11 + 4.13 test From b9ba335b5e467b836caf368831b218e1bfc0b9a7 Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Thu, 2 Jan 2020 15:07:23 +0100 Subject: [PATCH 2/3] Use full Hamcrest library --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index 3d61abb66..d32f9c225 100644 --- a/pom.xml +++ b/pom.xml @@ -286,6 +286,12 @@ under the License. 4.13 test + + org.hamcrest + hamcrest-all + 1.3 + test + org.apache.maven.plugin-testing maven-plugin-testing-tools From 2b6c1f06e78bbf23457af032133220ab9bc0a84f Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Thu, 2 Jan 2020 15:07:54 +0100 Subject: [PATCH 3/3] First two test cases re-implemented as unit test --- .../testUtils/stubs/CapturingLog.java | 171 ++++++++++++++++++ .../plugins/dependency/tree/TestTreeMojo.java | 155 +++++++++++++--- 2 files changed, 303 insertions(+), 23 deletions(-) create mode 100644 src/test/java/org/apache/maven/plugins/dependency/testUtils/stubs/CapturingLog.java 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 000000000..2c6227313 --- /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 5a640dd4f..666d43539 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 @@ * 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 TreeMojo. @@ -39,33 +65,108 @@ * @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.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 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 void _testTreeTestEnvironment() 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 void _testTreeTestEnvironment() * Test the DOT format serialization * * @throws Exception in case of an error. - */ + * public void _testTreeDotSerializing() throws Exception { @@ -112,7 +220,7 @@ public void _testTreeDotSerializing() * Test the GraphML format serialization * * @throws Exception in case of an error. - */ + * public void _testTreeGraphMLSerializing() throws Exception { @@ -131,7 +239,7 @@ public void _testTreeGraphMLSerializing() * Test the TGF format serialization * * @throws Exception in case of an error. - */ + * public void _testTreeTGFSerializing() throws Exception { @@ -148,7 +256,7 @@ public void _testTreeTGFSerializing() * @param format The format. * @throws Exception in case of an error. * @return list of strings in the output file - */ + * private List runTreeMojo( String outputFile, String format ) throws Exception { @@ -190,7 +298,7 @@ private List runTreeMojo( String outputFile, String format ) * * @param contents The contents. * @param str The content which should be checked for. - */ + * private boolean findString( List contents, String str ) { for ( String line : contents ) @@ -233,4 +341,5 @@ private void assertNodeEquals( String expectedGroupId, String expectedArtifactId assertEquals( "version", expectedVersion, actualArtifact.getVersion() ); assertEquals( "scope", expectedScope, actualArtifact.getScope() ); } + */ }