Skip to content

Commit

Permalink
Simplify testing
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jul 14, 2024
1 parent 15ab5d4 commit 30434ed
Showing 1 changed file with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,11 @@ public class AutoCloseInputStreamTest {

private AutoCloseInputStream stream;

private boolean closed;

@SuppressWarnings("deprecation")
@BeforeEach
public void setUp() {
data = new byte[] { 'x', 'y', 'z' };
stream = new AutoCloseInputStream(new ByteArrayInputStream(data) {
@Override
public void close() throws IOException {
closed = true;
}
});
closed = false;
stream = new AutoCloseInputStream(new ByteArrayInputStream(data));
}

@Test
Expand Down Expand Up @@ -91,7 +83,6 @@ public void testBuilderGet() {
@Test
public void testClose() throws IOException {
stream.close();
assertTrue(closed, "closed");
assertTrue(stream.isClosed(), "closed");
assertEquals(-1, stream.read(), "read()");
assertTrue(stream.isClosed(), "closed");
Expand All @@ -113,33 +104,33 @@ public void testCloseHandleIOException() throws IOException {
@Test
public void testFinalize() throws Throwable {
stream.finalize();
assertTrue(closed, "closed");
assertTrue(stream.isClosed(), "closed");
assertEquals(-1, stream.read(), "read()");
}

@Test
public void testRead() throws IOException {
for (final byte element : data) {
assertEquals(element, stream.read(), "read()");
assertFalse(closed, "closed");
assertFalse(stream.isClosed(), "closed");
}
assertEquals(-1, stream.read(), "read()");
assertTrue(closed, "closed");
assertTrue(stream.isClosed(), "closed");
}

@Test
public void testReadBuffer() throws IOException {
final byte[] b = new byte[data.length * 2];
int total = 0;
for (int n = 0; n != -1; n = stream.read(b)) {
assertFalse(closed, "closed");
assertFalse(stream.isClosed(), "closed");
for (int i = 0; i < n; i++) {
assertEquals(data[total + i], b[i], "read(b)");
}
total += n;
}
assertEquals(data.length, total, "read(b)");
assertTrue(closed, "closed");
assertTrue(stream.isClosed(), "closed");
assertEquals(-1, stream.read(b), "read(b)");
}

Expand All @@ -148,14 +139,14 @@ public void testReadBufferOffsetLength() throws IOException {
final byte[] b = new byte[data.length * 2];
int total = 0;
for (int n = 0; n != -1; n = stream.read(b, total, b.length - total)) {
assertFalse(closed, "closed");
assertFalse(stream.isClosed(), "closed");
total += n;
}
assertEquals(data.length, total, "read(b, off, len)");
for (int i = 0; i < data.length; i++) {
assertEquals(data[i], b[i], "read(b, off, len)");
}
assertTrue(closed, "closed");
assertTrue(stream.isClosed(), "closed");
assertEquals(-1, stream.read(b, 0, b.length), "read(b, off, len)");
}

Expand Down

0 comments on commit 30434ed

Please sign in to comment.