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 t-w-r completes abruptly if the initialization of resource completes abruptly
29 */
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.File;
35 /*
36 * If the initialization of the resource completes abruptly because of a
37 * throw of a value V ... and the automatic ->closing of the resource completes normally,
38 * then the try-with-resources statement completes abruptly because of the throw of value V.
39 */
40 public class TestTwr09 {
42 /**
43 * throw from ctor of nested resource
44 * Check first resource is not open.
45 */
46 String test1() {
47 String ret = null;
48 try (ResCloseable tr = new ResCloseable(new ResCloseable("throw from inner resource ctor",3))) {
49 ret = "FAIL";
50 } catch (RuntimeException re) {
51 ret = re.getMessage();
52 }
53 return ret;
54 }
56 /**
57 * throw from ctor of 2nd resource.
58 * 1st resource, FileInputStream should be automatically closed.
59 */
60 String test2() {
61 String ret = null;
62 byte[] buf = new byte[1];
63 try (java.io.ByteArrayInputStream tr = new java.io.ByteArrayInputStream(buf);
64 ResCloseable str = new ResCloseable("throw from inner resource ctor",3)) {
65 ret = "FAIL";
66 } catch (final IOException fe) {
67 ret = "FAIL test2";
68 } catch (RuntimeException re) {
69 ret = "PASS test2";
70 }
71 System.out.println("Ret = " + ret);
72 return ret;
73 }
75 public static void main(String... args) {
76 TestTwr09 t = new TestTwr09();
77 if (t.test1().compareTo("throw from inner resource ctor") != 0) {
78 throw new RuntimeException("FAIL-test1");
79 }
80 if (t.test2().compareTo("PASS test2") != 0) {
81 throw new RuntimeException("FAIL-test2");
82 }
83 }
84 }
86 /** a simple resource the implements AutoCloseable so it can be used
87 * in twr's resource specification block.
88 */
89 class ResCloseable implements AutoCloseable {
91 ResCloseable testres = null;
92 String msg = "default";
93 boolean bOpen = false;
95 public ResCloseable() {
96 bOpen = true;
97 }
99 public ResCloseable(ResCloseable tr) {
100 bOpen = true;
101 msg = tr.getMsg();
102 }
104 public ResCloseable(String s) {
105 bOpen = true;
106 msg = s;
107 }
109 public ResCloseable(String msg, int c) {
110 bOpen = true;
111 if (c == 3) {
112 throw new RuntimeException(msg);
113 }
114 }
116 @Override
117 public void close() {
118 bOpen = false;
119 }
121 public boolean isOpen() {
122 return bOpen;
123 }
125 public String getMsg() {
126 return msg;
127 }
128 }