From e648224f5374e7dc1b2d7bc402ed2758d9bd23f1 Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Tue, 14 Aug 2018 18:43:53 +0200 Subject: [PATCH] Use try-with-resources and ExpectedException --- .../org/apache/tools/ant/TestHelper.java | 14 ++-------- .../ant/taskdefs/email/EmailTaskTest.java | 26 +++++++------------ 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/tests/junit/org/apache/tools/ant/TestHelper.java b/src/tests/junit/org/apache/tools/ant/TestHelper.java index f36dd4693..815929456 100644 --- a/src/tests/junit/org/apache/tools/ant/TestHelper.java +++ b/src/tests/junit/org/apache/tools/ant/TestHelper.java @@ -33,21 +33,11 @@ public class TestHelper { * after this method since the associated socket is released and some other process can now use it. */ public static int getMaybeAvailablePort() { - final ServerSocket s; - try { - s = new ServerSocket(0); + try (ServerSocket s = new ServerSocket(0)) { s.setReuseAddress(true); - int port = s.getLocalPort(); - try { - s.close(); - } catch (IOException e) { - // ignore - } - return port; + return s.getLocalPort(); } catch (IOException e) { // ignore - } finally { - } throw new IllegalStateException("No TCP/IP port available"); } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailTaskTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailTaskTest.java index a77fc92db..0d0c36a2e 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailTaskTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/email/EmailTaskTest.java @@ -25,6 +25,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * TODO : develop these testcases - the email task needs to have attributes allowing @@ -35,6 +36,9 @@ public class EmailTaskTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/email/mail.xml"); @@ -45,14 +49,9 @@ public class EmailTaskTest { */ @Test public void test1() { - try { - buildRule.executeTarget("test1"); - } catch (BuildException e) { - // assert it's the expected one - if (!e.getMessage().equals("SMTP auth only possible with MIME mail")) { - throw e; - } - } + thrown.expect(BuildException.class); + thrown.expectMessage("SMTP auth only possible with MIME mail"); + buildRule.executeTarget("test1"); } /** @@ -60,14 +59,9 @@ public class EmailTaskTest { */ @Test public void test2() { - try { - buildRule.executeTarget("test2"); - } catch (BuildException e) { - // assert it's the expected one - if (!e.getMessage().equals("SSL and STARTTLS only possible with MIME mail")) { - throw e; - } - } + thrown.expect(BuildException.class); + thrown.expectMessage("SSL and STARTTLS only possible with MIME mail"); + buildRule.executeTarget("test2"); } /**