test/tools/javac/TryWithResources/DuplicateResource.java

Mon, 02 Aug 2010 13:35:39 -0700

author
darcy
date
Mon, 02 Aug 2010 13:35:39 -0700
changeset 622
38e2c23309f1
parent 609
13354e1abba7
child 840
7f8794f9cc14
permissions
-rw-r--r--

6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
Reviewed-by: jjb

darcy@609 1 /*
darcy@609 2 * Copyright (c) 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 6965277
darcy@609 27 * @author Maurizio Cimadamore
darcy@609 28 * @summary Check that lowered arm block does not end up creating resource twice
darcy@609 29 */
darcy@609 30
darcy@609 31 import java.util.ArrayList;
darcy@609 32
darcy@609 33 public class DuplicateResource {
darcy@609 34
darcy@609 35 static class TestResource implements AutoCloseable {
darcy@609 36 TestResource() {
darcy@609 37 resources.add(this);
darcy@609 38 }
darcy@609 39 boolean isClosed = false;
darcy@609 40 public void close() throws Exception {
darcy@609 41 isClosed = true;
darcy@609 42 }
darcy@609 43 }
darcy@609 44
darcy@609 45 static ArrayList<TestResource> resources = new ArrayList<TestResource>();
darcy@609 46
darcy@609 47 public static void main(String[] args) {
darcy@609 48 try(new TestResource()) {
darcy@609 49 //do something
darcy@609 50 } catch (Exception e) {
darcy@609 51 throw new AssertionError("Shouldn't reach here", e);
darcy@609 52 }
darcy@609 53 check();
darcy@609 54 }
darcy@609 55
darcy@609 56 public static void check() {
darcy@609 57 if (resources.size() != 1) {
darcy@609 58 throw new AssertionError("Expected one resource, found: " + resources.size());
darcy@609 59 }
darcy@609 60 TestResource resource = resources.get(0);
darcy@609 61 if (!resource.isClosed) {
darcy@609 62 throw new AssertionError("Resource used in ARM block has not been automatically closed");
darcy@609 63 }
darcy@609 64 }
darcy@609 65 }

mercurial