forked from graphql-java/java-dataloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryTest.java
More file actions
206 lines (152 loc) · 6.22 KB
/
TryTest.java
File metadata and controls
206 lines (152 loc) · 6.22 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.dataloader;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TryTest {
interface RunThatCanThrow {
void run() throws Throwable;
}
private void expectThrowable(RunThatCanThrow runnable, Class<? extends Throwable> throwableClass) {
try {
runnable.run();
} catch (Throwable e) {
if (throwableClass.isInstance(e)) {
return;
}
}
Assertions.fail("Expected throwable : " + throwableClass.getName());
}
@SuppressWarnings("SameParameterValue")
private void assertFailure(Try<String> sTry, String expectedString) {
assertThat(sTry.isSuccess(), equalTo(false));
assertThat(sTry.isFailure(), equalTo(true));
assertThat(sTry.getThrowable() instanceof RuntimeException, equalTo(true));
assertThat(sTry.getThrowable().getMessage(), equalTo(expectedString));
expectThrowable(sTry::get, UnsupportedOperationException.class);
}
private void assertSuccess(Try<String> sTry, String expectedStr) {
assertThat(sTry.isSuccess(), equalTo(true));
assertThat(sTry.isFailure(), equalTo(false));
assertThat(sTry.get(), equalTo(expectedStr));
expectThrowable(sTry::getThrowable, UnsupportedOperationException.class);
}
@Test
public void tryFailed() {
Try<String> sTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
assertFailure(sTry, "Goodbye Cruel World");
}
@Test
public void trySucceeded() {
Try<String> sTry = Try.succeeded("Hello World");
assertSuccess(sTry, "Hello World");
}
@Test
public void tryCallable() {
Try<String> sTry = Try.tryCall(() -> "Hello World");
assertSuccess(sTry, "Hello World");
sTry = Try.tryCall(() -> {
throw new RuntimeException("Goodbye Cruel World");
});
assertFailure(sTry, "Goodbye Cruel World");
}
@Test
public void triedStage() {
CompletionStage<Try<String>> sTry = Try.tryStage(CompletableFuture.completedFuture("Hello World"));
sTry.thenAccept(stageTry -> assertSuccess(stageTry, "Hello World"));
sTry.toCompletableFuture().join();
CompletableFuture<String> failure = new CompletableFuture<>();
failure.completeExceptionally(new RuntimeException("Goodbye Cruel World"));
sTry = Try.tryStage(failure);
sTry.thenAccept(stageTry -> assertFailure(stageTry, "Goodbye Cruel World"));
sTry.toCompletableFuture().join();
}
@Test
public void map() {
Try<Integer> iTry = Try.succeeded(666);
Try<String> sTry = iTry.map(Object::toString);
assertSuccess(sTry, "666");
iTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
sTry = iTry.map(Object::toString);
assertFailure(sTry, "Goodbye Cruel World");
}
@Test
public void flatMap() {
Function<Integer, Try<String>> intToStringFunc = i -> Try.succeeded(i.toString());
Try<Integer> iTry = Try.succeeded(666);
Try<String> sTry = iTry.flatMap(intToStringFunc);
assertSuccess(sTry, "666");
iTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
sTry = iTry.flatMap(intToStringFunc);
assertFailure(sTry, "Goodbye Cruel World");
}
@Test
public void toOptional() {
Try<Integer> iTry = Try.succeeded(666);
Optional<Integer> optional = iTry.toOptional();
assertThat(optional.isPresent(), equalTo(true));
assertThat(optional.get(), equalTo(666));
iTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
optional = iTry.toOptional();
assertThat(optional.isPresent(), equalTo(false));
}
@Test
public void orElse() {
Try<String> sTry = Try.tryCall(() -> "Hello World");
String result = sTry.orElse("other");
assertThat(result, equalTo("Hello World"));
sTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
result = sTry.orElse("other");
assertThat(result, equalTo("other"));
}
@Test
public void orElseGet() {
Try<String> sTry = Try.tryCall(() -> "Hello World");
String result = sTry.orElseGet(() -> "other");
assertThat(result, equalTo("Hello World"));
sTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
result = sTry.orElseGet(() -> "other");
assertThat(result, equalTo("other"));
}
@Test
public void reThrow() {
Try<String> sTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
expectThrowable(sTry::reThrow, RuntimeException.class);
sTry = Try.tryCall(() -> "Hello World");
expectThrowable(sTry::reThrow, UnsupportedOperationException.class);
}
@Test
public void forEach() {
AtomicReference<String> sRef = new AtomicReference<>();
Try<String> sTry = Try.tryCall(() -> "Hello World");
sTry.forEach(sRef::set);
assertThat(sRef.get(), equalTo("Hello World"));
sRef.set(null);
sTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
sTry.forEach(sRef::set);
assertThat(sRef.get(), nullValue());
}
@Test
public void recover() {
Try<String> sTry = Try.failed(new RuntimeException("Goodbye Cruel World"));
sTry = sTry.recover(t -> "Hello World");
assertSuccess(sTry, "Hello World");
sTry = Try.succeeded("Hello Again");
sTry = sTry.recover(t -> "Hello World");
assertSuccess(sTry, "Hello Again");
}
@Test
public void canAlwaysFail() {
Try<Object> failedTry = Try.alwaysFailed();
assertTrue(failedTry.isFailure());
assertFalse(failedTry.isSuccess());
}
}