608N/A/*
608N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
608N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
608N/A *
608N/A * This code is free software; you can redistribute it and/or modify it
608N/A * under the terms of the GNU General Public License version 2 only, as
608N/A * published by the Free Software Foundation.
608N/A *
608N/A * This code is distributed in the hope that it will be useful, but WITHOUT
608N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
608N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
608N/A * version 2 for more details (a copy is included in the LICENSE file that
608N/A * accompanied this code).
608N/A *
608N/A * You should have received a copy of the GNU General Public License version
608N/A * 2 along with this work; if not, write to the Free Software Foundation,
608N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
608N/A *
608N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
608N/A * or visit www.oracle.com if you need additional information or have any
608N/A * questions.
608N/A */
608N/A
608N/A/*
608N/A * @test
608N/A * @bug 6911256 6964740
608N/A * @summary Tests of generated TWR code.
608N/A */
608N/A
608N/Aimport java.util.List;
608N/Aimport java.util.ArrayList;
608N/A
608N/Apublic class TwrTests {
608N/A public static void main(String[] args) {
608N/A testCreateFailure1();
608N/A testCreateFailure2();
608N/A testCreateFailure2Nested();
608N/A testCreateFailure3();
608N/A testCreateFailure3Nested();
608N/A testCreateFailure4();
608N/A testCreateFailure4Nested();
608N/A testCreateFailure5();
608N/A testCreateFailure5Nested();
608N/A
608N/A testCreateSuccess1();
608N/A testCreateSuccess2();
608N/A testCreateSuccess2Nested();
608N/A testCreateSuccess3();
608N/A testCreateSuccess3Nested();
608N/A testCreateSuccess4();
608N/A testCreateSuccess4Nested();
608N/A testCreateSuccess5();
608N/A testCreateSuccess5Nested();
608N/A }
608N/A
608N/A /*
608N/A * The following tests simulate a creation failure of every possible
608N/A * resource in an TWR block, and check to make sure that the failure
608N/A * prevents creation of subsequent resources, and that all created
608N/A * resources are properly closed, even if one or more of the close
608N/A * attempts fails.
608N/A */
608N/A
608N/A public static void testCreateFailure1() {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>(0);
608N/A try (Resource r0 = createResource(0, 0, 0, closedList)) {
608N/A throw new AssertionError("Resource creation succeeded");
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A if (e.resourceId() != 0) {
608N/A throw new AssertionError("Wrong resource creation "
608N/A + e.resourceId() + " failed");
608N/A }
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Unexpected CloseFailException: " + e.resourceId());
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, 0);
608N/A }
608N/A
608N/A public static void testCreateFailure2() {
608N/A for (int createFailureId = 0; createFailureId < 2; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
608N/A Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateFailure2Nested() {
608N/A for (int createFailureId = 0; createFailureId < 2; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
608N/A try(Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateFailure3() {
608N/A for (int createFailureId = 0; createFailureId < 3; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
608N/A Resource r1 = createResource(1, createFailureId, bitMap, closedList);
608N/A Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed:" + e);
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateFailure3Nested() {
608N/A for (int createFailureId = 0; createFailureId < 3; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
608N/A try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
608N/A try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A }
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed:" + e);
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateFailure4() {
608N/A for (int createFailureId = 0; createFailureId < 4; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
608N/A Resource r1 = createResource(1, createFailureId, bitMap, closedList);
608N/A Resource r2 = createResource(2, createFailureId, bitMap, closedList);
608N/A Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed:" + e);
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateFailure4Nested() {
608N/A for (int createFailureId = 0; createFailureId < 4; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
608N/A try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
608N/A try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
608N/A try (Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A }
608N/A }
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed:" + e);
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateFailure5() {
608N/A for (int createFailureId = 0; createFailureId < 5; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
608N/A Resource r1 = createResource(1, createFailureId, bitMap, closedList);
608N/A Resource r2 = createResource(2, createFailureId, bitMap, closedList);
608N/A Resource r3 = createResource(3, createFailureId, bitMap, closedList);
608N/A Resource r4 = createResource(4, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed:" + e);
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateFailure5Nested() {
608N/A for (int createFailureId = 0; createFailureId < 5; createFailureId++) {
608N/A for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
608N/A int creationFailuresDetected = 0;
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
608N/A try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
608N/A try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
608N/A try (Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
608N/A try (Resource r4 = createResource(4, createFailureId, bitMap, closedList)) {
608N/A throw new AssertionError("Entire resource creation succeeded");
608N/A }
608N/A }
608N/A }
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A creationFailuresDetected++;
608N/A checkCreateFailureId(e.resourceId(), createFailureId);
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A throw new AssertionError("Secondary exception suppression failed:" + e);
608N/A }
608N/A checkForSingleCreationFailure(creationFailuresDetected);
608N/A checkClosedList(closedList, createFailureId);
608N/A }
608N/A }
608N/A }
608N/A
608N/A /**
608N/A * Create a resource with the specified ID. The ID must be less than createFailureId.
608N/A * A subsequent attempt to close the resource will fail iff the corresponding bit
608N/A * is set in closeFailureBitMap. When an attempt is made to close this resource,
608N/A * its ID will be added to closedList, regardless of whether the attempt succeeds.
608N/A *
608N/A * @param id the ID of this resource
608N/A * @param createFailureId the ID of the resource whose creation will fail
608N/A * @param closeFailureBitMap a bit vector describing which resources should throw an
608N/A * exception when close is attempted
608N/A * @param closedList a list on which to record resource close attempts
608N/A * @throws AssertionError if no attempt should be made to create this resource
608N/A */
608N/A private static Resource createResource(int id,
608N/A int createFailureId,
608N/A int closeFailureBitMap,
608N/A List<Integer> closedList) throws Resource.CreateFailException {
608N/A if (id > createFailureId)
608N/A throw new AssertionError("Resource " + id + " shouldn't be created");
608N/A boolean createSucceeds = id != createFailureId;
608N/A boolean closeSucceeds = (closeFailureBitMap & (1 << id)) == 0;
608N/A return new Resource(id, createSucceeds, closeSucceeds, closedList);
608N/A }
608N/A
608N/A
608N/A /**
608N/A * Check that an observed creation failure has the expected resource ID.
608N/A *
608N/A * @param foundId the ID of the resource whose creation failed
608N/A * @param expectedId the ID of the resource whose creation should have failed
608N/A */
608N/A private static void checkCreateFailureId(int foundId, int expectedId) {
608N/A if (foundId != expectedId)
608N/A throw new AssertionError("Wrong resource creation failed. Found ID "
608N/A + foundId + " expected " + expectedId);
608N/A }
608N/A
608N/A /**
608N/A * Check for proper suppressed exceptions in proper order.
608N/A *
608N/A * @param suppressedExceptions the suppressed exceptions array returned by
744N/A * getSuppressed()
608N/A * @bitmap a bitmap indicating which suppressed exceptions are expected.
608N/A * Bit i is set iff id should throw a CloseFailException.
608N/A */
608N/A private static void checkSuppressedExceptions(Throwable[] suppressedExceptions, int bitMap) {
608N/A if (suppressedExceptions.length != Integer.bitCount(bitMap))
608N/A throw new AssertionError("Expected " + Integer.bitCount(bitMap)
608N/A + " suppressed exceptions, got " + suppressedExceptions.length);
608N/A
608N/A int prevCloseFailExceptionId = Integer.MAX_VALUE;
608N/A for (Throwable t : suppressedExceptions) {
608N/A int id = ((Resource.CloseFailException) t).resourceId();
608N/A if ((1 << id & bitMap) == 0)
608N/A throw new AssertionError("Unexpected suppressed CloseFailException: " + id);
608N/A if (id > prevCloseFailExceptionId)
608N/A throw new AssertionError("Suppressed CloseFailException" + id
608N/A + " followed " + prevCloseFailExceptionId);
608N/A }
608N/A }
608N/A
608N/A /**
608N/A * Check that exactly one resource creation failed.
608N/A *
608N/A * @param numCreationFailuresDetected the number of creation failures detected
608N/A */
608N/A private static void checkForSingleCreationFailure(int numCreationFailuresDetected) {
608N/A if (numCreationFailuresDetected != 1)
608N/A throw new AssertionError("Wrong number of creation failures: "
608N/A + numCreationFailuresDetected);
608N/A }
608N/A
608N/A /**
608N/A * Check that a close was attempted on every resourced that was successfully opened,
608N/A * and that the close attempts occurred in the proper order.
608N/A *
608N/A * @param closedList the resource IDs of the close attempts, in the order they occurred
608N/A * @param the ID of the resource whose creation failed. Close attempts should occur
608N/A * for all previous resources, in reverse order.
608N/A */
608N/A private static void checkClosedList(List<Integer> closedList, int createFailureId) {
608N/A List<Integer> expectedList = new ArrayList<Integer>(createFailureId);
608N/A for (int i = createFailureId - 1; i >= 0; i--)
608N/A expectedList.add(i);
608N/A if (!closedList.equals(expectedList))
608N/A throw new AssertionError("Closing sequence " + closedList + " != " + expectedList);
608N/A }
608N/A
608N/A /*
608N/A * The following tests simulate the creation of several resources, followed
608N/A * by success or failure of forward processing. They test that all resources
608N/A * are properly closed, even if one or more of the close attempts fails.
608N/A */
608N/A
608N/A public static void testCreateSuccess1() {
608N/A for (int bitMap = 0, n = 1 << 1; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError(
608N/A "Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 1);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess2() {
608N/A for (int bitMap = 0, n = 1 << 2; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList);
608N/A Resource r1 = createResource(1, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError(
608N/A "Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 2);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess2Nested() {
608N/A for (int bitMap = 0, n = 1 << 2; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList)) {
608N/A try (Resource r1 = createResource(1, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError(
608N/A "Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 2);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess3() {
608N/A for (int bitMap = 0, n = 1 << 3; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList);
608N/A Resource r1 = createResource(1, bitMap, closedList);
608N/A Resource r2 = createResource(2, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError(
608N/A "Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 3);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess3Nested() {
608N/A for (int bitMap = 0, n = 1 << 3; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList)) {
608N/A try (Resource r1 = createResource(1, bitMap, closedList)) {
608N/A try (Resource r2 = createResource(2, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A }
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError(
608N/A "Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 3);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess4() {
608N/A for (int bitMap = 0, n = 1 << 4; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList);
608N/A Resource r1 = createResource(1, bitMap, closedList);
608N/A Resource r2 = createResource(2, bitMap, closedList);
608N/A Resource r3 = createResource(3, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError(
608N/A "Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 4);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess4Nested() {
608N/A for (int bitMap = 0, n = 1 << 4; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList)) {
608N/A try (Resource r1 = createResource(1, bitMap, closedList)) {
608N/A try (Resource r2 = createResource(2, bitMap, closedList)) {
608N/A try (Resource r3 = createResource(3, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A }
608N/A }
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError(
608N/A "Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 4);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess5() {
608N/A for (int bitMap = 0, n = 1 << 5; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList);
608N/A Resource r1 = createResource(1, bitMap, closedList);
608N/A Resource r2 = createResource(2, bitMap, closedList);
608N/A Resource r3 = createResource(3, bitMap, closedList);
608N/A Resource r4 = createResource(4, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError("Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 5);
608N/A }
608N/A }
608N/A }
608N/A
608N/A public static void testCreateSuccess5Nested() {
608N/A for (int bitMap = 0, n = 1 << 5; bitMap < n; bitMap++) {
608N/A for (int failure = 0; failure < 2; failure++) {
608N/A List<Integer> closedList = new ArrayList<Integer>();
608N/A try (Resource r0 = createResource(0, bitMap, closedList)) {
608N/A try (Resource r1 = createResource(1, bitMap, closedList)) {
608N/A try (Resource r2 = createResource(2, bitMap, closedList)) {
608N/A try (Resource r3 = createResource(3, bitMap, closedList)) {
608N/A try (Resource r4 = createResource(4, bitMap, closedList)) {
608N/A if (failure != 0)
608N/A throw new MyKindOfException();
608N/A }
608N/A }
608N/A }
608N/A }
608N/A } catch (Resource.CreateFailException e) {
608N/A throw new AssertionError("Resource creation failed: " + e.resourceId());
608N/A } catch (MyKindOfException e) {
608N/A if (failure == 0)
608N/A throw new AssertionError("Unexpected MyKindOfException");
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap);
608N/A } catch (Resource.CloseFailException e) {
608N/A if (failure == 1)
608N/A throw new AssertionError("Secondary exception suppression failed");
608N/A int id = e.resourceId();
608N/A if (bitMap == 0)
608N/A throw new AssertionError("Unexpected CloseFailException: " + id);
608N/A int highestCloseFailBit = Integer.highestOneBit(bitMap);
608N/A if (1 << id != highestCloseFailBit) {
608N/A throw new AssertionError("CloseFailException: got id " + id
608N/A + ", expected lg(" + highestCloseFailBit +")");
608N/A }
744N/A checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
608N/A }
608N/A checkClosedList(closedList, 5);
608N/A }
608N/A }
608N/A }
608N/A
608N/A private static Resource createResource(int id,
608N/A int closeFailureBitMap,
608N/A List<Integer> closedList) throws Resource.CreateFailException {
608N/A boolean closeSucceeds = (closeFailureBitMap & (1 << id)) == 0;
608N/A return new Resource(id, true, closeSucceeds, closedList);
608N/A }
608N/A
608N/A private static class MyKindOfException extends Exception {
608N/A }
608N/A}
608N/A
608N/Aclass Resource implements AutoCloseable {
608N/A /** A number identifying this resource */
608N/A private final int resourceId;
608N/A
608N/A /** Whether the close call on this resource should succeed or fail */
608N/A private final boolean closeSucceeds;
608N/A
608N/A /** When resource is closed, it records its ID in this list */
608N/A private final List<Integer> closedList;
608N/A
608N/A Resource(int resourceId, boolean createSucceeds, boolean closeSucceeds,
608N/A List<Integer> closedList) throws CreateFailException {
608N/A if (!createSucceeds)
608N/A throw new CreateFailException(resourceId);
608N/A this.resourceId = resourceId;
608N/A this.closeSucceeds = closeSucceeds;
608N/A this.closedList = closedList;
608N/A }
608N/A
608N/A public void close() throws CloseFailException {
608N/A closedList.add(resourceId);
608N/A if (!closeSucceeds)
608N/A throw new CloseFailException(resourceId);
608N/A }
608N/A
608N/A public static class ResourceException extends RuntimeException {
608N/A private final int resourceId;
608N/A
608N/A public ResourceException(int resourceId) {
608N/A super("Resource ID = " + resourceId);
608N/A this.resourceId = resourceId;
608N/A }
608N/A
608N/A public int resourceId() {
608N/A return resourceId;
608N/A }
608N/A }
608N/A
608N/A public static class CreateFailException extends ResourceException {
608N/A public CreateFailException(int resourceId) {
608N/A super(resourceId);
608N/A }
608N/A }
608N/A
608N/A public static class CloseFailException extends ResourceException {
608N/A public CloseFailException(int resourceId) {
608N/A super(resourceId);
608N/A }
608N/A }
608N/A}