Skip to content

Commit 3107a2f

Browse files
committed
add unit tests for IOFunctions#bracket
1 parent d784b7f commit 3107a2f

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package fj.data;
2+
3+
import fj.Unit;
4+
import org.hamcrest.core.Is;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.*;
9+
import java.io.Reader;
10+
import java.util.concurrent.atomic.AtomicBoolean;
11+
12+
import static org.junit.Assert.*;
13+
14+
public class IOFunctionsTest {
15+
16+
@Test
17+
public void bracket_happy_path() throws Exception {
18+
AtomicBoolean closed = new AtomicBoolean();
19+
Reader reader = new StringReader("Read OK") {
20+
@Override
21+
public void close() {
22+
super.close();
23+
closed.set(true);
24+
}
25+
};
26+
27+
IO<String> bracketed = IOFunctions.bracket(
28+
() -> reader,
29+
IOFunctions.closeReader,
30+
r -> () -> new BufferedReader(r).readLine()
31+
);
32+
33+
Assert.assertThat(bracketed.run(), Is.is("Read OK"));
34+
Assert.assertThat(closed.get(), Is.is(true));
35+
}
36+
37+
@Test
38+
public void bracket_exception_path() throws Exception {
39+
AtomicBoolean closed = new AtomicBoolean();
40+
Reader reader = new StringReader("Read OK") {
41+
@Override
42+
public void close() {
43+
super.close();
44+
closed.set(true);
45+
throw new IllegalStateException("Should be suppressed");
46+
}
47+
};
48+
49+
IO<String> bracketed = IOFunctions.bracket(
50+
() -> reader,
51+
IOFunctions.closeReader,
52+
r -> () -> {throw new IllegalArgumentException("OoO");}
53+
);
54+
55+
try {
56+
bracketed.run();
57+
fail("Exception expected");
58+
} catch (IllegalArgumentException e) {
59+
Assert.assertThat(e.getMessage(), Is.is("OoO"));
60+
}
61+
Assert.assertThat(closed.get(), Is.is(true));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)