883N/A/*
883N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
883N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
883N/A *
883N/A * This code is free software; you can redistribute it and/or modify it
883N/A * under the terms of the GNU General Public License version 2 only, as
883N/A * published by the Free Software Foundation.
883N/A *
883N/A * This code is distributed in the hope that it will be useful, but WITHOUT
883N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
883N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
883N/A * version 2 for more details (a copy is included in the LICENSE file that
883N/A * accompanied this code).
883N/A *
883N/A * You should have received a copy of the GNU General Public License version
883N/A * 2 along with this work; if not, write to the Free Software Foundation,
883N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
883N/A *
883N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
883N/A * or visit www.oracle.com if you need additional information or have any
883N/A * questions.
883N/A */
883N/A
883N/A/*
883N/A * @test
883N/A * @bug 7020047
883N/A * @summary Test null handling of try-with-resources statement
883N/A */
883N/A
883N/Apublic class TwrNullTests {
883N/A /*
883N/A * Each try-with-resources statement generates two calls to the
883N/A * close method for each resource: one for when there is a primary
883N/A * exception present and the second for when a primary exception
883N/A * is absent. The null handling of both cases needs to be
883N/A * checked.
883N/A */
883N/A public static void main(String... args) {
883N/A testNormalCompletion();
883N/A testNoSuppression();
883N/A }
883N/A
883N/A /*
883N/A * Verify empty try-with-resources on a null resource completes
883N/A * normally; no NPE from the generated close call.
883N/A */
883N/A private static void testNormalCompletion() {
883N/A try(AutoCloseable resource = null) {
883N/A return; // Nothing to see here, move along.
883N/A } catch (Exception e) {
883N/A throw new AssertionError("Should not be reached", e);
883N/A }
883N/A }
883N/A
883N/A /*
883N/A * Verify that a NPE on a null resource is <em>not</em> added as a
883N/A * suppressed exception to an exception from try block.
883N/A */
883N/A private static void testNoSuppression() {
883N/A try(AutoCloseable resource = null) {
883N/A throw new java.io.IOException();
883N/A } catch(java.io.IOException ioe) {
883N/A Throwable[] suppressed = ioe.getSuppressed();
883N/A if (suppressed.length != 0) {
883N/A throw new AssertionError("Non-empty suppressed exceptions",
883N/A ioe);
883N/A }
883N/A } catch (Exception e) {
883N/A throw new AssertionError("Should not be reached", e);
883N/A }
883N/A }
883N/A}