コード例 #1
0
  @Test
  public final void testAutoClose() throws Exception {

    final MutableBoolean closeCalled = new MutableBoolean(false);
    assertFalse(closeCalled.booleanValue());
    assertTrue(AutoCloseable.class.isAssignableFrom(TestInputStream.class));

    // read the input stream
    // @NEW
    try (TestInputStream in = new TestInputStream(closeCalled)) {
      IOUtils.toByteArray(in);
    }

    // assert that the close method is called automatically
    assertTrue(closeCalled.booleanValue());
  }
コード例 #2
0
  @Test
  public final void testAutoCloseTwoResources() throws Exception {

    final MutableBoolean close1Called = new MutableBoolean(false);
    final MutableBoolean close2Called = new MutableBoolean(false);

    // read the input streams
    // @NEW
    try (TestInputStream in1 = new TestInputStream(close1Called);
        TestInputStream in2 = new TestInputStream(close2Called)) {
      IOUtils.toByteArray(in1);
      IOUtils.toByteArray(in2);
    }

    // assert that the close methods are called automatically
    assertTrue(close1Called.booleanValue());
    assertTrue(close2Called.booleanValue());
  }