forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOFunctionsTest.java
More file actions
64 lines (53 loc) · 1.52 KB
/
Copy pathIOFunctionsTest.java
File metadata and controls
64 lines (53 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fj.data;
import fj.Unit;
import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.io.Reader;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.*;
public class IOFunctionsTest {
@Test
public void bracket_happy_path() throws Exception {
AtomicBoolean closed = new AtomicBoolean();
Reader reader = new StringReader("Read OK") {
@Override
public void close() {
super.close();
closed.set(true);
}
};
IO<String> bracketed = IOFunctions.bracket(
() -> reader,
IOFunctions.closeReader,
r -> () -> new BufferedReader(r).readLine()
);
Assert.assertThat(bracketed.run(), Is.is("Read OK"));
Assert.assertThat(closed.get(), Is.is(true));
}
@Test
public void bracket_exception_path() throws Exception {
AtomicBoolean closed = new AtomicBoolean();
Reader reader = new StringReader("Read OK") {
@Override
public void close() {
super.close();
closed.set(true);
throw new IllegalStateException("Should be suppressed");
}
};
IO<String> bracketed = IOFunctions.bracket(
() -> reader,
IOFunctions.closeReader,
r -> () -> {throw new IllegalArgumentException("OoO");}
);
try {
bracketed.run();
fail("Exception expected");
} catch (IllegalArgumentException e) {
Assert.assertThat(e.getMessage(), Is.is("OoO"));
}
Assert.assertThat(closed.get(), Is.is(true));
}
}