Fix sonarlint warnings
MODIFY
src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java
src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java
@@ -259,6 +259,7 @@ public void testConstructor() {
}
@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());

MODIFY
src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
@@ -122,6 +122,7 @@ public void testEOFException() throws Exception {
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");

MODIFY
src/test/java/org/apache/commons/io/input/NullReaderTest.java
src/test/java/org/apache/commons/io/input/NullReaderTest.java
@@ -119,6 +119,7 @@ public void testEOFException() throws Exception {
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");

MODIFY
src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java
src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java
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 void testConstructor1() {
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 void testConstructor2() {
}
@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 void testInvalidConstructor3OffsetUnder() {
}
@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 void testInvalidReadArrayExplicitLenUnder() {
@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 void testInvalidReadArrayExplicitOffsetUnder() {
@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 void testInvalidReadArrayExplicitRangeOver() {
@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 void testInvalidReadArrayNull() {
@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 void testInvalidSkipNUnder() {
@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 void testMarkReset() {
@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 void testReadArray() {
@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 void testReadArrayExplicit() {
@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 void testReadSingle() {
@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());

MODIFY
src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java
src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java
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;
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 void testArrayIOExceptionOnAppendChar1() throws IOException {
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 void testArrayIOExceptionOnAppendChar2() throws IOException {
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 void testArrayIOExceptionOnAppendCharSequence1() throws IOException {
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 void testArrayIOExceptionOnAppendCharSequence2() throws IOException {
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 void testArrayIOExceptionOnAppendCharSequenceIntInt1() throws IOException
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 void testArrayIOExceptionOnAppendCharSequenceIntInt2() throws IOException
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 void testArrayIOExceptionOnClose1() throws IOException {
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 void testArrayIOExceptionOnClose2() throws IOException {
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 void testArrayIOExceptionOnFlush1() throws IOException {
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 void testArrayIOExceptionOnFlush2() throws IOException {
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 void testArrayIOExceptionOnWriteCharArray1() throws IOException {
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 void testArrayIOExceptionOnWriteCharArray2() throws IOException {
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 void testArrayIOExceptionOnWriteCharArrayIntInt1() throws IOException {
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 void testArrayIOExceptionOnWriteCharArrayIntInt2() throws IOException {
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 void testArrayIOExceptionOnWriteInt1() throws IOException {
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 void testArrayIOExceptionOnWriteInt2() throws IOException {
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 void testArrayIOExceptionOnWriteString1() throws IOException {
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 void testArrayIOExceptionOnWriteString2() throws IOException {
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 void testArrayIOExceptionOnWriteStringIntInt1() throws IOException {
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 void testArrayIOExceptionOnWriteStringIntInt2() throws IOException {
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 void testConstructorsNull() throws IOException {
teeWriter.append('a');
teeWriter.flush();
}
+ assertTrue(true, "Dummy to show test completed OK");
}
@Test

MODIFY
src/test/java/org/apache/commons/io/output/TeeWriterTest.java
src/test/java/org/apache/commons/io/output/TeeWriterTest.java
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;
/**
* JUnit Test Case for {@link TeeWriter}.
*/
+@SuppressWarnings("resource") // not necessary to close these resources
public class TeeWriterTest {
@Test
@@ -382,6 +384,7 @@ public void testConstructorsNull() throws IOException {
teeWriter.append('a');
teeWriter.flush();
}
+ assertTrue(true, "Dummy to show test completed OK");
}
@Test

Fix sonarlint warnings