Skip to content

Commit

Permalink
Fixed some IDE warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
robtimus committed Oct 4, 2024
1 parent ba7ee80 commit 6690167
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
29 changes: 21 additions & 8 deletions src/test/java/com/github/robtimus/io/stream/BinaryPipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ void testClosed() {
@DisplayName("interrupt")
void testInterrupt() throws InterruptedException {
BinaryPipe pipe = new BinaryPipe();
@SuppressWarnings("resource")
InputStream input = pipe.input();
AtomicReference<InterruptedIOException> exception = new AtomicReference<>();
Thread thread = new Thread(() -> {
exception.set(assertThrows(InterruptedIOException.class, () -> pipe.input().read()));
exception.set(assertThrows(InterruptedIOException.class, () -> input.read()));
});
thread.start();
thread.interrupt();
Expand Down Expand Up @@ -288,7 +290,9 @@ void testWriteInt() throws IOException, InterruptedException {
readLatch.await();
assertArrayEquals(expected, result.get());

assertClosed(() -> pipe.output().write(0));
@SuppressWarnings("resource")
OutputStream output = pipe.output();
assertClosed(() -> output.write(0));
}

@Test
Expand All @@ -315,7 +319,9 @@ void testWriteByteArrayRange() throws IOException, InterruptedException {
readLatch.await();
assertArrayEquals(expected, result.get());

assertClosed(() -> pipe.output().write(expected));
@SuppressWarnings("resource")
OutputStream output = pipe.output();
assertClosed(() -> output.write(expected));
}

@Test
Expand All @@ -334,20 +340,23 @@ void testOperationsWithReadError() throws IOException {

BinaryPipe pipe = new BinaryPipe();
try (OutputStream output = pipe.output()) {
@SuppressWarnings("resource")
PipeInputStream input = pipe.input();

IOException error = new IOException();
pipe.input().close(error);
input.close(error);

assertSame(error, assertThrows(IOException.class, () -> output.write(bytes[0])));
assertSame(error, assertThrows(IOException.class, () -> output.write(bytes, 0, 5)));
assertSame(error, assertThrows(IOException.class, () -> output.flush()));

pipe.input().close();
input.close();

assertSame(error, assertThrows(IOException.class, () -> output.write(bytes[0])));
assertSame(error, assertThrows(IOException.class, () -> output.write(bytes, 0, 5)));
assertSame(error, assertThrows(IOException.class, () -> output.flush()));

pipe.input().close(null);
input.close(null);

assertClosed(() -> output.write(bytes[0]));
assertClosed(() -> output.write(bytes, 0, 5));
Expand Down Expand Up @@ -388,7 +397,9 @@ void testWriteInt() throws InterruptedException {
Arrays.sort(actual);
assertArrayEquals(expected, actual);

assertClosed(() -> pipe.output().write(0));
@SuppressWarnings("resource")
OutputStream output = pipe.output();
assertClosed(() -> output.write(0));
}

@Test
Expand Down Expand Up @@ -420,7 +431,9 @@ void testWriteByteArrayRange() throws InterruptedException {
Arrays.sort(actual);
assertArrayEquals(expected, actual);

assertClosed(() -> pipe.output().write(bytes));
@SuppressWarnings("resource")
OutputStream output = pipe.output();
assertClosed(() -> output.write(bytes));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LimitOutputStreamTest extends TestBase {
@Test
@DisplayName("negative limit")
void testNegativeLimit() {
OutputStream outputStream = NullOutputStream.NULL_OUTPUT_STREAM;
OutputStream outputStream = NullOutputStream.INSTANCE;
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new LimitOutputStream(outputStream, -1));
assertEquals("-1 < 0", exception.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LimitWriterTest extends TestBase {
@Test
@DisplayName("negative limit")
void testNegativeLimit() {
Writer writer = NullWriter.NULL_WRITER;
Writer writer = NullWriter.INSTANCE;
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new LimitWriter(writer, -1));
assertEquals("-1 < 0", exception.getMessage());
}
Expand Down
50 changes: 36 additions & 14 deletions src/test/java/com/github/robtimus/io/stream/TextPipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ void testClosed() {
@DisplayName("interrupt")
void testInterrupt() throws InterruptedException {
TextPipe pipe = new TextPipe();
@SuppressWarnings("resource")
Reader input = pipe.input();
AtomicReference<InterruptedIOException> exception = new AtomicReference<>();
Thread thread = new Thread(() -> {
exception.set(assertThrows(InterruptedIOException.class, () -> pipe.input().read()));
exception.set(assertThrows(InterruptedIOException.class, () -> input.read()));
});
thread.start();
thread.interrupt();
Expand Down Expand Up @@ -301,7 +303,9 @@ void testWriteInt() throws IOException, InterruptedException {
readLatch.await();
assertEquals(SOURCE, result.get());

assertClosed(() -> pipe.output().write('*'));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write('*'));
}

@Test
Expand All @@ -328,7 +332,9 @@ void testWriteCharArrayRange() throws IOException, InterruptedException {
readLatch.await();
assertEquals(SOURCE, result.get());

assertClosed(() -> pipe.output().write(chars));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write(chars));
}

@Test
Expand All @@ -353,7 +359,9 @@ void testWriteStringRange() throws IOException, InterruptedException {
readLatch.await();
assertEquals(SOURCE, result.get());

assertClosed(() -> pipe.output().write(SOURCE));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write(SOURCE));
}

@Test
Expand Down Expand Up @@ -382,7 +390,8 @@ private void testAppendCharSequence(Function<String, CharSequence> mapper) throw
readLatch.await();
assertEquals(SOURCE + "null", result.get());

assertClosed(() -> pipe.output().append(SOURCE));
Writer output = pipe.output();
assertClosed(() -> output.append(SOURCE));
}

@Test
Expand Down Expand Up @@ -416,7 +425,9 @@ private void testAppendSubSequence(Function<String, CharSequence> mapper) throws
readLatch.await();
assertEquals(SOURCE + "ul", result.get());

assertClosed(() -> pipe.output().write(SOURCE, 0, 10));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write(SOURCE, 0, 10));
}

@Test
Expand All @@ -434,7 +445,9 @@ void testAppendChar() throws IOException, InterruptedException {
readLatch.await();
assertEquals(SOURCE, result.get());

assertClosed(() -> pipe.output().write('*'));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write('*'));
}

@Test
Expand All @@ -453,8 +466,11 @@ void testOperationsWithReadError() throws IOException {

TextPipe pipe = new TextPipe();
try (Writer output = pipe.output()) {
@SuppressWarnings("resource")
PipeReader input = pipe.input();

IOException error = new IOException();
pipe.input().close(error);
input.close(error);

assertSame(error, assertThrows(IOException.class, () -> output.write(chars[0])));
assertSame(error, assertThrows(IOException.class, () -> output.write(chars, 0, 5)));
Expand All @@ -464,7 +480,7 @@ void testOperationsWithReadError() throws IOException {
assertSame(error, assertThrows(IOException.class, () -> output.append(chars[0])));
assertSame(error, assertThrows(IOException.class, () -> output.flush()));

pipe.input().close();
input.close();

assertSame(error, assertThrows(IOException.class, () -> output.write(chars[0])));
assertSame(error, assertThrows(IOException.class, () -> output.write(chars, 0, 5)));
Expand All @@ -474,7 +490,7 @@ void testOperationsWithReadError() throws IOException {
assertSame(error, assertThrows(IOException.class, () -> output.append(chars[0])));
assertSame(error, assertThrows(IOException.class, () -> output.flush()));

pipe.input().close(null);
input.close(null);

assertClosed(() -> output.write(chars[0]));
assertClosed(() -> output.write(chars, 0, 5));
Expand Down Expand Up @@ -517,7 +533,9 @@ void testWriteInt() throws InterruptedException {
Arrays.sort(actual);
assertArrayEquals(expected, actual);

assertClosed(() -> pipe.output().write(0));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write(0));
}

@Test
Expand Down Expand Up @@ -547,7 +565,9 @@ void testWriteCharArrayRange() throws InterruptedException {
Arrays.sort(actual);
assertArrayEquals(expected, actual);

assertClosed(() -> pipe.output().write(SOURCE));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write(SOURCE));
}

@Test
Expand Down Expand Up @@ -577,7 +597,9 @@ void testAppendSubSequence() throws InterruptedException {
Arrays.sort(actual);
assertArrayEquals(expected, actual);

assertClosed(() -> pipe.output().write(SOURCE));
@SuppressWarnings("resource")
Writer output = pipe.output();
assertClosed(() -> output.write(SOURCE));
}
}

Expand Down Expand Up @@ -665,7 +687,7 @@ void testAppendChar() throws IOException {
try (Writer output = pipe.output()) {
output.write(0);
IOException thrown = assertThrows(IOException.class, () -> {
output.write(0);
output.append('\0');
});
assertEquals(Messages.pipe.readerDied(), thrown.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private CapturingHttpServletRequest(HttpServletRequest request) {
}

@Override
@SuppressWarnings("resource")
public ServletInputStream getInputStream() throws IOException {
if (inputStream == null) {
CapturingInputStream capturing = new CapturingInputStream(super.getInputStream(), CapturingInputStream.config()
Expand Down

0 comments on commit 6690167

Please sign in to comment.