This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 5fbc496 Use try-with-resources and format to 120 line length. 5fbc496 is described below commit 5fbc4964bcb351cab189faaa042ac79969a66716 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue May 5 15:52:28 2020 -0400 Use try-with-resources and format to 120 line length. --- .../apache/commons/io/function/IOFunctionTest.java | 5 +- .../io/output/ByteArrayOutputStreamTestCase.java | 33 +++-- .../commons/io/output/ChunkedOutputStreamTest.java | 26 ++-- .../io/output/DeferredFileOutputStreamTest.java | 148 ++++++++------------- .../apache/commons/io/output/ProxyWriterTest.java | 2 +- .../commons/io/output/WriterOutputStreamTest.java | 42 +++--- 6 files changed, 112 insertions(+), 144 deletions(-) diff --git a/src/test/java/org/apache/commons/io/function/IOFunctionTest.java b/src/test/java/org/apache/commons/io/function/IOFunctionTest.java index 5050566..34590ee 100644 --- a/src/test/java/org/apache/commons/io/function/IOFunctionTest.java +++ b/src/test/java/org/apache/commons/io/function/IOFunctionTest.java @@ -154,8 +154,9 @@ public class IOFunctionTest { @Test public void testIdentity() throws IOException { final IOFunction<InputStream, InputStream> identityFunction = IOFunction.identity(); - final InputStream is = new ByteArrayInputStream(new byte[] { (byte)0xa, (byte)0xb, (byte)0xc}); - assertEquals(is, identityFunction.apply(is)); + try (final InputStream is = new ByteArrayInputStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc })) { + assertEquals(is, identityFunction.apply(is)); + } } private static class Holder<T> { diff --git a/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java b/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java index cda2539..e9b2aed 100644 --- a/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java +++ b/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java @@ -59,11 +59,10 @@ public class ByteArrayOutputStreamTestCase { baout.write(100); ref.write(100); return 1; - } else { - baout.write(DATA, 0, count); - ref.write(DATA, 0, count); - return count; } + baout.write(DATA, 0, count); + ref.write(DATA, 0, count); + return count; } private int writeData(final AbstractByteArrayOutputStream baout, @@ -180,14 +179,14 @@ public class ByteArrayOutputStreamTestCase { @ParameterizedTest(name = "[{index}] {0}") @MethodSource("toBufferedInputStreamFunctionFactories") public void testToBufferedInputStreamEmpty(final String baosName, final IOFunction<InputStream, InputStream> toBufferedInputStreamFunction) throws IOException { - final ByteArrayInputStream bain = new ByteArrayInputStream(new byte[0]); - assertEquals(0, bain.available()); + try (final ByteArrayInputStream bain = new ByteArrayInputStream(new byte[0])) { + assertEquals(0, bain.available()); - final InputStream buffered = toBufferedInputStreamFunction.apply(bain); - assertEquals(0, buffered.available()); + try (final InputStream buffered = toBufferedInputStreamFunction.apply(bain)) { + assertEquals(0, buffered.available()); - buffered.close(); - bain.close(); + } + } } @ParameterizedTest(name = "[{index}] {0}") @@ -195,16 +194,16 @@ public class ByteArrayOutputStreamTestCase { public void testToBufferedInputStream(final String baosName, final IOFunction<InputStream, InputStream> toBufferedInputStreamFunction) throws IOException { final byte data[] = {(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE}; - final ByteArrayInputStream bain = new ByteArrayInputStream(data); - assertEquals(data.length, bain.available()); + try (final ByteArrayInputStream bain = new ByteArrayInputStream(data)) { + assertEquals(data.length, bain.available()); - final InputStream buffered = toBufferedInputStreamFunction.apply(bain); - assertEquals(data.length, buffered.available()); + try (final InputStream buffered = toBufferedInputStreamFunction.apply(bain)) { + assertEquals(data.length, buffered.available()); - assertArrayEquals(data, IOUtils.toByteArray(buffered)); + assertArrayEquals(data, IOUtils.toByteArray(buffered)); - buffered.close(); - bain.close(); + } + } } @ParameterizedTest(name = "[{index}] {0}") diff --git a/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java index 867e749..a198d8f 100644 --- a/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java @@ -32,30 +32,29 @@ public class ChunkedOutputStreamTest { @Test public void write_four_chunks() throws Exception { final AtomicInteger numWrites = new AtomicInteger(); - final ByteArrayOutputStream baos = getByteArrayOutputStream(numWrites); - final ChunkedOutputStream chunked = new ChunkedOutputStream(baos, 10); - chunked.write("0123456789012345678901234567891".getBytes()); - assertEquals(4, numWrites.get()); - chunked.close(); + try (final ByteArrayOutputStream baos = newByteArrayOutputStream(numWrites); + final ChunkedOutputStream chunked = new ChunkedOutputStream(baos, 10)) { + chunked.write("0123456789012345678901234567891".getBytes()); + assertEquals(4, numWrites.get()); + } } @Test public void negative_chunksize_not_permitted() { - assertThrows(IllegalArgumentException.class, - () -> new ChunkedOutputStream(new ByteArrayOutputStream(), 0)); + assertThrows(IllegalArgumentException.class, () -> new ChunkedOutputStream(new ByteArrayOutputStream(), 0)); } @Test public void defaultConstructor() throws IOException { final AtomicInteger numWrites = new AtomicInteger(); - final ByteArrayOutputStream baos = getByteArrayOutputStream(numWrites); - final ChunkedOutputStream chunked = new ChunkedOutputStream(baos); - chunked.write(new byte[1024 * 4 + 1]); - assertEquals(2, numWrites.get()); - chunked.close(); + try (final ByteArrayOutputStream baos = newByteArrayOutputStream(numWrites); + final ChunkedOutputStream chunked = new ChunkedOutputStream(baos)) { + chunked.write(new byte[1024 * 4 + 1]); + assertEquals(2, numWrites.get()); + } } - private ByteArrayOutputStream getByteArrayOutputStream(final AtomicInteger numWrites) { + private ByteArrayOutputStream newByteArrayOutputStream(final AtomicInteger numWrites) { return new ByteArrayOutputStream() { @Override public void write(final byte[] b, final int off, final int len) { @@ -65,5 +64,4 @@ public class ChunkedOutputStreamTest { }; } - } diff --git a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java index 74d6bb1..ab11773 100644 --- a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java @@ -55,21 +55,17 @@ public class DeferredFileOutputStreamTest { private final byte[] testBytes = testString.getBytes(); /** - * Tests the case where the amount of data falls below the threshold, and - * is therefore confined to memory. + * Tests the case where the amount of data falls below the threshold, and is therefore confined to memory. */ @ParameterizedTest(name = "initialBufferSize = {0}") @MethodSource("data") - public void testBelowThreshold(int initialBufferSize) - { - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, null); - try - { + public void testBelowThreshold(int initialBufferSize) { + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, + null); + try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } assertTrue(dfos.isInMemory()); @@ -80,21 +76,17 @@ public class DeferredFileOutputStreamTest { } /** - * Tests the case where the amount of data is exactly the same as the - * threshold. The behavior should be the same as that for the amount of - * data being below (i.e. not exceeding) the threshold. + * Tests the case where the amount of data is exactly the same as the threshold. The behavior should be the same as + * that for the amount of data being below (i.e. not exceeding) the threshold. */ @ParameterizedTest(name = "initialBufferSize = {0}") @MethodSource("data") public void testAtThreshold(int initialBufferSize) { - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length, initialBufferSize, null); - try - { + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length, initialBufferSize, null); + try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } assertTrue(dfos.isInMemory()); @@ -105,9 +97,8 @@ public class DeferredFileOutputStreamTest { } /** - * Tests the case where the amount of data exceeds the threshold, and is - * therefore written to disk. The actual data written to disk is verified, - * as is the file itself. + * Tests the case where the amount of data exceeds the threshold, and is therefore written to disk. The actual data + * written to disk is verified, as is the file itself. */ @ParameterizedTest(name = "initialBufferSize = {0}") @MethodSource("data") @@ -117,14 +108,12 @@ public class DeferredFileOutputStreamTest { // Ensure that the test starts from a clean base. testFile.delete(); - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, testFile); - try - { + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, + testFile); + try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); @@ -137,9 +126,8 @@ public class DeferredFileOutputStreamTest { } /** - * Tests the case where there are multiple writes beyond the threshold, to - * ensure that the <code>thresholdReached()</code> method is only called - * once, as the threshold is crossed for the first time. + * Tests the case where there are multiple writes beyond the threshold, to ensure that the + * <code>thresholdReached()</code> method is only called once, as the threshold is crossed for the first time. */ @ParameterizedTest(name = "initialBufferSize = {0}") @MethodSource("data") @@ -149,19 +137,16 @@ public class DeferredFileOutputStreamTest { // Ensure that the test starts from a clean base. testFile.delete(); - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length / 2, initialBufferSize, testFile); + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length / 2, initialBufferSize, + testFile); final int chunkSize = testBytes.length / 3; - try - { + try { dfos.write(testBytes, 0, chunkSize); dfos.write(testBytes, chunkSize, chunkSize); - dfos.write(testBytes, chunkSize * 2, - testBytes.length - chunkSize * 2); + dfos.write(testBytes, chunkSize * 2, testBytes.length - chunkSize * 2); dfos.close(); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); @@ -173,21 +158,20 @@ public class DeferredFileOutputStreamTest { testFile.delete(); } - /** * Test whether writeTo() properly writes small content. */ @ParameterizedTest(name = "initialBufferSize = {0}") @MethodSource("data") - public void testWriteToSmall(int initialBufferSize){ + public void testWriteToSmall(int initialBufferSize) { final File testFile = new File("testWriteToMem.dat"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize); // Ensure that the test starts from a clean base. testFile.delete(); - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length *2, initialBufferSize, testFile); - try{ + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length * 2, initialBufferSize, + testFile); + try { dfos.write(testBytes); assertFalse(testFile.exists()); @@ -205,7 +189,7 @@ public class DeferredFileOutputStreamTest { } catch (final IOException ioe) { fail("Unexpected IOException"); } - final byte[] copiedBytes = baos.toByteArray(); + final byte[] copiedBytes = baos.toByteArray(); assertTrue(Arrays.equals(testBytes, copiedBytes)); testFile.delete(); @@ -216,15 +200,14 @@ public class DeferredFileOutputStreamTest { */ @ParameterizedTest(name = "initialBufferSize = {0}") @MethodSource("data") - public void testWriteToLarge(int initialBufferSize){ + public void testWriteToLarge(int initialBufferSize) { final File testFile = new File("testWriteToFile.dat"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize); // Ensure that the test starts from a clean base. testFile.delete(); - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length / 2, testFile); - try{ + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length / 2, testFile); + try { dfos.write(testBytes); assertTrue(testFile.exists()); @@ -242,7 +225,7 @@ public class DeferredFileOutputStreamTest { } catch (final IOException ioe) { fail("Unexpected IOException"); } - final byte[] copiedBytes = baos.toByteArray(); + final byte[] copiedBytes = baos.toByteArray(); assertTrue(Arrays.equals(testBytes, copiedBytes)); verifyResultFile(testFile); testFile.delete(); @@ -257,16 +240,14 @@ public class DeferredFileOutputStreamTest { final String prefix = "commons-io-test"; final String suffix = ".out"; - final File tempDir = new File("."); - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, prefix, suffix, tempDir); + final File tempDir = new File("."); + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, + prefix, suffix, tempDir); assertNull(dfos.getFile(), "Check file is null-A"); - try - { + try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } assertTrue(dfos.isInMemory()); @@ -282,16 +263,14 @@ public class DeferredFileOutputStreamTest { final String prefix = "commons-io-test"; final String suffix = ".out"; - final File tempDir = new File("."); - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir); + final File tempDir = new File("."); + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, + prefix, suffix, tempDir); assertNull(dfos.getFile(), "Check file is null-A"); - try - { + try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); @@ -317,16 +296,14 @@ public class DeferredFileOutputStreamTest { final String prefix = "commons-io-test"; final String suffix = null; - final File tempDir = null; - final DeferredFileOutputStream dfos = - new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir); + final File tempDir = null; + final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, + prefix, suffix, tempDir); assertNull(dfos.getFile(), "Check file is null-A"); - try - { + try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); @@ -344,6 +321,7 @@ public class DeferredFileOutputStreamTest { /** * Test specifying a temporary file and the threshold is reached. + * * @throws Exception */ @Test @@ -351,26 +329,22 @@ public class DeferredFileOutputStreamTest { final String prefix = null; final String suffix = ".out"; - final File tempDir = new File("."); - try - { - (new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir)).close(); + final File tempDir = new File("."); + try { + new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir).close(); fail("Expected IllegalArgumentException "); - } - catch (final IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { // expected } } /** - * Verifies that the specified file contains the same data as the original - * test data. + * Verifies that the specified file contains the same data as the original test data. * * @param testFile The file containing the test output. */ private void verifyResultFile(final File testFile) { - try - { + try { final FileInputStream fis = new FileInputStream(testFile); assertEquals(testBytes.length, fis.available()); @@ -380,18 +354,14 @@ public class DeferredFileOutputStreamTest { assertTrue(Arrays.equals(resultBytes, testBytes)); assertEquals(-1, fis.read(resultBytes)); - try - { + try { fis.close(); - } - catch (final IOException e) { + } catch (final IOException e) { // Ignore an exception on close } - } - catch (final FileNotFoundException e) { + } catch (final FileNotFoundException e) { fail("Unexpected FileNotFoundException"); - } - catch (final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException"); } } diff --git a/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java b/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java index 03df568..a2d1a41 100644 --- a/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java +++ b/src/test/java/org/apache/commons/io/output/ProxyWriterTest.java @@ -258,7 +258,7 @@ public class ProxyWriterTest { } @Test - public void exceptions_in_close() throws IOException { + public void exceptions_in_close() { assertThrows(UnsupportedEncodingException.class, () -> { try (final OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream()) { @Override diff --git a/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java index 03312cc..e50e1c9 100644 --- a/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java @@ -42,25 +42,25 @@ public class WriterOutputStreamTest { private void testWithSingleByteWrite(final String testString, final String charsetName) throws IOException { final byte[] bytes = testString.getBytes(charsetName); final StringWriter writer = new StringWriter(); - final WriterOutputStream out = new WriterOutputStream(writer, charsetName); - for (final byte b : bytes) { - out.write(b); + try (final WriterOutputStream out = new WriterOutputStream(writer, charsetName)) { + for (final byte b : bytes) { + out.write(b); + } } - out.close(); assertEquals(testString, writer.toString()); } private void testWithBufferedWrite(final String testString, final String charsetName) throws IOException { final byte[] expected = testString.getBytes(charsetName); final StringWriter writer = new StringWriter(); - final WriterOutputStream out = new WriterOutputStream(writer, charsetName); - int offset = 0; - while (offset < expected.length) { - final int length = Math.min(random.nextInt(128), expected.length-offset); - out.write(expected, offset, length); - offset += length; + try (final WriterOutputStream out = new WriterOutputStream(writer, charsetName)) { + int offset = 0; + while (offset < expected.length) { + final int length = Math.min(random.nextInt(128), expected.length - offset); + out.write(expected, offset, length); + offset += length; + } } - out.close(); assertEquals(testString, writer.toString()); } @@ -130,20 +130,20 @@ public class WriterOutputStreamTest { @Test public void testFlush() throws IOException { final StringWriter writer = new StringWriter(); - final WriterOutputStream out = new WriterOutputStream(writer, "us-ascii", 1024, false); - out.write("abc".getBytes("us-ascii")); - assertEquals(0, writer.getBuffer().length()); - out.flush(); - assertEquals("abc", writer.toString()); - out.close(); + try (final WriterOutputStream out = new WriterOutputStream(writer, "us-ascii", 1024, false)) { + out.write("abc".getBytes("us-ascii")); + assertEquals(0, writer.getBuffer().length()); + out.flush(); + assertEquals("abc", writer.toString()); + } } @Test public void testWriteImmediately() throws IOException { final StringWriter writer = new StringWriter(); - final WriterOutputStream out = new WriterOutputStream(writer, "us-ascii", 1024, true); - out.write("abc".getBytes("us-ascii")); - assertEquals("abc", writer.toString()); - out.close(); + try (final WriterOutputStream out = new WriterOutputStream(writer, "us-ascii", 1024, true)) { + out.write("abc".getBytes("us-ascii")); + assertEquals("abc", writer.toString()); + } } }