25
25
import static com .jnape .palatable .lambda .adt .Try .failure ;
26
26
import static com .jnape .palatable .lambda .adt .Try .success ;
27
27
import static com .jnape .palatable .lambda .adt .Try .trying ;
28
+ import static com .jnape .palatable .lambda .adt .Try .withResources ;
28
29
import static com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly .constantly ;
29
30
import static com .jnape .palatable .lambda .functions .builtin .fn1 .Id .id ;
30
31
import static com .jnape .palatable .lambda .functor .builtin .Lazy .lazy ;
@@ -173,38 +174,38 @@ public void tryingCatchesAnyThrowableThrownDuringEvaluation() {
173
174
@ Test
174
175
public void withResourcesCleansUpAutoCloseableInSuccessCase () {
175
176
AtomicBoolean closed = new AtomicBoolean (false );
176
- assertEquals (success (1 ), Try . withResources (() -> () -> closed .set (true ), resource -> success (1 )));
177
+ assertEquals (success (1 ), withResources (() -> ( AutoCloseable ) () -> closed .set (true ), resource -> success (1 )));
177
178
assertTrue (closed .get ());
178
179
}
179
180
180
181
@ Test
181
182
public void withResourcesCleansUpAutoCloseableInFailureCase () {
182
183
AtomicBoolean closed = new AtomicBoolean (false );
183
184
RuntimeException exception = new RuntimeException ();
184
- assertEquals (Try .failure (exception ), Try . withResources (() -> () -> closed .set (true ),
185
- resource -> { throw exception ; }));
185
+ assertEquals (Try .failure (exception ), withResources (() -> ( AutoCloseable ) () -> closed .set (true ),
186
+ resource -> { throw exception ; }));
186
187
assertTrue (closed .get ());
187
188
}
188
189
189
190
@ Test
190
191
public void withResourcesExposesResourceCreationFailure () {
191
192
IOException ioException = new IOException ();
192
- assertEquals (Try .failure (ioException ), Try . withResources (() -> { throw ioException ; }, resource -> success (1 )));
193
+ assertEquals (Try .failure (ioException ), withResources (() -> { throw ioException ; }, resource -> success (1 )));
193
194
}
194
195
195
196
@ Test
196
197
public void withResourcesExposesResourceCloseFailure () {
197
198
IOException ioException = new IOException ();
198
- assertEquals (Try .failure (ioException ), Try . withResources (() -> () -> { throw ioException ; },
199
- resource -> success (1 )));
199
+ assertEquals (Try .failure (ioException ), withResources (() -> ( AutoCloseable ) () -> { throw ioException ; },
200
+ resource -> success (1 )));
200
201
}
201
202
202
203
@ Test
203
204
public void withResourcesPreservesSuppressedExceptionThrownDuringClose () {
204
205
RuntimeException rootException = new RuntimeException ();
205
206
IOException nestedIOException = new IOException ();
206
- Try <Throwable > failure = Try . withResources (() -> () -> { throw nestedIOException ; },
207
- resource -> { throw rootException ; });
207
+ Try <Throwable > failure = withResources (() -> ( AutoCloseable ) () -> { throw nestedIOException ; },
208
+ resource -> { throw rootException ; });
208
209
Throwable thrown = failure .recover (id ());
209
210
210
211
assertEquals (thrown , rootException );
@@ -214,10 +215,10 @@ public void withResourcesPreservesSuppressedExceptionThrownDuringClose() {
214
215
@ Test
215
216
public void cascadingWithResourcesClosesInInverseOrder () {
216
217
List <String > closeMessages = new ArrayList <>();
217
- assertEquals (success (1 ), Try . withResources (() -> (AutoCloseable ) () -> closeMessages .add ("close a" ),
218
- a -> () -> closeMessages .add ("close b" ),
219
- b -> () -> closeMessages .add ("close c" ),
220
- c -> success (1 )));
218
+ assertEquals (success (1 ), withResources (() -> (AutoCloseable ) () -> closeMessages .add ("close a" ),
219
+ a -> () -> closeMessages .add ("close b" ),
220
+ b -> () -> closeMessages .add ("close c" ),
221
+ c -> success (1 )));
221
222
assertEquals (asList ("close c" , "close b" , "close a" ), closeMessages );
222
223
}
223
224
0 commit comments