summaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/test/scala/com/yahoo/container/plugin/util/IOTest.scala
blob: 16f4f5237f103936e2159235ea0c307b13b24914 (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
// Copyright 2016 Yahoo Inc. 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.{JUnitSuite, ShouldMatchersForJUnit}
import org.junit.Test

import IO.using
import java.io.Closeable

/**
 * @author  tonytv
 */
class IOTest extends JUnitSuite with ShouldMatchersForJUnit  {
  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)
    }
  }
}