Rewrite assertTrue to assertThat to get more meaningful messages

This commit is contained in:
rfscholte 2019-10-13 15:33:42 +02:00
parent 877fcc9cd5
commit 3f3d775ede
13 changed files with 141 additions and 91 deletions

View File

@ -136,6 +136,11 @@ under the License.
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -15,6 +15,9 @@
* the License.
*/
import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.util.Collections;
import java.util.List;
@ -109,6 +112,6 @@ public void testSystemScopeDependencyIsPresentInTheCompileClasspathElements()
@SuppressWarnings( "deprecation" )
List<Artifact> artifacts = project.getCompileArtifacts();
assertEquals( 1, artifacts.size() );
assertTrue( artifacts.get( 0 ).getFile().getName().endsWith( "tools.jar" ) );
assertThat( artifacts.get( 0 ).getFile().getName(), endsWith( "tools.jar" ) );
}
}

View File

@ -18,20 +18,23 @@
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.project.MavenProject;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThat;
import java.util.List;
import junit.framework.TestCase;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
/**
* @author Benjamin Bentmann
*/
public class DefaultMavenExecutionTest
extends TestCase
{
@Test
public void testCopyDefault()
{
MavenExecutionRequest original = new DefaultMavenExecutionRequest();
@ -40,13 +43,14 @@ public void testCopyDefault()
assertNotSame( copy, original );
}
@Test
public void testResultWithNullTopologicallySortedProjectsIsEmptyList()
{
MavenExecutionResult result = new DefaultMavenExecutionResult();
result.setTopologicallySortedProjects( null );
List<MavenProject> projects = result.getTopologicallySortedProjects();
assertNotNull( projects );
assertTrue( projects.isEmpty() );
assertThat( projects, is( empty() ) );
}
}

View File

@ -15,24 +15,29 @@
* the License.
*/
import junit.framework.TestCase;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
import org.junit.Test;
/**
* @author Kristian Rosenvold
*/
public class ProjectBuildListTest
extends TestCase
{
@Test
public void testGetByTaskSegment()
throws Exception
{
final MavenSession session = ProjectDependencyGraphStub.getMavenSession();
ProjectBuildList projectBuildList = ProjectDependencyGraphStub.getProjectBuildList( session );
TaskSegment taskSegment = projectBuildList.get( 0 ).getTaskSegment();
assertTrue( "This test assumes there are at least 6 elements in projectBuilds", projectBuildList.size() >= 6 );
assertThat( "This test assumes there are at least 6 elements in projectBuilds",
projectBuildList.size(), is( greaterThanOrEqualTo( 6 ) ) );
final ProjectBuildList byTaskSegment = projectBuildList.getByTaskSegment( taskSegment );
assertEquals( projectBuildList.size(),

View File

@ -86,9 +86,7 @@ protected ProjectBuilder getProjectBuilder()
@Override
protected String getCustomConfigurationName()
{
String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
System.out.println( name );
return name;
return AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
}
// ----------------------------------------------------------------------

View File

@ -18,16 +18,18 @@
* specific language governing permissions and limitations
* under the License.
*/
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class DefaultMavenProjectBuilderTest
extends AbstractMavenProjectTestCase
{
@ -287,7 +289,7 @@ public void testBuildParentVersionRangeLocallyWithoutChildVersion() throws Excep
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
}
}
@ -310,7 +312,7 @@ public void testBuildParentVersionRangeLocallyWithChildVersionExpression() throw
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
}
}
@ -352,7 +354,7 @@ public void testBuildParentVersionRangeExternallyWithoutChildVersion() throws Ex
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
}
}
@ -375,7 +377,7 @@ public void testBuildParentVersionRangeExternallyWithChildVersionExpression() th
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
}
}

View File

@ -19,6 +19,10 @@
* under the License.
*/
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@ -76,9 +80,9 @@ public void testEmptyDescriptor()
assertNotNull( ed );
assertNotNull( ed.getExportedPackages() );
assertTrue( ed.getExportedPackages().isEmpty() );
assertThat( ed.getExportedPackages(), is( empty() ) );
assertNotNull( ed.getExportedArtifacts() );
assertTrue( ed.getExportedArtifacts().isEmpty() );
assertThat( ed.getExportedArtifacts(), is( empty() ) );
}
public void testCompleteDescriptor()

View File

