Skip to content

Commit 824da22

Browse files
committed
docs: example input stream exception
1 parent f45e934 commit 824da22

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.fugerit.java.turbo.unit.core.ex;
2+
3+
public class ExampleRuntimeException extends RuntimeException {
4+
5+
public ExampleRuntimeException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.fugerit.java.turbo.unit.core.io;
2+
3+
import org.fugerit.java.turbo.unit.core.ex.ExampleRuntimeException;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
9+
/**
10+
* This is a simple class reading the content of an InputStream.
11+
*
12+
* If successful, a byte[] array containing the data read is returned.
13+
*
14+
* If an exception occurs, it is caught and rethrow as a RuntimeException.
15+
*
16+
*/
17+
public class ExampleReadStream {
18+
19+
public byte[] readBytes( InputStream inputStream ) {
20+
try ( ByteArrayOutputStream buffer = new ByteArrayOutputStream() ) {
21+
byte[] data = new byte[1024];
22+
int read = inputStream.read( data );
23+
while ( read > 0 ) {
24+
buffer.write( data, 0, read );
25+
read = inputStream.read( data );
26+
}
27+
return buffer.toByteArray();
28+
} catch ( IOException e ) {
29+
String message = String.format( "Error reading input stream : %s", e );
30+
throw new ExampleRuntimeException( message, e );
31+
}
32+
}
33+
34+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.fugerit.java.turbo.unit.core.io;
2+
3+
import org.fugerit.java.turbo.unit.core.ex.ExampleRuntimeException;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
11+
/**
12+
* When testing {@link ExampleReadStream} it is easy to check the successful scenario.
13+
*
14+
* But if you want to try the Exception case too, some workaround it is needed.
15+
*
16+
* In this example, we are using a custom InputStream to generate an exception.
17+
*
18+
* I used something similar in the repository :
19+
*
20+
* https://github.com/fugerit-org/fj-lib/blob/main/fj-core/src/test/java/test/org/fugerit/java/core/testhelpers/FailInputStream.java
21+
* https://github.com/fugerit-org/fj-lib/blob/main/fj-core/src/test/java/test/org/fugerit/java/core/cfg/store/TestConfigStore.java#L81
22+
*
23+
*/
24+
class TestExampleReadStream {
25+
26+
@Test
27+
void testKo() throws IOException {
28+
ExampleReadStream example = new ExampleReadStream();
29+
try (InputStream is = new InputStream() {
30+
@Override
31+
public int read() throws IOException {
32+
if (Boolean.TRUE) {
33+
throw new IOException("read failed (by scenario)");
34+
}
35+
return 0;
36+
}
37+
}) {
38+
Assertions.assertThrows(ExampleRuntimeException.class, () -> example.readBytes(is));
39+
}
40+
}
41+
42+
@Test
43+
void testOk() throws IOException {
44+
ExampleReadStream example = new ExampleReadStream();
45+
String testOk = "OK";
46+
try (InputStream is = new ByteArrayInputStream(testOk.getBytes())) {
47+
// Assertions.assertThrows(ExampleRuntimeException.class, () -> example.readBytes(is));
48+
byte[] data = example.readBytes(is);
49+
Assertions.assertArrayEquals( data, testOk.getBytes() );
50+
}
51+
}
52+
53+
}

0 commit comments

Comments
 (0)