blob: 7efb0392f5eea7a6bef67fdbbd709cc07c007e66 (
plain) (
blame)
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.util
import org.scalatest.junit.{AssertionsForJUnit, JUnitSuite}
import org.junit.Test
import IO.using
import java.io.Closeable
import org.scalatest.Matchers
/**
* @author tonytv
*/
class IOTest extends JUnitSuite with AssertionsForJUnit with Matchers {
class ClosingException extends RuntimeException
class FunctionException extends RuntimeException
object throwWhenClosingResource extends Closeable {
def close() {
throw new ClosingException()
}
}
def throwFunction(r : throwWhenClosingResource.type) = throw new FunctionException
def nonThrowingFunction(r : throwWhenClosingResource.type) = 42
@Test
def require_that_function_exception_is_prioritized_over_closing_exception() {
intercept[FunctionException]{
using(throwWhenClosingResource, readOnly = false)(throwFunction)
}
}
@Test
def require_that_closing_exception_is_ignored_when_read_only() {
using(throwWhenClosingResource, readOnly = true)(nonThrowingFunction) should be (nonThrowingFunction(null))
}
@Test
def require_that_closing_exception_is_rethrown_when_not_read_only() {
intercept[ClosingException] {
using(throwWhenClosingResource, readOnly = false)(nonThrowingFunction)
}
}
}
|