Fri, 24 Apr 2020 03:58:51 +0100
Merge
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 8025113
27 * @author sogoel
28 * @summary Resource creation in nested expressions
29 */
31 /**
32 * This test checks for resource creation in nested expressions.
33 * test1() - Create 3 resource in nested new expressions, style 1
34 * test2() - Create 3 resource in nested new expressions, style 2
35 * test3() - Create 4 resources with resources as parameters: new expression; typeid & new expression
36 */
38 public class ResInNestedExpr {
40 static final int expected = 5;
41 static int closed = 0;
43 static void closing(String clazz) {
44 closed++;
45 }
47 static void checkClosedCount() {
48 if (expected != closed) {
49 throw new RuntimeException("Did not find enough closed resources."
50 + "Expected " + expected + ", but found " + closed);
51 }
52 }
53 /**
54 * The "expected output" is each class name gotten with getSimpleName() to unclutter things.
55 * Each test method returns a classname of the resource and that is compared with
56 * values in this array.
57 */
58 static String[] expectedOutput = {
59 "aResource::bResource::cResource", //test1
60 "aResource::bResource::cResource&aResource::cResource", //test3
61 "aResource::bResource::cResource&aResource::cResource"}; //test2
63 static void compare(String s1, String s2) {
64 if (s1.compareTo(s2) != 0) {
65 throw new RuntimeException(s1 + "!=" + s2);
66 }
67 }
69 String test1() {
70 String ret = null;
71 try (bResource br = new bResource(new cResource());
72 aResource ar = new aResource(br)) {
73 ret = ar.getClass().getSimpleName() + "::" +
74 ar.getB().getClass().getSimpleName() + "::" +
75 ar.getB().getC().getClass().getSimpleName();
76 }
77 return ret;
78 }
80 String test2() {
81 String ret = null;
82 try (aResource ar = new aResource(new bResource(new cResource()), new cResource())) {
83 String abc = ar.getClass().getSimpleName() + "::" +
84 ar.getB().getClass().getSimpleName() + "::" +
85 ar.getB().getC().getClass().getSimpleName();
86 String ac = ar.getClass().getSimpleName() + "::" +
87 ar.getC().getClass().getSimpleName();
88 ret = abc + "&" + ac;
89 }
90 return ret;
91 }
93 String test3() {
94 String ret = null;
95 try (bResource br = new bResource(new cResource());
96 aResource ar = new aResource(br, new cResource())) {
97 String abc = ar.getClass().getSimpleName() + "::" +
98 ar.getB().getClass().getSimpleName() + "::" +
99 ar.getB().getC().getClass().getSimpleName();
100 String ac = ar.getClass().getSimpleName() + "::" +
101 ar.getC().getClass().getSimpleName();
102 ret = abc + "&" + ac;
103 }
104 return ret;
105 }
107 public static void main(String... args) {
108 ResInNestedExpr t = new ResInNestedExpr();
109 int eo = 0;
110 compare(expectedOutput[eo++], t.test1());
111 compare(expectedOutput[eo++], t.test3());
112 compare(expectedOutput[eo++], t.test2());
113 ResInNestedExpr.checkClosedCount();
114 }
116 /**
117 * A resource to implement AutoCloseable
118 * Contains two other resources as data items.
119 */
120 static class aResource implements AutoCloseable {
122 bResource bR;
123 cResource cR;
125 public aResource() {
126 bR = null;
127 cR = null;
128 }
130 public aResource(bResource br) {
131 bR = br;
132 }
134 public aResource(cResource cr) {
135 cR = cr;
136 }
138 public aResource(bResource br, cResource cr) {
139 bR = br;
140 cR = cr;
141 }
143 public bResource getB() {
144 return bR;
145 }
147 public cResource getC() {
148 return cR;
149 }
151 @Override
152 public void close() {
153 ResInNestedExpr.closing(this.getClass().getName());
154 }
155 }
157 /**
158 * A resource to implement AutoCloseable
159 * Contains one other resources as a data item.
160 */
161 static class bResource implements AutoCloseable {
163 cResource cR;
165 public bResource() {
166 cR = null;
167 }
169 public bResource(cResource cr) {
170 cR = cr;
171 }
173 public cResource getC() {
174 return cR;
175 }
177 @Override
178 public void close() {
179 ResInNestedExpr.closing(this.getClass().getName());
180 }
181 }
183 /** A resource to implement AutoCloseable */
184 static class cResource implements AutoCloseable {
186 public cResource() {
187 }
189 @Override
190 public void close() {
191 ResInNestedExpr.closing(this.getClass().getName());
192 }
193 }
194 }