This is an automated email from the ASF dual-hosted git repository. sebb 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 6322b13 Fix sonarlint warnings 6322b13 is described below commit 6322b135793d5b2250dd513c454800cd261ccbea Author: Sebb <s...@apache.org> AuthorDate: Sat Aug 8 15:48:23 2020 +0100 Fix sonarlint warnings --- .../commons/io/input/CharSequenceReaderTest.java | 1 + .../commons/io/input/NullInputStreamTest.java | 1 + .../apache/commons/io/input/NullReaderTest.java | 1 + .../UnsynchronizedByteArrayInputStreamTest.java | 19 +++++++++++++++++- .../io/output/ProxyCollectionWriterTest.java | 23 ++++++++++++++++++++++ .../apache/commons/io/output/TeeWriterTest.java | 3 +++ 6 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java b/src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java index f8ef35f..bc88578 100644 --- a/src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java +++ b/src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java @@ -259,6 +259,7 @@ public class CharSequenceReaderTest { } @Test + @SuppressWarnings("resource") // don't really need to close CharSequenceReader here public void testToString() { assertEquals("FooBar", new CharSequenceReader("FooBar").toString()); assertEquals("FooBar", new CharSequenceReader("xFooBarx", 1, 7).toString()); diff --git a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java index 88e7095..b886024 100644 --- a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java @@ -122,6 +122,7 @@ public class NullInputStreamTest { public void testMarkAndReset() throws Exception { int position = 0; final int readlimit = 10; + @SuppressWarnings("resource") // this is actually closed final InputStream input = new TestNullInputStream(100, true, false); assertTrue(input.markSupported(), "Mark Should be Supported"); diff --git a/src/test/java/org/apache/commons/io/input/NullReaderTest.java b/src/test/java/org/apache/commons/io/input/NullReaderTest.java index e0bd0d7..9782ef5 100644 --- a/src/test/java/org/apache/commons/io/input/NullReaderTest.java +++ b/src/test/java/org/apache/commons/io/input/NullReaderTest.java @@ -119,6 +119,7 @@ public class NullReaderTest { public void testMarkAndReset() throws Exception { int position = 0; final int readlimit = 10; + @SuppressWarnings("resource") // this is actually closed final Reader reader = new TestNullReader(100, true, false); assertTrue(reader.markSupported(), "Mark Should be Supported"); diff --git a/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java index 3b54741..482e3ac 100644 --- a/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java @@ -24,13 +24,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; + /** * Basic unit tests for the alternative ByteArrayInputStream implementation. */ public class UnsynchronizedByteArrayInputStreamTest { @Test - public void testConstructor1() { + public void testConstructor1() throws IOException { final byte[] empty = new byte[0]; final byte[] one = new byte[1]; final byte[] some = new byte[25]; @@ -38,14 +40,18 @@ public class UnsynchronizedByteArrayInputStreamTest { UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(empty); assertEquals(empty.length, is.available()); + is.close(); is = new UnsynchronizedByteArrayInputStream(one); assertEquals(one.length, is.available()); + is.close(); is = new UnsynchronizedByteArrayInputStream(some); assertEquals(some.length, is.available()); + is.close(); } @Test + @SuppressWarnings("resource") // not necessary to close these resources public void testConstructor2() { final byte[] empty = new byte[0]; final byte[] one = new byte[1]; @@ -74,6 +80,7 @@ public class UnsynchronizedByteArrayInputStreamTest { } @Test + @SuppressWarnings("resource") // not necessary to close these resources public void testConstructor3() { final byte[] empty = new byte[0]; final byte[] one = new byte[1]; @@ -149,6 +156,7 @@ public class UnsynchronizedByteArrayInputStreamTest { } @Test + @SuppressWarnings("resource") // not necessary to close these resources public void testInvalidReadArrayExplicitLenUnder() { final byte[] buf = new byte[0]; final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); @@ -160,6 +168,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testInvalidReadArrayExplicitOffsetUnder() { final byte[] buf = new byte[0]; + @SuppressWarnings("resource") // not necessary to close these resources final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); assertThrows(IndexOutOfBoundsException.class, () -> { is.read(buf, -1, 1); @@ -169,6 +178,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testInvalidReadArrayExplicitRangeOver() { final byte[] buf = new byte[0]; + @SuppressWarnings("resource") // not necessary to close these resources final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); assertThrows(IndexOutOfBoundsException.class, () -> { is.read(buf, 0, 1); @@ -178,6 +188,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testInvalidReadArrayNull() { final byte[] buf = null; + @SuppressWarnings("resource") // not necessary to close these resources final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); assertThrows(NullPointerException.class, () -> { is.read(buf); @@ -186,6 +197,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testInvalidSkipNUnder() { + @SuppressWarnings("resource") // not necessary to close these resources final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); assertThrows(IllegalArgumentException.class, () -> { is.skip(-1); @@ -194,6 +206,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testMarkReset() { + @SuppressWarnings("resource") // not necessary to close these resources final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); assertTrue(is.markSupported()); assertEquals(0xa, is.read()); @@ -220,6 +233,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testReadArray() { byte[] buf = new byte[10]; + @SuppressWarnings("resource") // not necessary to close these resources UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]); int read = is.read(buf); assertEquals(END_OF_STREAM, read); @@ -253,6 +267,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testReadArrayExplicit() { byte[] buf = new byte[10]; + @SuppressWarnings("resource") // not necessary to close these resources UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]); int read = is.read(buf, 0, 10); assertEquals(END_OF_STREAM, read); @@ -289,6 +304,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testReadSingle() { + @SuppressWarnings("resource") // not necessary to close these resources UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]); assertEquals(END_OF_STREAM, is.read()); @@ -301,6 +317,7 @@ public class UnsynchronizedByteArrayInputStreamTest { @Test public void testSkip() { + @SuppressWarnings("resource") // not necessary to close these resources UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc}); assertEquals(3, is.available()); diff --git a/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java b/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java index aa6e345..39785ac 100644 --- a/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java +++ b/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java @@ -17,6 +17,7 @@ package org.apache.commons.io.output; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -40,6 +41,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnAppendChar1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final char data = 'A'; try { @@ -56,6 +58,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnAppendChar2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); final char data = 'A'; try { @@ -72,6 +75,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnAppendCharSequence1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final CharSequence data = "A"; try { @@ -88,6 +92,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnAppendCharSequence2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); final CharSequence data = "A"; try { @@ -104,6 +109,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnAppendCharSequenceIntInt1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final CharSequence data = "A"; try { @@ -120,6 +126,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnAppendCharSequenceIntInt2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); final CharSequence data = "A"; try { @@ -136,6 +143,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnClose1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); try { tw.close(); @@ -151,6 +159,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnClose2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); try { tw.close(); @@ -166,6 +175,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnFlush1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); try { tw.flush(); @@ -181,6 +191,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnFlush2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); try { tw.flush(); @@ -196,6 +207,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteCharArray1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final char[] data = new char[] { 'a' }; try { @@ -212,6 +224,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteCharArray2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); final char[] data = new char[] { 'a' }; try { @@ -228,6 +241,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteCharArrayIntInt1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final char[] data = new char[] { 'a' }; try { @@ -244,6 +258,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteCharArrayIntInt2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); final char[] data = new char[] { 'a' }; try { @@ -260,6 +275,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteInt1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final int data = 32; try { @@ -276,6 +292,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteInt2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); try { tw.write(32); @@ -292,6 +309,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteString1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final String data = "A"; try { @@ -308,6 +326,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteString2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); final String data = "A"; try { @@ -325,6 +344,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteStringIntInt1() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null); final String data = "A"; try { @@ -341,6 +361,7 @@ public class ProxyCollectionWriterTest { public void testArrayIOExceptionOnWriteStringIntInt2() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null); final String data = "A"; try { @@ -358,6 +379,7 @@ public class ProxyCollectionWriterTest { public void testCollectionCloseBranchIOException() throws IOException { final Writer badW = new BrokenWriter(); final StringWriter goodW = mock(StringWriter.class); + @SuppressWarnings("resource") // not necessary to close this final ProxyCollectionWriter tw = new ProxyCollectionWriter(Arrays.asList(goodW, badW, null)); try { tw.close(); @@ -382,6 +404,7 @@ public class ProxyCollectionWriterTest { teeWriter.append('a'); teeWriter.flush(); } + assertTrue(true, "Dummy to show test completed OK"); } @Test diff --git a/src/test/java/org/apache/commons/io/output/TeeWriterTest.java b/src/test/java/org/apache/commons/io/output/TeeWriterTest.java index 886b173..ef86837 100644 --- a/src/test/java/org/apache/commons/io/output/TeeWriterTest.java +++ b/src/test/java/org/apache/commons/io/output/TeeWriterTest.java @@ -17,6 +17,7 @@ package org.apache.commons.io.output; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -34,6 +35,7 @@ import org.junit.jupiter.api.Test; /** * JUnit Test Case for {@link TeeWriter}. */ +@SuppressWarnings("resource") // not necessary to close these resources public class TeeWriterTest { @Test @@ -382,6 +384,7 @@ public class TeeWriterTest { teeWriter.append('a'); teeWriter.flush(); } + assertTrue(true, "Dummy to show test completed OK"); } @Test