final IOException exception = new IOException("test exception"); final TaggedOutputStream stream = new TaggedOutputStream(new BrokenOutputStream(exception)); stream.write('x'); fail("Expected exception not thrown."); } catch (final IOException e) { assertTrue(stream.isCauseOf(e)); try { stream.throwIfCauseOf(e); fail("Expected exception not thrown."); } catch (final IOException e2) { stream.flush(); fail("Expected exception not thrown."); } catch (final IOException e) { assertTrue(stream.isCauseOf(e)); try { stream.throwIfCauseOf(e); fail("Expected exception not thrown."); } catch (final IOException e2) { stream.close(); fail("Expected exception not thrown."); } catch (final IOException e) { assertTrue(stream.isCauseOf(e)); try { stream.throwIfCauseOf(e); fail("Expected exception not thrown.");
@Test public void testOtherException() throws Exception { final IOException exception = new IOException("test exception"); final OutputStream closed = new ClosedOutputStream(); final TaggedOutputStream stream = new TaggedOutputStream(closed); assertFalse(stream.isCauseOf(exception)); assertFalse(stream.isCauseOf( new TaggedIOException(exception, UUID.randomUUID()))); try { stream.throwIfCauseOf(exception); } catch (final IOException e) { fail("Unexpected exception thrown"); } try { stream.throwIfCauseOf( new TaggedIOException(exception, UUID.randomUUID())); } catch (final IOException e) { fail("Unexpected exception thrown"); } stream.close(); }
tagged = new TaggedOutputStream(output); copy(reader, tagged); } catch (IOException e) { if (null != tagged && tagged.isCauseOf(e)) { throw new TaggedIOException(new IOException(String.format("Error writing output to file: \"%s\".", contentsOutputPath), e), this);
@Test public void testNormalStream() { try { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final OutputStream stream = new TaggedOutputStream(buffer); stream.write('a'); stream.write(new byte[] { 'b' }); stream.write(new byte[] { 'c' }, 0, 1); stream.flush(); stream.close(); assertEquals(3, buffer.size()); assertEquals('a', buffer.toByteArray()[0]); assertEquals('b', buffer.toByteArray()[1]); assertEquals('c', buffer.toByteArray()[2]); } catch (final IOException e) { fail("Unexpected exception thrown"); } }