author | mchung |
Wed, 27 May 2015 13:25:18 -0700 | |
changeset 30846 | 2b3f379840f0 |
parent 21714 | f5b7edec4304 |
permissions | -rw-r--r-- |
21714
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
1 |
/* |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
4 |
* |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
8 |
* |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
13 |
* accompanied this code). |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
14 |
* |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
18 |
* |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
21 |
* questions. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
22 |
*/ |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
23 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
24 |
/* |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
25 |
* @test |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
26 |
* @bug 8025113 |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
27 |
* @author sogoel |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
28 |
* @summary t-w-r completes abruptly if the initialization of resource completes abruptly |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
29 |
*/ |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
30 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
31 |
import java.io.FileInputStream; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
32 |
import java.io.IOException; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
33 |
import java.io.File; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
34 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
35 |
/* |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
36 |
* If the initialization of the resource completes abruptly because of a |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
37 |
* throw of a value V ... and the automatic ->closing of the resource completes normally, |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
38 |
* then the try-with-resources statement completes abruptly because of the throw of value V. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
39 |
*/ |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
40 |
public class TestTwr09 { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
41 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
42 |
/** |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
43 |
* throw from ctor of nested resource |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
44 |
* Check first resource is not open. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
45 |
*/ |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
46 |
String test1() { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
47 |
String ret = null; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
48 |
try (ResCloseable tr = new ResCloseable(new ResCloseable("throw from inner resource ctor",3))) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
49 |
ret = "FAIL"; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
50 |
} catch (RuntimeException re) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
51 |
ret = re.getMessage(); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
52 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
53 |
return ret; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
54 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
55 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
56 |
/** |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
57 |
* throw from ctor of 2nd resource. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
58 |
* 1st resource, FileInputStream should be automatically closed. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
59 |
*/ |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
60 |
String test2() { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
61 |
String ret = null; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
62 |
byte[] buf = new byte[1]; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
63 |
try (java.io.ByteArrayInputStream tr = new java.io.ByteArrayInputStream(buf); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
64 |
ResCloseable str = new ResCloseable("throw from inner resource ctor",3)) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
65 |
ret = "FAIL"; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
66 |
} catch (final IOException fe) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
67 |
ret = "FAIL test2"; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
68 |
} catch (RuntimeException re) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
69 |
ret = "PASS test2"; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
70 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
71 |
System.out.println("Ret = " + ret); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
72 |
return ret; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
73 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
74 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
75 |
public static void main(String... args) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
76 |
TestTwr09 t = new TestTwr09(); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
77 |
if (t.test1().compareTo("throw from inner resource ctor") != 0) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
78 |
throw new RuntimeException("FAIL-test1"); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
79 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
80 |
if (t.test2().compareTo("PASS test2") != 0) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
81 |
throw new RuntimeException("FAIL-test2"); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
82 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
83 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
84 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
85 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
86 |
/** a simple resource the implements AutoCloseable so it can be used |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
87 |
* in twr's resource specification block. |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
88 |
*/ |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
89 |
class ResCloseable implements AutoCloseable { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
90 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
91 |
ResCloseable testres = null; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
92 |
String msg = "default"; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
93 |
boolean bOpen = false; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
94 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
95 |
public ResCloseable() { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
96 |
bOpen = true; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
97 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
98 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
99 |
public ResCloseable(ResCloseable tr) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
100 |
bOpen = true; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
101 |
msg = tr.getMsg(); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
102 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
103 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
104 |
public ResCloseable(String s) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
105 |
bOpen = true; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
106 |
msg = s; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
107 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
108 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
109 |
public ResCloseable(String msg, int c) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
110 |
bOpen = true; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
111 |
if (c == 3) { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
112 |
throw new RuntimeException(msg); |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
113 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
114 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
115 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
116 |
@Override |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
117 |
public void close() { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
118 |
bOpen = false; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
119 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
120 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
121 |
public boolean isOpen() { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
122 |
return bOpen; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
123 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
124 |
|
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
125 |
public String getMsg() { |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
126 |
return msg; |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
127 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
128 |
} |
f5b7edec4304
8025113: Convert 7 tools TryWithResources tests to jtreg format
sogoel
parents:
diff
changeset
|
129 |