Add junittests for DefaultToolchain

Implement toString() for better log messages
This commit is contained in:
Robert Scholte 2015-01-12 23:23:31 +01:00
parent 3e133902fb
commit 32053c99d7
4 changed files with 150 additions and 3 deletions

View File

@ -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<Map.Entry<String, RequirementMatcher>> providesIter = provides.entrySet().iterator();
while ( providesIter.hasNext() )
{
Map.Entry<String, RequirementMatcher> provideEntry = providesIter.next();
builder.append( provideEntry.getKey() ).append( " = " ).append( provideEntry.getValue() );
if ( providesIter.hasNext() )
{
builder.append( ';' );
}
}
builder.append( '}' );
return builder.toString();
}
}

View File

@ -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();
}
}
}

View File

@ -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 ) );

View File

@ -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() );
}
}