From 32053c99d745744bcd7f71bd817946456a5bb9c8 Mon Sep 17 00:00:00 2001 From: Robert Scholte Date: Mon, 12 Jan 2015 23:23:31 +0100 Subject: [PATCH] Add junittests for DefaultToolchain Implement toString() for better log messages --- .../maven/toolchain/DefaultToolchain.java | 35 +++++++ .../toolchain/RequirementMatcherFactory.java | 15 ++- .../maven/toolchain/DefaultToolchainTest.java | 97 ++++++++++++++++++- .../RequirementMatcherFactoryTest.java | 6 ++ 4 files changed, 150 insertions(+), 3 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java index 733f30834a..043e5e68cf 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java @@ -20,6 +20,7 @@ package org.apache.maven.toolchain; */ import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Properties; @@ -47,6 +48,11 @@ public abstract class DefaultToolchain // should have been AbstractToolchain... private Logger logger; + /** + * + * @param model the model, must not be {@code null} + * @param logger the logger, must not be {@code null} + */ protected DefaultToolchain( ToolchainModel model, Logger logger ) { this.model = model; @@ -54,6 +60,12 @@ public abstract class DefaultToolchain // should have been AbstractToolchain... this.logger = logger; } + /** + * + * @param model the model, must not be {@code null} + * @param type the type + * @param logger the logger, must not be {@code null} + */ protected DefaultToolchain( ToolchainModel model, String type, Logger logger ) { this( model, logger ); @@ -152,4 +164,27 @@ public abstract class DefaultToolchain // should have been AbstractToolchain... } return hashCode; } + + @Override + public String toString() + { + StringBuilder builder = new StringBuilder(); + builder.append( "type:" ).append( getType() ); + builder.append( '{' ); + + Iterator> providesIter = provides.entrySet().iterator(); + while ( providesIter.hasNext() ) + { + Map.Entry provideEntry = providesIter.next(); + builder.append( provideEntry.getKey() ).append( " = " ).append( provideEntry.getValue() ); + if ( providesIter.hasNext() ) + { + builder.append( ';' ); + } + } + + builder.append( '}' ); + + return builder.toString(); + } } diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/RequirementMatcherFactory.java b/maven-core/src/main/java/org/apache/maven/toolchain/RequirementMatcherFactory.java index c638a2f8d7..3d6917bd82 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/RequirementMatcherFactory.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/RequirementMatcherFactory.java @@ -54,16 +54,22 @@ public final class RequirementMatcherFactory this.provides = provides; } + @Override public boolean matches( String requirement ) { return provides.equalsIgnoreCase( requirement ); } + + @Override + public String toString() + { + return provides; + } } private static final class VersionMatcher implements RequirementMatcher { - DefaultArtifactVersion version; private VersionMatcher( String version ) @@ -71,6 +77,7 @@ public final class RequirementMatcherFactory this.version = new DefaultArtifactVersion( version ); } + @Override public boolean matches( String requirement ) { try @@ -92,5 +99,11 @@ public final class RequirementMatcherFactory return false; } } + + @Override + public String toString() + { + return version.toString(); + } } } \ No newline at end of file diff --git a/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainTest.java b/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainTest.java index 9eea862ca2..3bba8177d1 100644 --- a/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainTest.java +++ b/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainTest.java @@ -19,21 +19,114 @@ package org.apache.maven.toolchain; * under the License. */ +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; import java.io.InputStream; +import java.util.Collections; import org.apache.maven.toolchain.java.DefaultJavaToolChain; import org.apache.maven.toolchain.model.PersistedToolchains; import org.apache.maven.toolchain.model.ToolchainModel; import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader; +import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.util.IOUtil; +import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; public class DefaultToolchainTest { + @Mock + private Logger logger; + private MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader(); + + @Before + public void setUp() throws Exception + { + MockitoAnnotations.initMocks( this ); + } + + private DefaultToolchain newDefaultToolchain( ToolchainModel model ) + { + return new DefaultToolchain( model, logger ) + { + @Override + public String findTool( String toolName ) + { + return null; + } + }; + } + + private DefaultToolchain newDefaultToolchain( ToolchainModel model, String type ) + { + return new DefaultToolchain( model, type, logger ) + { + @Override + public String findTool( String toolName ) + { + return null; + } + }; + } + + @Test + public void testGetModel() + { + ToolchainModel model = new ToolchainModel(); + DefaultToolchain toolchain = newDefaultToolchain( model ); + assertEquals( model, toolchain.getModel() ); + } + + @Test + public void testGetType() + { + ToolchainModel model = new ToolchainModel(); + DefaultToolchain toolchain = newDefaultToolchain( model, "TYPE" ); + assertEquals( "TYPE", toolchain.getType() ); + + model.setType( "MODEL_TYPE" ); + toolchain = newDefaultToolchain( model ); + assertEquals( "MODEL_TYPE", toolchain.getType() ); + } + + @Test + public void testGetLogger() + { + ToolchainModel model = new ToolchainModel(); + DefaultToolchain toolchain = newDefaultToolchain( model ); + assertEquals( logger, toolchain.getLog() ); + } + + @Test + public void testMissingRequirementProperty() + { + ToolchainModel model = new ToolchainModel(); + model.setType( "TYPE" ); + DefaultToolchain toolchain = newDefaultToolchain( model ); + + assertFalse( toolchain.matchesRequirements( Collections.singletonMap( "name", "John Doe" ) ) ); + verify( logger ).debug( "Toolchain type:TYPE{} is missing required property: name" ); + } + + + @Test + public void testNonMatchingRequirementProperty() + { + ToolchainModel model = new ToolchainModel(); + model.setType( "TYPE" ); + DefaultToolchain toolchain = newDefaultToolchain( model ); + toolchain.addProvideToken( "name", RequirementMatcherFactory.createExactMatcher( "Jane Doe" ) ); + + assertFalse( toolchain.matchesRequirements( Collections.singletonMap( "name", "John Doe" ) ) ); + verify( logger ).debug( "Toolchain type:TYPE{name = Jane Doe} doesn't match required property: name" ); + } + @Test public void testEquals() throws Exception @@ -48,8 +141,8 @@ public class DefaultToolchainTest PersistedToolchains jdks = reader.read( jdksIS ); PersistedToolchains jdksExtra = reader.read( jdksExtraIS ); - DefaultJavaToolChain tc1 = new DefaultJavaToolChain( jdks.getToolchains().get( 0 ), null ); - DefaultJavaToolChain tc2 = new DefaultJavaToolChain( jdksExtra.getToolchains().get( 0 ), null ); + DefaultToolchain tc1 = new DefaultJavaToolChain( jdks.getToolchains().get( 0 ), null ); + DefaultToolchain tc2 = new DefaultJavaToolChain( jdksExtra.getToolchains().get( 0 ), null ); assertTrue( tc1.equals( tc1 ) ); assertFalse( tc1.equals( tc2 ) ); diff --git a/maven-core/src/test/java/org/apache/maven/toolchain/RequirementMatcherFactoryTest.java b/maven-core/src/test/java/org/apache/maven/toolchain/RequirementMatcherFactoryTest.java index 29010c995c..c102b11f81 100644 --- a/maven-core/src/test/java/org/apache/maven/toolchain/RequirementMatcherFactoryTest.java +++ b/maven-core/src/test/java/org/apache/maven/toolchain/RequirementMatcherFactoryTest.java @@ -61,6 +61,12 @@ public class RequirementMatcherFactoryTest assertFalse( matcher.matches( "(1.5.2,1.6)" ) ); assertTrue( matcher.matches( "(1.4,1.5.2]" ) ); assertTrue( matcher.matches( "(1.5,)" ) ); + assertEquals( "1.5.2", matcher.toString() ); + + // Ensure it is not printed as 1.5.0 + matcher = RequirementMatcherFactory.createVersionMatcher( "1.5" ); + assertEquals( "1.5", matcher.toString() ); + } }