From 0d77b4e5c87528f2d3e810742997483f403c8968 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Wed, 20 Jan 2016 11:38:35 +0900 Subject: [PATCH] SideBandOutputStreamTest: Use try-with-resource Factor out the creation of the SideBandOutputStream objects into a utility method that wraps it in a try-with-resource. Remove the "unused" suppression that is now unnecessary, and add declaration that the tests methods can throw Exception. Change-Id: Iff02e4e3532bd6ab6e423f197e70d44c4f328d0b Signed-off-by: David Pursehouse --- .../transport/SideBandOutputStreamTest.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/SideBandOutputStreamTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/SideBandOutputStreamTest.java index 3661677cd..d2c3a0b92 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/SideBandOutputStreamTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/SideBandOutputStreamTest.java @@ -195,25 +195,31 @@ public class SideBandOutputStreamTest { assertEquals(1, flushCnt[0]); } - @SuppressWarnings("unused") + private void createSideBandOutputStream(int chan, int sz, OutputStream os) + throws Exception { + try (SideBandOutputStream s = new SideBandOutputStream(chan, sz, os)) { + // Unused + } + } + @Test - public void testConstructor_RejectsBadChannel() { + public void testConstructor_RejectsBadChannel() throws Exception { try { - new SideBandOutputStream(-1, MAX_BUF, rawOut); + createSideBandOutputStream(-1, MAX_BUF, rawOut); fail("Accepted -1 channel number"); } catch (IllegalArgumentException e) { assertEquals("channel -1 must be in range [1, 255]", e.getMessage()); } try { - new SideBandOutputStream(0, MAX_BUF, rawOut); + createSideBandOutputStream(0, MAX_BUF, rawOut); fail("Accepted 0 channel number"); } catch (IllegalArgumentException e) { assertEquals("channel 0 must be in range [1, 255]", e.getMessage()); } try { - new SideBandOutputStream(256, MAX_BUF, rawOut); + createSideBandOutputStream(256, MAX_BUF, rawOut); fail("Accepted 256 channel number"); } catch (IllegalArgumentException e) { assertEquals("channel 256 must be in range [1, 255]", e @@ -221,32 +227,31 @@ public class SideBandOutputStreamTest { } } - @SuppressWarnings("unused") @Test - public void testConstructor_RejectsBadBufferSize() { + public void testConstructor_RejectsBadBufferSize() throws Exception { try { - new SideBandOutputStream(CH_DATA, -1, rawOut); + createSideBandOutputStream(CH_DATA, -1, rawOut); fail("Accepted -1 for buffer size"); } catch (IllegalArgumentException e) { assertEquals("packet size -1 must be >= 5", e.getMessage()); } try { - new SideBandOutputStream(CH_DATA, 0, rawOut); + createSideBandOutputStream(CH_DATA, 0, rawOut); fail("Accepted 0 for buffer size"); } catch (IllegalArgumentException e) { assertEquals("packet size 0 must be >= 5", e.getMessage()); } try { - new SideBandOutputStream(CH_DATA, 1, rawOut); + createSideBandOutputStream(CH_DATA, 1, rawOut); fail("Accepted 1 for buffer size"); } catch (IllegalArgumentException e) { assertEquals("packet size 1 must be >= 5", e.getMessage()); } try { - new SideBandOutputStream(CH_DATA, Integer.MAX_VALUE, rawOut); + createSideBandOutputStream(CH_DATA, Integer.MAX_VALUE, rawOut); fail("Accepted " + Integer.MAX_VALUE + " for buffer size"); } catch (IllegalArgumentException e) { assertEquals(MessageFormat.format(