This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch MNG-7035-maven5 in repository https://gitbox.apache.org/repos/asf/maven.git
commit 36d2476882ba4a74fa162c58c30bc11daf4ef4d6 Author: Guillaume Nodet <[email protected]> AuthorDate: Thu Nov 26 16:08:36 2020 +0100 [MNG-7035] Avoid calls to fail() when an assertXxx is usable --- .../artifact/versioning/ComparableVersionIT.java | 4 +- .../artifact/versioning/VersionRangeTest.java | 14 +-- .../org/apache/maven/building/FileSourceTest.java | 15 +-- .../org/apache/maven/building/UrlSourceTest.java | 15 +-- .../AbstractArtifactComponentTestCase.java | 23 +--- .../artifact/resolver/ArtifactResolverTest.java | 14 +-- .../inheritance/t02/ProjectInheritanceTest.java | 42 +++---- .../repository/legacy/DefaultWagonManagerTest.java | 108 +++------------- .../resolver/DefaultArtifactCollectorTest.java | 26 ++-- .../scope/internal/MojoExecutionScopeTest.java | 11 +- .../maven/lifecycle/LifecycleExecutorTest.java | 30 ++--- .../project/DefaultMavenProjectBuilderTest.java | 137 ++++++++------------- .../apache/maven/project/PomConstructionTest.java | 74 +++-------- .../maven/project/ProjectModelResolverTest.java | 96 +++++---------- .../internal/DefaultRuntimeInformationTest.java | 38 ++---- .../java/org/apache/maven/cli/MavenCliTest.java | 43 ++----- .../DefaultInheritanceAssemblerTest.java | 22 ++-- .../DefaultArtifactDescriptorReaderTest.java | 8 +- .../internal/DefaultModelResolverTest.java | 97 +++++---------- .../apache/maven/wrapper/WrapperExecutorTest.java | 85 ++++++------- 20 files changed, 286 insertions(+), 616 deletions(-) diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionIT.java b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionIT.java index 326ff57..f79eb1a 100644 --- a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionIT.java +++ b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionIT.java @@ -20,6 +20,7 @@ package org.apache.maven.artifact.versioning; */ import java.io.IOException; +import java.io.InterruptedIOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -31,7 +32,6 @@ import java.util.regex.Pattern; import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; public class ComparableVersionIT { @@ -64,7 +64,7 @@ public class ComparableVersionIT } catch ( InterruptedException e ) { - fail( e.getMessage() ); + throw new InterruptedIOException( e.toString() ); } return FileVisitResult.TERMINATE; } diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java index 7e6f5f4..c5bceb4 100644 --- a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java +++ b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java @@ -28,8 +28,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Tests version range construction. @@ -699,15 +699,9 @@ public class VersionRangeTest private void checkInvalidRange( String version ) { - try - { - VersionRange.createFromVersionSpec( version ); - fail( "Version " + version + " should have failed to construct" ); - } - catch ( InvalidVersionSpecificationException expected ) - { - // expected - } + assertThrows( "Version " + version + " should have failed to construct", + InvalidVersionSpecificationException.class, + () -> VersionRange.createFromVersionSpec( version ) ); } @Test diff --git a/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java b/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java index 40e4808..f1ac6e6 100644 --- a/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java +++ b/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java @@ -26,7 +26,7 @@ import java.util.Scanner; import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; public class FileSourceTest { @@ -34,15 +34,10 @@ public class FileSourceTest @Test public void testFileSource() { - try - { - new FileSource( null ); - fail( "Should fail, since you must specify a file" ); - } - catch ( NullPointerException e ) - { - assertEquals( "file cannot be null", e.getMessage() ); - } + NullPointerException e = assertThrows( "Should fail, since you must specify a file", + NullPointerException.class, + () -> new FileSource( null ) ); + assertEquals( "file cannot be null", e.getMessage() ); } @Test diff --git a/maven-builder-support/src/test/java/org/apache/maven/building/UrlSourceTest.java b/maven-builder-support/src/test/java/org/apache/maven/building/UrlSourceTest.java index fab547a..05bdc4c 100644 --- a/maven-builder-support/src/test/java/org/apache/maven/building/UrlSourceTest.java +++ b/maven-builder-support/src/test/java/org/apache/maven/building/UrlSourceTest.java @@ -27,7 +27,7 @@ import java.util.Scanner; import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; public class UrlSourceTest { @@ -35,15 +35,10 @@ public class UrlSourceTest @Test public void testUrlSource() { - try - { - new UrlSource( null ); - fail( "Should fail, since you must specify a url" ); - } - catch ( NullPointerException e ) - { - assertEquals( "url cannot be null", e.getMessage() ); - } + NullPointerException e = assertThrows( "Should fail, since you must specify a url", + NullPointerException.class, + () -> new UrlSource( null ) ); + assertEquals( "url cannot be null", e.getMessage() ); } @Test diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/AbstractArtifactComponentTestCase.java b/maven-compat/src/test/java/org/apache/maven/artifact/AbstractArtifactComponentTestCase.java index 6c4540c..6e6b8ca 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/AbstractArtifactComponentTestCase.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/AbstractArtifactComponentTestCase.java @@ -70,7 +70,8 @@ import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy; import org.junit.After; import org.junit.Before; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author <a href="mailto:[email protected]">Jason van Zyl </a> @@ -198,10 +199,7 @@ public abstract class AbstractArtifactComponentTestCase File file = new File( remoteRepo.getBasedir(), path ); - if ( !file.exists() ) - { - fail( "Remote artifact " + file + " should be present." ); - } + assertTrue( "Remote artifact " + file + " should be present.", file.exists() ); } protected void assertLocalArtifactPresent( Artifact artifact ) @@ -213,10 +211,7 @@ public abstract class AbstractArtifactComponentTestCase File file = new File( localRepo.getBasedir(), path ); - if ( !file.exists() ) - { - fail( "Local artifact " + file + " should be present." ); - } + assertTrue( "Local artifact " + file + " should be present.", file.exists() ); } protected void assertRemoteArtifactNotPresent( Artifact artifact ) @@ -228,10 +223,7 @@ public abstract class AbstractArtifactComponentTestCase File file = new File( remoteRepo.getBasedir(), path ); - if ( file.exists() ) - { - fail( "Remote artifact " + file + " should not be present." ); - } + assertFalse( "Remote artifact " + file + " should not be present.", file.exists() ); } protected void assertLocalArtifactNotPresent( Artifact artifact ) @@ -243,10 +235,7 @@ public abstract class AbstractArtifactComponentTestCase File file = new File( localRepo.getBasedir(), path ); - if ( file.exists() ) - { - fail( "Local artifact " + file + " should not be present." ); - } + assertFalse( "Local artifact " + file + " should not be present.", file.exists() ); } // ---------------------------------------------------------------------- diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java b/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java index 68cb8d8..81272ee 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java @@ -40,8 +40,8 @@ import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import javax.inject.Inject; @@ -170,15 +170,9 @@ public class ArtifactResolverTest { Artifact k = createArtifact( "k", "1.0" ); - try - { - artifactResolver.resolve( k, remoteRepositories(), localRepository() ); - fail( "Resolution succeeded when it should have failed" ); - } - catch ( ArtifactNotFoundException expected ) - { - assertTrue( true ); - } + assertThrows( "Resolution succeeded when it should have failed", + ArtifactNotFoundException.class, + () -> artifactResolver.resolve( k, remoteRepositories(), localRepository() ) ); } @Test diff --git a/maven-compat/src/test/java/org/apache/maven/project/inheritance/t02/ProjectInheritanceTest.java b/maven-compat/src/test/java/org/apache/maven/project/inheritance/t02/ProjectInheritanceTest.java index 485e5f0..dbcc369 100644 --- a/maven-compat/src/test/java/org/apache/maven/project/inheritance/t02/ProjectInheritanceTest.java +++ b/maven-compat/src/test/java/org/apache/maven/project/inheritance/t02/ProjectInheritanceTest.java @@ -26,12 +26,14 @@ import java.util.Map; import org.apache.maven.model.Build; import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginExecution; import org.apache.maven.project.MavenProject; import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase; import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /** * A test which demonstrates maven's recursive inheritance where @@ -139,33 +141,25 @@ public class ProjectInheritanceTest { String pluginArtifactId = plugin.getArtifactId(); - if ( !validPluginCounts.containsKey( pluginArtifactId ) ) - { - fail( "Illegal plugin found: " + pluginArtifactId ); - } - else + assertTrue( "Illegal plugin found: " + pluginArtifactId, validPluginCounts.containsKey( pluginArtifactId ) ); + + if ( pluginArtifactId.equals( testPluginArtifactId ) ) { - if ( pluginArtifactId.equals( testPluginArtifactId ) ) - { - testPlugin = plugin; - } - - Integer count = validPluginCounts.get( pluginArtifactId ); - - if ( count > 0 ) - { - fail( "Multiple copies of plugin: " + pluginArtifactId + " found in POM." ); - } - else - { - count = count + 1; - - validPluginCounts.put( pluginArtifactId, count ); - } + testPlugin = plugin; } + + Integer count = validPluginCounts.get( pluginArtifactId ); + + assertEquals( "Multiple copies of plugin: " + pluginArtifactId + " found in POM.", 0, (int) count ); + + count = count + 1; + + validPluginCounts.put( pluginArtifactId, count ); } - List executions = testPlugin.getExecutions(); + assertNotNull( testPlugin ); + + List<PluginExecution> executions = testPlugin.getExecutions(); assertEquals( 1, executions.size() ); } diff --git a/maven-compat/src/test/java/org/apache/maven/repository/legacy/DefaultWagonManagerTest.java b/maven-compat/src/test/java/org/apache/maven/repository/legacy/DefaultWagonManagerTest.java index fb8c2ca..1c28bd4 100644 --- a/maven-compat/src/test/java/org/apache/maven/repository/legacy/DefaultWagonManagerTest.java +++ b/maven-compat/src/test/java/org/apache/maven/repository/legacy/DefaultWagonManagerTest.java @@ -50,14 +50,15 @@ import org.apache.maven.PlexusTestCase; import org.codehaus.plexus.util.FileUtils; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import javax.inject.Inject; @@ -151,16 +152,8 @@ public class DefaultWagonManagerTest ArtifactRepository repo = createStringRepo(); - try - { - wagonManager.getArtifact( artifact, repo, null, false ); - - fail(); - } - catch ( ResourceDoesNotExistException e ) - { - assertTrue( true ); - } + assertThrows( ResourceDoesNotExistException.class, + () -> wagonManager.getArtifact( artifact, repo, null, false ) ); assertFalse( artifact.getFile().exists() ); } @@ -172,16 +165,8 @@ public class DefaultWagonManagerTest ArtifactRepository repo = createStringRepo(); - try - { - wagonManager.getArtifact( artifact, repo, null, true ); - - fail(); - } - catch ( ResourceDoesNotExistException e ) - { - assertTrue( true ); - } + assertThrows( ResourceDoesNotExistException.class, + () -> wagonManager.getArtifact( artifact, repo, null, true ) ); assertFalse( artifact.getFile().exists() ); } @@ -276,17 +261,7 @@ public class DefaultWagonManagerTest assertWagon( "string" ); - try - { - assertWagon( "d" ); - - fail( "Expected :" + UnsupportedProtocolException.class.getName() ); - } - catch ( UnsupportedProtocolException e ) - { - // ok - assertTrue( true ); - } + assertThrows( UnsupportedProtocolException.class, () -> assertWagon( "d" ) ); } /** @@ -321,7 +296,9 @@ public class DefaultWagonManagerTest /** * Checks the verification of checksums. */ - public void xtestChecksumVerification() + @Ignore + @Test + public void testChecksumVerification() throws Exception { ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL ); @@ -338,82 +315,35 @@ public class DefaultWagonManagerTest wagon.clearExpectedContent(); wagon.addExpectedContent( "path", "lower-case-checksum" ); wagon.addExpectedContent( "path.sha1", "2a25dc564a3b34f68237fc849066cbc7bb7a36a1" ); - - try - { - wagonManager.getArtifact( artifact, repo, null, false ); - } - catch ( ChecksumFailedException e ) - { - fail( "Checksum verification did not pass: " + e.getMessage() ); - } + wagonManager.getArtifact( artifact, repo, null, false ); wagon.clearExpectedContent(); wagon.addExpectedContent( "path", "upper-case-checksum" ); wagon.addExpectedContent( "path.sha1", "B7BB97D7D0B9244398D9B47296907F73313663E6" ); - - try - { - wagonManager.getArtifact( artifact, repo, null, false ); - } - catch ( ChecksumFailedException e ) - { - fail( "Checksum verification did not pass: " + e.getMessage() ); - } + wagonManager.getArtifact( artifact, repo, null, false ); wagon.clearExpectedContent(); wagon.addExpectedContent( "path", "expected-failure" ); wagon.addExpectedContent( "path.sha1", "b7bb97d7d0b9244398d9b47296907f73313663e6" ); - - try - { - wagonManager.getArtifact( artifact, repo, null, false ); - fail( "Checksum verification did not fail" ); - } - catch ( ChecksumFailedException e ) - { - // expected - } + assertThrows( "Checksum verification did not fail", ChecksumFailedException.class, () -> + wagonManager.getArtifact( artifact, repo, null, false ) ); wagon.clearExpectedContent(); wagon.addExpectedContent( "path", "lower-case-checksum" ); wagon.addExpectedContent( "path.md5", "50b2cf50a103a965efac62b983035cac" ); - - try - { - wagonManager.getArtifact( artifact, repo, null, false ); - } - catch ( ChecksumFailedException e ) - { - fail( "Checksum verification did not pass: " + e.getMessage() ); - } + wagonManager.getArtifact( artifact, repo, null, false ); wagon.clearExpectedContent(); wagon.addExpectedContent( "path", "upper-case-checksum" ); wagon.addExpectedContent( "path.md5", "842F568FCCFEB7E534DC72133D42FFDC" ); - - try - { - wagonManager.getArtifact( artifact, repo, null, false ); - } - catch ( ChecksumFailedException e ) - { - fail( "Checksum verification did not pass: " + e.getMessage() ); - } + wagonManager.getArtifact( artifact, repo, null, false ); wagon.clearExpectedContent(); wagon.addExpectedContent( "path", "expected-failure" ); wagon.addExpectedContent( "path.md5", "b7bb97d7d0b9244398d9b47296907f73313663e6" ); - - try - { - wagonManager.getArtifact( artifact, repo, null, false ); - fail( "Checksum verification did not fail" ); - } - catch ( ChecksumFailedException e ) - { - // expected - } + assertThrows( "Checksum verification did not fail", + ChecksumFailedException.class, + () -> wagonManager.getArtifact( artifact, repo, null, false ) ); } @Test diff --git a/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java b/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java index be742fc..c0e1d49 100644 --- a/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java +++ b/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java @@ -56,8 +56,8 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Test the default artifact collector. @@ -116,15 +116,9 @@ public class DefaultArtifactCollectorTest ArtifactSpec a = createArtifactSpec( "a", "1.0" ); ArtifactSpec b = a.addDependency( "b", "1.0" ); b.addDependency( "a", "1.0" ); - try - { - collect( a ); - fail( "Should have failed on cyclic dependency not involving project" ); - } - catch ( CyclicDependencyException expected ) - { - assertTrue( true ); - } + assertThrows( "Should have failed on cyclic dependency not involving project", + CyclicDependencyException.class, + () -> collect( a ) ); } // works, but we don't fail on cycles presently @@ -134,15 +128,9 @@ public class DefaultArtifactCollectorTest ArtifactSpec a = createArtifactSpec( "a", "1.0" ); ArtifactSpec b = a.addDependency( "b", "1.0" ); b.addDependency( "project", "1.0" ); - try - { - collect( a ); - fail( "Should have failed on cyclic dependency involving project" ); - } - catch ( CyclicDependencyException expected ) - { - assertTrue( true ); - } + assertThrows( "Should have failed on cyclic dependency not involving project", + CyclicDependencyException.class, + () -> collect( a ) ); } @Test diff --git a/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java b/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java index 589ac87..124703b 100644 --- a/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java +++ b/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java @@ -24,7 +24,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; public class MojoExecutionScopeTest { @@ -50,14 +50,7 @@ public class MojoExecutionScopeTest scope.exit(); - try - { - scope.exit(); - fail(); - } - catch ( IllegalStateException expected ) - { - } + assertThrows( IllegalStateException.class, () -> scope.exit() ); } @Test diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java index 0c47e79..38231c4 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java @@ -51,7 +51,7 @@ import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import javax.inject.Inject; @@ -320,25 +320,15 @@ public class LifecycleExecutorTest { File pom = getProject( "project-basic" ); MavenSession session = createMavenSession( pom ); - try - { - getExecutions( calculateExecutionPlan( session, "resources:" ) ); - fail( "expected a MojoNotFoundException" ); - } - catch ( MojoNotFoundException e ) - { - assertEquals( "", e.getGoal() ); - } - - try - { - getExecutions( calculateExecutionPlan( session, "org.apache.maven.plugins:maven-resources-plugin:0.1:resources:toomany" ) ); - fail( "expected a MojoNotFoundException" ); - } - catch ( MojoNotFoundException e ) - { - assertEquals( "resources:toomany", e.getGoal() ); - } + MojoNotFoundException e = assertThrows( "expected a MojoNotFoundException", + MojoNotFoundException.class, + () -> getExecutions( calculateExecutionPlan( session, "resources:" ) ) ); + assertEquals( "", e.getGoal() ); + + e = assertThrows( "expected a MojoNotFoundException", + MojoNotFoundException.class, + () -> getExecutions( calculateExecutionPlan( session, "org.apache.maven.plugins:maven-resources-plugin:0.1:resources:toomany" ) ) ); + assertEquals( "resources:toomany", e.getGoal() ); } diff --git a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java index fff4ed5..dac8489 100644 --- a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java @@ -28,12 +28,13 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.codehaus.plexus.util.FileUtils; import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; public class DefaultMavenProjectBuilderTest @@ -128,15 +129,10 @@ public class DefaultMavenProjectBuilderTest { File f1 = getTestFile( "src/test/resources/projects/future-model-version-pom.xml" ); - try - { - getProject( f1 ); - fail( "Expected to fail for future versions" ); - } - catch ( ProjectBuildingException e ) - { - assertContains( "Building this project requires a newer version of Maven", e.getMessage() ); - } + ProjectBuildingException e = assertThrows( "Expected to fail for future versions", + ProjectBuildingException.class, + () -> getProject( f1 ) ); + assertContains( "Building this project requires a newer version of Maven", e.getMessage() ); } @Test @@ -147,15 +143,10 @@ public class DefaultMavenProjectBuilderTest // update the resource if we stop supporting modelVersion 4.0.0 File f1 = getTestFile( "src/test/resources/projects/past-model-version-pom.xml" ); - try - { - getProject( f1 ); - fail( "Expected to fail for past versions" ); - } - catch ( ProjectBuildingException e ) - { - assertContains( "Building this project requires an older version of Maven", e.getMessage() ); - } + ProjectBuildingException e = assertThrows( "Expected to fail for past versions", + ProjectBuildingException.class, + () -> getProject( f1 ) ); + assertContains( "Building this project requires an older version of Maven", e.getMessage() ); } @Test @@ -164,15 +155,10 @@ public class DefaultMavenProjectBuilderTest { File f1 = getTestFile( "src/test/resources/projects/future-schema-model-version-pom.xml" ); - try - { - getProject( f1 ); - fail( "Expected to fail for future versions" ); - } - catch ( ProjectBuildingException e ) - { - assertContains( "Building this project requires a newer version of Maven", e.getMessage() ); - } + ProjectBuildingException e = assertThrows( "Expected to fail for future versions", + ProjectBuildingException.class, + () -> getProject( f1 ) ); + assertContains( "Building this project requires a newer version of Maven", e.getMessage() ); } private void assertContains( String expected, String actual ) @@ -229,26 +215,21 @@ public class DefaultMavenProjectBuilderTest { File pomFile = getTestFile( "src/test/resources/projects/bad-dependency.xml" ); - try - { - ProjectBuildingRequest request = newBuildingRequest(); - request.setProcessPlugins( false ); - request.setResolveDependencies( true ); - projectBuilder.build( pomFile, request ); - fail( "Project building did not fail despite invalid POM" ); - } - catch ( ProjectBuildingException e ) - { - List<ProjectBuildingResult> results = e.getResults(); - assertNotNull( results ); - assertEquals( 1, results.size() ); - ProjectBuildingResult result = results.get( 0 ); - assertNotNull( result ); - assertNotNull( result.getProject() ); - assertEquals( 1, result.getProblems().size() ); - assertEquals( 1, result.getProject().getArtifacts().size() ); - assertNotNull( result.getDependencyResolutionResult() ); - } + ProjectBuildingRequest request = newBuildingRequest(); + request.setProcessPlugins( false ); + request.setResolveDependencies( true ); + ProjectBuildingException e = assertThrows( "Project building did not fail despite invalid POM", + ProjectBuildingException.class, + () -> projectBuilder.build( pomFile, request ) ); + List<ProjectBuildingResult> results = e.getResults(); + assertNotNull( results ); + assertEquals( 1, results.size() ); + ProjectBuildingResult result = results.get( 0 ); + assertNotNull( result ); + assertNotNull( result.getProject() ); + assertEquals( 1, result.getProblems().size() ); + assertEquals( 1, result.getProject().getArtifacts().size() ); + assertNotNull( result.getDependencyResolutionResult() ); } /** @@ -282,16 +263,11 @@ public class DefaultMavenProjectBuilderTest File f1 = getTestFile( "src/test/resources/projects/parent-version-range-local-child-without-version/child/pom.xml" ); - try - { - getProject( f1 ); - fail( "Expected 'ProjectBuildingException' not thrown." ); - } - catch ( final ProjectBuildingException e ) - { - assertNotNull( e.getMessage() ); - assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); - } + ProjectBuildingException e = assertThrows( "Expected 'ProjectBuildingException' not thrown.", + ProjectBuildingException.class, + () -> getProject( f1 ) ); + assertNotNull( e.getMessage() ); + assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); } /** @@ -306,16 +282,11 @@ public class DefaultMavenProjectBuilderTest getTestFile( "src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml" ); - try - { - getProject( f1 ); - fail( "Expected 'ProjectBuildingException' not thrown." ); - } - catch ( final ProjectBuildingException e ) - { - assertNotNull( e.getMessage() ); - assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); - } + ProjectBuildingException e = assertThrows( "Expected 'ProjectBuildingException' not thrown.", + ProjectBuildingException.class, + () -> getProject( f1 ) ); + assertNotNull( e.getMessage() ); + assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); } /** @@ -350,16 +321,11 @@ public class DefaultMavenProjectBuilderTest getTestFile( "src/test/resources/projects/parent-version-range-external-child-without-version/pom.xml" ); - try - { - this.getProjectFromRemoteRepository( f1 ); - fail( "Expected 'ProjectBuildingException' not thrown." ); - } - catch ( final ProjectBuildingException e ) - { - assertNotNull( e.getMessage() ); - assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); - } + ProjectBuildingException e = assertThrows( "Expected 'ProjectBuildingException' not thrown.", + ProjectBuildingException.class, + () -> getProjectFromRemoteRepository( f1 ) ); + assertNotNull( e.getMessage() ); + assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); } /** @@ -374,16 +340,11 @@ public class DefaultMavenProjectBuilderTest getTestFile( "src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml" ); - try - { - this.getProjectFromRemoteRepository( f1 ); - fail( "Expected 'ProjectBuildingException' not thrown." ); - } - catch ( final ProjectBuildingException e ) - { - assertNotNull( e.getMessage() ); - assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); - } + ProjectBuildingException e = assertThrows( "Expected 'ProjectBuildingException' not thrown.", + ProjectBuildingException.class, + () -> getProjectFromRemoteRepository( f1 ) ); + assertNotNull( e.getMessage() ); + assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); } } diff --git a/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java b/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java index 6562075..c5d76bd 100644 --- a/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java @@ -56,7 +56,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; public class PomConstructionTest extends PlexusTestCase @@ -1689,15 +1689,9 @@ public class PomConstructionTest public void testValidationErrorUponNonUniqueArtifactRepositoryId() throws Exception { - try - { - buildPom( "unique-repo-id/artifact-repo" ); - fail( "Non-unique repository ids did not cause validation error" ); - } - catch ( ProjectBuildingException e ) - { - // expected - } + assertThrows( "Non-unique repository ids did not cause validation error", + ProjectBuildingException.class, + () -> buildPom( "unique-repo-id/artifact-repo" ) ); } /* MNG-4193 */ @@ -1705,15 +1699,9 @@ public class PomConstructionTest public void testValidationErrorUponNonUniquePluginRepositoryId() throws Exception { - try - { - buildPom( "unique-repo-id/plugin-repo" ); - fail( "Non-unique repository ids did not cause validation error" ); - } - catch ( ProjectBuildingException e ) - { - // expected - } + assertThrows( "Non-unique repository ids did not cause validation error", + ProjectBuildingException.class, + () -> buildPom( "unique-repo-id/plugin-repo" ) ); } /* MNG-4193 */ @@ -1721,15 +1709,9 @@ public class PomConstructionTest public void testValidationErrorUponNonUniqueArtifactRepositoryIdInProfile() throws Exception { - try - { - buildPom( "unique-repo-id/artifact-repo-in-profile" ); - fail( "Non-unique repository ids did not cause validation error" ); - } - catch ( ProjectBuildingException e ) - { - // expected - } + assertThrows( "Non-unique repository ids did not cause validation error", + ProjectBuildingException.class, + () -> buildPom( "unique-repo-id/artifact-repo-in-profile" ) ); } /* MNG-4193 */ @@ -1737,15 +1719,9 @@ public class PomConstructionTest public void testValidationErrorUponNonUniquePluginRepositoryIdInProfile() throws Exception { - try - { - buildPom( "unique-repo-id/plugin-repo-in-profile" ); - fail( "Non-unique repository ids did not cause validation error" ); - } - catch ( ProjectBuildingException e ) - { - // expected - } + assertThrows( "Non-unique repository ids did not cause validation error", + ProjectBuildingException.class, + () -> buildPom( "unique-repo-id/plugin-repo-in-profile" ) ); } /** MNG-3843 */ @@ -1824,15 +1800,9 @@ public class PomConstructionTest public void testParentPomPackagingMustBePom() throws Exception { - try - { - buildPom( "parent-pom-packaging/sub" ); - fail( "Wrong packaging of parent POM was not rejected" ); - } - catch ( ProjectBuildingException e ) - { - // expected - } + assertThrows( "Wrong packaging of parent POM was not rejected", + ProjectBuildingException.class, + () -> buildPom( "parent-pom-packaging/sub" ) ); } /** MNG-522, MNG-3018 */ @@ -1953,15 +1923,9 @@ public class PomConstructionTest public void testProjectArtifactIdIsNotInheritedButMandatory() throws Exception { - try - { - buildPom( "artifact-id-inheritance/child" ); - fail( "Missing artifactId did not cause validation error" ); - } - catch ( ProjectBuildingException e ) - { - // expected - } + assertThrows( "Missing artifactId did not cause validation error", + ProjectBuildingException.class, + () -> buildPom( "artifact-id-inheritance/child" ) ); } private void assertPathSuffixEquals( String expected, Object actual ) diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java index c83a692..85d8a47 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java @@ -39,7 +39,7 @@ import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; /** * Test cases for the project {@code ModelResolver} implementation. @@ -66,16 +66,11 @@ public class ProjectModelResolverTest extends AbstractMavenProjectTestCase parent.setArtifactId( "apache" ); parent.setVersion( "0" ); - try - { - this.newModelResolver().resolveModel( parent ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertNotNull( e.getMessage() ); - assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) ); - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( parent ) ); + assertNotNull( e.getMessage() ); + assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) ); } @Test @@ -86,17 +81,11 @@ public class ProjectModelResolverTest extends AbstractMavenProjectTestCase parent.setArtifactId( "apache" ); parent.setVersion( "[2.0,2.1)" ); - try - { - this.newModelResolver().resolveModel( parent ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "No versions matched the requested parent version range '[2.0,2.1)'", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( parent ) ); + assertEquals( "No versions matched the requested parent version range '[2.0,2.1)'", + e.getMessage() ); } @Test @@ -107,17 +96,11 @@ public class ProjectModelResolverTest extends AbstractMavenProjectTestCase parent.setArtifactId( "apache" ); parent.setVersion( "[1,)" ); - try - { - this.newModelResolver().resolveModel( parent ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "The requested parent version range '[1,)' does not specify an upper bound", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( parent ) ); + assertEquals( "The requested parent version range '[1,)' does not specify an upper bound", + e.getMessage() ); } @Test @@ -152,16 +135,11 @@ public class ProjectModelResolverTest extends AbstractMavenProjectTestCase dependency.setArtifactId( "apache" ); dependency.setVersion( "0" ); - try - { - this.newModelResolver().resolveModel( dependency ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertNotNull( e.getMessage() ); - assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) ); - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( dependency ) ); + assertNotNull( e.getMessage() ); + assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) ); } @Test @@ -172,17 +150,11 @@ public class ProjectModelResolverTest extends AbstractMavenProjectTestCase dependency.setArtifactId( "apache" ); dependency.setVersion( "[2.0,2.1)" ); - try - { - this.newModelResolver().resolveModel( dependency ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "No versions matched the requested dependency version range '[2.0,2.1)'", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( dependency ) ); + assertEquals( "No versions matched the requested dependency version range '[2.0,2.1)'", + e.getMessage() ); } @Test @@ -193,17 +165,11 @@ public class ProjectModelResolverTest extends AbstractMavenProjectTestCase dependency.setArtifactId( "apache" ); dependency.setVersion( "[1,)" ); - try - { - this.newModelResolver().resolveModel( dependency ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "The requested dependency version range '[1,)' does not specify an upper bound", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( dependency ) ); + assertEquals( "The requested dependency version range '[1,)' does not specify an upper bound", + e.getMessage() ); } @Test diff --git a/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java b/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java index a4f61b6..5ed7863 100644 --- a/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java +++ b/maven-core/src/test/java/org/apache/maven/rtinfo/internal/DefaultRuntimeInformationTest.java @@ -28,8 +28,8 @@ import org.junit.Test; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import javax.inject.Inject; import java.util.Collections; @@ -75,35 +75,17 @@ public class DefaultRuntimeInformationTest assertTrue( rtInfo.isMavenVersion( "[2.0.11,2.1.0),[3.0,)" ) ); assertFalse( rtInfo.isMavenVersion( "[9.0,)" ) ); - try - { - rtInfo.isMavenVersion( "[3.0," ); - fail( "Bad version range wasn't rejected" ); - } - catch ( IllegalArgumentException e ) - { - assertTrue( true ); - } + assertThrows( "Bad version range wasn't rejected", + IllegalArgumentException.class, + () -> rtInfo.isMavenVersion( "[3.0," ) ); - try - { - rtInfo.isMavenVersion( "" ); - fail( "Bad version range wasn't rejected" ); - } - catch ( IllegalArgumentException e ) - { - assertTrue( true ); - } + assertThrows( "Bad version range wasn't rejected", + IllegalArgumentException.class, + () -> rtInfo.isMavenVersion( "" ) ); - try - { - rtInfo.isMavenVersion( null ); - fail( "Bad version range wasn't rejected" ); - } - catch ( NullPointerException e ) - { - assertTrue( true ); - } + assertThrows( "Bad version range wasn't rejected", + NullPointerException.class, + () -> rtInfo.isMavenVersion( null ) ); } } diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java index 491fb0b..1edb84e 100644 --- a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java @@ -28,8 +28,8 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; import static org.mockito.ArgumentMatchers.any; import static org.hamcrest.collection.IsIterableContainingInOrder.contains; @@ -149,15 +149,9 @@ public class MavenCliTest // -TC2.2 assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2.2C" ) ); - try - { - cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" ); - fail( "Should have failed with a NumberFormatException" ); - } - catch ( NumberFormatException e ) - { - // carry on - } + assertThrows( "Should have failed with a NumberFormatException", + NumberFormatException.class, + () -> cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" ) ); } @Test @@ -189,15 +183,7 @@ public class MavenCliTest CliRequest request = new CliRequest( new String[0], null ); cli.initialize( request ); - try - { - cli.cli( request ); - fail(); - } - catch ( ParseException expected ) - { - - } + assertThrows( ParseException.class, () -> cli.cli( request ) ); } /** @@ -359,19 +345,12 @@ public class MavenCliTest cli.logging( request ); assertTrue( MessageUtils.isColorEnabled() ); - try - { - MessageUtils.setColorEnabled( false ); - request = new CliRequest( new String[] { "-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log" }, null ); - cli.cli( request ); - cli.properties( request ); - cli.logging( request ); - fail( "maybe is not a valid option" ); - } - catch ( IllegalArgumentException e ) - { - // noop - } + MessageUtils.setColorEnabled( false ); + CliRequest maybeColorRequest = new CliRequest( new String[] { "-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log" }, null ); + cli.cli( maybeColorRequest ); + cli.properties( maybeColorRequest ); + assertThrows( "maybe is not a valid option", IllegalArgumentException.class, + () -> cli.logging( maybeColorRequest ) ); } /** diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java index 777c47d..4df8953 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java @@ -43,8 +43,8 @@ import org.junit.Test; import org.xmlunit.matchers.CompareMatcher; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * @author Hervé Boutemy @@ -182,18 +182,14 @@ public class DefaultInheritanceAssemblerTest // parent references child with directory name (which is not artifact id) // then relative path calculation will success during build from disk but fail when calculated from repo testInheritance( "tricky-flat-directory-urls", false ); - try - { - testInheritance( "tricky-flat-directory-urls", true ); - fail( "should have failed since module reference == directory name != artifactId" ); - } - catch ( AssertionError afe ) - { - // expected failure - assertTrue( afe.getMessage(), afe.getMessage().contains( - "Expected text value 'http://www.apache.org/path/to/parent/../child-artifact-id/' but was " + - "'http://www.apache.org/path/to/parent/child-artifact-id/'" ) ); - } + + AssertionError afe = assertThrows( "should have failed since module reference == directory name != artifactId", + AssertionError.class, + () -> testInheritance( "tricky-flat-directory-urls", true ) ); + // expected failure + assertTrue( afe.getMessage(), afe.getMessage().contains( + "Expected text value 'http://www.apache.org/path/to/parent/../child-artifact-id/' but was " + + "'http://www.apache.org/path/to/parent/child-artifact-id/'" ) ); } @Test diff --git a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReaderTest.java b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReaderTest.java index 7787f10..154ebc7 100644 --- a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReaderTest.java +++ b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReaderTest.java @@ -29,7 +29,7 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -73,9 +73,7 @@ public class DefaultArtifactDescriptorReaderTest } } - if( !missingArtifactDescriptor ) - { - fail( "Expected missing artifact descriptor for org.apache.maven.its:dep-mng5459:pom:0.4.0-20130404.090532-2" ); - } + assertTrue( "Expected missing artifact descriptor for org.apache.maven.its:dep-mng5459:pom:0.4.0-20130404.090532-2", + missingArtifactDescriptor ); } } diff --git a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultModelResolverTest.java b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultModelResolverTest.java index b106704..3b13e2c 100644 --- a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultModelResolverTest.java +++ b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/DefaultModelResolverTest.java @@ -34,8 +34,8 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Test cases for the default {@code ModelResolver} implementation. @@ -62,16 +62,11 @@ public final class DefaultModelResolverTest extends AbstractRepositoryTestCase parent.setArtifactId( "artifact" ); parent.setVersion( "0" ); - try - { - this.newModelResolver().resolveModel( parent ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertNotNull( e.getMessage() ); - assertTrue( e.getMessage().startsWith( "Could not find artifact ut.simple:artifact:pom:0 in repo" ) ); - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( parent ) ); + assertNotNull( e.getMessage() ); + assertTrue( e.getMessage().startsWith( "Could not find artifact ut.simple:artifact:pom:0 in repo" ) ); } @Test @@ -82,17 +77,12 @@ public final class DefaultModelResolverTest extends AbstractRepositoryTestCase parent.setArtifactId( "artifact" ); parent.setVersion( "[2.0,2.1)" ); - try - { - this.newModelResolver().resolveModel( parent ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "No versions matched the requested parent version range '[2.0,2.1)'", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( parent ) ); + assertNotNull( e.getMessage() ); + assertEquals( "No versions matched the requested parent version range '[2.0,2.1)'", + e.getMessage() ); } @Test @@ -103,17 +93,11 @@ public final class DefaultModelResolverTest extends AbstractRepositoryTestCase parent.setArtifactId( "artifact" ); parent.setVersion( "[1.0,)" ); - try - { - this.newModelResolver().resolveModel( parent ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "The requested parent version range '[1.0,)' does not specify an upper bound", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( parent ) ); + assertEquals( "The requested parent version range '[1.0,)' does not specify an upper bound", + e.getMessage() ); } @Test @@ -148,16 +132,11 @@ public final class DefaultModelResolverTest extends AbstractRepositoryTestCase dependency.setArtifactId( "artifact" ); dependency.setVersion( "0" ); - try - { - this.newModelResolver().resolveModel( dependency ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertNotNull( e.getMessage() ); - assertTrue( e.getMessage().startsWith( "Could not find artifact ut.simple:artifact:pom:0 in repo" ) ); - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( dependency ) ); + assertNotNull( e.getMessage() ); + assertTrue( e.getMessage().startsWith( "Could not find artifact ut.simple:artifact:pom:0 in repo" ) ); } @Test @@ -168,17 +147,11 @@ public final class DefaultModelResolverTest extends AbstractRepositoryTestCase dependency.setArtifactId( "artifact" ); dependency.setVersion( "[2.0,2.1)" ); - try - { - this.newModelResolver().resolveModel( dependency ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "No versions matched the requested dependency version range '[2.0,2.1)'", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( dependency ) ); + assertEquals( "No versions matched the requested dependency version range '[2.0,2.1)'", + e.getMessage() ); } @Test @@ -189,17 +162,11 @@ public final class DefaultModelResolverTest extends AbstractRepositoryTestCase dependency.setArtifactId( "artifact" ); dependency.setVersion( "[1.0,)" ); - try - { - this.newModelResolver().resolveModel( dependency ); - fail( "Expected 'UnresolvableModelException' not thrown." ); - } - catch ( final UnresolvableModelException e ) - { - assertEquals( "The requested dependency version range '[1.0,)' does not specify an upper bound", - e.getMessage() ); - - } + UnresolvableModelException e = assertThrows( "Expected 'UnresolvableModelException' not thrown.", + UnresolvableModelException.class, + () -> newModelResolver().resolveModel( dependency ) ); + assertEquals( "The requested dependency version range '[1.0,)' does not specify an upper bound", + e.getMessage() ); } @Test diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java index b160be0..970245e 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java @@ -19,6 +19,11 @@ package org.apache.maven.wrapper; * under the License. */ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -79,12 +84,12 @@ public class WrapperExecutorTest { WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile ); - Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() ); - Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() ); - Assert.assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() ); - Assert.assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath() ); - Assert.assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() ); - Assert.assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath() ); + assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() ); + assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() ); + assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() ); + assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath() ); + assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() ); + assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath() ); } @Test @@ -93,12 +98,12 @@ public class WrapperExecutorTest { WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( testDir.getRoot().toPath() ); - Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() ); - Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() ); - Assert.assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() ); - Assert.assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath() ); - Assert.assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() ); - Assert.assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath() ); + assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() ); + assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() ); + assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() ); + assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath() ); + assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() ); + assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath() ); } @Test @@ -107,12 +112,12 @@ public class WrapperExecutorTest { WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( testDir.getRoot().toPath().resolve( "unknown" ) ); - Assert.assertNull( wrapper.getDistribution() ); - Assert.assertNull( wrapper.getConfiguration().getDistribution() ); - Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() ); - Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() ); - Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() ); - Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() ); + assertNull( wrapper.getDistribution() ); + assertNull( wrapper.getConfiguration().getDistribution() ); + assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() ); + assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() ); + assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() ); + assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() ); } @Test @@ -126,12 +131,12 @@ public class WrapperExecutorTest WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile ); - Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() ); - Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() ); - Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() ); - Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() ); - Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() ); - Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() ); + assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() ); + assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() ); + assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() ); + assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() ); + assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() ); + assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() ); } @Test @@ -152,16 +157,11 @@ public class WrapperExecutorTest properties = new Properties(); writePropertiesFile( properties, propertiesFile, "header" ); - try - { - WrapperExecutor.forWrapperPropertiesFile( propertiesFile ); - Assert.fail( "Expected RuntimeException" ); - } - catch ( RuntimeException e ) - { - Assert.assertEquals( "No value with key 'distributionUrl' specified in wrapper properties file '" - + propertiesFile + "'.", e.getMessage() ); - } + RuntimeException e = assertThrows( "Expected RuntimeException", + RuntimeException.class, + () -> WrapperExecutor.forWrapperPropertiesFile( propertiesFile ) ); + assertEquals( "No value with key 'distributionUrl' specified in wrapper properties file '" + + propertiesFile + "'.", e.getMessage() ); } @Test @@ -169,15 +169,10 @@ public class WrapperExecutorTest { propertiesFile = testDir.getRoot().toPath().resolve( "unknown.properties" ); - try - { - WrapperExecutor.forWrapperPropertiesFile( propertiesFile ); - Assert.fail( "Expected RuntimeException" ); - } - catch ( RuntimeException e ) - { - Assert.assertEquals( "Wrapper properties file '" + propertiesFile + "' does not exist.", e.getMessage() ); - } + RuntimeException e = assertThrows( "Expected RuntimeException", + RuntimeException.class, + () -> WrapperExecutor.forWrapperPropertiesFile( propertiesFile ) ); + assertEquals( "Wrapper properties file '" + propertiesFile + "' does not exist.", e.getMessage() ); } @Test @@ -190,8 +185,8 @@ public class WrapperExecutorTest writePropertiesFile( properties, propertiesFile, "header" ); WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile ); - Assert.assertNotEquals( "some/relative/url/to/bin.zip", wrapper.getDistribution().getSchemeSpecificPart() ); - Assert.assertTrue( wrapper.getDistribution().getSchemeSpecificPart().endsWith( "some/relative/url/to/bin.zip" ) ); + assertNotEquals( "some/relative/url/to/bin.zip", wrapper.getDistribution().getSchemeSpecificPart() ); + assertTrue( wrapper.getDistribution().getSchemeSpecificPart().endsWith( "some/relative/url/to/bin.zip" ) ); } private void writePropertiesFile( Properties properties, Path propertiesFile, String message )
