aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8025113 aoqi@0: * @author sogoel aoqi@0: * @summary Resource creation in nested expressions aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * This test checks for resource creation in nested expressions. aoqi@0: * test1() - Create 3 resource in nested new expressions, style 1 aoqi@0: * test2() - Create 3 resource in nested new expressions, style 2 aoqi@0: * test3() - Create 4 resources with resources as parameters: new expression; typeid & new expression aoqi@0: */ aoqi@0: aoqi@0: public class ResInNestedExpr { aoqi@0: aoqi@0: static final int expected = 5; aoqi@0: static int closed = 0; aoqi@0: aoqi@0: static void closing(String clazz) { aoqi@0: closed++; aoqi@0: } aoqi@0: aoqi@0: static void checkClosedCount() { aoqi@0: if (expected != closed) { aoqi@0: throw new RuntimeException("Did not find enough closed resources." aoqi@0: + "Expected " + expected + ", but found " + closed); aoqi@0: } aoqi@0: } aoqi@0: /** aoqi@0: * The "expected output" is each class name gotten with getSimpleName() to unclutter things. aoqi@0: * Each test method returns a classname of the resource and that is compared with aoqi@0: * values in this array. aoqi@0: */ aoqi@0: static String[] expectedOutput = { aoqi@0: "aResource::bResource::cResource", //test1 aoqi@0: "aResource::bResource::cResource&aResource::cResource", //test3 aoqi@0: "aResource::bResource::cResource&aResource::cResource"}; //test2 aoqi@0: aoqi@0: static void compare(String s1, String s2) { aoqi@0: if (s1.compareTo(s2) != 0) { aoqi@0: throw new RuntimeException(s1 + "!=" + s2); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: String test1() { aoqi@0: String ret = null; aoqi@0: try (bResource br = new bResource(new cResource()); aoqi@0: aResource ar = new aResource(br)) { aoqi@0: ret = ar.getClass().getSimpleName() + "::" + aoqi@0: ar.getB().getClass().getSimpleName() + "::" + aoqi@0: ar.getB().getC().getClass().getSimpleName(); aoqi@0: } aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: String test2() { aoqi@0: String ret = null; aoqi@0: try (aResource ar = new aResource(new bResource(new cResource()), new cResource())) { aoqi@0: String abc = ar.getClass().getSimpleName() + "::" + aoqi@0: ar.getB().getClass().getSimpleName() + "::" + aoqi@0: ar.getB().getC().getClass().getSimpleName(); aoqi@0: String ac = ar.getClass().getSimpleName() + "::" + aoqi@0: ar.getC().getClass().getSimpleName(); aoqi@0: ret = abc + "&" + ac; aoqi@0: } aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: String test3() { aoqi@0: String ret = null; aoqi@0: try (bResource br = new bResource(new cResource()); aoqi@0: aResource ar = new aResource(br, new cResource())) { aoqi@0: String abc = ar.getClass().getSimpleName() + "::" + aoqi@0: ar.getB().getClass().getSimpleName() + "::" + aoqi@0: ar.getB().getC().getClass().getSimpleName(); aoqi@0: String ac = ar.getClass().getSimpleName() + "::" + aoqi@0: ar.getC().getClass().getSimpleName(); aoqi@0: ret = abc + "&" + ac; aoqi@0: } aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) { aoqi@0: ResInNestedExpr t = new ResInNestedExpr(); aoqi@0: int eo = 0; aoqi@0: compare(expectedOutput[eo++], t.test1()); aoqi@0: compare(expectedOutput[eo++], t.test3()); aoqi@0: compare(expectedOutput[eo++], t.test2()); aoqi@0: ResInNestedExpr.checkClosedCount(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * A resource to implement AutoCloseable aoqi@0: * Contains two other resources as data items. aoqi@0: */ aoqi@0: static class aResource implements AutoCloseable { aoqi@0: aoqi@0: bResource bR; aoqi@0: cResource cR; aoqi@0: aoqi@0: public aResource() { aoqi@0: bR = null; aoqi@0: cR = null; aoqi@0: } aoqi@0: aoqi@0: public aResource(bResource br) { aoqi@0: bR = br; aoqi@0: } aoqi@0: aoqi@0: public aResource(cResource cr) { aoqi@0: cR = cr; aoqi@0: } aoqi@0: aoqi@0: public aResource(bResource br, cResource cr) { aoqi@0: bR = br; aoqi@0: cR = cr; aoqi@0: } aoqi@0: aoqi@0: public bResource getB() { aoqi@0: return bR; aoqi@0: } aoqi@0: aoqi@0: public cResource getC() { aoqi@0: return cR; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void close() { aoqi@0: ResInNestedExpr.closing(this.getClass().getName()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * A resource to implement AutoCloseable aoqi@0: * Contains one other resources as a data item. aoqi@0: */ aoqi@0: static class bResource implements AutoCloseable { aoqi@0: aoqi@0: cResource cR; aoqi@0: aoqi@0: public bResource() { aoqi@0: cR = null; aoqi@0: } aoqi@0: aoqi@0: public bResource(cResource cr) { aoqi@0: cR = cr; aoqi@0: } aoqi@0: aoqi@0: public cResource getC() { aoqi@0: return cR; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void close() { aoqi@0: ResInNestedExpr.closing(this.getClass().getName()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** A resource to implement AutoCloseable */ aoqi@0: static class cResource implements AutoCloseable { aoqi@0: aoqi@0: public cResource() { aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void close() { aoqi@0: ResInNestedExpr.closing(this.getClass().getName()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: