-
Notifications
You must be signed in to change notification settings - Fork 5
/
TryTest.groovy
285 lines (229 loc) · 7.71 KB
/
TryTest.groovy
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.utils
import edu.ie3.datamodel.exceptions.FailureException
import edu.ie3.datamodel.exceptions.SourceException
import edu.ie3.datamodel.exceptions.TryException
import spock.lang.Specification
class TryTest extends Specification {
def "A method can be applied to a try object"() {
when:
Try<String, Exception> actual = Try.of(() -> "success", Exception)
then:
actual.success
actual.data() == "success"
}
def "A failing method can be applied to a try object"() {
when:
Try<Void, SourceException> actual = Try.of(() -> {
throw new SourceException("Exception thrown.")
}, SourceException)
then:
actual.failure
actual.exception().class == SourceException
actual.exception().message == "Exception thrown."
}
def "A failure is returned if an expected exception type is thrown when using #of()"() {
when:
def exception = new SourceException("source exception")
Try<Void, SourceException> actual = Try.of(() -> {
throw exception
}, SourceException)
then:
actual.failure
actual.exception.get() == exception
}
def "A TryException is thrown if an unexpected exception type is thrown when using #of()"() {
when:
Try.of(() -> {
throw new SourceException("source exception")
}, FailureException)
then:
Exception ex = thrown()
ex.class == TryException
ex.message == "Wrongly caught exception: "
Throwable cause = ex.cause
cause.class == SourceException
cause.message == "source exception"
}
def "A failure is returned when using Failure#ofVoid() with an exception"() {
when:
def exception = new SourceException("source exception")
Try<Void, SourceException> actual = Try.Failure.ofVoid(exception)
then:
actual.failure
actual.exception.get() == exception
}
def "A failure is returned when using Failure#of() with an exception"() {
when:
def exception = new SourceException("source exception")
Try<String, SourceException> actual = Try.Failure.of(exception)
then:
actual.failure
actual.exception.get() == exception
}
def "A failure is returned when using Failure#of() with a failure"() {
when:
def exception = new SourceException("source exception")
Try<Void, SourceException> actual = Try.Failure.of(new Try.Failure<String,SourceException>(exception))
then:
actual.failure
actual.exception.get() == exception
}
def "A failure is returned if an expected exception type is thrown when using Try#ofVoid()"() {
when:
def exception = new SourceException("source exception")
Try<Void, SourceException> actual = Try.ofVoid(() -> {
throw exception
}, SourceException)
then:
actual.failure
actual.exception.get() == exception
}
def "A TryException is thrown if an unexpected exception type is thrown when using Try#ofVoid()"() {
when:
Try.ofVoid(() -> {
throw new SourceException("source exception")
}, FailureException)
then:
Exception ex = thrown()
ex.class == TryException
ex.message == "Wrongly caught exception: "
Throwable cause = ex.cause
cause.class == SourceException
cause.message == "source exception"
}
def "A void method can be applied to a try object"() {
when:
Try<Void, Exception> actual = Try.ofVoid(() -> null, Exception)
then:
actual.success
actual.empty
actual.data.empty
}
def "A success object can be resolved with get method"() {
given:
Try<String, Exception> success = new Try.Success<>("success")
when:
String str = success.get()
then:
str == "success"
}
def "A failure object can be resolved with get method"() {
given:
Try<String, Exception> failure = new Try.Failure<>(new Exception("failure"))
when:
Exception ex = failure.get()
then:
ex.message == "failure"
}
def "An empty Success should work as expected"() {
given:
Try<Void, Exception> empty = Try.Success.empty() as Try<Void, Exception>
expect:
empty.success
empty.data == Optional.empty()
empty.empty
}
def "A scan for exceptions should work as expected when failures are included"() {
given:
Set<Try<String, Exception>> set = Set.of(
new Try.Success<>("one"),
new Try.Failure<>(new Exception("exception")),
new Try.Success<>("two"),
new Try.Success<>("three")
)
when:
Try<Set<String>, Exception> scan = Try.scanCollection(set, String)
then:
scan.failure
scan.exception().message == "1 exception(s) occurred within \"String\" data, one is: java.lang.Exception: exception"
}
def "A scan for exceptions should work as expected when no failures are included"() {
given:
Set<Try<String, Exception>> set = Set.of(
new Try.Success<>("one"),
new Try.Success<>("two"),
new Try.Success<>("three")
)
when:
Try<Set<String>, Exception> scan = Try.scanCollection(set, String)
then:
scan.success
scan.data().size() == 3
}
def "The getOrThrow method should work as expected"() {
given:
Try<String, SourceException> failure = new Try.Failure<>(new SourceException("source exception"))
when:
failure.orThrow
then:
Exception ex = thrown()
ex.class == SourceException
ex.message == "source exception"
}
def "The getOrElse method should work as expected"() {
given:
Try<String, Exception> success = new Try.Success<>("success")
Try<String, SourceException> failure = new Try.Failure<>(new SourceException("exception"))
when:
String successResult = success.getOrElse("else")
String failureResult = failure.getOrElse("else")
then:
successResult == "success"
failureResult == "else"
}
def "A Try objects transformation should work as correctly for successes"() {
given:
Try<String, Exception> success = new Try.Success<>("5")
when:
Try<Integer, Exception> first = success.transformS(str -> Integer.parseInt(str) )
Try<Integer, Exception> second = success.transform(str -> Integer.parseInt(str), ex -> new Exception(ex) )
then:
first.success
second.success
first.data() == 5
second.data() == 5
}
def "A Try objects transformation should work as correctly for failures"() {
given:
Try<String, Exception> failure = new Try.Failure<>(new SourceException(""))
when:
Try<Integer, Exception> first = failure.transformS(str -> Integer.parseInt(str) )
Try<Integer, Exception> second = failure.transform(str -> Integer.parseInt(str), ex -> new Exception(ex) )
then:
first.failure
second.failure
first.exception().class == SourceException
second.exception().class == Exception
}
def "All exceptions of a collection of try objects should be returned"() {
given:
List<Try<String, Exception>> tries = List.of(
new Try.Success<>("one"),
new Try.Failure<>(new SourceException("source exception")),
new Try.Failure<>(new UnsupportedOperationException("unsupported operation exception")),
new Try.Success<>("two"),
new Try.Failure<>(new SourceException("source exception 2"))
)
when:
List<Exception> exceptions = Try.getExceptions(tries)
then:
exceptions.size() == 3
exceptions.get(0).with {
assert it.class == SourceException
assert it.message == "source exception"
}
exceptions.get(1).with {
assert it.class == UnsupportedOperationException
assert it.message == "unsupported operation exception"
}
exceptions.get(2).with {
assert it.class == SourceException
assert it.message == "source exception 2"
}
}
}