-
Notifications
You must be signed in to change notification settings - Fork 5
/
TryTest.groovy
183 lines (146 loc) · 4.96 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
/*
* © 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 org.codehaus.groovy.runtime.typehandling.GroovyCastException
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")
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.")
})
then:
actual.failure
actual.exception().class == SourceException
actual.exception().message == "Exception thrown."
}
def "A CastException is thrown if a wrong exception type is set"() {
when:
Try<String, FailureException> actual = Try.of(() -> {
throw new SourceException("")
})
FailureException failureException = actual.exception()
then:
Exception ex = thrown()
ex.class == GroovyCastException
ex.message == "Cannot cast object 'edu.ie3.datamodel.exceptions.SourceException: ' with class 'edu.ie3.datamodel.exceptions.SourceException' to class 'edu.ie3.datamodel.exceptions.FailureException'"
}
def "A void method can be applied to a try object"() {
when:
Try<Void, Exception> actual = Try.ofVoid(() -> null)
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"
success.get() == "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"() {
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: exception"
}
def "The getOrThrow method should work as expected"() {
given:
Try<String, SourceException> failure = new Try.Failure<>(new SourceException("source exception"))
when:
failure.getOrThrow()
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 failures"() {
given:
Try<String, Exception> failure = new Try.Failure<>(new SourceException(""))
when:
Try<Integer, Exception> first = failure.transform( 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"
}
}
}