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. 228
      org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/UnionInputStreamTest.java

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

@ -69,53 +69,52 @@ public class UnionInputStreamTest {
@Test
public void testReadSingleBytes() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream();
assertTrue(u.isEmpty());
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 }));
u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
try (UnionInputStream u = new UnionInputStream()) {
assertTrue(u.isEmpty());
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 }));
u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
assertFalse(u.isEmpty());
assertEquals(3, u.available());
assertEquals(1, u.read());
assertEquals(0, u.read());
assertEquals(2, u.read());
assertEquals(0, u.available());
assertFalse(u.isEmpty());
assertEquals(3, u.available());
assertEquals(1, u.read());
assertEquals(0, u.read());
assertEquals(2, u.read());
assertEquals(0, u.available());
assertEquals(3, u.read());
assertEquals(0, u.available());
assertEquals(3, u.read());
assertEquals(0, u.available());
assertEquals(4, u.read());
assertEquals(1, u.available());
assertEquals(5, u.read());
assertEquals(0, u.available());
assertEquals(-1, u.read());
assertEquals(4, u.read());
assertEquals(1, u.available());
assertEquals(5, u.read());
assertEquals(0, u.available());
assertEquals(-1, u.read());
assertTrue(u.isEmpty());
u.add(new ByteArrayInputStream(new byte[] { (byte) 255 }));
assertEquals(255, u.read());
assertEquals(-1, u.read());
assertTrue(u.isEmpty());
assertTrue(u.isEmpty());
u.add(new ByteArrayInputStream(new byte[] { (byte) 255 }));
assertEquals(255, u.read());
assertEquals(-1, u.read());
assertTrue(u.isEmpty());
}
}
@Test
public void testReadByteBlocks() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final 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 }));
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 }));
final byte[] r = new byte[5];
assertEquals(3, u.read(r, 0, 5));
assertTrue(Arrays.equals(new byte[] { 1, 0, 2, }, slice(r, 3)));
assertEquals(1, u.read(r, 0, 5));
assertEquals(3, r[0]);
assertEquals(2, u.read(r, 0, 5));
assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
assertEquals(-1, u.read(r, 0, 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)));
assertEquals(1, u.read(r, 0, 5));
assertEquals(3, r[0]);
assertEquals(2, u.read(r, 0, 5));
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) {
@ -126,89 +125,88 @@ 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 }));
final byte[] r = new byte[5];
assertEquals(3, u.read(r, 0, 5));
assertTrue(Arrays.equals(new byte[] { 1, 0, 2, }, slice(r, 3)));
assertEquals(1, u.read(r, 0, 5));
assertEquals(3, r[0]);
assertEquals(2, u.read(r, 0, 5));
assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
assertEquals(-1, u.read(r, 0, 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)));
assertEquals(1, u.read(r, 0, 5));
assertEquals(3, r[0]);
assertEquals(2, u.read(r, 0, 5));
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();
assertFalse(u.markSupported());
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
assertFalse(u.markSupported());
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();
u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
u.add(new ByteArrayInputStream(new byte[] { 3 }));
u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
assertEquals(0, u.skip(0));
assertEquals(3, u.skip(3));
assertEquals(3, u.read());
assertEquals(2, u.skip(5));
assertEquals(0, u.skip(5));
assertEquals(-1, u.read());
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 }));
assertEquals(0, u.skip(0));
assertEquals(3, u.skip(3));
assertEquals(3, u.read());
assertEquals(2, u.skip(5));
assertEquals(0, u.skip(5));
assertEquals(-1, u.read());
u.add(new ByteArrayInputStream(new byte[] { 20, 30 }) {
@Override
@SuppressWarnings("UnsynchronizedOverridesSynchronized")
// This is only used in tests and is thread-safe
public long skip(long n) {
return 0;
}
});
assertEquals(2, u.skip(8));
assertEquals(-1, u.read());
u.add(new ByteArrayInputStream(new byte[] { 20, 30 }) {
@Override
@SuppressWarnings("UnsynchronizedOverridesSynchronized")
// This is only used in tests and is thread-safe
public long skip(long n) {
return 0;
}
});
assertEquals(2, u.skip(8));
assertEquals(-1, u.read());
}
}
@Test
public void testAutoCloseDuringRead() throws IOException {
@SuppressWarnings("resource" /* java 7 */)
final UnionInputStream u = new UnionInputStream();
final boolean closed[] = new boolean[2];
u.add(new ByteArrayInputStream(new byte[] { 1 }) {
@Override
public void close() {
closed[0] = true;
}
});
u.add(new ByteArrayInputStream(new byte[] { 2 }) {
@Override
public void close() {
closed[1] = true;
}
});
try (UnionInputStream u = new UnionInputStream()) {
final boolean closed[] = new boolean[2];
u.add(new ByteArrayInputStream(new byte[] { 1 }) {
@Override
public void close() {
closed[0] = true;
}
});
u.add(new ByteArrayInputStream(new byte[] { 2 }) {
@Override
public void close() {
closed[1] = true;
}
});
assertFalse(closed[0]);
assertFalse(closed[1]);
assertFalse(closed[0]);
assertFalse(closed[1]);
assertEquals(1, u.read());
assertFalse(closed[0]);
assertFalse(closed[1]);
assertEquals(1, u.read());
assertFalse(closed[0]);
assertFalse(closed[1]);
assertEquals(2, u.read());
assertTrue(closed[0]);
assertFalse(closed[1]);
assertEquals(2, u.read());
assertTrue(closed[0]);
assertFalse(closed[1]);
assertEquals(-1, u.read());
assertTrue(closed[0]);
assertTrue(closed[1]);
assertEquals(-1, u.read());
assertTrue(closed[0]);
assertTrue(closed[1]);
}
}
@Test
@ -267,18 +265,18 @@ public class UnionInputStreamTest {
throw new IOException("Expected");
}
};
@SuppressWarnings("resource" /* java 7 */)
final 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)));
try {
u.read(buf, 0, 1);
fail("Expected exception from errorReadStream");
} catch (IOException e) {
assertEquals("Expected", e.getMessage());
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)));
try {
u.read(buf, 0, 1);
fail("Expected exception from errorReadStream");
} catch (IOException e) {
assertEquals("Expected", e.getMessage());
}
}
}
}

Loading…
Cancel
Save