@ -19,6 +19,11 @@
* under the License.
*/
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
@ -726,8 +731,8 @@ public void testInterpolationOfLegacyExpressionsThatDontIncludeTheProjectPrefix(
assertEquals( "parent", pom.getValue( "properties/parentArtifactId" ) );
assertEquals( "1.0", pom.getValue( "properties/parentVersion" ) );
assertTrue( pom.getValue( "properties/projectBuildOut" ).toString().endsWith( "bin" ) );
assertTrue( pom.getValue( "properties/projectSiteOut" ).toString().endsWith( "doc" ) );
assertThat( pom.getValue( "properties/projectBuildOut" ).toString(), endsWith( "bin" ) );
assertThat( pom.getValue( "properties/projectSiteOut" ).toString(), endsWith( "doc" ) );
}
public void testInterpolationWithBasedirAlignedDirectories()
@ -936,13 +941,13 @@ public void testMergedFilterOrder()
PomTestWrapper pom = buildPom( "merged-filter-order/sub" );
assertEquals( 7, ( (List<?>) pom.getValue( "build/filters" ) ).size() );
assertTrue( pom.getValue( "build/filters[1]" ).toString().endsWith( "child-a.properties" ) );
assertTrue( pom.getValue( "build/filters[2]" ).toString().endsWith( "child-c.properties" ) );
assertTrue( pom.getValue( "build/filters[3]" ).toString().endsWith( "child-b.properties" ) );
assertTrue( pom.getValue( "build/filters[4]" ).toString().endsWith( "child-d.properties" ) );
assertTrue( pom.getValue( "build/filters[5]" ).toString().endsWith( "parent-c.properties" ) );
assertTrue( pom.getValue( "build/filters[6]" ).toString().endsWith( "parent-b.properties" ) );
assertTrue( pom.getValue( "build/filters[7]" ).toString().endsWith( "parent-d.properties" ) );
assertThat( pom.getValue( "build/filters[1]" ).toString(), endsWith( "child-a.properties" ) );
assertThat( pom.getValue( "build/filters[2]" ).toString(), endsWith( "child-c.properties" ) );
assertThat( pom.getValue( "build/filters[3]" ).toString(), endsWith( "child-b.properties" ) );
assertThat( pom.getValue( "build/filters[4]" ).toString(), endsWith( "child-d.properties" ) );
assertThat( pom.getValue( "build/filters[5]" ).toString(), endsWith( "parent-c.properties" ) );
assertThat( pom.getValue( "build/filters[6]" ).toString(), endsWith( "parent-b.properties" ) );
assertThat( pom.getValue( "build/filters[7]" ).toString(), endsWith( "parent-d.properties" ) );
}
/** MNG-4027*/
@ -1097,7 +1102,7 @@ public void testInterpolationOfRfc3986BaseUri()
PomTestWrapper pom = buildPom( "baseuri-interpolation/pom.xml" );
String prop1 = pom.getValue( "properties/prop1" ).toString();
assertEquals( pom.getBasedir().toPath().toUri().toASCIIString(), prop1 );
assertTrue( prop1.startsWith( "file:///" ) );
assertThat( prop1, startsWith( "file:///" ) );
}
/* MNG-3811*/
@ -1408,8 +1413,8 @@ public void testBooleanInterpolation()
throws Exception
{
PomTestWrapper pom = buildPom( "boolean-interpolation" );
assertTrue ((Boolean) pom.getValue( "repositories[1]/releases/enabled" ) );
assertTrue((Boolean) pom.getValue( "build/resources[1]/filtering" ) );
assertEquals(true, pom.getValue( "repositories[1]/releases/enabled" ) );
assertEquals(true, pom.getValue( "build/resources[1]/filtering" ) );
}
@ -1734,16 +1739,16 @@ public void testPluginDeclarationsRetainPomOrderAfterInjectionOfDefaultPlugins()
Plugin plugin = plugins.get( i );
if ( "maven-resources-plugin".equals( plugin.getArtifactId() ) )
{
assertTrue( resourcesPlugin < 0 );
assertThat( resourcesPlugin, lessThan( 0 ) );
resourcesPlugin = i;
}
else if ( "maven-it-plugin-log-file".equals( plugin.getArtifactId() ) )
{
assertTrue( customPlugin < 0 );
assertThat( customPlugin, lessThan( 0 ) );
customPlugin = i;
}
}
assertTrue( plugins.toString(), customPlugin == resourcesPlugin - 1 );
assertEquals( plugins.toString(), customPlugin, resourcesPlugin - 1 );
}
/** MNG-4415 */

