test/tools/javac/TryWithResources/TwrTests.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 745
4328728e0409
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

darcy@609 1 /*
darcy@609 2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
darcy@609 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@609 4 *
darcy@609 5 * This code is free software; you can redistribute it and/or modify it
darcy@609 6 * under the terms of the GNU General Public License version 2 only, as
darcy@609 7 * published by the Free Software Foundation.
darcy@609 8 *
darcy@609 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@609 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@609 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@609 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@609 13 * accompanied this code).
darcy@609 14 *
darcy@609 15 * You should have received a copy of the GNU General Public License version
darcy@609 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@609 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@609 18 *
darcy@609 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@609 20 * or visit www.oracle.com if you need additional information or have any
darcy@609 21 * questions.
darcy@609 22 */
darcy@609 23
darcy@609 24 /*
darcy@609 25 * @test
darcy@609 26 * @bug 6911256 6964740
darcy@609 27 * @summary Tests of generated TWR code.
darcy@609 28 */
darcy@609 29
darcy@609 30 import java.util.List;
darcy@609 31 import java.util.ArrayList;
darcy@609 32
darcy@609 33 public class TwrTests {
darcy@609 34 public static void main(String[] args) {
darcy@609 35 testCreateFailure1();
darcy@609 36 testCreateFailure2();
darcy@609 37 testCreateFailure2Nested();
darcy@609 38 testCreateFailure3();
darcy@609 39 testCreateFailure3Nested();
darcy@609 40 testCreateFailure4();
darcy@609 41 testCreateFailure4Nested();
darcy@609 42 testCreateFailure5();
darcy@609 43 testCreateFailure5Nested();
darcy@609 44
darcy@609 45 testCreateSuccess1();
darcy@609 46 testCreateSuccess2();
darcy@609 47 testCreateSuccess2Nested();
darcy@609 48 testCreateSuccess3();
darcy@609 49 testCreateSuccess3Nested();
darcy@609 50 testCreateSuccess4();
darcy@609 51 testCreateSuccess4Nested();
darcy@609 52 testCreateSuccess5();
darcy@609 53 testCreateSuccess5Nested();
darcy@609 54 }
darcy@609 55
darcy@609 56 /*
darcy@609 57 * The following tests simulate a creation failure of every possible
darcy@609 58 * resource in an TWR block, and check to make sure that the failure
darcy@609 59 * prevents creation of subsequent resources, and that all created
darcy@609 60 * resources are properly closed, even if one or more of the close
darcy@609 61 * attempts fails.
darcy@609 62 */
darcy@609 63
darcy@609 64 public static void testCreateFailure1() {
darcy@609 65 int creationFailuresDetected = 0;
darcy@609 66 List<Integer> closedList = new ArrayList<Integer>(0);
darcy@609 67 try (Resource r0 = createResource(0, 0, 0, closedList)) {
darcy@609 68 throw new AssertionError("Resource creation succeeded");
darcy@609 69 } catch (Resource.CreateFailException e) {
darcy@609 70 creationFailuresDetected++;
darcy@609 71 if (e.resourceId() != 0) {
darcy@609 72 throw new AssertionError("Wrong resource creation "
darcy@609 73 + e.resourceId() + " failed");
darcy@609 74 }
darcy@609 75 } catch (Resource.CloseFailException e) {
darcy@609 76 throw new AssertionError("Unexpected CloseFailException: " + e.resourceId());
darcy@609 77 }
darcy@609 78 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 79 checkClosedList(closedList, 0);
darcy@609 80 }
darcy@609 81
darcy@609 82 public static void testCreateFailure2() {
darcy@609 83 for (int createFailureId = 0; createFailureId < 2; createFailureId++) {
darcy@609 84 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 85 int creationFailuresDetected = 0;
darcy@609 86 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 87 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
darcy@609 88 Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
darcy@609 89 throw new AssertionError("Entire resource creation succeeded");
darcy@609 90 } catch (Resource.CreateFailException e) {
darcy@609 91 creationFailuresDetected++;
darcy@609 92 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 93 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 94 } catch (Resource.CloseFailException e) {
darcy@609 95 throw new AssertionError("Secondary exception suppression failed");
darcy@609 96 }
darcy@609 97 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 98 checkClosedList(closedList, createFailureId);
darcy@609 99 }
darcy@609 100 }
darcy@609 101 }
darcy@609 102
darcy@609 103 public static void testCreateFailure2Nested() {
darcy@609 104 for (int createFailureId = 0; createFailureId < 2; createFailureId++) {
darcy@609 105 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 106 int creationFailuresDetected = 0;
darcy@609 107 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 108 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
darcy@609 109 try(Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
darcy@609 110 throw new AssertionError("Entire resource creation succeeded");
darcy@609 111 }
darcy@609 112 } catch (Resource.CreateFailException e) {
darcy@609 113 creationFailuresDetected++;
darcy@609 114 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 115 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 116 } catch (Resource.CloseFailException e) {
darcy@609 117 throw new AssertionError("Secondary exception suppression failed");
darcy@609 118 }
darcy@609 119 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 120 checkClosedList(closedList, createFailureId);
darcy@609 121 }
darcy@609 122 }
darcy@609 123 }
darcy@609 124
darcy@609 125 public static void testCreateFailure3() {
darcy@609 126 for (int createFailureId = 0; createFailureId < 3; createFailureId++) {
darcy@609 127 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 128 int creationFailuresDetected = 0;
darcy@609 129 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 130 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
darcy@609 131 Resource r1 = createResource(1, createFailureId, bitMap, closedList);
darcy@609 132 Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
darcy@609 133 throw new AssertionError("Entire resource creation succeeded");
darcy@609 134 } catch (Resource.CreateFailException e) {
darcy@609 135 creationFailuresDetected++;
darcy@609 136 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 137 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 138 } catch (Resource.CloseFailException e) {
darcy@609 139 throw new AssertionError("Secondary exception suppression failed:" + e);
darcy@609 140 }
darcy@609 141 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 142 checkClosedList(closedList, createFailureId);
darcy@609 143 }
darcy@609 144 }
darcy@609 145 }
darcy@609 146
darcy@609 147 public static void testCreateFailure3Nested() {
darcy@609 148 for (int createFailureId = 0; createFailureId < 3; createFailureId++) {
darcy@609 149 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 150 int creationFailuresDetected = 0;
darcy@609 151 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 152 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
darcy@609 153 try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
darcy@609 154 try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
darcy@609 155 throw new AssertionError("Entire resource creation succeeded");
darcy@609 156 }
darcy@609 157 }
darcy@609 158 } catch (Resource.CreateFailException e) {
darcy@609 159 creationFailuresDetected++;
darcy@609 160 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 161 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 162 } catch (Resource.CloseFailException e) {
darcy@609 163 throw new AssertionError("Secondary exception suppression failed:" + e);
darcy@609 164 }
darcy@609 165 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 166 checkClosedList(closedList, createFailureId);
darcy@609 167 }
darcy@609 168 }
darcy@609 169 }
darcy@609 170
darcy@609 171 public static void testCreateFailure4() {
darcy@609 172 for (int createFailureId = 0; createFailureId < 4; createFailureId++) {
darcy@609 173 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 174 int creationFailuresDetected = 0;
darcy@609 175 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 176 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
darcy@609 177 Resource r1 = createResource(1, createFailureId, bitMap, closedList);
darcy@609 178 Resource r2 = createResource(2, createFailureId, bitMap, closedList);
darcy@609 179 Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
darcy@609 180 throw new AssertionError("Entire resource creation succeeded");
darcy@609 181 } catch (Resource.CreateFailException e) {
darcy@609 182 creationFailuresDetected++;
darcy@609 183 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 184 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 185 } catch (Resource.CloseFailException e) {
darcy@609 186 throw new AssertionError("Secondary exception suppression failed:" + e);
darcy@609 187 }
darcy@609 188 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 189 checkClosedList(closedList, createFailureId);
darcy@609 190 }
darcy@609 191 }
darcy@609 192 }
darcy@609 193
darcy@609 194 public static void testCreateFailure4Nested() {
darcy@609 195 for (int createFailureId = 0; createFailureId < 4; createFailureId++) {
darcy@609 196 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 197 int creationFailuresDetected = 0;
darcy@609 198 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 199 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
darcy@609 200 try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
darcy@609 201 try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
darcy@609 202 try (Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
darcy@609 203 throw new AssertionError("Entire resource creation succeeded");
darcy@609 204 }
darcy@609 205 }
darcy@609 206 }
darcy@609 207 } catch (Resource.CreateFailException e) {
darcy@609 208 creationFailuresDetected++;
darcy@609 209 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 210 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 211 } catch (Resource.CloseFailException e) {
darcy@609 212 throw new AssertionError("Secondary exception suppression failed:" + e);
darcy@609 213 }
darcy@609 214 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 215 checkClosedList(closedList, createFailureId);
darcy@609 216 }
darcy@609 217 }
darcy@609 218 }
darcy@609 219
darcy@609 220 public static void testCreateFailure5() {
darcy@609 221 for (int createFailureId = 0; createFailureId < 5; createFailureId++) {
darcy@609 222 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 223 int creationFailuresDetected = 0;
darcy@609 224 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 225 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList);
darcy@609 226 Resource r1 = createResource(1, createFailureId, bitMap, closedList);
darcy@609 227 Resource r2 = createResource(2, createFailureId, bitMap, closedList);
darcy@609 228 Resource r3 = createResource(3, createFailureId, bitMap, closedList);
darcy@609 229 Resource r4 = createResource(4, createFailureId, bitMap, closedList)) {
darcy@609 230 throw new AssertionError("Entire resource creation succeeded");
darcy@609 231 } catch (Resource.CreateFailException e) {
darcy@609 232 creationFailuresDetected++;
darcy@609 233 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 234 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 235 } catch (Resource.CloseFailException e) {
darcy@609 236 throw new AssertionError("Secondary exception suppression failed:" + e);
darcy@609 237 }
darcy@609 238 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 239 checkClosedList(closedList, createFailureId);
darcy@609 240 }
darcy@609 241 }
darcy@609 242 }
darcy@609 243
darcy@609 244 public static void testCreateFailure5Nested() {
darcy@609 245 for (int createFailureId = 0; createFailureId < 5; createFailureId++) {
darcy@609 246 for (int bitMap = 0, n = 1 << createFailureId; bitMap < n; bitMap++) {
darcy@609 247 int creationFailuresDetected = 0;
darcy@609 248 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 249 try (Resource r0 = createResource(0, createFailureId, bitMap, closedList)) {
darcy@609 250 try (Resource r1 = createResource(1, createFailureId, bitMap, closedList)) {
darcy@609 251 try (Resource r2 = createResource(2, createFailureId, bitMap, closedList)) {
darcy@609 252 try (Resource r3 = createResource(3, createFailureId, bitMap, closedList)) {
darcy@609 253 try (Resource r4 = createResource(4, createFailureId, bitMap, closedList)) {
darcy@609 254 throw new AssertionError("Entire resource creation succeeded");
darcy@609 255 }
darcy@609 256 }
darcy@609 257 }
darcy@609 258 }
darcy@609 259 } catch (Resource.CreateFailException e) {
darcy@609 260 creationFailuresDetected++;
darcy@609 261 checkCreateFailureId(e.resourceId(), createFailureId);
darcy@745 262 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 263 } catch (Resource.CloseFailException e) {
darcy@609 264 throw new AssertionError("Secondary exception suppression failed:" + e);
darcy@609 265 }
darcy@609 266 checkForSingleCreationFailure(creationFailuresDetected);
darcy@609 267 checkClosedList(closedList, createFailureId);
darcy@609 268 }
darcy@609 269 }
darcy@609 270 }
darcy@609 271
darcy@609 272 /**
darcy@609 273 * Create a resource with the specified ID. The ID must be less than createFailureId.
darcy@609 274 * A subsequent attempt to close the resource will fail iff the corresponding bit
darcy@609 275 * is set in closeFailureBitMap. When an attempt is made to close this resource,
darcy@609 276 * its ID will be added to closedList, regardless of whether the attempt succeeds.
darcy@609 277 *
darcy@609 278 * @param id the ID of this resource
darcy@609 279 * @param createFailureId the ID of the resource whose creation will fail
darcy@609 280 * @param closeFailureBitMap a bit vector describing which resources should throw an
darcy@609 281 * exception when close is attempted
darcy@609 282 * @param closedList a list on which to record resource close attempts
darcy@609 283 * @throws AssertionError if no attempt should be made to create this resource
darcy@609 284 */
darcy@609 285 private static Resource createResource(int id,
darcy@609 286 int createFailureId,
darcy@609 287 int closeFailureBitMap,
darcy@609 288 List<Integer> closedList) throws Resource.CreateFailException {
darcy@609 289 if (id > createFailureId)
darcy@609 290 throw new AssertionError("Resource " + id + " shouldn't be created");
darcy@609 291 boolean createSucceeds = id != createFailureId;
darcy@609 292 boolean closeSucceeds = (closeFailureBitMap & (1 << id)) == 0;
darcy@609 293 return new Resource(id, createSucceeds, closeSucceeds, closedList);
darcy@609 294 }
darcy@609 295
darcy@609 296
darcy@609 297 /**
darcy@609 298 * Check that an observed creation failure has the expected resource ID.
darcy@609 299 *
darcy@609 300 * @param foundId the ID of the resource whose creation failed
darcy@609 301 * @param expectedId the ID of the resource whose creation should have failed
darcy@609 302 */
darcy@609 303 private static void checkCreateFailureId(int foundId, int expectedId) {
darcy@609 304 if (foundId != expectedId)
darcy@609 305 throw new AssertionError("Wrong resource creation failed. Found ID "
darcy@609 306 + foundId + " expected " + expectedId);
darcy@609 307 }
darcy@609 308
darcy@609 309 /**
darcy@609 310 * Check for proper suppressed exceptions in proper order.
darcy@609 311 *
darcy@609 312 * @param suppressedExceptions the suppressed exceptions array returned by
darcy@745 313 * getSuppressed()
darcy@609 314 * @bitmap a bitmap indicating which suppressed exceptions are expected.
darcy@609 315 * Bit i is set iff id should throw a CloseFailException.
darcy@609 316 */
darcy@609 317 private static void checkSuppressedExceptions(Throwable[] suppressedExceptions, int bitMap) {
darcy@609 318 if (suppressedExceptions.length != Integer.bitCount(bitMap))
darcy@609 319 throw new AssertionError("Expected " + Integer.bitCount(bitMap)
darcy@609 320 + " suppressed exceptions, got " + suppressedExceptions.length);
darcy@609 321
darcy@609 322 int prevCloseFailExceptionId = Integer.MAX_VALUE;
darcy@609 323 for (Throwable t : suppressedExceptions) {
darcy@609 324 int id = ((Resource.CloseFailException) t).resourceId();
darcy@609 325 if ((1 << id & bitMap) == 0)
darcy@609 326 throw new AssertionError("Unexpected suppressed CloseFailException: " + id);
darcy@609 327 if (id > prevCloseFailExceptionId)
darcy@609 328 throw new AssertionError("Suppressed CloseFailException" + id
darcy@609 329 + " followed " + prevCloseFailExceptionId);
darcy@609 330 }
darcy@609 331 }
darcy@609 332
darcy@609 333 /**
darcy@609 334 * Check that exactly one resource creation failed.
darcy@609 335 *
darcy@609 336 * @param numCreationFailuresDetected the number of creation failures detected
darcy@609 337 */
darcy@609 338 private static void checkForSingleCreationFailure(int numCreationFailuresDetected) {
darcy@609 339 if (numCreationFailuresDetected != 1)
darcy@609 340 throw new AssertionError("Wrong number of creation failures: "
darcy@609 341 + numCreationFailuresDetected);
darcy@609 342 }
darcy@609 343
darcy@609 344 /**
darcy@609 345 * Check that a close was attempted on every resourced that was successfully opened,
darcy@609 346 * and that the close attempts occurred in the proper order.
darcy@609 347 *
darcy@609 348 * @param closedList the resource IDs of the close attempts, in the order they occurred
darcy@609 349 * @param the ID of the resource whose creation failed. Close attempts should occur
darcy@609 350 * for all previous resources, in reverse order.
darcy@609 351 */
darcy@609 352 private static void checkClosedList(List<Integer> closedList, int createFailureId) {
darcy@609 353 List<Integer> expectedList = new ArrayList<Integer>(createFailureId);
darcy@609 354 for (int i = createFailureId - 1; i >= 0; i--)
darcy@609 355 expectedList.add(i);
darcy@609 356 if (!closedList.equals(expectedList))
darcy@609 357 throw new AssertionError("Closing sequence " + closedList + " != " + expectedList);
darcy@609 358 }
darcy@609 359
darcy@609 360 /*
darcy@609 361 * The following tests simulate the creation of several resources, followed
darcy@609 362 * by success or failure of forward processing. They test that all resources
darcy@609 363 * are properly closed, even if one or more of the close attempts fails.
darcy@609 364 */
darcy@609 365
darcy@609 366 public static void testCreateSuccess1() {
darcy@609 367 for (int bitMap = 0, n = 1 << 1; bitMap < n; bitMap++) {
darcy@609 368 for (int failure = 0; failure < 2; failure++) {
darcy@609 369 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 370 try (Resource r0 = createResource(0, bitMap, closedList)) {
darcy@609 371 if (failure != 0)
darcy@609 372 throw new MyKindOfException();
darcy@609 373 } catch (Resource.CreateFailException e) {
darcy@609 374 throw new AssertionError(
darcy@609 375 "Resource creation failed: " + e.resourceId());
darcy@609 376 } catch (MyKindOfException e) {
darcy@609 377 if (failure == 0)
darcy@609 378 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 379 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 380 } catch (Resource.CloseFailException e) {
darcy@609 381 if (failure == 1)
darcy@609 382 throw new AssertionError("Secondary exception suppression failed");
darcy@609 383 int id = e.resourceId();
darcy@609 384 if (bitMap == 0)
darcy@609 385 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 386 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 387 if (1 << id != highestCloseFailBit) {
darcy@609 388 throw new AssertionError("CloseFailException: got id " + id
darcy@609 389 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 390 }
darcy@745 391 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 392 }
darcy@609 393 checkClosedList(closedList, 1);
darcy@609 394 }
darcy@609 395 }
darcy@609 396 }
darcy@609 397
darcy@609 398 public static void testCreateSuccess2() {
darcy@609 399 for (int bitMap = 0, n = 1 << 2; bitMap < n; bitMap++) {
darcy@609 400 for (int failure = 0; failure < 2; failure++) {
darcy@609 401 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 402 try (Resource r0 = createResource(0, bitMap, closedList);
darcy@609 403 Resource r1 = createResource(1, bitMap, closedList)) {
darcy@609 404 if (failure != 0)
darcy@609 405 throw new MyKindOfException();
darcy@609 406 } catch (Resource.CreateFailException e) {
darcy@609 407 throw new AssertionError(
darcy@609 408 "Resource creation failed: " + e.resourceId());
darcy@609 409 } catch (MyKindOfException e) {
darcy@609 410 if (failure == 0)
darcy@609 411 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 412 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 413 } catch (Resource.CloseFailException e) {
darcy@609 414 if (failure == 1)
darcy@609 415 throw new AssertionError("Secondary exception suppression failed");
darcy@609 416 int id = e.resourceId();
darcy@609 417 if (bitMap == 0)
darcy@609 418 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 419 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 420 if (1 << id != highestCloseFailBit) {
darcy@609 421 throw new AssertionError("CloseFailException: got id " + id
darcy@609 422 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 423 }
darcy@745 424 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 425 }
darcy@609 426 checkClosedList(closedList, 2);
darcy@609 427 }
darcy@609 428 }
darcy@609 429 }
darcy@609 430
darcy@609 431 public static void testCreateSuccess2Nested() {
darcy@609 432 for (int bitMap = 0, n = 1 << 2; bitMap < n; bitMap++) {
darcy@609 433 for (int failure = 0; failure < 2; failure++) {
darcy@609 434 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 435 try (Resource r0 = createResource(0, bitMap, closedList)) {
darcy@609 436 try (Resource r1 = createResource(1, bitMap, closedList)) {
darcy@609 437 if (failure != 0)
darcy@609 438 throw new MyKindOfException();
darcy@609 439 }
darcy@609 440 } catch (Resource.CreateFailException e) {
darcy@609 441 throw new AssertionError(
darcy@609 442 "Resource creation failed: " + e.resourceId());
darcy@609 443 } catch (MyKindOfException e) {
darcy@609 444 if (failure == 0)
darcy@609 445 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 446 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 447 } catch (Resource.CloseFailException e) {
darcy@609 448 if (failure == 1)
darcy@609 449 throw new AssertionError("Secondary exception suppression failed");
darcy@609 450 int id = e.resourceId();
darcy@609 451 if (bitMap == 0)
darcy@609 452 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 453 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 454 if (1 << id != highestCloseFailBit) {
darcy@609 455 throw new AssertionError("CloseFailException: got id " + id
darcy@609 456 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 457 }
darcy@745 458 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 459 }
darcy@609 460 checkClosedList(closedList, 2);
darcy@609 461 }
darcy@609 462 }
darcy@609 463 }
darcy@609 464
darcy@609 465 public static void testCreateSuccess3() {
darcy@609 466 for (int bitMap = 0, n = 1 << 3; bitMap < n; bitMap++) {
darcy@609 467 for (int failure = 0; failure < 2; failure++) {
darcy@609 468 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 469 try (Resource r0 = createResource(0, bitMap, closedList);
darcy@609 470 Resource r1 = createResource(1, bitMap, closedList);
darcy@609 471 Resource r2 = createResource(2, bitMap, closedList)) {
darcy@609 472 if (failure != 0)
darcy@609 473 throw new MyKindOfException();
darcy@609 474 } catch (Resource.CreateFailException e) {
darcy@609 475 throw new AssertionError(
darcy@609 476 "Resource creation failed: " + e.resourceId());
darcy@609 477 } catch (MyKindOfException e) {
darcy@609 478 if (failure == 0)
darcy@609 479 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 480 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 481 } catch (Resource.CloseFailException e) {
darcy@609 482 if (failure == 1)
darcy@609 483 throw new AssertionError("Secondary exception suppression failed");
darcy@609 484 int id = e.resourceId();
darcy@609 485 if (bitMap == 0)
darcy@609 486 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 487 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 488 if (1 << id != highestCloseFailBit) {
darcy@609 489 throw new AssertionError("CloseFailException: got id " + id
darcy@609 490 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 491 }
darcy@745 492 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 493 }
darcy@609 494 checkClosedList(closedList, 3);
darcy@609 495 }
darcy@609 496 }
darcy@609 497 }
darcy@609 498
darcy@609 499 public static void testCreateSuccess3Nested() {
darcy@609 500 for (int bitMap = 0, n = 1 << 3; bitMap < n; bitMap++) {
darcy@609 501 for (int failure = 0; failure < 2; failure++) {
darcy@609 502 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 503 try (Resource r0 = createResource(0, bitMap, closedList)) {
darcy@609 504 try (Resource r1 = createResource(1, bitMap, closedList)) {
darcy@609 505 try (Resource r2 = createResource(2, bitMap, closedList)) {
darcy@609 506 if (failure != 0)
darcy@609 507 throw new MyKindOfException();
darcy@609 508 }
darcy@609 509 }
darcy@609 510 } catch (Resource.CreateFailException e) {
darcy@609 511 throw new AssertionError(
darcy@609 512 "Resource creation failed: " + e.resourceId());
darcy@609 513 } catch (MyKindOfException e) {
darcy@609 514 if (failure == 0)
darcy@609 515 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 516 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 517 } catch (Resource.CloseFailException e) {
darcy@609 518 if (failure == 1)
darcy@609 519 throw new AssertionError("Secondary exception suppression failed");
darcy@609 520 int id = e.resourceId();
darcy@609 521 if (bitMap == 0)
darcy@609 522 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 523 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 524 if (1 << id != highestCloseFailBit) {
darcy@609 525 throw new AssertionError("CloseFailException: got id " + id
darcy@609 526 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 527 }
darcy@745 528 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 529 }
darcy@609 530 checkClosedList(closedList, 3);
darcy@609 531 }
darcy@609 532 }
darcy@609 533 }
darcy@609 534
darcy@609 535 public static void testCreateSuccess4() {
darcy@609 536 for (int bitMap = 0, n = 1 << 4; bitMap < n; bitMap++) {
darcy@609 537 for (int failure = 0; failure < 2; failure++) {
darcy@609 538 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 539 try (Resource r0 = createResource(0, bitMap, closedList);
darcy@609 540 Resource r1 = createResource(1, bitMap, closedList);
darcy@609 541 Resource r2 = createResource(2, bitMap, closedList);
darcy@609 542 Resource r3 = createResource(3, bitMap, closedList)) {
darcy@609 543 if (failure != 0)
darcy@609 544 throw new MyKindOfException();
darcy@609 545 } catch (Resource.CreateFailException e) {
darcy@609 546 throw new AssertionError(
darcy@609 547 "Resource creation failed: " + e.resourceId());
darcy@609 548 } catch (MyKindOfException e) {
darcy@609 549 if (failure == 0)
darcy@609 550 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 551 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 552 } catch (Resource.CloseFailException e) {
darcy@609 553 if (failure == 1)
darcy@609 554 throw new AssertionError("Secondary exception suppression failed");
darcy@609 555 int id = e.resourceId();
darcy@609 556 if (bitMap == 0)
darcy@609 557 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 558 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 559 if (1 << id != highestCloseFailBit) {
darcy@609 560 throw new AssertionError("CloseFailException: got id " + id
darcy@609 561 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 562 }
darcy@745 563 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 564 }
darcy@609 565 checkClosedList(closedList, 4);
darcy@609 566 }
darcy@609 567 }
darcy@609 568 }
darcy@609 569
darcy@609 570 public static void testCreateSuccess4Nested() {
darcy@609 571 for (int bitMap = 0, n = 1 << 4; bitMap < n; bitMap++) {
darcy@609 572 for (int failure = 0; failure < 2; failure++) {
darcy@609 573 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 574 try (Resource r0 = createResource(0, bitMap, closedList)) {
darcy@609 575 try (Resource r1 = createResource(1, bitMap, closedList)) {
darcy@609 576 try (Resource r2 = createResource(2, bitMap, closedList)) {
darcy@609 577 try (Resource r3 = createResource(3, bitMap, closedList)) {
darcy@609 578 if (failure != 0)
darcy@609 579 throw new MyKindOfException();
darcy@609 580 }
darcy@609 581 }
darcy@609 582 }
darcy@609 583 } catch (Resource.CreateFailException e) {
darcy@609 584 throw new AssertionError(
darcy@609 585 "Resource creation failed: " + e.resourceId());
darcy@609 586 } catch (MyKindOfException e) {
darcy@609 587 if (failure == 0)
darcy@609 588 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 589 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 590 } catch (Resource.CloseFailException e) {
darcy@609 591 if (failure == 1)
darcy@609 592 throw new AssertionError("Secondary exception suppression failed");
darcy@609 593 int id = e.resourceId();
darcy@609 594 if (bitMap == 0)
darcy@609 595 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 596 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 597 if (1 << id != highestCloseFailBit) {
darcy@609 598 throw new AssertionError("CloseFailException: got id " + id
darcy@609 599 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 600 }
darcy@745 601 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 602 }
darcy@609 603 checkClosedList(closedList, 4);
darcy@609 604 }
darcy@609 605 }
darcy@609 606 }
darcy@609 607
darcy@609 608 public static void testCreateSuccess5() {
darcy@609 609 for (int bitMap = 0, n = 1 << 5; bitMap < n; bitMap++) {
darcy@609 610 for (int failure = 0; failure < 2; failure++) {
darcy@609 611 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 612 try (Resource r0 = createResource(0, bitMap, closedList);
darcy@609 613 Resource r1 = createResource(1, bitMap, closedList);
darcy@609 614 Resource r2 = createResource(2, bitMap, closedList);
darcy@609 615 Resource r3 = createResource(3, bitMap, closedList);
darcy@609 616 Resource r4 = createResource(4, bitMap, closedList)) {
darcy@609 617 if (failure != 0)
darcy@609 618 throw new MyKindOfException();
darcy@609 619 } catch (Resource.CreateFailException e) {
darcy@609 620 throw new AssertionError("Resource creation failed: " + e.resourceId());
darcy@609 621 } catch (MyKindOfException e) {
darcy@609 622 if (failure == 0)
darcy@609 623 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 624 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 625 } catch (Resource.CloseFailException e) {
darcy@609 626 if (failure == 1)
darcy@609 627 throw new AssertionError("Secondary exception suppression failed");
darcy@609 628 int id = e.resourceId();
darcy@609 629 if (bitMap == 0)
darcy@609 630 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 631 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 632 if (1 << id != highestCloseFailBit) {
darcy@609 633 throw new AssertionError("CloseFailException: got id " + id
darcy@609 634 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 635 }
darcy@745 636 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 637 }
darcy@609 638 checkClosedList(closedList, 5);
darcy@609 639 }
darcy@609 640 }
darcy@609 641 }
darcy@609 642
darcy@609 643 public static void testCreateSuccess5Nested() {
darcy@609 644 for (int bitMap = 0, n = 1 << 5; bitMap < n; bitMap++) {
darcy@609 645 for (int failure = 0; failure < 2; failure++) {
darcy@609 646 List<Integer> closedList = new ArrayList<Integer>();
darcy@609 647 try (Resource r0 = createResource(0, bitMap, closedList)) {
darcy@609 648 try (Resource r1 = createResource(1, bitMap, closedList)) {
darcy@609 649 try (Resource r2 = createResource(2, bitMap, closedList)) {
darcy@609 650 try (Resource r3 = createResource(3, bitMap, closedList)) {
darcy@609 651 try (Resource r4 = createResource(4, bitMap, closedList)) {
darcy@609 652 if (failure != 0)
darcy@609 653 throw new MyKindOfException();
darcy@609 654 }
darcy@609 655 }
darcy@609 656 }
darcy@609 657 }
darcy@609 658 } catch (Resource.CreateFailException e) {
darcy@609 659 throw new AssertionError("Resource creation failed: " + e.resourceId());
darcy@609 660 } catch (MyKindOfException e) {
darcy@609 661 if (failure == 0)
darcy@609 662 throw new AssertionError("Unexpected MyKindOfException");
darcy@745 663 checkSuppressedExceptions(e.getSuppressed(), bitMap);
darcy@609 664 } catch (Resource.CloseFailException e) {
darcy@609 665 if (failure == 1)
darcy@609 666 throw new AssertionError("Secondary exception suppression failed");
darcy@609 667 int id = e.resourceId();
darcy@609 668 if (bitMap == 0)
darcy@609 669 throw new AssertionError("Unexpected CloseFailException: " + id);
darcy@609 670 int highestCloseFailBit = Integer.highestOneBit(bitMap);
darcy@609 671 if (1 << id != highestCloseFailBit) {
darcy@609 672 throw new AssertionError("CloseFailException: got id " + id
darcy@609 673 + ", expected lg(" + highestCloseFailBit +")");
darcy@609 674 }
darcy@745 675 checkSuppressedExceptions(e.getSuppressed(), bitMap & ~highestCloseFailBit);
darcy@609 676 }
darcy@609 677 checkClosedList(closedList, 5);
darcy@609 678 }
darcy@609 679 }
darcy@609 680 }
darcy@609 681
darcy@609 682 private static Resource createResource(int id,
darcy@609 683 int closeFailureBitMap,
darcy@609 684 List<Integer> closedList) throws Resource.CreateFailException {
darcy@609 685 boolean closeSucceeds = (closeFailureBitMap & (1 << id)) == 0;
darcy@609 686 return new Resource(id, true, closeSucceeds, closedList);
darcy@609 687 }
darcy@609 688
darcy@609 689 private static class MyKindOfException extends Exception {
darcy@609 690 }
darcy@609 691 }
darcy@609 692
darcy@609 693 class Resource implements AutoCloseable {
darcy@609 694 /** A number identifying this resource */
darcy@609 695 private final int resourceId;
darcy@609 696
darcy@609 697 /** Whether the close call on this resource should succeed or fail */
darcy@609 698 private final boolean closeSucceeds;
darcy@609 699
darcy@609 700 /** When resource is closed, it records its ID in this list */
darcy@609 701 private final List<Integer> closedList;
darcy@609 702
darcy@609 703 Resource(int resourceId, boolean createSucceeds, boolean closeSucceeds,
darcy@609 704 List<Integer> closedList) throws CreateFailException {
darcy@609 705 if (!createSucceeds)
darcy@609 706 throw new CreateFailException(resourceId);
darcy@609 707 this.resourceId = resourceId;
darcy@609 708 this.closeSucceeds = closeSucceeds;
darcy@609 709 this.closedList = closedList;
darcy@609 710 }
darcy@609 711
darcy@609 712 public void close() throws CloseFailException {
darcy@609 713 closedList.add(resourceId);
darcy@609 714 if (!closeSucceeds)
darcy@609 715 throw new CloseFailException(resourceId);
darcy@609 716 }
darcy@609 717
darcy@609 718 public static class ResourceException extends RuntimeException {
darcy@609 719 private final int resourceId;
darcy@609 720
darcy@609 721 public ResourceException(int resourceId) {
darcy@609 722 super("Resource ID = " + resourceId);
darcy@609 723 this.resourceId = resourceId;
darcy@609 724 }
darcy@609 725
darcy@609 726 public int resourceId() {
darcy@609 727 return resourceId;
darcy@609 728 }
darcy@609 729 }
darcy@609 730
darcy@609 731 public static class CreateFailException extends ResourceException {
darcy@609 732 public CreateFailException(int resourceId) {
darcy@609 733 super(resourceId);
darcy@609 734 }
darcy@609 735 }
darcy@609 736
darcy@609 737 public static class CloseFailException extends ResourceException {
darcy@609 738 public CloseFailException(int resourceId) {
darcy@609 739 super(resourceId);
darcy@609 740 }
darcy@609 741 }
darcy@609 742 }

mercurial