### Question:
DataSourceType extends AbstractXOPType { @Override protected byte[] getBytes(Object object) { DataSource dataSource = (DataSource) object; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { IOUtils.copyAndCloseInput(dataSource.getInputStream(), baos); } catch (IOException e) { throw new RuntimeException(e); } return baos.toByteArray(); } DataSourceType(boolean useXmimeBinaryType, String expectedContentTypes);   }### Answer:
@Test public void inputStreamShouldBeClosedOnHappyPath() throws Exception { DataSource ds = mock(MockType.STRICT, DataSource.class); InputStream is = mock(MockType.STRICT, InputStream.class); expect(ds.getInputStream()).andReturn(is); replay(ds); expect(is.available()).andReturn(1); expect(is.read(anyObject(byte[].class))).andReturn(-1); is.close(); replay(is); DataSourceType dst = new DataSourceType(false, null); dst.getBytes(ds); verify(ds); verify(is); }
@Test(expected = RuntimeException.class) public void inputStreamShouldBeClosedOnReadingException() throws Exception { DataSource ds = mock(MockType.STRICT, DataSource.class); InputStream is = mock(MockType.STRICT, InputStream.class); expect(ds.getInputStream()).andReturn(is); replay(ds); expect(is.available()).andThrow(new IOException()); is.close(); replay(is); DataSourceType dst = new DataSourceType(false, null); try { dst.getBytes(ds); } finally { verify(ds); verify(is); } }