test/tools/javac/TryWithResources/TwrTests.java

changeset 609
13354e1abba7
child 745
4328728e0409
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/TryWithResources/TwrTests.java	Fri Jul 16 19:35:24 2010 -0700
     1.3 @@ -0,0 +1,742 @@
     1.4 +/*
     1.5 + * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6911256 6964740
    1.30 + * @summary Tests of generated TWR code.
    1.31 + */
    1.32 +
    1.33 +import java.util.List;
    1.34 +import java.util.ArrayList;
    1.35 +
    1.36 +public class TwrTests {
    1.37 +    public static void main(String[] args) {
    1.38 +        testCreateFailure1();
    1.39 +        testCreateFailure2();
    1.40 +        testCreateFailure2Nested();
    1.41 +        testCreateFailure3();
    1.42 +        testCreateFailure3Nested();
    1.43 +        testCreateFailure4();
    1.44 +        testCreateFailure4Nested();
    1.45 +        testCreateFailure5();
    1.46 +        testCreateFailure5Nested();
    1.47 +
    1.48 +        testCreateSuccess1();
    1.49 +        testCreateSuccess2();
    1.50 +        testCreateSuccess2Nested();
    1.51 +        testCreateSuccess3();
    1.52 +        testCreateSuccess3Nested();
    1.53 +        testCreateSuccess4();
    1.54 +        testCreateSuccess4Nested();
    1.55 +        testCreateSuccess5();
    1.56 +        testCreateSuccess5Nested();
    1.57 +    }
    1.58 +
    1.59 +    /*
    1.60 +     * The following tests simulate a creation failure of every possible
    1.61 +     * resource in an TWR block, and check to make sure that the failure
    1.62 +     * prevents creation of subsequent resources, and that all created
    1.63 +     * resources are properly closed, even if one or more of the close
    1.64 +     * attempts fails.
    1.65 +     */
    1.66 +
    1.67 +    public static void testCreateFailure1() {
    1.68 +        int creationFailuresDetected = 0;
    1.69 +        List<Integer> closedList = new ArrayList<Integer>(0);
    1.70 +        try (Resource r0 = createResource(0, 0, 0, closedList)) {
    1.71 +            throw new AssertionError("Resource creation succeeded");
    1.72 +        } catch (Resource.CreateFailException e) {
    1.73 +            creationFailuresDetected++;
    1.74 +            if (e.resourceId() != 0) {
    1.75 +                throw new AssertionError("Wrong resource creation "
    1.76 +                                         + e.resourceId() + " failed");
    1.77 +            }
    1.78 +        } catch (Resource.CloseFailException e) {
    1.79 +            throw new AssertionError("Unexpected CloseFailException: " + e.resourceId());
    1.80 +        }
    1.81 +        checkForSingleCreationFailure(creationFailuresDetected);
    1.82 +        checkClosedList(closedList, 0);
    1.83 +    }
    1.84 +
    1.85 +    public static void testCreateFailure2() {
    1.86 +        for (int createFailureId = 0; createFailureId < 2; createFailureId++) {
    1.87 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
    1.88 +                int creationFailuresDetected = 0;
    1.89 +                List<Integer> closedList = new ArrayList<Integer>();
    1.90 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
    1.91 +                     Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
    1.92 +                    throw new AssertionError("Entire resource creation succeeded");
    1.93 +                } catch (Resource.CreateFailException e) {
    1.94 +                    creationFailuresDetected++;
    1.95 +                    checkCreateFailureId(e.resourceId(), createFailureId);
    1.96 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
    1.97 +                } catch (Resource.CloseFailException e) {
    1.98 +                    throw new AssertionError("Secondary exception suppression failed");
    1.99 +                }
   1.100 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.101 +                checkClosedList(closedList, createFailureId);
   1.102 +            }
   1.103 +        }
   1.104 +    }
   1.105 +
   1.106 +    public static void testCreateFailure2Nested() {
   1.107 +        for (int createFailureId = 0; createFailureId < 2; createFailureId++) {
   1.108 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
   1.109 +                int creationFailuresDetected = 0;
   1.110 +                List<Integer> closedList = new ArrayList<Integer>();
   1.111 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
   1.112 +                    try(Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
   1.113 +                        throw new AssertionError("Entire resource creation succeeded");
   1.114 +                    }
   1.115 +                } catch (Resource.CreateFailException e) {
   1.116 +                    creationFailuresDetected++;
   1.117 +                    checkCreateFailureId(e.resourceId(), createFailureId);
   1.118 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.119 +                } catch (Resource.CloseFailException e) {
   1.120 +                    throw new AssertionError("Secondary exception suppression failed");
   1.121 +                }
   1.122 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.123 +                checkClosedList(closedList, createFailureId);
   1.124 +            }
   1.125 +        }
   1.126 +    }
   1.127 +
   1.128 +    public static void testCreateFailure3() {
   1.129 +        for (int createFailureId = 0; createFailureId < 3; createFailureId++) {
   1.130 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
   1.131 +                int creationFailuresDetected = 0;
   1.132 +                List<Integer> closedList = new ArrayList<Integer>();
   1.133 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
   1.134 +                     Resource r1 = createResource(1, createFailureId, bitMap, closedList);
   1.135 +                     Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
   1.136 +                    throw new AssertionError("Entire resource creation succeeded");
   1.137 +                } catch (Resource.CreateFailException e) {
   1.138 +                    creationFailuresDetected++;
   1.139 +                    checkCreateFailureId(e.resourceId(), createFailureId);
   1.140 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.141 +                } catch (Resource.CloseFailException e) {
   1.142 +                    throw new AssertionError("Secondary exception suppression failed:" + e);
   1.143 +                }
   1.144 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.145 +                checkClosedList(closedList, createFailureId);
   1.146 +            }
   1.147 +        }
   1.148 +    }
   1.149 +
   1.150 +    public static void testCreateFailure3Nested() {
   1.151 +        for (int createFailureId = 0; createFailureId < 3; createFailureId++) {
   1.152 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
   1.153 +                int creationFailuresDetected = 0;
   1.154 +                List<Integer> closedList = new ArrayList<Integer>();
   1.155 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
   1.156 +                    try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
   1.157 +                        try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
   1.158 +                            throw new AssertionError("Entire resource creation succeeded");
   1.159 +                        }
   1.160 +                    }
   1.161 +                } catch (Resource.CreateFailException e) {
   1.162 +                    creationFailuresDetected++;
   1.163 +                    checkCreateFailureId(e.resourceId(), createFailureId);
   1.164 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.165 +                } catch (Resource.CloseFailException e) {
   1.166 +                    throw new AssertionError("Secondary exception suppression failed:" + e);
   1.167 +                }
   1.168 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.169 +                checkClosedList(closedList, createFailureId);
   1.170 +            }
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +    public static void testCreateFailure4() {
   1.175 +        for (int createFailureId = 0; createFailureId < 4; createFailureId++) {
   1.176 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
   1.177 +                int creationFailuresDetected = 0;
   1.178 +                List<Integer> closedList = new ArrayList<Integer>();
   1.179 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
   1.180 +                     Resource r1 = createResource(1, createFailureId, bitMap, closedList);
   1.181 +                     Resource r2 = createResource(2, createFailureId, bitMap, closedList);
   1.182 +                     Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
   1.183 +                    throw new AssertionError("Entire resource creation succeeded");
   1.184 +                } catch (Resource.CreateFailException e) {
   1.185 +                    creationFailuresDetected++;
   1.186 +                    checkCreateFailureId(e.resourceId(), createFailureId);
   1.187 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.188 +                } catch (Resource.CloseFailException e) {
   1.189 +                    throw new AssertionError("Secondary exception suppression failed:" + e);
   1.190 +                }
   1.191 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.192 +                checkClosedList(closedList, createFailureId);
   1.193 +            }
   1.194 +        }
   1.195 +    }
   1.196 +
   1.197 +    public static void testCreateFailure4Nested() {
   1.198 +        for (int createFailureId = 0; createFailureId < 4; createFailureId++) {
   1.199 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
   1.200 +                int creationFailuresDetected = 0;
   1.201 +                List<Integer> closedList = new ArrayList<Integer>();
   1.202 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
   1.203 +                    try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
   1.204 +                        try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
   1.205 +                            try (Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
   1.206 +                                throw new AssertionError("Entire resource creation succeeded");
   1.207 +                            }
   1.208 +                        }
   1.209 +                    }
   1.210 +                } catch (Resource.CreateFailException e) {
   1.211 +                    creationFailuresDetected++;
   1.212 +                    checkCreateFailureId(e.resourceId(), createFailureId);
   1.213 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.214 +                } catch (Resource.CloseFailException e) {
   1.215 +                    throw new AssertionError("Secondary exception suppression failed:" + e);
   1.216 +                }
   1.217 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.218 +                checkClosedList(closedList, createFailureId);
   1.219 +            }
   1.220 +        }
   1.221 +    }
   1.222 +
   1.223 +    public static void testCreateFailure5() {
   1.224 +        for (int createFailureId = 0; createFailureId < 5; createFailureId++) {
   1.225 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
   1.226 +                int creationFailuresDetected = 0;
   1.227 +                List<Integer> closedList = new ArrayList<Integer>();
   1.228 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
   1.229 +                     Resource r1 = createResource(1, createFailureId, bitMap, closedList);
   1.230 +                     Resource r2 = createResource(2, createFailureId, bitMap, closedList);
   1.231 +                     Resource r3 = createResource(3, createFailureId, bitMap, closedList);
   1.232 +                     Resource r4 = createResource(4, createFailureId, bitMap, closedList)) {
   1.233 +                    throw new AssertionError("Entire resource creation succeeded");
   1.234 +                } catch (Resource.CreateFailException e) {
   1.235 +                    creationFailuresDetected++;
   1.236 +                    checkCreateFailureId(e.resourceId(), createFailureId);
   1.237 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.238 +                } catch (Resource.CloseFailException e) {
   1.239 +                    throw new AssertionError("Secondary exception suppression failed:" + e);
   1.240 +                }
   1.241 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.242 +                checkClosedList(closedList, createFailureId);
   1.243 +            }
   1.244 +        }
   1.245 +    }
   1.246 +
   1.247 +    public static void testCreateFailure5Nested() {
   1.248 +        for (int createFailureId = 0; createFailureId < 5; createFailureId++) {
   1.249 +            for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
   1.250 +                int creationFailuresDetected = 0;
   1.251 +                List<Integer> closedList = new ArrayList<Integer>();
   1.252 +                try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
   1.253 +                    try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
   1.254 +                        try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
   1.255 +                            try (Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
   1.256 +                                try (Resource r4 = createResource(4, createFailureId, bitMap, closedList)) {
   1.257 +                                    throw new AssertionError("Entire resource creation succeeded");
   1.258 +                                }
   1.259 +                            }
   1.260 +                        }
   1.261 +                    }
   1.262 +                } catch (Resource.CreateFailException e) {
   1.263 +                    creationFailuresDetected++;
   1.264 +                    checkCreateFailureId(e.resourceId(), createFailureId);
   1.265 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.266 +                } catch (Resource.CloseFailException e) {
   1.267 +                    throw new AssertionError("Secondary exception suppression failed:" + e);
   1.268 +                }
   1.269 +                checkForSingleCreationFailure(creationFailuresDetected);
   1.270 +                checkClosedList(closedList, createFailureId);
   1.271 +            }
   1.272 +        }
   1.273 +    }
   1.274 +
   1.275 +    /**
   1.276 +     * Create a resource with the specified ID. The ID must be less than createFailureId.
   1.277 +     * A subsequent attempt to close the resource will fail iff the corresponding bit
   1.278 +     * is set in closeFailureBitMap.  When an attempt is made to close this resource,
   1.279 +     * its ID will be added to closedList, regardless of whether the attempt succeeds.
   1.280 +     *
   1.281 +     * @param id the ID of this resource
   1.282 +     * @param createFailureId the ID of the resource whose creation will fail
   1.283 +     * @param closeFailureBitMap a bit vector describing which resources should throw an
   1.284 +     *        exception when close is attempted
   1.285 +     * @param closedList a list on which to record resource close attempts
   1.286 +     * @throws AssertionError if no attempt should be made to create this resource
   1.287 +     */
   1.288 +    private static Resource createResource(int id,
   1.289 +                                           int createFailureId,
   1.290 +                                           int closeFailureBitMap,
   1.291 +                                           List<Integer> closedList) throws Resource.CreateFailException {
   1.292 +        if (id > createFailureId)
   1.293 +            throw new AssertionError("Resource " + id + " shouldn't be created");
   1.294 +        boolean createSucceeds = id != createFailureId;
   1.295 +        boolean closeSucceeds = (closeFailureBitMap & (1 << id)) == 0;
   1.296 +        return new Resource(id, createSucceeds, closeSucceeds, closedList);
   1.297 +    }
   1.298 +
   1.299 +
   1.300 +    /**
   1.301 +     * Check that an observed creation failure has the expected resource ID.
   1.302 +     *
   1.303 +     * @param foundId the ID of the resource whose creation failed
   1.304 +     * @param expectedId the ID of the resource whose creation should have failed
   1.305 +     */
   1.306 +    private static void checkCreateFailureId(int foundId, int expectedId) {
   1.307 +        if (foundId != expectedId)
   1.308 +            throw new AssertionError("Wrong resource creation failed. Found ID "
   1.309 +                                     + foundId + " expected " + expectedId);
   1.310 +    }
   1.311 +
   1.312 +    /**
   1.313 +     * Check for proper suppressed exceptions in proper order.
   1.314 +     *
   1.315 +     * @param suppressedExceptions the suppressed exceptions array returned by
   1.316 +     *        getSuppressedExceptions()
   1.317 +     * @bitmap a bitmap indicating which suppressed exceptions are expected.
   1.318 +     *         Bit i is set iff id should throw a CloseFailException.
   1.319 +     */
   1.320 +    private static void checkSuppressedExceptions(Throwable[] suppressedExceptions, int bitMap) {
   1.321 +        if (suppressedExceptions.length != Integer.bitCount(bitMap))
   1.322 +            throw new AssertionError("Expected " + Integer.bitCount(bitMap)
   1.323 +                                     + " suppressed exceptions, got " +  suppressedExceptions.length);
   1.324 +
   1.325 +        int prevCloseFailExceptionId = Integer.MAX_VALUE;
   1.326 +        for (Throwable t : suppressedExceptions) {
   1.327 +            int id = ((Resource.CloseFailException) t).resourceId();
   1.328 +            if ((1 << id  & bitMap) == 0)
   1.329 +                throw new AssertionError("Unexpected suppressed CloseFailException: " + id);
   1.330 +            if (id > prevCloseFailExceptionId)
   1.331 +                throw new AssertionError("Suppressed CloseFailException" + id
   1.332 +                                         + " followed " + prevCloseFailExceptionId);
   1.333 +        }
   1.334 +    }
   1.335 +
   1.336 +    /**
   1.337 +     * Check that exactly one resource creation failed.
   1.338 +     *
   1.339 +     * @param numCreationFailuresDetected the number of creation failures detected
   1.340 +     */
   1.341 +    private static void checkForSingleCreationFailure(int numCreationFailuresDetected) {
   1.342 +        if (numCreationFailuresDetected != 1)
   1.343 +            throw new AssertionError("Wrong number of creation failures: "
   1.344 +                                     + numCreationFailuresDetected);
   1.345 +    }
   1.346 +
   1.347 +    /**
   1.348 +     * Check that a close was attempted on every resourced that was successfully opened,
   1.349 +     * and that the close attempts occurred in the proper order.
   1.350 +     *
   1.351 +     * @param closedList the resource IDs of the close attempts, in the order they occurred
   1.352 +     * @param the ID of the resource whose creation failed.  Close attempts should occur
   1.353 +     *        for all previous resources, in reverse order.
   1.354 +     */
   1.355 +    private static void checkClosedList(List<Integer> closedList, int createFailureId) {
   1.356 +        List<Integer> expectedList = new ArrayList<Integer>(createFailureId);
   1.357 +        for (int i = createFailureId - 1; i >= 0; i--)
   1.358 +            expectedList.add(i);
   1.359 +        if (!closedList.equals(expectedList))
   1.360 +            throw new AssertionError("Closing sequence " + closedList + " != " + expectedList);
   1.361 +    }
   1.362 +
   1.363 +    /*
   1.364 +     * The following tests simulate the creation of several resources, followed
   1.365 +     * by success or failure of forward processing.  They test that all resources
   1.366 +     * are properly closed, even if one or more of the close attempts fails.
   1.367 +     */
   1.368 +
   1.369 +    public static void testCreateSuccess1() {
   1.370 +        for (int bitMap = 0, n = 1 << 1; bitMap < n; bitMap++) {
   1.371 +            for (int failure = 0; failure < 2; failure++) {
   1.372 +                List<Integer> closedList = new ArrayList<Integer>();
   1.373 +                try (Resource r0 = createResource(0, bitMap, closedList)) {
   1.374 +                    if (failure != 0)
   1.375 +                        throw new MyKindOfException();
   1.376 +                } catch (Resource.CreateFailException e) {
   1.377 +                    throw new AssertionError(
   1.378 +                                             "Resource creation failed: " + e.resourceId());
   1.379 +                } catch (MyKindOfException e) {
   1.380 +                    if (failure == 0)
   1.381 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.382 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.383 +                } catch (Resource.CloseFailException e) {
   1.384 +                    if (failure == 1)
   1.385 +                        throw new AssertionError("Secondary exception suppression failed");
   1.386 +                    int id = e.resourceId();
   1.387 +                    if (bitMap == 0)
   1.388 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.389 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.390 +                    if (1 << id != highestCloseFailBit) {
   1.391 +                        throw new AssertionError("CloseFailException: got id " + id
   1.392 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.393 +                    }
   1.394 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.395 +                }
   1.396 +                checkClosedList(closedList, 1);
   1.397 +            }
   1.398 +        }
   1.399 +    }
   1.400 +
   1.401 +    public static void testCreateSuccess2() {
   1.402 +        for (int bitMap = 0, n = 1 << 2; bitMap < n; bitMap++) {
   1.403 +            for (int failure = 0; failure < 2; failure++) {
   1.404 +                List<Integer> closedList = new ArrayList<Integer>();
   1.405 +                try (Resource r0 = createResource(0, bitMap, closedList);
   1.406 +                     Resource r1 = createResource(1, bitMap, closedList)) {
   1.407 +                    if (failure != 0)
   1.408 +                        throw new MyKindOfException();
   1.409 +                } catch (Resource.CreateFailException e) {
   1.410 +                    throw new AssertionError(
   1.411 +                                             "Resource creation failed: " + e.resourceId());
   1.412 +                } catch (MyKindOfException e) {
   1.413 +                    if (failure == 0)
   1.414 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.415 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.416 +                } catch (Resource.CloseFailException e) {
   1.417 +                    if (failure == 1)
   1.418 +                        throw new AssertionError("Secondary exception suppression failed");
   1.419 +                    int id = e.resourceId();
   1.420 +                    if (bitMap == 0)
   1.421 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.422 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.423 +                    if (1 << id != highestCloseFailBit) {
   1.424 +                        throw new AssertionError("CloseFailException: got id " + id
   1.425 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.426 +                    }
   1.427 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.428 +                }
   1.429 +                checkClosedList(closedList, 2);
   1.430 +            }
   1.431 +        }
   1.432 +    }
   1.433 +
   1.434 +    public static void testCreateSuccess2Nested() {
   1.435 +        for (int bitMap = 0, n = 1 << 2; bitMap < n; bitMap++) {
   1.436 +            for (int failure = 0; failure < 2; failure++) {
   1.437 +                List<Integer> closedList = new ArrayList<Integer>();
   1.438 +                try (Resource r0 = createResource(0, bitMap, closedList)) {
   1.439 +                    try (Resource r1 = createResource(1, bitMap, closedList)) {
   1.440 +                        if (failure != 0)
   1.441 +                            throw new MyKindOfException();
   1.442 +                    }
   1.443 +                } catch (Resource.CreateFailException e) {
   1.444 +                    throw new AssertionError(
   1.445 +                                             "Resource creation failed: " + e.resourceId());
   1.446 +                } catch (MyKindOfException e) {
   1.447 +                    if (failure == 0)
   1.448 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.449 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.450 +                } catch (Resource.CloseFailException e) {
   1.451 +                    if (failure == 1)
   1.452 +                        throw new AssertionError("Secondary exception suppression failed");
   1.453 +                    int id = e.resourceId();
   1.454 +                    if (bitMap == 0)
   1.455 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.456 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.457 +                    if (1 << id != highestCloseFailBit) {
   1.458 +                        throw new AssertionError("CloseFailException: got id " + id
   1.459 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.460 +                    }
   1.461 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.462 +                }
   1.463 +                checkClosedList(closedList, 2);
   1.464 +            }
   1.465 +        }
   1.466 +    }
   1.467 +
   1.468 +    public static void testCreateSuccess3() {
   1.469 +        for (int bitMap = 0, n = 1 << 3; bitMap < n; bitMap++) {
   1.470 +            for (int failure = 0; failure < 2; failure++) {
   1.471 +                List<Integer> closedList = new ArrayList<Integer>();
   1.472 +                try (Resource r0 = createResource(0, bitMap, closedList);
   1.473 +                     Resource r1 = createResource(1, bitMap, closedList);
   1.474 +                     Resource r2 = createResource(2, bitMap, closedList)) {
   1.475 +                    if (failure != 0)
   1.476 +                        throw new MyKindOfException();
   1.477 +                } catch (Resource.CreateFailException e) {
   1.478 +                    throw new AssertionError(
   1.479 +                                             "Resource creation failed: " + e.resourceId());
   1.480 +                } catch (MyKindOfException e) {
   1.481 +                    if (failure == 0)
   1.482 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.483 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.484 +                } catch (Resource.CloseFailException e) {
   1.485 +                    if (failure == 1)
   1.486 +                        throw new AssertionError("Secondary exception suppression failed");
   1.487 +                    int id = e.resourceId();
   1.488 +                    if (bitMap == 0)
   1.489 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.490 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.491 +                    if (1 << id != highestCloseFailBit) {
   1.492 +                        throw new AssertionError("CloseFailException: got id " + id
   1.493 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.494 +                    }
   1.495 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.496 +                }
   1.497 +                checkClosedList(closedList, 3);
   1.498 +            }
   1.499 +        }
   1.500 +    }
   1.501 +
   1.502 +    public static void testCreateSuccess3Nested() {
   1.503 +        for (int bitMap = 0, n = 1 << 3; bitMap < n; bitMap++) {
   1.504 +            for (int failure = 0; failure < 2; failure++) {
   1.505 +                List<Integer> closedList = new ArrayList<Integer>();
   1.506 +                try (Resource r0 = createResource(0, bitMap, closedList)) {
   1.507 +                    try (Resource r1 = createResource(1, bitMap, closedList)) {
   1.508 +                        try (Resource r2 = createResource(2, bitMap, closedList)) {
   1.509 +                            if (failure != 0)
   1.510 +                                throw new MyKindOfException();
   1.511 +                        }
   1.512 +                    }
   1.513 +                } catch (Resource.CreateFailException e) {
   1.514 +                    throw new AssertionError(
   1.515 +                                             "Resource creation failed: " + e.resourceId());
   1.516 +                } catch (MyKindOfException e) {
   1.517 +                    if (failure == 0)
   1.518 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.519 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.520 +                } catch (Resource.CloseFailException e) {
   1.521 +                    if (failure == 1)
   1.522 +                        throw new AssertionError("Secondary exception suppression failed");
   1.523 +                    int id = e.resourceId();
   1.524 +                    if (bitMap == 0)
   1.525 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.526 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.527 +                    if (1 << id != highestCloseFailBit) {
   1.528 +                        throw new AssertionError("CloseFailException: got id " + id
   1.529 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.530 +                    }
   1.531 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.532 +                }
   1.533 +                checkClosedList(closedList, 3);
   1.534 +            }
   1.535 +        }
   1.536 +    }
   1.537 +
   1.538 +    public static void testCreateSuccess4() {
   1.539 +        for (int bitMap = 0, n = 1 << 4; bitMap < n; bitMap++) {
   1.540 +            for (int failure = 0; failure < 2; failure++) {
   1.541 +                List<Integer> closedList = new ArrayList<Integer>();
   1.542 +                try (Resource r0 = createResource(0, bitMap, closedList);
   1.543 +                     Resource r1 = createResource(1, bitMap, closedList);
   1.544 +                     Resource r2 = createResource(2, bitMap, closedList);
   1.545 +                     Resource r3 = createResource(3, bitMap, closedList)) {
   1.546 +                    if (failure != 0)
   1.547 +                        throw new MyKindOfException();
   1.548 +                } catch (Resource.CreateFailException e) {
   1.549 +                    throw new AssertionError(
   1.550 +                                             "Resource creation failed: " + e.resourceId());
   1.551 +                } catch (MyKindOfException e) {
   1.552 +                    if (failure == 0)
   1.553 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.554 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.555 +                } catch (Resource.CloseFailException e) {
   1.556 +                    if (failure == 1)
   1.557 +                        throw new AssertionError("Secondary exception suppression failed");
   1.558 +                    int id = e.resourceId();
   1.559 +                    if (bitMap == 0)
   1.560 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.561 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.562 +                    if (1 << id != highestCloseFailBit) {
   1.563 +                        throw new AssertionError("CloseFailException: got id " + id
   1.564 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.565 +                    }
   1.566 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.567 +                }
   1.568 +                checkClosedList(closedList, 4);
   1.569 +            }
   1.570 +        }
   1.571 +    }
   1.572 +
   1.573 +    public static void testCreateSuccess4Nested() {
   1.574 +        for (int bitMap = 0, n = 1 << 4; bitMap < n; bitMap++) {
   1.575 +            for (int failure = 0; failure < 2; failure++) {
   1.576 +                List<Integer> closedList = new ArrayList<Integer>();
   1.577 +                try (Resource r0 = createResource(0, bitMap, closedList)) {
   1.578 +                    try (Resource r1 = createResource(1, bitMap, closedList)) {
   1.579 +                        try (Resource r2 = createResource(2, bitMap, closedList)) {
   1.580 +                            try (Resource r3 = createResource(3, bitMap, closedList)) {
   1.581 +                                if (failure != 0)
   1.582 +                                    throw new MyKindOfException();
   1.583 +                            }
   1.584 +                        }
   1.585 +                    }
   1.586 +                } catch (Resource.CreateFailException e) {
   1.587 +                    throw new AssertionError(
   1.588 +                                             "Resource creation failed: " + e.resourceId());
   1.589 +                } catch (MyKindOfException e) {
   1.590 +                    if (failure == 0)
   1.591 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.592 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.593 +                } catch (Resource.CloseFailException e) {
   1.594 +                    if (failure == 1)
   1.595 +                        throw new AssertionError("Secondary exception suppression failed");
   1.596 +                    int id = e.resourceId();
   1.597 +                    if (bitMap == 0)
   1.598 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.599 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.600 +                    if (1 << id != highestCloseFailBit) {
   1.601 +                        throw new AssertionError("CloseFailException: got id " + id
   1.602 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.603 +                    }
   1.604 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.605 +                }
   1.606 +                checkClosedList(closedList, 4);
   1.607 +            }
   1.608 +        }
   1.609 +    }
   1.610 +
   1.611 +    public static void testCreateSuccess5() {
   1.612 +        for (int bitMap = 0, n = 1 << 5; bitMap < n; bitMap++) {
   1.613 +            for (int failure = 0; failure < 2; failure++) {
   1.614 +                List<Integer> closedList = new ArrayList<Integer>();
   1.615 +                try (Resource r0 = createResource(0, bitMap, closedList);
   1.616 +                     Resource r1 = createResource(1, bitMap, closedList);
   1.617 +                     Resource r2 = createResource(2, bitMap, closedList);
   1.618 +                     Resource r3 = createResource(3, bitMap, closedList);
   1.619 +                     Resource r4 = createResource(4, bitMap, closedList)) {
   1.620 +                    if (failure != 0)
   1.621 +                        throw new MyKindOfException();
   1.622 +                } catch (Resource.CreateFailException e) {
   1.623 +                    throw new AssertionError("Resource creation failed: " + e.resourceId());
   1.624 +                } catch (MyKindOfException e) {
   1.625 +                    if (failure == 0)
   1.626 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.627 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.628 +                } catch (Resource.CloseFailException e) {
   1.629 +                    if (failure == 1)
   1.630 +                        throw new AssertionError("Secondary exception suppression failed");
   1.631 +                    int id = e.resourceId();
   1.632 +                    if (bitMap == 0)
   1.633 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.634 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.635 +                    if (1 << id != highestCloseFailBit) {
   1.636 +                        throw new AssertionError("CloseFailException: got id " + id
   1.637 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.638 +                    }
   1.639 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.640 +                }
   1.641 +                checkClosedList(closedList, 5);
   1.642 +            }
   1.643 +        }
   1.644 +    }
   1.645 +
   1.646 +    public static void testCreateSuccess5Nested() {
   1.647 +        for (int bitMap = 0, n = 1 << 5; bitMap < n; bitMap++) {
   1.648 +            for (int failure = 0; failure < 2; failure++) {
   1.649 +                List<Integer> closedList = new ArrayList<Integer>();
   1.650 +                try (Resource r0 = createResource(0, bitMap, closedList)) {
   1.651 +                    try (Resource r1 = createResource(1, bitMap, closedList)) {
   1.652 +                        try (Resource r2 = createResource(2, bitMap, closedList)) {
   1.653 +                            try (Resource r3 = createResource(3, bitMap, closedList)) {
   1.654 +                                try (Resource r4 = createResource(4, bitMap, closedList)) {
   1.655 +                                    if (failure != 0)
   1.656 +                                        throw new MyKindOfException();
   1.657 +                                }
   1.658 +                            }
   1.659 +                        }
   1.660 +                    }
   1.661 +                } catch (Resource.CreateFailException e) {
   1.662 +                    throw new AssertionError("Resource creation failed: " + e.resourceId());
   1.663 +                } catch (MyKindOfException e) {
   1.664 +                    if (failure == 0)
   1.665 +                        throw new AssertionError("Unexpected MyKindOfException");
   1.666 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap);
   1.667 +                } catch (Resource.CloseFailException e) {
   1.668 +                    if (failure == 1)
   1.669 +                        throw new AssertionError("Secondary exception suppression failed");
   1.670 +                    int id = e.resourceId();
   1.671 +                    if (bitMap == 0)
   1.672 +                        throw new AssertionError("Unexpected CloseFailException: " + id);
   1.673 +                    int highestCloseFailBit = Integer.highestOneBit(bitMap);
   1.674 +                    if (1 << id != highestCloseFailBit) {
   1.675 +                        throw new AssertionError("CloseFailException: got id " + id
   1.676 +                                                 + ", expected lg(" + highestCloseFailBit +")");
   1.677 +                    }
   1.678 +                    checkSuppressedExceptions(e.getSuppressedExceptions(), bitMap & ~highestCloseFailBit);
   1.679 +                }
   1.680 +                checkClosedList(closedList, 5);
   1.681 +            }
   1.682 +        }
   1.683 +    }
   1.684 +
   1.685 +    private static Resource createResource(int id,
   1.686 +                                           int closeFailureBitMap,
   1.687 +                                           List<Integer> closedList) throws Resource.CreateFailException {
   1.688 +        boolean closeSucceeds = (closeFailureBitMap & (1 << id)) == 0;
   1.689 +        return new Resource(id, true, closeSucceeds, closedList);
   1.690 +    }
   1.691 +
   1.692 +    private static class MyKindOfException extends Exception {
   1.693 +    }
   1.694 +}
   1.695 +
   1.696 +class Resource implements AutoCloseable {
   1.697 +    /** A number identifying this resource */
   1.698 +    private final int resourceId;
   1.699 +
   1.700 +    /** Whether the close call on this resource should succeed or fail */
   1.701 +    private final boolean closeSucceeds;
   1.702 +
   1.703 +    /** When resource is closed, it records its ID in this list */
   1.704 +    private final List<Integer> closedList;
   1.705 +
   1.706 +    Resource(int resourceId, boolean createSucceeds, boolean closeSucceeds,
   1.707 +             List<Integer> closedList) throws CreateFailException {
   1.708 +        if (!createSucceeds)
   1.709 +            throw new CreateFailException(resourceId);
   1.710 +        this.resourceId = resourceId;
   1.711 +        this.closeSucceeds = closeSucceeds;
   1.712 +        this.closedList = closedList;
   1.713 +    }
   1.714 +
   1.715 +    public void close() throws CloseFailException {
   1.716 +        closedList.add(resourceId);
   1.717 +        if (!closeSucceeds)
   1.718 +            throw new CloseFailException(resourceId);
   1.719 +    }
   1.720 +
   1.721 +    public static class ResourceException extends RuntimeException {
   1.722 +        private final int resourceId;
   1.723 +
   1.724 +        public ResourceException(int resourceId) {
   1.725 +            super("Resource ID = " + resourceId);
   1.726 +            this.resourceId = resourceId;
   1.727 +        }
   1.728 +
   1.729 +        public int resourceId() {
   1.730 +            return resourceId;
   1.731 +        }
   1.732 +    }
   1.733 +
   1.734 +    public static class CreateFailException extends ResourceException {
   1.735 +        public CreateFailException(int resourceId) {
   1.736 +            super(resourceId);
   1.737 +        }
   1.738 +    }
   1.739 +
   1.740 +    public static class CloseFailException extends ResourceException {
   1.741 +        public CloseFailException(int resourceId) {
   1.742 +            super(resourceId);
   1.743 +        }
   1.744 +    }
   1.745 +}

mercurial