use of com.helger.commons.exception.mock.MockIOException in project ph-web by phax.
the class StreamingFuncTest method testIOException.
/**
* Tests, whether an IOException is properly delegated.
*
* @throws IOException
* In case of error
*/
@Test
public void testIOException() throws IOException {
final byte[] request = _newRequest();
final InputStream stream = new FilterInputStream(new NonBlockingByteArrayInputStream(request)) {
private int m_nNum;
@Override
public int read() throws IOException {
if (++m_nNum > 123)
throw new MockIOException("123");
return super.read();
}
@Override
public int read(final byte[] pB, final int pOff, final int pLen) throws IOException {
for (int i = 0; i < pLen; i++) {
final int res = read();
if (res < 0)
return i == 0 ? -1 : i;
pB[pOff + i] = (byte) res;
}
return pLen;
}
};
try {
_parseUploadToList(stream, request.length);
fail("Expected IOException");
} catch (final FileUploadException e) {
assertTrue(e.getCause() instanceof MockIOException);
assertEquals("123", e.getCause().getMessage());
}
}
use of com.helger.commons.exception.mock.MockIOException in project ph-commons by phax.
the class FileIOErrorTest method testStdMethods.
@Test
public void testStdMethods() {
// ctor 1
final FileIOError e1 = new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST);
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(e1, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e1, new FileIOError(EFileIOOperation.COPY_DIR_RECURSIVE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e1, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_EQUALS_TARGET));
// ctor 2
final FileIOError e2 = new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f1);
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(e2, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f1));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e2, new FileIOError(EFileIOOperation.COPY_DIR_RECURSIVE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f1));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e2, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_EQUALS_TARGET, f1));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e2, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f2));
// ctor 3
final FileIOError e3 = new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f1, f2);
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(e3, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f1, f2));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e3, new FileIOError(EFileIOOperation.COPY_DIR_RECURSIVE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f1, f2));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e3, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_EQUALS_TARGET, f1, f2));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e3, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f2, f2));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e3, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, f1, f1));
// ctor 4
final Exception e = new MockException();
final FileIOError e4 = new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, e);
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(e4, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, e));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e4, new FileIOError(EFileIOOperation.COPY_DIR_RECURSIVE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, e));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e4, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_EQUALS_TARGET, e));
CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(e4, new FileIOError(EFileIOOperation.COPY_FILE, EFileIOErrorCode.SOURCE_DOES_NOT_EXIST, new MockIOException()));
}
use of com.helger.commons.exception.mock.MockIOException in project ph-commons by phax.
the class StreamHelperTest method testCopyInputStreamToOutputStream.
/**
* Test method copyInputStreamToOutputStream
*/
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION")
public void testCopyInputStreamToOutputStream() {
final byte[] aInput = "Hallo".getBytes(StandardCharsets.ISO_8859_1);
final NonBlockingByteArrayInputStream aBAIS = new NonBlockingByteArrayInputStream(aInput);
final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
assertTrue(StreamHelper.copyInputStreamToOutputStream(aBAIS, aBAOS).isSuccess());
assertArrayEquals(aInput, aBAOS.toByteArray());
// try with null streams
assertTrue(StreamHelper.copyInputStreamToOutputStream(aBAIS, null).isFailure());
assertTrue(StreamHelper.copyInputStreamToOutputStream(null, aBAOS).isFailure());
assertTrue(StreamHelper.copyInputStreamToOutputStream(aBAIS, aBAOS, new byte[10]).isSuccess());
final MutableLong aML = new MutableLong(0);
aBAIS.reset();
assertTrue(StreamHelper.copyInputStreamToOutputStream(aBAIS, false, aBAOS, false, new byte[10], null, null, aML).isSuccess());
assertEquals(aML.longValue(), aInput.length);
// Must be a ByteArrayInputStream so that an IOException can be thrown!
assertTrue(StreamHelper.copyInputStreamToOutputStream(new WrappedInputStream(aBAIS) {
@Override
public int read(final byte[] aBuf, final int nOfs, final int nLen) throws IOException {
throw new MockIOException();
}
}, aBAOS).isFailure());
// null buffer is handled internally
StreamHelper.copyInputStreamToOutputStream(aBAIS, aBAOS, (byte[]) null);
// empty buffer is handled internally
StreamHelper.copyInputStreamToOutputStream(aBAIS, aBAOS, new byte[0]);
}
use of com.helger.commons.exception.mock.MockIOException in project ph-commons by phax.
the class StreamHelperTest method testCopyReaderToWriter.
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION")
public void testCopyReaderToWriter() {
final String sInput = "Hallo";
NonBlockingStringReader aBAIS = new NonBlockingStringReader(sInput);
final NonBlockingStringWriter aBAOS = new NonBlockingStringWriter();
assertTrue(StreamHelper.copyReaderToWriter(aBAIS, aBAOS).isSuccess());
assertEquals(sInput, aBAOS.getAsString());
// try with null streams
aBAIS = new NonBlockingStringReader(sInput);
assertTrue(StreamHelper.copyReaderToWriter(aBAIS, null).isFailure());
assertTrue(StreamHelper.copyReaderToWriter(null, aBAOS).isFailure());
aBAIS = new NonBlockingStringReader(sInput);
assertTrue(StreamHelper.copyReaderToWriter(aBAIS, aBAOS, new char[10]).isSuccess());
final MutableLong aML = new MutableLong(0);
aBAIS = new NonBlockingStringReader(sInput);
assertTrue(StreamHelper.copyReaderToWriter(aBAIS, false, aBAOS, false, new char[10], null, null, aML).isSuccess());
assertEquals(aML.longValue(), sInput.length());
// Must be a ByteArrayReader so that an IOException can be thrown!
assertTrue(StreamHelper.copyReaderToWriter(new WrappedReader(aBAIS) {
@Override
public int read(final char[] aBuf, final int nOfs, final int nLen) throws IOException {
throw new MockIOException();
}
}, aBAOS).isFailure());
// null buffer
StreamHelper.copyReaderToWriter(aBAIS, aBAOS, (char[]) null);
// empty buffer
StreamHelper.copyReaderToWriter(aBAIS, aBAOS, new char[0]);
}
Aggregations