Browse Source

UnionInputStreamTest: Open UnionInputStream in try-with-resource

The tests were written for Java 7 which did not have AutoCloseable
and the try-with-resource concept. When the project was updated to
build with Java 8, the warnings were suppressed.

Remove the suppressions and convert to use try-with-resource.

Change-Id: Ic805bd571c4a2e4376ce5e7c34ca7ac86cbf5104
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.4
David Pursehouse 6 years ago
parent
commit
ecd16a7906
  1. 40
      org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/UnionInputStreamTest.java

40
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/UnionInputStreamTest.java

@ -69,9 +69,7 @@ public class UnionInputStreamTest {
@Test
public void testReadSingleBytes() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream();
try (UnionInputStream u = new UnionInputStream()) {
assertTrue(u.isEmpty());
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 }));
@ -99,11 +97,11 @@ public class UnionInputStreamTest {
assertEquals(-1, u.read());
assertTrue(u.isEmpty());
}
}
@Test
public void testReadByteBlocks() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream();
try (UnionInputStream u = new UnionInputStream()) {
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 }));
u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
@ -117,6 +115,7 @@ public class UnionInputStreamTest {
assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
assertEquals(-1, u.read(r, 0, 5));
}
}
private static byte[] slice(byte[] in, int len) {
byte[] r = new byte[len];
@ -126,12 +125,10 @@ public class UnionInputStreamTest {
@Test
public void testArrayConstructor() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream(
try (UnionInputStream u = new UnionInputStream(
new ByteArrayInputStream(new byte[] { 1, 0, 2 }),
new ByteArrayInputStream(new byte[] { 3 }),
new ByteArrayInputStream(new byte[] { 4, 5 }));
new ByteArrayInputStream(new byte[] { 4, 5 }))) {
final byte[] r = new byte[5];
assertEquals(3, u.read(r, 0, 5));
assertTrue(Arrays.equals(new byte[] { 1, 0, 2, }, slice(r, 3)));
@ -141,20 +138,20 @@ public class UnionInputStreamTest {
assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
assertEquals(-1, u.read(r, 0, 5));
}
}
@Test
public void testMarkSupported() {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream();
public void testMarkSupported() throws IOException {
try (UnionInputStream u = new UnionInputStream()) {
assertFalse(u.markSupported());
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
assertFalse(u.markSupported());
}
}
@Test
public void testSkip() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream();
try (UnionInputStream u = new UnionInputStream()) {
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 }));
u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
@ -176,11 +173,11 @@ public class UnionInputStreamTest {
assertEquals(2, u.skip(8));
assertEquals(-1, u.read());
}
}
@Test
public void testAutoCloseDuringRead() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream();
try (UnionInputStream u = new UnionInputStream()) {
final boolean closed[] = new boolean[2];
u.add(new ByteArrayInputStream(new byte[] { 1 }) {
@Override
@ -210,6 +207,7 @@ public class UnionInputStreamTest {
assertTrue(closed[0]);
assertTrue(closed[1]);
}
}
@Test
public void testCloseDuringClose() throws IOException {
@ -267,13 +265,12 @@ public class UnionInputStreamTest {
throw new IOException("Expected");
}
};
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream(
new ByteArrayInputStream(new byte[]{1,2,3}),
errorReadStream);
try (UnionInputStream u = new UnionInputStream(
new ByteArrayInputStream(new byte[] { 1, 2, 3 }),
errorReadStream)) {
byte buf[] = new byte[10];
assertEquals(3, u.read(buf, 0, 10));
assertTrue(Arrays.equals(new byte[] {1,2,3}, slice(buf, 3)));
assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, slice(buf, 3)));
try {
u.read(buf, 0, 1);
fail("Expected exception from errorReadStream");
@ -281,4 +278,5 @@ public class UnionInputStreamTest {
assertEquals("Expected", e.getMessage());
}
}
}
}

Loading…
Cancel
Save