From d7036d97bef720db534b1a132fbffed0851f622e Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Fri, 1 Aug 2025 09:35:51 -0500 Subject: [PATCH] use `assertThrows` instead of `ExpectedException` rule --- test/java/org/apache/ivy/MainTest.java | 91 +++++------ .../apache/ivy/ant/IvyCacheFilesetTest.java | 32 ++-- .../org/apache/ivy/ant/IvyCleanCacheTest.java | 21 ++- .../org/apache/ivy/ant/IvyConfigureTest.java | 38 ++--- .../ant/IvyDependencyUpdateCheckerTest.java | 12 +- .../org/apache/ivy/ant/IvyPublishTest.java | 70 ++++---- .../org/apache/ivy/ant/IvyResolveTest.java | 16 +- .../core/module/id/ModuleRevisionIdTest.java | 19 +-- .../apache/ivy/core/resolve/ResolveTest.java | 153 +++++++----------- .../core/settings/XmlSettingsParserTest.java | 27 ++-- .../ivy/osgi/core/ManifestHeaderTest.java | 29 ++-- .../conflict/RegexpConflictManagerTest.java | 23 ++- .../m2/PomModuleDescriptorParserTest.java | 41 ++--- .../xml/XmlModuleDescriptorParserTest.java | 95 +++++------ .../resolver/FileSystemResolverTest.java | 60 +++---- .../plugins/resolver/IvyRepResolverTest.java | 28 ++-- ...erTest.java => HttpClientHandlerTest.java} | 66 ++++---- 17 files changed, 330 insertions(+), 491 deletions(-) rename test/java/org/apache/ivy/util/url/{HttpclientURLHandlerTest.java => HttpClientHandlerTest.java} (86%) diff --git a/test/java/org/apache/ivy/MainTest.java b/test/java/org/apache/ivy/MainTest.java index 994afb27..ba207a59 100644 --- a/test/java/org/apache/ivy/MainTest.java +++ b/test/java/org/apache/ivy/MainTest.java @@ -17,17 +17,6 @@ */ package org.apache.ivy; -import org.apache.ivy.core.retrieve.RetrieveOptions; -import org.apache.ivy.util.CacheCleaner; -import org.apache.ivy.util.cli.CommandLine; -import org.apache.ivy.util.cli.ParseException; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; - import java.io.File; import java.net.URL; import java.nio.file.Files; @@ -37,18 +26,27 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import org.apache.ivy.core.retrieve.RetrieveOptions; +import org.apache.ivy.util.CacheCleaner; +import org.apache.ivy.util.cli.CommandLine; +import org.apache.ivy.util.cli.ParseException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class MainTest { private File cache; - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Rule public TemporaryFolder tempDir = new TemporaryFolder(); @@ -65,51 +63,47 @@ public class MainTest { @Test public void testHelp() throws Exception { - run(new String[] {"-?"}); + run("-?"); } @Test - public void testBadOption() throws Exception { - expExc.expect(ParseException.class); - expExc.expectMessage("Unrecognized option: -bad"); - - run(new String[] {"-bad"}); + public void testBadOption() { + Exception exception = assertThrows(ParseException.class, () -> run("-bad")); + assertEquals("Unrecognized option: -bad", exception.getMessage()); } @Test - public void testMissingParameter() throws Exception { - expExc.expect(ParseException.class); - expExc.expectMessage("no argument for: ivy"); - - run(new String[] {"-ivy"}); + public void testMissingParameter() { + Exception exception = assertThrows(ParseException.class, () -> run("-ivy")); + assertEquals("no argument for: ivy", exception.getMessage()); } @Test public void testResolveSimple() throws Exception { - run(new String[] {"-settings", "test/repositories/ivysettings.xml", "-ivy", - "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"}); + run("-settings", "test/repositories/ivysettings.xml", "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"); assertTrue(new File("build/cache/org1/mod1.2/ivy-2.0.xml").exists()); } @Test public void testResolveSimpleWithConfs() throws Exception { - run(new String[] {"-settings", "test/repositories/ivysettings.xml", "-ivy", - "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml", "-confs", "default"}); + run("-settings", "test/repositories/ivysettings.xml", "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml", "-confs", "default"); assertTrue(new File("build/cache/org1/mod1.2/ivy-2.0.xml").exists()); } @Test public void testResolveSimpleWithConfs2() throws Exception { - run(new String[] {"-settings", "test/repositories/ivysettings.xml", "-confs", "default", - "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"}); + run("-settings", "test/repositories/ivysettings.xml", "-confs", "default", "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"); assertTrue(new File("build/cache/org1/mod1.2/ivy-2.0.xml").exists()); } @Test public void testExtraParams1() throws Exception { - String[] params = new String[] {"-settings", "test/repositories/ivysettings.xml", "-confs", - "default", "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml", "foo1", - "foo2"}; + String[] params = { + "-settings", "test/repositories/ivysettings.xml", + "-confs", "default", + "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml", + "foo1", "foo2" + }; CommandLine line = Main.getParser().parse(params); String[] leftOver = line.getLeftOverArgs(); assertNotNull(leftOver); @@ -120,9 +114,12 @@ public class MainTest { @Test public void testExtraParams2() throws Exception { - String[] params = new String[] {"-settings", "test/repositories/ivysettings.xml", "-confs", - "default", "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml", "--", - "foo1", "foo2"}; + String[] params = { + "-settings", "test/repositories/ivysettings.xml", + "-confs", "default", + "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml", + "--", "foo1", "foo2" + }; CommandLine line = Main.getParser().parse(params); String[] leftOver = line.getLeftOverArgs(); assertNotNull(leftOver); @@ -133,8 +130,11 @@ public class MainTest { @Test public void testExtraParams3() throws Exception { - String[] params = new String[] {"-settings", "test/repositories/ivysettings.xml", "-confs", - "default", "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"}; + String[] params = { + "-settings", "test/repositories/ivysettings.xml", + "-confs", "default", + "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml" + }; CommandLine line = Main.getParser().parse(params); String[] leftOver = line.getLeftOverArgs(); assertNotNull(leftOver); @@ -146,7 +146,6 @@ public class MainTest { * {@code types} argument to the command line must be parsed correctly when it's passed * more than one value for the argument. * - * @throws Exception if something goes wrong * @see IVY-1355 */ @Test @@ -165,8 +164,6 @@ public class MainTest { /** * Tests that the {@code overwriteMode} passed for the retrieve command works as expected - * - * @throws Exception if something goes wrong */ @Test public void testRetrieveOverwriteMode() throws Exception { @@ -190,8 +187,6 @@ public class MainTest { /** * Tests that the {@code makepom} option works as expected - * - * @throws Exception if something goes wrong */ @Test public void testMakePom() throws Exception { @@ -212,14 +207,12 @@ public class MainTest { */ @Test public void testSettingsURL() throws Exception { - final URL settingsURL = new File("test/repositories/ivysettings.xml").toURI().toURL(); - run(new String[] {"-settings", settingsURL.toString(), "-ivy", - "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"}); - + URL settingsURL = new File("test/repositories/ivysettings.xml").toURI().toURL(); + run("-settings", settingsURL.toString(), "-ivy", "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml"); assertTrue(new File("build/cache/org1/mod1.2/ivy-2.0.xml").exists()); } - private void run(String[] args) throws Exception { + private void run(String... args) throws Exception { Main.run(Main.getParser(), args); } } diff --git a/test/java/org/apache/ivy/ant/IvyCacheFilesetTest.java b/test/java/org/apache/ivy/ant/IvyCacheFilesetTest.java index 4cb5e48a..782d9ac5 100644 --- a/test/java/org/apache/ivy/ant/IvyCacheFilesetTest.java +++ b/test/java/org/apache/ivy/ant/IvyCacheFilesetTest.java @@ -17,11 +17,6 @@ */ package org.apache.ivy.ant; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.util.Arrays; import java.util.Collections; @@ -29,16 +24,22 @@ import java.util.List; import org.apache.ivy.TestHelper; import org.apache.ivy.core.report.ArtifactDownloadReport; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Delete; import org.apache.tools.ant.types.FileSet; + import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; public class IvyCacheFilesetTest { @@ -46,9 +47,6 @@ public class IvyCacheFilesetTest { private Project project; - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Before public void setUp() { TestHelper.createCache(); @@ -227,11 +225,10 @@ public class IvyCacheFilesetTest { @Test public void requireCommonBaseDirEmptyList() { + List reports = Collections.emptyList(); // we expect a BuildException when we try to find a (non-existent) common base dir // across file system roots - expExc.expect(BuildException.class); - List reports = Collections.emptyList(); - fileset.requireCommonBaseDir(reports); + assertThrows(BuildException.class, () -> fileset.requireCommonBaseDir(reports)); } @Test @@ -241,14 +238,13 @@ public class IvyCacheFilesetTest { // single file system root isn't what we are interested in, in this test method return; } - // we expect a BuildException when we try to find a (non-existent) common base dir - // across file system roots - expExc.expect(BuildException.class); List reports = Arrays.asList( artifactDownloadReport(new File(fileSystemRoots[0], "a/b/c/d")), artifactDownloadReport(new File(fileSystemRoots[1], "a/b/e/f")) - ); - fileset.requireCommonBaseDir(reports); + ); + // we expect a BuildException when we try to find a (non-existent) common base dir + // across file system roots + assertThrows(BuildException.class, () -> fileset.requireCommonBaseDir(reports)); } @Test diff --git a/test/java/org/apache/ivy/ant/IvyCleanCacheTest.java b/test/java/org/apache/ivy/ant/IvyCleanCacheTest.java index 1d19088f..90a07d39 100644 --- a/test/java/org/apache/ivy/ant/IvyCleanCacheTest.java +++ b/test/java/org/apache/ivy/ant/IvyCleanCacheTest.java @@ -17,20 +17,23 @@ */ package org.apache.ivy.ant; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.File; import org.apache.ivy.TestHelper; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; + import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; public class IvyCleanCacheTest { + private IvyCleanCache cleanCache; private File cacheDir; @@ -41,9 +44,6 @@ public class IvyCleanCacheTest { private File resolutionCache; - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Before public void setUp() throws Exception { Project p = TestHelper.newProject(); @@ -106,10 +106,9 @@ public class IvyCleanCacheTest { */ @Test public void testUnknownCache() { - expExc.expect(BuildException.class); - expExc.expectMessage("unknown cache 'yourcache'"); cleanCache.setResolution(false); cleanCache.setCache("yourcache"); - cleanCache.perform(); + Exception exception = assertThrows(BuildException.class, () -> cleanCache.perform()); + assertEquals("unknown cache 'yourcache'", exception.getMessage()); } } diff --git a/test/java/org/apache/ivy/ant/IvyConfigureTest.java b/test/java/org/apache/ivy/ant/IvyConfigureTest.java index 97b34371..5b4c8751 100644 --- a/test/java/org/apache/ivy/ant/IvyConfigureTest.java +++ b/test/java/org/apache/ivy/ant/IvyConfigureTest.java @@ -17,12 +17,6 @@ */ package org.apache.ivy.ant; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.util.List; @@ -33,22 +27,27 @@ import org.apache.ivy.core.settings.IvySettings; import org.apache.ivy.plugins.resolver.DependencyResolver; import org.apache.ivy.plugins.resolver.IBiblioResolver; import org.apache.ivy.plugins.resolver.IvyRepResolver; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Reference; + import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; public class IvyConfigureTest { + private IvyConfigure configure; private Project project; - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Before public void setUp() { project = TestHelper.newProject(); @@ -278,10 +277,6 @@ public class IvyConfigureTest { */ @Test public void testOverrideNotAllowed() { - expExc.expect(BuildException.class); - expExc.expectMessage("Overriding a previous definition of ivy:settings with the id '" - + configure.getSettingsId() + "' is not allowed when using override='notallowed'."); - configure.setFile(new File("test/repositories/ivysettings.xml")); configure.execute(); @@ -293,7 +288,9 @@ public class IvyConfigureTest { configure.setOverride("notallowed"); configure.setFile(new File("test/repositories/ivysettings.xml")); - configure.execute(); + Exception exception = assertThrows(BuildException.class, () -> configure.execute()); + assertEquals("Overriding a previous definition of ivy:settings with the id '" + + configure.getSettingsId() + "' is not allowed when using override='notallowed'.", exception.getMessage()); } /** @@ -301,18 +298,13 @@ public class IvyConfigureTest { */ @Test public void testInvalidOverride() { - expExc.expect(IllegalArgumentException.class); - expExc.expectMessage("invalid override value 'unknown'. Valid values are " - + "[true, false, notallowed]"); - - configure.setOverride("unknown"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> configure.setOverride("unknown")); + assertEquals("invalid override value 'unknown'. Valid values are [true, false, notallowed]", exception.getMessage()); } /** * Tests that if the Ivy settings file includes another file as * optional, then the absence of that file doesn't lead to failures - * - * @throws Exception if something goes wrong */ @Test public void testOptionalFileInclude() throws Exception { diff --git a/test/java/org/apache/ivy/ant/IvyDependencyUpdateCheckerTest.java b/test/java/org/apache/ivy/ant/IvyDependencyUpdateCheckerTest.java index 4fa96065..26f14e84 100644 --- a/test/java/org/apache/ivy/ant/IvyDependencyUpdateCheckerTest.java +++ b/test/java/org/apache/ivy/ant/IvyDependencyUpdateCheckerTest.java @@ -28,21 +28,17 @@ import org.apache.tools.ant.Project; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; public class IvyDependencyUpdateCheckerTest extends AntTaskTestCase { private IvyDependencyUpdateChecker dependencyUpdateChecker; - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Before public void setUp() { TestHelper.createCache(); @@ -146,12 +142,10 @@ public class IvyDependencyUpdateCheckerTest extends AntTaskTestCase { */ @Test public void testFailureWithMissingConfigurations() { - expExc.expect(BuildException.class); - expExc.expectMessage("unknown"); - dependencyUpdateChecker.setFile(new File("test/java/org/apache/ivy/ant/ivy-simple.xml")); dependencyUpdateChecker.setConf("default,unknown"); - dependencyUpdateChecker.execute(); + Exception exception = assertThrows(BuildException.class, () -> dependencyUpdateChecker.execute()); + assertEquals("requested configuration not found in apache#resolve-simple;1.0: [ 'unknown' ]", exception.getCause().getMessage()); } /** diff --git a/test/java/org/apache/ivy/ant/IvyPublishTest.java b/test/java/org/apache/ivy/ant/IvyPublishTest.java index 01e6dd5a..430d77df 100644 --- a/test/java/org/apache/ivy/ant/IvyPublishTest.java +++ b/test/java/org/apache/ivy/ant/IvyPublishTest.java @@ -39,23 +39,20 @@ import org.apache.tools.ant.taskdefs.Echo; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; public class IvyPublishTest { - private File cache; private IvyPublish publish; private Project project; - @Rule - public ExpectedException expExc = ExpectedException.none(); + private File cache; @Before public void setUp() { @@ -405,8 +402,6 @@ public class IvyPublishTest { */ @Test public void testHaltOnMissing() { - expExc.expect(BuildException.class); - expExc.expectMessage("missing artifact apache#resolve-simple;1.2!resolve-simple.jar"); project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-multiconf.xml"); IvyResolve res = new IvyResolve(); res.setProject(project); @@ -415,19 +410,16 @@ public class IvyPublishTest { publish.setPubrevision("1.2"); publish.setResolver("1"); publish.setHaltonmissing(true); - try { - publish.execute(); - } finally { - // should have delivered the ivy file - assertTrue(new File("build/test/publish/ivy-1.2.xml").exists()); - // should not have published the files with "1" resolver - assertFalse(new File("test/repositories/1/apache/resolve-simple/ivys/ivy-1.2.xml") - .exists()); - assertFalse(new File( - "test/repositories/1/apache/resolve-simple/jars/resolve-simple-1.2.jar") - .exists()); - } + Exception exception = assertThrows(BuildException.class, () -> publish.execute()); + assertEquals("missing artifact apache#resolve-simple;1.2!resolve-simple.jar", exception.getCause().getMessage()); + + // should have delivered the ivy file + assertTrue(new File("build/test/publish/ivy-1.2.xml").exists()); + + // should not have published the files with "1" resolver + assertFalse(new File("test/repositories/1/apache/resolve-simple/ivys/ivy-1.2.xml").exists()); + assertFalse(new File("test/repositories/1/apache/resolve-simple/jars/resolve-simple-1.2.jar").exists()); } /** @@ -437,8 +429,6 @@ public class IvyPublishTest { */ @Test public void testHaltOnMissing2() throws IOException { - expExc.expect(BuildException.class); - expExc.expectMessage("missing artifact apache#multi;1.2!multi2.jar"); project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-publish-multi.xml"); IvyResolve res = new IvyResolve(); res.setProject(project); @@ -451,15 +441,15 @@ public class IvyPublishTest { publish.setPubrevision("1.2"); publish.setResolver("transactional"); publish.setHaltonmissing(true); - try { - publish.execute(); - } finally { - // should have delivered the ivy file - assertTrue(new File("build/test/publish/ivy-1.2.xml").exists()); - // should not have published the files with "transactional" resolver - assertFalse(new File("build/test/transactional/apache/multi/1.2").exists()); - } + Exception exception = assertThrows(BuildException.class, () -> publish.execute()); + assertEquals("missing artifact apache#multi;1.2!multi2.jar", exception.getCause().getMessage()); + + // should have delivered the ivy file + assertTrue(new File("build/test/publish/ivy-1.2.xml").exists()); + + // should not have published the files with "transactional" resolver + assertFalse(new File("build/test/transactional/apache/multi/1.2").exists()); } /** @@ -469,8 +459,6 @@ public class IvyPublishTest { */ @Test public void testHaltOnMissing3() throws IOException { - expExc.expect(BuildException.class); - expExc.expectMessage("missing artifact apache#multi;1.2!multi2.jar"); project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-publish-multi.xml"); IvyResolve res = new IvyResolve(); res.setProject(project); @@ -483,15 +471,15 @@ public class IvyPublishTest { publish.setPubrevision("1.2"); publish.setResolver("1"); publish.setHaltonmissing(true); - try { - publish.execute(); - } finally { - // should have delivered the ivy file - assertTrue(new File("build/test/publish/ivy-1.2.xml").exists()); - // should not have published the files with "transactional" resolver - assertFalse(new File("test/repositories/1/apache/multi").exists()); - } + Exception exception = assertThrows(BuildException.class, () -> publish.execute()); + assertEquals("missing artifact apache#multi;1.2!multi2.jar", exception.getCause().getMessage()); + + // should have delivered the ivy file + assertTrue(new File("build/test/publish/ivy-1.2.xml").exists()); + + // should not have published the files with "transactional" resolver + assertFalse(new File("test/repositories/1/apache/multi").exists()); } @Test diff --git a/test/java/org/apache/ivy/ant/IvyResolveTest.java b/test/java/org/apache/ivy/ant/IvyResolveTest.java index d69a8317..088968e7 100644 --- a/test/java/org/apache/ivy/ant/IvyResolveTest.java +++ b/test/java/org/apache/ivy/ant/IvyResolveTest.java @@ -31,24 +31,20 @@ import org.apache.tools.ant.taskdefs.Parallel; import org.junit.After; import org.junit.Before; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; public class IvyResolveTest { - private Project project; - private IvyResolve resolve; - @Rule - public ExpectedException expExc = ExpectedException.none(); + private Project project; @Before public void setUp() { @@ -324,12 +320,10 @@ public class IvyResolveTest { @Test public void testFailureWithMissingConfigurations() { - expExc.expect(BuildException.class); - expExc.expectMessage("unknown"); - resolve.setFile(new File("test/java/org/apache/ivy/ant/ivy-simple.xml")); resolve.setConf("default,unknown"); - resolve.execute(); + Exception exception = assertThrows(BuildException.class, () -> resolve.execute()); + assertEquals("requested configuration not found in apache#resolve-simple;1.0: [ 'unknown' ]", exception.getCause().getMessage()); } @Test(expected = BuildException.class) diff --git a/test/java/org/apache/ivy/core/module/id/ModuleRevisionIdTest.java b/test/java/org/apache/ivy/core/module/id/ModuleRevisionIdTest.java index 0c8fc3c9..9a7667e6 100644 --- a/test/java/org/apache/ivy/core/module/id/ModuleRevisionIdTest.java +++ b/test/java/org/apache/ivy/core/module/id/ModuleRevisionIdTest.java @@ -17,20 +17,17 @@ */ package org.apache.ivy.core.module.id; -import static org.junit.Assert.assertEquals; - import java.util.HashMap; import java.util.Map; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; public class ModuleRevisionIdTest { - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Test public void testParse() { testParse("#A;1.0"); @@ -67,10 +64,8 @@ public class ModuleRevisionIdTest { } private void testParseFailure(String mrid) { - expExc.expect(IllegalArgumentException.class); - expExc.expectMessage(mrid); - - ModuleRevisionId.parse(mrid); + Exception exception = assertThrows(IllegalArgumentException.class, () -> ModuleRevisionId.parse(mrid)); + assertTrue(exception.getMessage().contains(mrid)); } @Test @@ -90,11 +85,9 @@ public class ModuleRevisionIdTest { extraAttributes.put("nullatt", null); testEncodeDecodeToString( ModuleRevisionId.newInstance("org/apache", "pre/name", "1.0-dev8/2", extraAttributes)); - } private void testEncodeDecodeToString(ModuleRevisionId mrid) { assertEquals(mrid, ModuleRevisionId.decode(mrid.encodeToString())); } - } diff --git a/test/java/org/apache/ivy/core/resolve/ResolveTest.java b/test/java/org/apache/ivy/core/resolve/ResolveTest.java index 53631de8..a18816de 100644 --- a/test/java/org/apache/ivy/core/resolve/ResolveTest.java +++ b/test/java/org/apache/ivy/core/resolve/ResolveTest.java @@ -17,6 +17,26 @@ */ package org.apache.ivy.core.resolve; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + import org.apache.ivy.Ivy; import org.apache.ivy.TestHelper; import org.apache.ivy.core.cache.ArtifactOrigin; @@ -52,56 +72,28 @@ import org.apache.ivy.util.CacheCleaner; import org.apache.ivy.util.FileUtil; import org.apache.ivy.util.MockMessageLogger; import org.apache.ivy.util.XMLHelper; + import org.junit.After; -import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - import static org.apache.ivy.util.StringUtils.joinArray; + 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.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -/** - * - */ -public class ResolveTest { +public final class ResolveTest { + private Ivy ivy; - private File cache; - - private File deliverDir; - - private File workDir; - - @Rule - public ExpectedException expExc = ExpectedException.none(); + private File cache, workDir, deliverDir; @Before public void setUp() throws Exception { @@ -746,29 +738,22 @@ public class ResolveTest { // set up repository FileUtil.forceDelete(new File("build/testCache2")); - FileUtil.copy(new File("test/repositories/1/org1/mod1.2/jars/mod1.2-2.0.jar"), new File( - "build/testCache2/mod1.2-1.5.jar"), null); + FileUtil.copy(new File("test/repositories/1/org1/mod1.2/jars/mod1.2-2.0.jar"), new File("build/testCache2/mod1.2-1.5.jar"), null); // we first do a simple resolve so that module is in cache - ResolveReport report = ivy.resolve(new File( - "test/repositories/1/org1/mod1.4/ivys/ivy-1.0.2.xml"), - getResolveOptions(new String[] {"*"})); + ResolveReport report = ivy.resolve(new File("test/repositories/1/org1/mod1.4/ivys/ivy-1.0.2.xml"), getResolveOptions(new String[] {"*"})); assertFalse(report.hasError()); - assertEquals(Collections.singleton(ModuleRevisionId.newInstance("org1", "mod1.2", "1.5")), - report.getConfigurationReport("default").getModuleRevisionIds()); + assertEquals(Collections.singleton(ModuleRevisionId.newInstance("org1", "mod1.2", "1.5")), report.getConfigurationReport("default").getModuleRevisionIds()); // now we update the repository - FileUtil.copy(new File("test/repositories/1/org1/mod1.2/jars/mod1.2-2.0.jar"), new File( - "build/testCache2/mod1.2-1.6.jar"), null); + FileUtil.copy(new File("test/repositories/1/org1/mod1.2/jars/mod1.2-2.0.jar"), new File("build/testCache2/mod1.2-1.6.jar"), null); // now do a new resolve: it should not use cached data - report = ivy.resolve(new File("test/repositories/1/org1/mod1.4/ivys/ivy-1.0.2.xml"), - getResolveOptions(new String[] {"*"})); + report = ivy.resolve(new File("test/repositories/1/org1/mod1.4/ivys/ivy-1.0.2.xml"), getResolveOptions(new String[] {"*"})); assertFalse(report.hasError()); - assertEquals(Collections.singleton(ModuleRevisionId.newInstance("org1", "mod1.2", "1.6")), - report.getConfigurationReport("default").getModuleRevisionIds()); + assertEquals(Collections.singleton(ModuleRevisionId.newInstance("org1", "mod1.2", "1.6")), report.getConfigurationReport("default").getModuleRevisionIds()); } @Test @@ -3479,34 +3464,24 @@ public class ResolveTest { /** * Circular dependency: mod6.3 depends on mod6.2, which itself depends on mod6.3; * circular dependency strategy set to error. - * - * @throws Exception if something goes wrong */ @Test public void testCircular() throws Exception { - expExc.expect(CircularDependencyException.class); - expExc.expectMessage("org6#mod6.3;1.0->org6#mod6.2;1.0->org6#mod6.3;latest.integration"); - - ResolveReport report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), - getResolveOptions(new String[] {"default"})); + ResolveReport report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), getResolveOptions(new String[] {"default"})); assertFalse(report.hasError()); - ivy.getSettings().setCircularDependencyStrategy( - IgnoreCircularDependencyStrategy.getInstance()); - report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), - getResolveOptions(new String[] {"default"})); + ivy.getSettings().setCircularDependencyStrategy(IgnoreCircularDependencyStrategy.getInstance()); + report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), getResolveOptions(new String[] {"default"})); assertFalse(report.hasError()); - ivy.getSettings().setCircularDependencyStrategy( - WarnCircularDependencyStrategy.getInstance()); - report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), - getResolveOptions(new String[] {"default"})); + ivy.getSettings().setCircularDependencyStrategy(WarnCircularDependencyStrategy.getInstance()); + report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), getResolveOptions(new String[] {"default"})); assertFalse(report.hasError()); - ivy.getSettings().setCircularDependencyStrategy( - ErrorCircularDependencyStrategy.getInstance()); - ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), - getResolveOptions(new String[] {"default"})); + ivy.getSettings().setCircularDependencyStrategy(ErrorCircularDependencyStrategy.getInstance()); + Exception e = assertThrows(CircularDependencyException.class, () -> + ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.0.xml"), getResolveOptions(new String[] {"default"}))); + assertEquals("org6#mod6.3;1.0->org6#mod6.2;1.0->org6#mod6.3;latest.integration", e.getMessage()); } /** @@ -3517,17 +3492,13 @@ public class ResolveTest { */ @Test public void testCircular2() throws Exception { - expExc.expect(CircularDependencyException.class); - expExc.expectMessage("org8#mod8.5;NONE->org8#mod8.6;2.+->org8#mod8.5;2.+"); - - ResolveReport report = ivy.resolve(new File("test/repositories/circular/ivy.xml"), - getResolveOptions(new String[] {"*"})); + ResolveReport report = ivy.resolve(new File("test/repositories/circular/ivy.xml"), getResolveOptions(new String[] {"*"})); assertFalse(report.hasError()); - ivy.getSettings().setCircularDependencyStrategy( - ErrorCircularDependencyStrategy.getInstance()); - ivy.resolve(new File("test/repositories/circular/ivy.xml"), - getResolveOptions(new String[] {"*"})); + ivy.getSettings().setCircularDependencyStrategy(ErrorCircularDependencyStrategy.getInstance()); + Exception e = assertThrows(CircularDependencyException.class, () -> + ivy.resolve(new File("test/repositories/circular/ivy.xml"), getResolveOptions(new String[] {"*"}))); + assertEquals("org8#mod8.5;NONE->org8#mod8.6;2.+->org8#mod8.5;2.+", e.getMessage()); } /** @@ -3540,36 +3511,28 @@ public class ResolveTest { */ @Test public void testCircular3() throws Exception { - expExc.expect(CircularDependencyException.class); - expExc.expectMessage("org6#mod6.3;1.2->org6#mod6.2;1.1->..."); - - ResolveReport report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), - getResolveOptions(new String[] {"default", "test"})); + ResolveReport report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), getResolveOptions(new String[] {"default", "test"})); assertFalse(report.hasError()); // we should have mod 6.2 artifact in both configurations assertEquals(1, report.getConfigurationReport("default").getArtifactsNumber()); assertEquals(1, report.getConfigurationReport("test").getArtifactsNumber()); - ivy.getSettings().setCircularDependencyStrategy( - IgnoreCircularDependencyStrategy.getInstance()); - report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), - getResolveOptions(new String[] {"default", "test"})); + ivy.getSettings().setCircularDependencyStrategy(IgnoreCircularDependencyStrategy.getInstance()); + report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), getResolveOptions(new String[] {"default", "test"})); assertFalse(report.hasError()); assertEquals(1, report.getConfigurationReport("default").getArtifactsNumber()); assertEquals(1, report.getConfigurationReport("test").getArtifactsNumber()); - ivy.getSettings().setCircularDependencyStrategy( - WarnCircularDependencyStrategy.getInstance()); - report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), - getResolveOptions(new String[] {"default", "test"})); + ivy.getSettings().setCircularDependencyStrategy(WarnCircularDependencyStrategy.getInstance()); + report = ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), getResolveOptions(new String[] {"default", "test"})); assertFalse(report.hasError()); assertEquals(1, report.getConfigurationReport("default").getArtifactsNumber()); assertEquals(1, report.getConfigurationReport("test").getArtifactsNumber()); - ivy.getSettings().setCircularDependencyStrategy( - ErrorCircularDependencyStrategy.getInstance()); - ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), - getResolveOptions(new String[] {"default", "test"})); + ivy.getSettings().setCircularDependencyStrategy(ErrorCircularDependencyStrategy.getInstance()); + Exception e = assertThrows(CircularDependencyException.class, () -> + ivy.resolve(new File("test/repositories/2/mod6.3/ivy-1.2.xml"), getResolveOptions(new String[] {"default", "test"}))); + assertEquals("org6#mod6.3;1.2->org6#mod6.2;1.1->...", e.getMessage()); } @Test @@ -4421,13 +4384,11 @@ public class ResolveTest { assertTrue(getArchiveFileInCache("org5", "mod5.1", "4.1", "art51B", "jar", "jar").exists()); } - /** * Test for issues IVY-982 and IVY-1547, * which have to do with resolve engine ignoring negated configurations * on the left side of the maps-to operator. */ - @Test public void testResolveWithNegation() throws Exception { ConfigurationResolveReport crp; @@ -5107,8 +5068,8 @@ public class ResolveTest { ivy.getSettings().setDefaultResolver("parentChain"); final File pom = new File("test/repositories/parentPom/org/apache/dm/sibling1/1.0/sibling1-1.0.pom"); final ResolveReport report = ivy.resolve(pom, getResolveOptions(new String[]{"*"})); - Assert.assertNotNull("Resolve report is null", report); - Assert.assertFalse("Resolve report has errors", report.hasError()); + assertNotNull("Resolve report is null", report); + assertFalse("Resolve report has errors", report.hasError()); } @Test diff --git a/test/java/org/apache/ivy/core/settings/XmlSettingsParserTest.java b/test/java/org/apache/ivy/core/settings/XmlSettingsParserTest.java index 4ba4387e..92499da5 100644 --- a/test/java/org/apache/ivy/core/settings/XmlSettingsParserTest.java +++ b/test/java/org/apache/ivy/core/settings/XmlSettingsParserTest.java @@ -17,6 +17,11 @@ */ package org.apache.ivy.core.settings; +import java.io.File; +import java.io.IOException; +import java.text.ParseException; +import java.util.List; + import org.apache.ivy.core.cache.DefaultRepositoryCacheManager; import org.apache.ivy.core.cache.ResolutionCacheManager; import org.apache.ivy.core.module.descriptor.Artifact; @@ -43,27 +48,20 @@ import org.apache.ivy.plugins.version.ChainVersionMatcher; import org.apache.ivy.plugins.version.MavenTimedSnapshotVersionMatcher; import org.apache.ivy.plugins.version.MockVersionMatcher; import org.apache.ivy.plugins.version.VersionMatcher; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import java.io.File; -import java.io.IOException; -import java.text.ParseException; -import java.util.List; +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.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; /** * Test the parsing of Ivy settings file through the {@link XmlSettingsParser} */ public class XmlSettingsParserTest { - @Rule - public ExpectedException expExc = ExpectedException.none(); @Test public void test() throws Exception { @@ -261,18 +259,15 @@ public class XmlSettingsParserTest { /** * Test of resolver referencing a non existent cache. - * - * @throws Exception if something goes wrong */ @Test - public void testInvalidCache() throws Exception { - expExc.expect(ParseException.class); - expExc.expectMessage("mycache"); - + public void testInvalidCache() { IvySettings settings = new IvySettings(); XmlSettingsParser parser = new XmlSettingsParser(settings); - parser.parse(XmlSettingsParserTest.class.getResource("ivysettings-cache-invalid.xml")); + Exception exception = assertThrows(ParseException.class, () -> + parser.parse(XmlSettingsParserTest.class.getResource("ivysettings-cache-invalid.xml"))); + assertTrue(exception.getMessage().endsWith("unknown cache manager 'mycache'. Available caches are [mycache2]")); } @Test diff --git a/test/java/org/apache/ivy/osgi/core/ManifestHeaderTest.java b/test/java/org/apache/ivy/osgi/core/ManifestHeaderTest.java index 745c9eb9..20f33100 100644 --- a/test/java/org/apache/ivy/osgi/core/ManifestHeaderTest.java +++ b/test/java/org/apache/ivy/osgi/core/ManifestHeaderTest.java @@ -17,20 +17,17 @@ */ package org.apache.ivy.osgi.core; -import org.junit.Rule; -import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - import java.text.ParseException; import java.util.Arrays; import java.util.Collection; -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.is; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; @RunWith(Enclosed.class) public class ManifestHeaderTest { @@ -92,15 +89,11 @@ public class ManifestHeaderTest { ManifestHeaderValue value = new ManifestHeaderValue("glassfish javax.servlet.3.1.0.b33"); assertEquals("glassfish javax.servlet.3.1.0.b33", value.getSingleValue()); } - } @RunWith(Parameterized.class) public static class IllegalOptionTests { - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Parameterized.Parameters(name = "Illegal token at {1} in {0}") public static Collection data() { return Arrays.asList(new Object[][] {{"value1=", 6}, @@ -119,12 +112,10 @@ public class ManifestHeaderTest { * Expected failure when the parameter is illegal */ @Test - public void testSyntaxError() throws ParseException { - expExc.expect(ParseException.class); - expExc.expect(hasProperty("errorOffset", is(offset))); - new ManifestHeaderValue(value); + public void testSyntaxError() { + ParseException exception = assertThrows(ParseException.class, () -> new ManifestHeaderValue(value)); + assertEquals(offset, exception.getErrorOffset()); } - } @RunWith(Parameterized.class) @@ -155,7 +146,5 @@ public class ManifestHeaderTest { public void testNormalisation() throws Exception { assertEquals(new ManifestHeaderValue(normalised), new ManifestHeaderValue(value)); } - } - } diff --git a/test/java/org/apache/ivy/plugins/conflict/RegexpConflictManagerTest.java b/test/java/org/apache/ivy/plugins/conflict/RegexpConflictManagerTest.java index 46c313c7..aac42bb2 100644 --- a/test/java/org/apache/ivy/plugins/conflict/RegexpConflictManagerTest.java +++ b/test/java/org/apache/ivy/plugins/conflict/RegexpConflictManagerTest.java @@ -22,24 +22,25 @@ import java.io.File; import org.apache.ivy.Ivy; import org.apache.ivy.core.resolve.ResolveOptions; import org.apache.ivy.util.FileUtil; + import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; public class RegexpConflictManagerTest { + private Ivy ivy; private File cache; - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Before public void setUp() throws Exception { ivy = new Ivy(); ivy.configure(RegexpConflictManagerTest.class.getResource("ivysettings-regexp-test.xml")); + cache = new File("build/cache"); cache.mkdirs(); } @@ -51,17 +52,13 @@ public class RegexpConflictManagerTest { @Test public void testNoApiConflictResolve() throws Exception { - ivy.resolve(RegexpConflictManagerTest.class.getResource("ivy-no-regexp-conflict.xml"), - getResolveOptions()); + ivy.resolve(RegexpConflictManagerTest.class.getResource("ivy-no-regexp-conflict.xml"), getResolveOptions()); } @Test - public void testConflictResolve() throws Exception { - expExc.expect(StrictConflictException.class); - expExc.expectMessage("org1#mod1.2;2.0.0:2.0 (needed by [apache#resolve-noconflict;1.0]) conflicts with org1#mod1.2;2.1.0:2.1 (needed by [apache#resolve-noconflict;1.0])"); - - ivy.resolve(RegexpConflictManagerTest.class.getResource("ivy-conflict.xml"), - getResolveOptions()); + public void testConflictResolve() { + Exception exception = assertThrows(StrictConflictException.class, () -> ivy.resolve(RegexpConflictManagerTest.class.getResource("ivy-conflict.xml"), getResolveOptions())); + assertEquals("org1#mod1.2;2.0.0:2.0 (needed by [apache#resolve-noconflict;1.0]) conflicts with org1#mod1.2;2.1.0:2.1 (needed by [apache#resolve-noconflict;1.0])", exception.getMessage()); } private ResolveOptions getResolveOptions() { diff --git a/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java b/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java index 58f6f47b..e933bde1 100644 --- a/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java +++ b/test/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorParserTest.java @@ -17,13 +17,6 @@ */ package org.apache.ivy.plugins.parser.m2; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; - import java.io.File; import java.io.IOException; import java.net.URL; @@ -58,13 +51,21 @@ import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest; import org.apache.ivy.plugins.repository.url.URLResource; import org.apache.ivy.plugins.resolver.DependencyResolver; import org.apache.ivy.plugins.resolver.MockResolver; + import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + public class PomModuleDescriptorParserTest extends AbstractModuleDescriptorParserTester { private IvySettings settings = new IvySettings(); @@ -86,9 +87,6 @@ public class PomModuleDescriptorParserTest extends AbstractModuleDescriptorParse private MockResolver mockedResolver = new MockedDependencyResolver(); - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Rule public TemporaryFolder workDir = new TemporaryFolder(); @@ -220,12 +218,10 @@ public class PomModuleDescriptorParserTest extends AbstractModuleDescriptorParse } @Test - public void testParentNotFound() throws Exception { - expExc.expect(IOException.class); - expExc.expectMessage("Impossible to load parent"); - - PomModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), - getClass().getResource("test-parent-not-found.pom"), false); + public void testParentNotFound() { + Exception exception = assertThrows(IOException.class, () -> + PomModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), getClass().getResource("test-parent-not-found.pom"), false)); + assertTrue(exception.getMessage().startsWith("Impossible to load parent")); } @Test @@ -427,7 +423,7 @@ public class PomModuleDescriptorParserTest extends AbstractModuleDescriptorParse assertEquals(5, dds.length); for (int i = 0; i < dds.length; i++) { assertEquals(2, dds[i].getAllDependencyArtifacts().length); - int withExt = i == 0 || i == 3 ? 1: 0; + int withExt = i == 0 || i == 3 ? 1 : 0; assertEquals(extraAtt, dds[i].getAllDependencyArtifacts()[withExt].getExtraAttributes()); } } @@ -1285,17 +1281,14 @@ public class PomModuleDescriptorParserTest extends AbstractModuleDescriptorParse for (final String expectedPackagingType : packagingTypesToTest) { String sysPropToSet = null; switch (expectedPackagingType) { - case "bundle": { + case "bundle": sysPropToSet = "PomModuleDescriptorParserTest.some-other-test-prop"; break; - } - case "jar": { + case "jar": sysPropToSet = "PomModuleDescriptorParserTest.some-test-prop"; break; - } - default: { + default: fail("Unexpected packaging type"); - } } // activate the relevant profile, based on a system property System.setProperty(sysPropToSet, "foo"); diff --git a/test/java/org/apache/ivy/plugins/parser/xml/XmlModuleDescriptorParserTest.java b/test/java/org/apache/ivy/plugins/parser/xml/XmlModuleDescriptorParserTest.java index 119f3557..1e552189 100644 --- a/test/java/org/apache/ivy/plugins/parser/xml/XmlModuleDescriptorParserTest.java +++ b/test/java/org/apache/ivy/plugins/parser/xml/XmlModuleDescriptorParserTest.java @@ -17,6 +17,22 @@ */ package org.apache.ivy.plugins.parser.xml; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.text.ParseException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashSet; +import java.util.Set; + import org.apache.ivy.Ivy; import org.apache.ivy.core.module.descriptor.Artifact; import org.apache.ivy.core.module.descriptor.Configuration; @@ -41,40 +57,23 @@ import org.apache.ivy.util.DefaultMessageLogger; import org.apache.ivy.util.FileUtil; import org.apache.ivy.util.Message; import org.apache.ivy.util.XMLHelper; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.text.ParseException; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashSet; -import java.util.Set; +import org.junit.Before; +import org.junit.Test; import static org.apache.ivy.core.module.descriptor.Configuration.Visibility.PRIVATE; import static org.apache.ivy.core.module.descriptor.Configuration.Visibility.PUBLIC; + 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.assertThrows; import static org.junit.Assert.assertTrue; public class XmlModuleDescriptorParserTest extends AbstractModuleDescriptorParserTester { - private IvySettings settings = null; - @Rule - public ExpectedException expExc = ExpectedException.none(); + private IvySettings settings; @Before public void setUp() { @@ -148,52 +147,42 @@ public class XmlModuleDescriptorParserTest extends AbstractModuleDescriptorParse } @Test - public void testBad() throws IOException, ParseException { - expExc.expect(ParseException.class); - expExc.expectMessage("'modul'"); - + public void testBad() { assertTrue(XMLHelper.canUseSchemaValidation()); - XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, - getClass().getResource("test-bad.xml"), true); + Exception exception = assertThrows(ParseException.class, () -> + XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-bad.xml"), true)); + assertTrue(exception.getMessage().contains("'modul'")); } @Test - public void testBadOrg() throws IOException, ParseException { - expExc.expect(ParseException.class); - expExc.expectMessage("organization"); - + public void testBadOrg() { assertTrue(XMLHelper.canUseSchemaValidation()); - XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, - getClass().getResource("test-bad-org.xml"), true); + Exception exception = assertThrows(ParseException.class, () -> + XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-bad-org.xml"), true)); + assertTrue(exception.getMessage().contains("organization")); } @Test - public void testBadConfs() throws IOException, ParseException { - expExc.expect(ParseException.class); - expExc.expectMessage("invalidConf"); - - XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, - getClass().getResource("test-bad-confs.xml"), true); + public void testBadConfs() { + Exception exception = assertThrows(ParseException.class, () -> + XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-bad-confs.xml"), true)); + assertTrue(exception.getMessage().contains("invalidConf")); } @Test - public void testCyclicConfs2() throws IOException, ParseException { - expExc.expect(ParseException.class); - expExc.expectMessage("A => B => A"); - - XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, - getClass().getResource("test-cyclic-confs1.xml"), true); + public void testCyclicConfs2() { + Exception exception = assertThrows(ParseException.class, () -> + XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-cyclic-confs1.xml"), true)); + assertTrue(exception.getMessage().contains("A => B => A")); } @Test - public void testCyclicConfs3() throws IOException, ParseException { - expExc.expect(ParseException.class); - expExc.expectMessage("A => C => B => A"); - - XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, - getClass().getResource("test-cyclic-confs2.xml"), true); + public void testCyclicConfs3() { + Exception exception = assertThrows(ParseException.class, () -> + XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-cyclic-confs2.xml"), true)); + assertTrue(exception.getMessage().contains("A => C => B => A")); } @Test @@ -1474,7 +1463,7 @@ public class XmlModuleDescriptorParserTest extends AbstractModuleDescriptorParse final Path targetDir = Paths.get(System.getProperty("java.io.tmpdir"), "foo%2Fbar"); Files.createDirectories(targetDir); final Path parentIvyXMLPath = Paths.get(targetDir.toString(), "parent-ivy.xml"); - try (final InputStream is = parentIvyXML.openStream()) { + try (InputStream is = parentIvyXML.openStream()) { Files.copy(is, parentIvyXMLPath, StandardCopyOption.REPLACE_EXISTING); } assertTrue("Parent ivy xml file wasn't copied", Files.isRegularFile(parentIvyXMLPath)); diff --git a/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java b/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java index a8b412f3..1cc3abeb 100644 --- a/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java +++ b/test/java/org/apache/ivy/plugins/resolver/FileSystemResolverTest.java @@ -51,21 +51,16 @@ import org.apache.ivy.util.FileUtil; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; 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.assertThrows; import static org.junit.Assert.assertTrue; -/** - * - */ public class FileSystemResolverTest extends AbstractDependencyResolverTest { - // CheckStyle:MagicNumberCheck OFF private static final String FS = File.separator; @@ -89,9 +84,6 @@ public class FileSystemResolverTest extends AbstractDependencyResolverTest { setupLastModified(); } - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Before public void setUp() { settings = new IvySettings(); @@ -889,21 +881,14 @@ public class FileSystemResolverTest extends AbstractDependencyResolverTest { resolver.abortPublishTransaction(); assertFalse(new File("test/repositories/1/myorg/mymodule/myrevision/ivy.xml").exists()); - assertFalse(new File( - "test/repositories/1/myorg/mymodule/myrevision/myartifact-myrevision.myext") - .exists()); + assertFalse(new File("test/repositories/1/myorg/mymodule/myrevision/myartifact-myrevision.myext").exists()); } /** * Publishing with transaction=true and an unsupported pattern must fail. - * - * @throws Exception if something goes wrong */ @Test - public void testUnsupportedTransaction() throws Exception { - expExc.expect(IllegalStateException.class); - expExc.expectMessage("transactional"); - + public void testUnsupportedTransaction() { FileSystemResolver resolver = new FileSystemResolver(); resolver.setName("test"); resolver.setSettings(settings); @@ -919,20 +904,18 @@ public class FileSystemResolverTest extends AbstractDependencyResolverTest { "myext"); File src = new File("test/repositories/ivysettings.xml"); - resolver.beginPublishTransaction(mrid, false); - resolver.publish(artifact, src, false); + Exception exception = assertThrows(IllegalStateException.class, () -> { + resolver.beginPublishTransaction(mrid, false); + resolver.publish(artifact, src, false); + }); + assertTrue(exception.getMessage().contains("transactional")); } /** * Publishing with transaction=true and an unsupported combination of patterns must fail. - * - * @throws Exception if something goes wrong */ @Test - public void testUnsupportedTransaction2() throws Exception { - expExc.expect(IllegalStateException.class); - expExc.expectMessage("transactional"); - + public void testUnsupportedTransaction2() { FileSystemResolver resolver = new FileSystemResolver(); resolver.setName("test"); resolver.setSettings(settings); @@ -950,21 +933,19 @@ public class FileSystemResolverTest extends AbstractDependencyResolverTest { "myext"); File src = new File("test/repositories/ivysettings.xml"); - resolver.beginPublishTransaction(mrid, false); - resolver.publish(ivyArtifact, src, false); - resolver.publish(artifact, src, false); + Exception exception = assertThrows(IllegalStateException.class, () -> { + resolver.beginPublishTransaction(mrid, false); + resolver.publish(ivyArtifact, src, false); + resolver.publish(artifact, src, false); + }); + assertTrue(exception.getMessage().contains("transactional")); } /** * Publishing with transaction=true and overwrite mode must fail. - * - * @throws Exception if something goes wrong */ @Test - public void testUnsupportedTransaction3() throws Exception { - expExc.expect(IllegalStateException.class); - expExc.expectMessage("transactional"); - + public void testUnsupportedTransaction3() { FileSystemResolver resolver = new FileSystemResolver(); resolver.setName("test"); resolver.setSettings(settings); @@ -978,9 +959,12 @@ public class FileSystemResolverTest extends AbstractDependencyResolverTest { "myext"); File src = new File("test/repositories/ivysettings.xml"); - // overwrite transaction not supported - resolver.beginPublishTransaction(mrid, true); - resolver.publish(artifact, src, true); + Exception exception = assertThrows(IllegalStateException.class, () -> { + // overwrite transaction not supported + resolver.beginPublishTransaction(mrid, true); + resolver.publish(artifact, src, true); + }); + assertTrue(exception.getMessage().contains("transactional")); } @Test diff --git a/test/java/org/apache/ivy/plugins/resolver/IvyRepResolverTest.java b/test/java/org/apache/ivy/plugins/resolver/IvyRepResolverTest.java index d8625015..de983122 100644 --- a/test/java/org/apache/ivy/plugins/resolver/IvyRepResolverTest.java +++ b/test/java/org/apache/ivy/plugins/resolver/IvyRepResolverTest.java @@ -17,9 +17,6 @@ */ package org.apache.ivy.plugins.resolver; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import java.io.File; import java.util.List; @@ -38,25 +35,24 @@ import org.apache.ivy.core.resolve.ResolveOptions; import org.apache.ivy.core.resolve.ResolvedModuleRevision; import org.apache.ivy.core.settings.IvySettings; import org.apache.ivy.core.sort.SortEngine; + import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; -/** - * - */ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + public class IvyRepResolverTest extends AbstractDependencyResolverTest { + private IvySettings settings; private ResolveEngine engine; private ResolveData data; - @Rule - public ExpectedException expExc = ExpectedException.none(); - @Before public void setUp() { settings = new IvySettings(); @@ -97,21 +93,19 @@ public class IvyRepResolverTest extends AbstractDependencyResolverTest { /** * Test case for IVY-625. Must fail if no ivyroot specified. * - * @throws Exception if something goes wrong * @see IVY-625 */ @Test - public void testMandatoryRoot() throws Exception { - expExc.expect(IllegalStateException.class); - expExc.expectMessage("ivyroot"); - + public void testMandatoryRoot() { IvyRepResolver resolver = new IvyRepResolver(); resolver.setName("test"); resolver.setSettings(settings); ModuleRevisionId mrid = ModuleRevisionId.newInstance("apache", "commons-cli", "1.0"); - resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), data); + Exception exception = assertThrows(IllegalStateException.class, () -> + resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), data)); + assertTrue(exception.getMessage().contains("ivyroot")); } @Test diff --git a/test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java b/test/java/org/apache/ivy/util/url/HttpClientHandlerTest.java similarity index 86% rename from test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java rename to test/java/org/apache/ivy/util/url/HttpClientHandlerTest.java index 113655d1..06dd7f04 100644 --- a/test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java +++ b/test/java/org/apache/ivy/util/url/HttpClientHandlerTest.java @@ -17,17 +17,6 @@ */ package org.apache.ivy.util.url; -import org.apache.ivy.TestHelper; -import org.apache.ivy.core.settings.NamedTimeoutConstraint; -import org.apache.ivy.core.settings.TimeoutConstraint; -import org.apache.ivy.util.FileUtil; -import org.apache.ivy.util.url.URLHandler.URLInfo; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; @@ -37,30 +26,31 @@ import java.nio.file.Path; import java.util.Collections; import java.util.Random; +import org.apache.ivy.TestHelper; +import org.apache.ivy.core.settings.NamedTimeoutConstraint; +import org.apache.ivy.core.settings.TimeoutConstraint; +import org.apache.ivy.util.FileUtil; +import org.apache.ivy.util.url.URLHandler.URLInfo; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import static org.apache.ivy.plugins.resolver.IBiblioResolver.DEFAULT_M2_ROOT; -import static org.hamcrest.Matchers.endsWith; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -/** - * Test {@link HttpClientHandler} - */ -public class HttpclientURLHandlerTest { +public class HttpClientHandlerTest { + // remote.test private File testDir; private HttpClientHandler handler; - private final TimeoutConstraint defaultTimeoutConstraint; - - { - defaultTimeoutConstraint = new NamedTimeoutConstraint("default-http-client-handler-timeout"); - ((NamedTimeoutConstraint) defaultTimeoutConstraint).setConnectionTimeout(5000); - } - - @Rule - public ExpectedException expExc = ExpectedException.none(); + private TimeoutConstraint defaultTimeoutConstraint; @Before public void setUp() { @@ -68,14 +58,16 @@ public class HttpclientURLHandlerTest { testDir.mkdirs(); handler = new HttpClientHandler(); + + defaultTimeoutConstraint = new NamedTimeoutConstraint("default-http-client-handler-timeout"); + ((NamedTimeoutConstraint) defaultTimeoutConstraint).setConnectionTimeout(5000); } @After public void tearDown() { try { handler.close(); - } catch (Exception e) { - // ignore + } catch (Exception ignore) { } FileUtil.forceDelete(testDir); } @@ -91,7 +83,6 @@ public class HttpclientURLHandlerTest { /** * Test case for IVY-390. * - * @throws Exception if something goes wrong * @see IVY-390 */ @SuppressWarnings({"resource", "deprecation"}) @@ -129,18 +120,10 @@ public class HttpclientURLHandlerTest { * {@link CredentialsStore Ivy credentials store} works as expected when it interacts * with a HTTP server which requires authentication for accessing resources. * - * @throws Exception if something goes wrong * @see IVY-1336 */ @Test public void testCredentials() throws Exception { - // we catch it and check for presence of 401 in the exception message. - // It's not exactly an contract that the IOException will have the 401 message - // but for now that's how it's implemented and it's fine to check for the presence - // of that message at the moment - expExc.expect(IOException.class); - expExc.expectMessage(endsWith("ivysettings.xml' 401 - 'Unauthorized")); - final CredentialsStore credentialsStore = CredentialsStore.INSTANCE; final String realm = "test-http-client-handler-realm"; final String host = "localhost"; @@ -153,7 +136,7 @@ public class HttpclientURLHandlerTest { final Path repoRoot = new File("test/repositories").toPath(); assertTrue(repoRoot + " is not a directory", Files.isDirectory(repoRoot)); // create a server backed by BASIC auth with the set of "allowed" credentials - try (final AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr, + try (AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr, contextRoot, repoRoot, realm, Collections.singletonMap(userName, password))) { final File target = new File(testDir, "downloaded.xml"); @@ -167,7 +150,7 @@ public class HttpclientURLHandlerTest { // now create a server backed by BASIC auth with a set of credentials that do *not* match // with what the Ivy credentials store will return for a given realm+host combination, i.e. // Ivy credential store will return back invalid credentials and the server will reject them - try (final AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr, + try (AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr, contextRoot, repoRoot, realm, Collections.singletonMap("other-" + userName, "other-" + password))) { final File target = new File(testDir, "should-not-have-been-downloaded.xml"); @@ -175,7 +158,12 @@ public class HttpclientURLHandlerTest { final URL src = new URL("http://localhost:" + serverBindAddr.getPort() + "/" + contextRoot + "/ivysettings.xml"); // download it (expected to fail) - handler.download(src, target, null, defaultTimeoutConstraint); + Exception exception = assertThrows(IOException.class, () -> handler.download(src, target, null, defaultTimeoutConstraint)); + // we catch it and check for presence of 401 in the exception message. + // It's not exactly an contract that the IOException will have the 401 message + // but for now that's how it's implemented and it's fine to check for the presence + // of that message at the moment + assertTrue(exception.getMessage().endsWith("ivysettings.xml' 401 - 'Unauthorized")); } }