test/tools/javac/TryWithResources/ResInNestedExpr.java

Mon, 26 Oct 2015 13:23:30 -0700

author
asaha
date
Mon, 26 Oct 2015 13:23:30 -0700
changeset 2999
683b3e7e05a7
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8u76-b00 for changeset 10ffafaf5340

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, 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 8025113
aoqi@0 27 * @author sogoel
aoqi@0 28 * @summary Resource creation in nested expressions
aoqi@0 29 */
aoqi@0 30
aoqi@0 31 /**
aoqi@0 32 * This test checks for resource creation in nested expressions.
aoqi@0 33 * test1() - Create 3 resource in nested new expressions, style 1
aoqi@0 34 * test2() - Create 3 resource in nested new expressions, style 2
aoqi@0 35 * test3() - Create 4 resources with resources as parameters: new expression; typeid & new expression
aoqi@0 36 */
aoqi@0 37
aoqi@0 38 public class ResInNestedExpr {
aoqi@0 39
aoqi@0 40 static final int expected = 5;
aoqi@0 41 static int closed = 0;
aoqi@0 42
aoqi@0 43 static void closing(String clazz) {
aoqi@0 44 closed++;
aoqi@0 45 }
aoqi@0 46
aoqi@0 47 static void checkClosedCount() {
aoqi@0 48 if (expected != closed) {
aoqi@0 49 throw new RuntimeException("Did not find enough closed resources."
aoqi@0 50 + "Expected " + expected + ", but found " + closed);
aoqi@0 51 }
aoqi@0 52 }
aoqi@0 53 /**
aoqi@0 54 * The "expected output" is each class name gotten with getSimpleName() to unclutter things.
aoqi@0 55 * Each test method returns a classname of the resource and that is compared with
aoqi@0 56 * values in this array.
aoqi@0 57 */
aoqi@0 58 static String[] expectedOutput = {
aoqi@0 59 "aResource::bResource::cResource", //test1
aoqi@0 60 "aResource::bResource::cResource&aResource::cResource", //test3
aoqi@0 61 "aResource::bResource::cResource&aResource::cResource"}; //test2
aoqi@0 62
aoqi@0 63 static void compare(String s1, String s2) {
aoqi@0 64 if (s1.compareTo(s2) != 0) {
aoqi@0 65 throw new RuntimeException(s1 + "!=" + s2);
aoqi@0 66 }
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 String test1() {
aoqi@0 70 String ret = null;
aoqi@0 71 try (bResource br = new bResource(new cResource());
aoqi@0 72 aResource ar = new aResource(br)) {
aoqi@0 73 ret = ar.getClass().getSimpleName() + "::" +
aoqi@0 74 ar.getB().getClass().getSimpleName() + "::" +
aoqi@0 75 ar.getB().getC().getClass().getSimpleName();
aoqi@0 76 }
aoqi@0 77 return ret;
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 String test2() {
aoqi@0 81 String ret = null;
aoqi@0 82 try (aResource ar = new aResource(new bResource(new cResource()), new cResource())) {
aoqi@0 83 String abc = ar.getClass().getSimpleName() + "::" +
aoqi@0 84 ar.getB().getClass().getSimpleName() + "::" +
aoqi@0 85 ar.getB().getC().getClass().getSimpleName();
aoqi@0 86 String ac = ar.getClass().getSimpleName() + "::" +
aoqi@0 87 ar.getC().getClass().getSimpleName();
aoqi@0 88 ret = abc + "&" + ac;
aoqi@0 89 }
aoqi@0 90 return ret;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 String test3() {
aoqi@0 94 String ret = null;
aoqi@0 95 try (bResource br = new bResource(new cResource());
aoqi@0 96 aResource ar = new aResource(br, new cResource())) {
aoqi@0 97 String abc = ar.getClass().getSimpleName() + "::" +
aoqi@0 98 ar.getB().getClass().getSimpleName() + "::" +
aoqi@0 99 ar.getB().getC().getClass().getSimpleName();
aoqi@0 100 String ac = ar.getClass().getSimpleName() + "::" +
aoqi@0 101 ar.getC().getClass().getSimpleName();
aoqi@0 102 ret = abc + "&" + ac;
aoqi@0 103 }
aoqi@0 104 return ret;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 public static void main(String... args) {
aoqi@0 108 ResInNestedExpr t = new ResInNestedExpr();
aoqi@0 109 int eo = 0;
aoqi@0 110 compare(expectedOutput[eo++], t.test1());
aoqi@0 111 compare(expectedOutput[eo++], t.test3());
aoqi@0 112 compare(expectedOutput[eo++], t.test2());
aoqi@0 113 ResInNestedExpr.checkClosedCount();
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 /**
aoqi@0 117 * A resource to implement AutoCloseable
aoqi@0 118 * Contains two other resources as data items.
aoqi@0 119 */
aoqi@0 120 static class aResource implements AutoCloseable {
aoqi@0 121
aoqi@0 122 bResource bR;
aoqi@0 123 cResource cR;
aoqi@0 124
aoqi@0 125 public aResource() {
aoqi@0 126 bR = null;
aoqi@0 127 cR = null;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public aResource(bResource br) {
aoqi@0 131 bR = br;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public aResource(cResource cr) {
aoqi@0 135 cR = cr;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public aResource(bResource br, cResource cr) {
aoqi@0 139 bR = br;
aoqi@0 140 cR = cr;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 public bResource getB() {
aoqi@0 144 return bR;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 public cResource getC() {
aoqi@0 148 return cR;
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 @Override
aoqi@0 152 public void close() {
aoqi@0 153 ResInNestedExpr.closing(this.getClass().getName());
aoqi@0 154 }
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 /**
aoqi@0 158 * A resource to implement AutoCloseable
aoqi@0 159 * Contains one other resources as a data item.
aoqi@0 160 */
aoqi@0 161 static class bResource implements AutoCloseable {
aoqi@0 162
aoqi@0 163 cResource cR;
aoqi@0 164
aoqi@0 165 public bResource() {
aoqi@0 166 cR = null;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 public bResource(cResource cr) {
aoqi@0 170 cR = cr;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 public cResource getC() {
aoqi@0 174 return cR;
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 @Override
aoqi@0 178 public void close() {
aoqi@0 179 ResInNestedExpr.closing(this.getClass().getName());
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 /** A resource to implement AutoCloseable */
aoqi@0 184 static class cResource implements AutoCloseable {
aoqi@0 185
aoqi@0 186 public cResource() {
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 @Override
aoqi@0 190 public void close() {
aoqi@0 191 ResInNestedExpr.closing(this.getClass().getName());
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194 }
aoqi@0 195

mercurial