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