View File

@ -19,6 +19,12 @@
* under the License.
*/
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
@ -155,7 +161,7 @@ public void testReadModifiedPoms() throws Exception {
FileUtils.fileWrite( parent, "UTF-8", parentContent );
// re-build pom with modified parent
ProjectBuildingResult result = projectBuilder.build( child, configuration );
assertTrue( result.getProject().getProperties().containsKey( "addedProperty" ) );
assertThat( result.getProject().getProperties(), hasKey( (Object) "addedProperty" ) );
}
finally
{
@ -229,7 +235,7 @@ public void testReadInvalidPom()
}
catch ( InvalidArtifactRTException iarte )
{
assertTrue( iarte.getMessage().contains( "The groupId cannot be empty." ) );
assertThat( iarte.getMessage(), containsString( "The groupId cannot be empty." ) );
}
// multi projects build entry point
@ -301,7 +307,7 @@ private void assertResultShowNoError(List<ProjectBuildingResult> results)
{
for ( ProjectBuildingResult result : results )
{
assertTrue( result.getProblems().isEmpty() );
assertThat( result.getProblems(), is( empty() ) );
assertNotNull( result.getProject() );
}
}

View File

@ -19,6 +19,9 @@
* under the License.
*/
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.util.Collections;
import java.util.List;
@ -34,12 +37,6 @@
import org.eclipse.aether.impl.RemoteRepositoryManager;
import org.eclipse.aether.repository.RemoteRepository;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
import static org.codehaus.plexus.PlexusTestCase.getBasedir;
/**
* Test cases for the project {@code ModelResolver} implementation.
*
@ -72,7 +69,7 @@ public void testResolveParentThrowsUnresolvableModelExceptionWhenNotFound() thro
catch ( final UnresolvableModelException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
}
}
@ -153,7 +150,7 @@ public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNotFound()
catch ( final UnresolvableModelException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
}
}

View File

@ -19,12 +19,14 @@
* under the License.
*/
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Extension;
@ -32,7 +34,9 @@
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginManagement;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Test sorting projects by dependencies.
@ -40,8 +44,9 @@
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class ProjectSorterTest
extends TestCase
{
@Rule
public ExpectedException expectedException = ExpectedException.none();
private Parent createParent( MavenProject project )
{
@ -104,8 +109,9 @@ private static MavenProject createProject( String groupId, String artifactId, St
return new MavenProject( model );
}
@Test
public void testShouldNotFailWhenPluginDepReferencesCurrentProject()
throws CycleDetectedException, DuplicateProjectException
throws Exception
{
MavenProject project = createProject( "group", "artifact", "1.0" );
@ -122,8 +128,9 @@ public void testShouldNotFailWhenPluginDepReferencesCurrentProject()
new ProjectSorter( Collections.singletonList( project ) );
}
@Test
public void testShouldNotFailWhenManagedPluginDepReferencesCurrentProject()
throws CycleDetectedException, DuplicateProjectException
throws Exception
{
MavenProject project = createProject( "group", "artifact", "1.0" );
@ -144,8 +151,9 @@ public void testShouldNotFailWhenManagedPluginDepReferencesCurrentProject()
new ProjectSorter( Collections.singletonList( project ) );
}
@Test
public void testShouldNotFailWhenProjectReferencesNonExistentProject()
throws CycleDetectedException, DuplicateProjectException
throws Exception
{
MavenProject project = createProject( "group", "artifact", "1.0" );
@ -158,8 +166,9 @@ public void testShouldNotFailWhenProjectReferencesNonExistentProject()
new ProjectSorter( Collections.singletonList( project ) );
}
@Test
public void testMatchingArtifactIdsDifferentGroupIds()
throws CycleDetectedException, DuplicateProjectException
throws Exception
{
List<MavenProject> projects = new ArrayList<>();
MavenProject project1 = createProject( "groupId1", "artifactId", "1.0" );
@ -174,8 +183,9 @@ public void testMatchingArtifactIdsDifferentGroupIds()
assertEquals( project1, projects.get( 1 ) );
}
@Test
public void testMatchingGroupIdsDifferentArtifactIds()
throws CycleDetectedException, DuplicateProjectException
throws Exception
{
List<MavenProject> projects = new ArrayList<>();
MavenProject project1 = createProject( "groupId", "artifactId1", "1.0" );
@ -190,29 +200,25 @@ public void testMatchingGroupIdsDifferentArtifactIds()
assertEquals( project1, projects.get( 1 ) );
}
@Test
public void testMatchingIdsAndVersions()
throws CycleDetectedException
throws Exception
{
List<MavenProject> projects = new ArrayList<>();
MavenProject project1 = createProject( "groupId", "artifactId", "1.0" );
projects.add( project1 );
MavenProject project2 = createProject( "groupId", "artifactId", "1.0" );
projects.add( project2 );
expectedException.expect( DuplicateProjectException.class );
expectedException.reportMissingExceptionWithMessage( "Duplicate projects should fail" );
try
{
projects = new ProjectSorter( projects ).getSortedProjects();
fail( "Duplicate projects should fail" );
}
catch ( DuplicateProjectException e )
{
// expected
assertTrue( true );
}
new ProjectSorter( projects ).getSortedProjects();
}
@Test
public void testMatchingIdsAndDifferentVersions()
throws CycleDetectedException, DuplicateProjectException
throws Exception
{
List<MavenProject> projects = new ArrayList<>();
MavenProject project1 = createProject( "groupId", "artifactId", "1.0" );
@ -225,6 +231,7 @@ public void testMatchingIdsAndDifferentVersions()
assertEquals( project2, projects.get( 1 ) );
}
@Test
public void testPluginDependenciesInfluenceSorting()
throws Exception
{
@ -261,13 +268,14 @@ public void testPluginDependenciesInfluenceSorting()
assertEquals( parentProject, projects.get( 0 ) );
// the order of these two is non-deterministic, based on when they're added to the reactor.
assertTrue( projects.contains( pluginProject ) );
assertTrue( projects.contains( pluginLevelDepProject ) );
assertThat( projects, hasItem( pluginProject ) );
assertThat( projects, hasItem( pluginLevelDepProject ) );
// the declaring project MUST be listed after the plugin and its plugin-level dep, though.
assertEquals( declaringProject, projects.get( 3 ) );
}
@Test
public void testPluginDependenciesInfluenceSorting_DeclarationInParent()
throws Exception
{
@ -296,15 +304,14 @@ public void testPluginDependenciesInfluenceSorting_DeclarationInParent()
projects = new ProjectSorter( projects ).getSortedProjects();
System.out.println( projects );
assertEquals( parentProject, projects.get( 0 ) );
// the order of these two is non-deterministic, based on when they're added to the reactor.
assertTrue( projects.contains( pluginProject ) );
assertTrue( projects.contains( pluginLevelDepProject ) );
assertThat( projects, hasItem( pluginProject ) );
assertThat( projects, hasItem( pluginLevelDepProject ) );
}
@Test
public void testPluginVersionsAreConsidered()
throws Exception
{
@ -320,10 +327,11 @@ public void testPluginVersionsAreConsidered()
projects = new ProjectSorter( projects ).getSortedProjects();
assertTrue( projects.contains( pluginProjectA ) );
assertTrue( projects.contains( pluginProjectB ) );
assertThat( projects, hasItem( pluginProjectA ) );
assertThat( projects, hasItem( pluginProjectB ) );
}
@Test
public void testDependencyPrecedesProjectThatUsesSpecificDependencyVersion()
throws Exception
{
@ -342,6 +350,7 @@ public void testDependencyPrecedesProjectThatUsesSpecificDependencyVersion()
assertEquals( usingProject, projects.get( 1 ) );
}
@Test
public void testDependencyPrecedesProjectThatUsesUnresolvedDependencyVersion()
throws Exception
{

View File

@ -19,6 +19,14 @@
* under the License.
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
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;
@ -29,14 +37,6 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.io.InputStream;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
public class DefaultToolchainTest
{
@Mock
@ -141,10 +141,10 @@ public void testEquals()
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 ) );
assertFalse( tc2.equals( tc1 ) );
assertTrue( tc2.equals( tc2 ) );
assertEquals( tc1, tc1 );
assertNotEquals( tc1, tc2 );
assertNotEquals( tc2, tc1 );
assertEquals( tc2, tc2 );
}
}
}

12
pom.xml
View File

@ -424,6 +424,18 @@ under the License.
<artifactId>powermock-reflect</artifactId>
<version>${powermockVersion}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--bootstrap-start-comment-->
</dependencyManagement>