Skip to content

Commit 6322b13

Browse files
committed
Fix sonarlint warnings
1 parent b20e61e commit 6322b13

6 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ public void testConstructor() {
259259
}
260260

261261
@Test
262+
@SuppressWarnings("resource") // don't really need to close CharSequenceReader here
262263
public void testToString() {
263264
assertEquals("FooBar", new CharSequenceReader("FooBar").toString());
264265
assertEquals("FooBar", new CharSequenceReader("xFooBarx", 1, 7).toString());

src/test/java/org/apache/commons/io/input/NullInputStreamTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void testEOFException() throws Exception {
122122
public void testMarkAndReset() throws Exception {
123123
int position = 0;
124124
final int readlimit = 10;
125+
@SuppressWarnings("resource") // this is actually closed
125126
final InputStream input = new TestNullInputStream(100, true, false);
126127

127128
assertTrue(input.markSupported(), "Mark Should be Supported");

src/test/java/org/apache/commons/io/input/NullReaderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public void testEOFException() throws Exception {
119119
public void testMarkAndReset() throws Exception {
120120
int position = 0;
121121
final int readlimit = 10;
122+
@SuppressWarnings("resource") // this is actually closed
122123
final Reader reader = new TestNullReader(100, true, false);
123124

124125
assertTrue(reader.markSupported(), "Mark Should be Supported");

src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,34 @@
2424
import static org.junit.jupiter.api.Assertions.assertThrows;
2525
import static org.junit.jupiter.api.Assertions.assertTrue;
2626

27+
import java.io.IOException;
28+
2729
/**
2830
* Basic unit tests for the alternative ByteArrayInputStream implementation.
2931
*/
3032
public class UnsynchronizedByteArrayInputStreamTest {
3133

3234
@Test
33-
public void testConstructor1() {
35+
public void testConstructor1() throws IOException {
3436
final byte[] empty = new byte[0];
3537
final byte[] one = new byte[1];
3638
final byte[] some = new byte[25];
3739

3840
UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(empty);
3941
assertEquals(empty.length, is.available());
4042

43+
is.close();
4144
is = new UnsynchronizedByteArrayInputStream(one);
4245
assertEquals(one.length, is.available());
4346

47+
is.close();
4448
is = new UnsynchronizedByteArrayInputStream(some);
4549
assertEquals(some.length, is.available());
50+
is.close();
4651
}
4752

4853
@Test
54+
@SuppressWarnings("resource") // not necessary to close these resources
4955
public void testConstructor2() {
5056
final byte[] empty = new byte[0];
5157
final byte[] one = new byte[1];
@@ -74,6 +80,7 @@ public void testConstructor2() {
7480
}
7581

7682
@Test
83+
@SuppressWarnings("resource") // not necessary to close these resources
7784
public void testConstructor3() {
7885
final byte[] empty = new byte[0];
7986
final byte[] one = new byte[1];
@@ -149,6 +156,7 @@ public void testInvalidConstructor3OffsetUnder() {
149156
}
150157

151158
@Test
159+
@SuppressWarnings("resource") // not necessary to close these resources
152160
public void testInvalidReadArrayExplicitLenUnder() {
153161
final byte[] buf = new byte[0];
154162
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
@@ -160,6 +168,7 @@ public void testInvalidReadArrayExplicitLenUnder() {
160168
@Test
161169
public void testInvalidReadArrayExplicitOffsetUnder() {
162170
final byte[] buf = new byte[0];
171+
@SuppressWarnings("resource") // not necessary to close these resources
163172
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
164173
assertThrows(IndexOutOfBoundsException.class, () -> {
165174
is.read(buf, -1, 1);
@@ -169,6 +178,7 @@ public void testInvalidReadArrayExplicitOffsetUnder() {
169178
@Test
170179
public void testInvalidReadArrayExplicitRangeOver() {
171180
final byte[] buf = new byte[0];
181+
@SuppressWarnings("resource") // not necessary to close these resources
172182
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
173183
assertThrows(IndexOutOfBoundsException.class, () -> {
174184
is.read(buf, 0, 1);
@@ -178,6 +188,7 @@ public void testInvalidReadArrayExplicitRangeOver() {
178188
@Test
179189
public void testInvalidReadArrayNull() {
180190
final byte[] buf = null;
191+
@SuppressWarnings("resource") // not necessary to close these resources
181192
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
182193
assertThrows(NullPointerException.class, () -> {
183194
is.read(buf);
@@ -186,6 +197,7 @@ public void testInvalidReadArrayNull() {
186197

187198
@Test
188199
public void testInvalidSkipNUnder() {
200+
@SuppressWarnings("resource") // not necessary to close these resources
189201
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
190202
assertThrows(IllegalArgumentException.class, () -> {
191203
is.skip(-1);
@@ -194,6 +206,7 @@ public void testInvalidSkipNUnder() {
194206

195207
@Test
196208
public void testMarkReset() {
209+
@SuppressWarnings("resource") // not necessary to close these resources
197210
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
198211
assertTrue(is.markSupported());
199212
assertEquals(0xa, is.read());
@@ -220,6 +233,7 @@ public void testMarkReset() {
220233
@Test
221234
public void testReadArray() {
222235
byte[] buf = new byte[10];
236+
@SuppressWarnings("resource") // not necessary to close these resources
223237
UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]);
224238
int read = is.read(buf);
225239
assertEquals(END_OF_STREAM, read);
@@ -253,6 +267,7 @@ public void testReadArray() {
253267
@Test
254268
public void testReadArrayExplicit() {
255269
byte[] buf = new byte[10];
270+
@SuppressWarnings("resource") // not necessary to close these resources
256271
UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]);
257272
int read = is.read(buf, 0, 10);
258273
assertEquals(END_OF_STREAM, read);
@@ -289,6 +304,7 @@ public void testReadArrayExplicit() {
289304

290305
@Test
291306
public void testReadSingle() {
307+
@SuppressWarnings("resource") // not necessary to close these resources
292308
UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]);
293309
assertEquals(END_OF_STREAM, is.read());
294310

@@ -301,6 +317,7 @@ public void testReadSingle() {
301317

302318
@Test
303319
public void testSkip() {
320+
@SuppressWarnings("resource") // not necessary to close these resources
304321
UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
305322
assertEquals(3, is.available());
306323

src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.io.output;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2021
import static org.junit.jupiter.api.Assertions.fail;
2122
import static org.mockito.Mockito.mock;
2223
import static org.mockito.Mockito.verify;
@@ -40,6 +41,7 @@ public class ProxyCollectionWriterTest {
4041
public void testArrayIOExceptionOnAppendChar1() throws IOException {
4142
final Writer badW = new BrokenWriter();
4243
final StringWriter goodW = mock(StringWriter.class);
44+
@SuppressWarnings("resource") // not necessary to close this
4345
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
4446
final char data = 'A';
4547
try {
@@ -56,6 +58,7 @@ public void testArrayIOExceptionOnAppendChar1() throws IOException {
5658
public void testArrayIOExceptionOnAppendChar2() throws IOException {
5759
final Writer badW = new BrokenWriter();
5860
final StringWriter goodW = mock(StringWriter.class);
61+
@SuppressWarnings("resource") // not necessary to close this
5962
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
6063
final char data = 'A';
6164
try {
@@ -72,6 +75,7 @@ public void testArrayIOExceptionOnAppendChar2() throws IOException {
7275
public void testArrayIOExceptionOnAppendCharSequence1() throws IOException {
7376
final Writer badW = new BrokenWriter();
7477
final StringWriter goodW = mock(StringWriter.class);
78+
@SuppressWarnings("resource") // not necessary to close this
7579
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
7680
final CharSequence data = "A";
7781
try {
@@ -88,6 +92,7 @@ public void testArrayIOExceptionOnAppendCharSequence1() throws IOException {
8892
public void testArrayIOExceptionOnAppendCharSequence2() throws IOException {
8993
final Writer badW = new BrokenWriter();
9094
final StringWriter goodW = mock(StringWriter.class);
95+
@SuppressWarnings("resource") // not necessary to close this
9196
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
9297
final CharSequence data = "A";
9398
try {
@@ -104,6 +109,7 @@ public void testArrayIOExceptionOnAppendCharSequence2() throws IOException {
104109
public void testArrayIOExceptionOnAppendCharSequenceIntInt1() throws IOException {
105110
final Writer badW = new BrokenWriter();
106111
final StringWriter goodW = mock(StringWriter.class);
112+
@SuppressWarnings("resource") // not necessary to close this
107113
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
108114
final CharSequence data = "A";
109115
try {
@@ -120,6 +126,7 @@ public void testArrayIOExceptionOnAppendCharSequenceIntInt1() throws IOException
120126
public void testArrayIOExceptionOnAppendCharSequenceIntInt2() throws IOException {
121127
final Writer badW = new BrokenWriter();
122128
final StringWriter goodW = mock(StringWriter.class);
129+
@SuppressWarnings("resource") // not necessary to close this
123130
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
124131
final CharSequence data = "A";
125132
try {
@@ -136,6 +143,7 @@ public void testArrayIOExceptionOnAppendCharSequenceIntInt2() throws IOException
136143
public void testArrayIOExceptionOnClose1() throws IOException {
137144
final Writer badW = new BrokenWriter();
138145
final StringWriter goodW = mock(StringWriter.class);
146+
@SuppressWarnings("resource") // not necessary to close this
139147
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
140148
try {
141149
tw.close();
@@ -151,6 +159,7 @@ public void testArrayIOExceptionOnClose1() throws IOException {
151159
public void testArrayIOExceptionOnClose2() throws IOException {
152160
final Writer badW = new BrokenWriter();
153161
final StringWriter goodW = mock(StringWriter.class);
162+
@SuppressWarnings("resource") // not necessary to close this
154163
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
155164
try {
156165
tw.close();
@@ -166,6 +175,7 @@ public void testArrayIOExceptionOnClose2() throws IOException {
166175
public void testArrayIOExceptionOnFlush1() throws IOException {
167176
final Writer badW = new BrokenWriter();
168177
final StringWriter goodW = mock(StringWriter.class);
178+
@SuppressWarnings("resource") // not necessary to close this
169179
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
170180
try {
171181
tw.flush();
@@ -181,6 +191,7 @@ public void testArrayIOExceptionOnFlush1() throws IOException {
181191
public void testArrayIOExceptionOnFlush2() throws IOException {
182192
final Writer badW = new BrokenWriter();
183193
final StringWriter goodW = mock(StringWriter.class);
194+
@SuppressWarnings("resource") // not necessary to close this
184195
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
185196
try {
186197
tw.flush();
@@ -196,6 +207,7 @@ public void testArrayIOExceptionOnFlush2() throws IOException {
196207
public void testArrayIOExceptionOnWriteCharArray1() throws IOException {
197208
final Writer badW = new BrokenWriter();
198209
final StringWriter goodW = mock(StringWriter.class);
210+
@SuppressWarnings("resource") // not necessary to close this
199211
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
200212
final char[] data = new char[] { 'a' };
201213
try {
@@ -212,6 +224,7 @@ public void testArrayIOExceptionOnWriteCharArray1() throws IOException {
212224
public void testArrayIOExceptionOnWriteCharArray2() throws IOException {
213225
final Writer badW = new BrokenWriter();
214226
final StringWriter goodW = mock(StringWriter.class);
227+
@SuppressWarnings("resource") // not necessary to close this
215228
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
216229
final char[] data = new char[] { 'a' };
217230
try {
@@ -228,6 +241,7 @@ public void testArrayIOExceptionOnWriteCharArray2() throws IOException {
228241
public void testArrayIOExceptionOnWriteCharArrayIntInt1() throws IOException {
229242
final Writer badW = new BrokenWriter();
230243
final StringWriter goodW = mock(StringWriter.class);
244+
@SuppressWarnings("resource") // not necessary to close this
231245
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
232246
final char[] data = new char[] { 'a' };
233247
try {
@@ -244,6 +258,7 @@ public void testArrayIOExceptionOnWriteCharArrayIntInt1() throws IOException {
244258
public void testArrayIOExceptionOnWriteCharArrayIntInt2() throws IOException {
245259
final Writer badW = new BrokenWriter();
246260
final StringWriter goodW = mock(StringWriter.class);
261+
@SuppressWarnings("resource") // not necessary to close this
247262
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
248263
final char[] data = new char[] { 'a' };
249264
try {
@@ -260,6 +275,7 @@ public void testArrayIOExceptionOnWriteCharArrayIntInt2() throws IOException {
260275
public void testArrayIOExceptionOnWriteInt1() throws IOException {
261276
final Writer badW = new BrokenWriter();
262277
final StringWriter goodW = mock(StringWriter.class);
278+
@SuppressWarnings("resource") // not necessary to close this
263279
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
264280
final int data = 32;
265281
try {
@@ -276,6 +292,7 @@ public void testArrayIOExceptionOnWriteInt1() throws IOException {
276292
public void testArrayIOExceptionOnWriteInt2() throws IOException {
277293
final Writer badW = new BrokenWriter();
278294
final StringWriter goodW = mock(StringWriter.class);
295+
@SuppressWarnings("resource") // not necessary to close this
279296
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
280297
try {
281298
tw.write(32);
@@ -292,6 +309,7 @@ public void testArrayIOExceptionOnWriteInt2() throws IOException {
292309
public void testArrayIOExceptionOnWriteString1() throws IOException {
293310
final Writer badW = new BrokenWriter();
294311
final StringWriter goodW = mock(StringWriter.class);
312+
@SuppressWarnings("resource") // not necessary to close this
295313
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
296314
final String data = "A";
297315
try {
@@ -308,6 +326,7 @@ public void testArrayIOExceptionOnWriteString1() throws IOException {
308326
public void testArrayIOExceptionOnWriteString2() throws IOException {
309327
final Writer badW = new BrokenWriter();
310328
final StringWriter goodW = mock(StringWriter.class);
329+
@SuppressWarnings("resource") // not necessary to close this
311330
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
312331
final String data = "A";
313332
try {
@@ -325,6 +344,7 @@ public void testArrayIOExceptionOnWriteString2() throws IOException {
325344
public void testArrayIOExceptionOnWriteStringIntInt1() throws IOException {
326345
final Writer badW = new BrokenWriter();
327346
final StringWriter goodW = mock(StringWriter.class);
347+
@SuppressWarnings("resource") // not necessary to close this
328348
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW, goodW, null);
329349
final String data = "A";
330350
try {
@@ -341,6 +361,7 @@ public void testArrayIOExceptionOnWriteStringIntInt1() throws IOException {
341361
public void testArrayIOExceptionOnWriteStringIntInt2() throws IOException {
342362
final Writer badW = new BrokenWriter();
343363
final StringWriter goodW = mock(StringWriter.class);
364+
@SuppressWarnings("resource") // not necessary to close this
344365
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW, badW, null);
345366
final String data = "A";
346367
try {
@@ -358,6 +379,7 @@ public void testArrayIOExceptionOnWriteStringIntInt2() throws IOException {
358379
public void testCollectionCloseBranchIOException() throws IOException {
359380
final Writer badW = new BrokenWriter();
360381
final StringWriter goodW = mock(StringWriter.class);
382+
@SuppressWarnings("resource") // not necessary to close this
361383
final ProxyCollectionWriter tw = new ProxyCollectionWriter(Arrays.asList(goodW, badW, null));
362384
try {
363385
tw.close();
@@ -382,6 +404,7 @@ public void testConstructorsNull() throws IOException {
382404
teeWriter.append('a');
383405
teeWriter.flush();
384406
}
407+
assertTrue(true, "Dummy to show test completed OK");
385408
}
386409

387410
@Test

src/test/java/org/apache/commons/io/output/TeeWriterTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.io.output;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2021
import static org.junit.jupiter.api.Assertions.fail;
2122
import static org.mockito.Mockito.mock;
2223
import static org.mockito.Mockito.verify;
@@ -34,6 +35,7 @@
3435
/**
3536
* JUnit Test Case for {@link TeeWriter}.
3637
*/
38+
@SuppressWarnings("resource") // not necessary to close these resources
3739
public class TeeWriterTest {
3840

3941
@Test
@@ -382,6 +384,7 @@ public void testConstructorsNull() throws IOException {
382384
teeWriter.append('a');
383385
teeWriter.flush();
384386
}
387+
assertTrue(true, "Dummy to show test completed OK");
385388
}
386389

387390
@Test

0 commit comments

Comments
 (0)