-
Notifications
You must be signed in to change notification settings - Fork 2
/
HandlerCaseTest.java
108 lines (94 loc) · 3.08 KB
/
HandlerCaseTest.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package systems.raptor.cafe_latte.handlers;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;
import systems.raptor.cafe_latte.conditions.Condition;
import systems.raptor.cafe_latte.conditions.Error;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static systems.raptor.cafe_latte.handlers.Handler.signal;
import static systems.raptor.cafe_latte.handlers.HandlerCase.ignoreErrors;
class HandlerCaseTest {
@Test
public void handlerCaseNoHandlersTest() {
HandlerCase<String> handlerCase = new HandlerCase<>(List.of(), () -> {
signal(new Condition());
return "bar";
});
String returnValue = handlerCase.get();
assertEquals("bar", returnValue);
}
@Test
public void handlerCaseMultipleHandlersTest() {
HandlerCase<String> handlerCase = new HandlerCase<>(List.of(
Pair.of(Condition.class, (condition) -> "foo"),
Pair.of(Condition.class, (condition) -> "bar"),
Pair.of(Condition.class, (condition) -> "baz")), () -> {
signal(new Condition());
return "quux";
});
String returnValue = handlerCase.get();
assertEquals("foo", returnValue);
}
private static class TestCondition extends Condition {}
@Test
public void handlerCaseMultipleHandlersInheritanceTest() {
HandlerCase<String> handlerCase = new HandlerCase<>(List.of(
Pair.of(TestCondition.class, (condition) -> "foo"),
Pair.of(Condition.class, (condition) -> "bar"),
Pair.of(TestCondition.class, (condition) -> "baz")), () -> {
signal(new Condition());
return "quux";
});
String returnValue = handlerCase.get();
assertEquals("bar", returnValue);
}
@Test
public void handlerCaseTransferTest() {
HandlerCase<String> handlerCase = new HandlerCase<>(List.of(
Pair.of(Condition.class, (condition) -> "foo")), () -> {
signal(new Condition());
return "bar";
});
String returnValue = handlerCase.get();
assertEquals("foo", returnValue);
}
@Test
public void handlerCaseNoTransferTest() {
HandlerCase<String> handlerCase = new HandlerCase<>(List.of(
Pair.of(TestCondition.class, (condition) -> "foo")), () -> {
signal(new Condition());
return "bar";
});
String returnValue = handlerCase.get();
assertEquals("bar", returnValue);
}
@Test
public void handlerCaseUnwindOrderTest() {
var ref = new Object() {
int counter = 0;
};
new HandlerCase<>(List.of(Pair.of(Condition.class, (c) -> ref.counter *= 2)), () -> {
try {
signal(new Condition());
} finally {
ref.counter += 10;
}
return null;
}).get();
assertEquals(20, ref.counter);
}
@Test
public void ignoreErrorsNoTransferTest() {
Condition returnValue = ignoreErrors(() -> {});
assertNull(returnValue);
}
@Test
public void ignoreErrorsTransferTest() {
Error error = new Error();
Condition returnValue = ignoreErrors(() -> {
signal(error);
});
assertNotNull(returnValue);
assertEquals(returnValue, error);
}
}