Wed, 13 Nov 2013 16:36:08 -0800
8025113: Convert 7 tools TryWithResources tests to jtreg format
Reviewed-by: darcy, jjg
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/TryWithResources/ResDeclOutsideTry.java Wed Nov 13 16:36:08 2013 -0800 1.3 @@ -0,0 +1,18 @@ 1.4 +/* 1.5 + * @test /nodynamiccopyright/ 1.6 + * @bug 8025113 1.7 + * @author sogoel 1.8 + * @summary Resources cannot be declared outside t-w-r block 1.9 + * @compile/fail/ref=ResDeclOutsideTry.out -XDrawDiagnostics ResDeclOutsideTry.java 1.10 + */ 1.11 + 1.12 +public class ResDeclOutsideTry implements AutoCloseable { 1.13 + ResDeclOutsideTry tr1; 1.14 + ResDeclOutsideTry tr2 = new ResDeclOutsideTry(); 1.15 + 1.16 + String test1() { 1.17 + try (tr1 = new ResDeclOutsideTry(); tr2;) { 1.18 + } 1.19 + } 1.20 +} 1.21 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/TryWithResources/ResDeclOutsideTry.out Wed Nov 13 16:36:08 2013 -0800 2.3 @@ -0,0 +1,3 @@ 2.4 +ResDeclOutsideTry.java:14:17: compiler.err.expected: token.identifier 2.5 +ResDeclOutsideTry.java:14:48: compiler.err.expected: token.identifier 2.6 +2 errors
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/TryWithResources/ResInNestedExpr.java Wed Nov 13 16:36:08 2013 -0800 3.3 @@ -0,0 +1,195 @@ 3.4 +/* 3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 8025113 3.30 + * @author sogoel 3.31 + * @summary Resource creation in nested expressions 3.32 + */ 3.33 + 3.34 +/** 3.35 + * This test checks for resource creation in nested expressions. 3.36 + * test1() - Create 3 resource in nested new expressions, style 1 3.37 + * test2() - Create 3 resource in nested new expressions, style 2 3.38 + * test3() - Create 4 resources with resources as parameters: new expression; typeid & new expression 3.39 + */ 3.40 + 3.41 +public class ResInNestedExpr { 3.42 + 3.43 + static final int expected = 5; 3.44 + static int closed = 0; 3.45 + 3.46 + static void closing(String clazz) { 3.47 + closed++; 3.48 + } 3.49 + 3.50 + static void checkClosedCount() { 3.51 + if (expected != closed) { 3.52 + throw new RuntimeException("Did not find enough closed resources." 3.53 + + "Expected " + expected + ", but found " + closed); 3.54 + } 3.55 + } 3.56 + /** 3.57 + * The "expected output" is each class name gotten with getSimpleName() to unclutter things. 3.58 + * Each test method returns a classname of the resource and that is compared with 3.59 + * values in this array. 3.60 + */ 3.61 + static String[] expectedOutput = { 3.62 + "aResource::bResource::cResource", //test1 3.63 + "aResource::bResource::cResource&aResource::cResource", //test3 3.64 + "aResource::bResource::cResource&aResource::cResource"}; //test2 3.65 + 3.66 + static void compare(String s1, String s2) { 3.67 + if (s1.compareTo(s2) != 0) { 3.68 + throw new RuntimeException(s1 + "!=" + s2); 3.69 + } 3.70 + } 3.71 + 3.72 + String test1() { 3.73 + String ret = null; 3.74 + try (bResource br = new bResource(new cResource()); 3.75 + aResource ar = new aResource(br)) { 3.76 + ret = ar.getClass().getSimpleName() + "::" + 3.77 + ar.getB().getClass().getSimpleName() + "::" + 3.78 + ar.getB().getC().getClass().getSimpleName(); 3.79 + } 3.80 + return ret; 3.81 + } 3.82 + 3.83 + String test2() { 3.84 + String ret = null; 3.85 + try (aResource ar = new aResource(new bResource(new cResource()), new cResource())) { 3.86 + String abc = ar.getClass().getSimpleName() + "::" + 3.87 + ar.getB().getClass().getSimpleName() + "::" + 3.88 + ar.getB().getC().getClass().getSimpleName(); 3.89 + String ac = ar.getClass().getSimpleName() + "::" + 3.90 + ar.getC().getClass().getSimpleName(); 3.91 + ret = abc + "&" + ac; 3.92 + } 3.93 + return ret; 3.94 + } 3.95 + 3.96 + String test3() { 3.97 + String ret = null; 3.98 + try (bResource br = new bResource(new cResource()); 3.99 + aResource ar = new aResource(br, new cResource())) { 3.100 + String abc = ar.getClass().getSimpleName() + "::" + 3.101 + ar.getB().getClass().getSimpleName() + "::" + 3.102 + ar.getB().getC().getClass().getSimpleName(); 3.103 + String ac = ar.getClass().getSimpleName() + "::" + 3.104 + ar.getC().getClass().getSimpleName(); 3.105 + ret = abc + "&" + ac; 3.106 + } 3.107 + return ret; 3.108 + } 3.109 + 3.110 + public static void main(String... args) { 3.111 + ResInNestedExpr t = new ResInNestedExpr(); 3.112 + int eo = 0; 3.113 + compare(expectedOutput[eo++], t.test1()); 3.114 + compare(expectedOutput[eo++], t.test3()); 3.115 + compare(expectedOutput[eo++], t.test2()); 3.116 + ResInNestedExpr.checkClosedCount(); 3.117 + } 3.118 + 3.119 + /** 3.120 + * A resource to implement AutoCloseable 3.121 + * Contains two other resources as data items. 3.122 + */ 3.123 + static class aResource implements AutoCloseable { 3.124 + 3.125 + bResource bR; 3.126 + cResource cR; 3.127 + 3.128 + public aResource() { 3.129 + bR = null; 3.130 + cR = null; 3.131 + } 3.132 + 3.133 + public aResource(bResource br) { 3.134 + bR = br; 3.135 + } 3.136 + 3.137 + public aResource(cResource cr) { 3.138 + cR = cr; 3.139 + } 3.140 + 3.141 + public aResource(bResource br, cResource cr) { 3.142 + bR = br; 3.143 + cR = cr; 3.144 + } 3.145 + 3.146 + public bResource getB() { 3.147 + return bR; 3.148 + } 3.149 + 3.150 + public cResource getC() { 3.151 + return cR; 3.152 + } 3.153 + 3.154 + @Override 3.155 + public void close() { 3.156 + ResInNestedExpr.closing(this.getClass().getName()); 3.157 + } 3.158 + } 3.159 + 3.160 + /** 3.161 + * A resource to implement AutoCloseable 3.162 + * Contains one other resources as a data item. 3.163 + */ 3.164 + static class bResource implements AutoCloseable { 3.165 + 3.166 + cResource cR; 3.167 + 3.168 + public bResource() { 3.169 + cR = null; 3.170 + } 3.171 + 3.172 + public bResource(cResource cr) { 3.173 + cR = cr; 3.174 + } 3.175 + 3.176 + public cResource getC() { 3.177 + return cR; 3.178 + } 3.179 + 3.180 + @Override 3.181 + public void close() { 3.182 + ResInNestedExpr.closing(this.getClass().getName()); 3.183 + } 3.184 + } 3.185 + 3.186 + /** A resource to implement AutoCloseable */ 3.187 + static class cResource implements AutoCloseable { 3.188 + 3.189 + public cResource() { 3.190 + } 3.191 + 3.192 + @Override 3.193 + public void close() { 3.194 + ResInNestedExpr.closing(this.getClass().getName()); 3.195 + } 3.196 + } 3.197 +} 3.198 +
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/TryWithResources/ResourceNameConflict.java Wed Nov 13 16:36:08 2013 -0800 4.3 @@ -0,0 +1,36 @@ 4.4 +/* 4.5 + * @test /nodynamiccopyright/ 4.6 + * @bug 8025113 4.7 + * @author sogoel 4.8 + * @summary Resource var cannot have same name as local variable 4.9 + * @compile/fail/ref=ResourceNameConflict.out -XDrawDiagnostics ResourceNameConflict.java 4.10 + */ 4.11 + 4.12 +/** 4.13 + * Test methods and their description 4.14 + * test1() - negative test - local variable used as test resource 4.15 + * test2() - negative test - test resource already defined in an enclosing for statement 4.16 + */ 4.17 + 4.18 +public class ResourceNameConflict implements AutoCloseable { 4.19 + 4.20 + static final String str = "asdf"; 4.21 + 4.22 + void test1() { 4.23 + String tr = "A resource spec var cannot have same name as local var."; 4.24 + try (ResourceNameConflict tr = new ResourceNameConflict()) { 4.25 + } 4.26 + } 4.27 + 4.28 + void test2(String... strArray) { 4.29 + for (String str : strArray) { 4.30 + try (ResourceNameConflict str = new ResourceNameConflict()) { 4.31 + } 4.32 + } 4.33 + } 4.34 + 4.35 + @Override 4.36 + public void close() { 4.37 + } 4.38 +} 4.39 +
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/TryWithResources/ResourceNameConflict.out Wed Nov 13 16:36:08 2013 -0800 5.3 @@ -0,0 +1,3 @@ 5.4 +ResourceNameConflict.java:21:35: compiler.err.already.defined: kindname.variable, tr, kindname.method, test1() 5.5 +ResourceNameConflict.java:27:39: compiler.err.already.defined: kindname.variable, str, kindname.method, test2(java.lang.String...) 5.6 +2 errors
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/TryWithResources/ResourceRedecl.java Wed Nov 13 16:36:08 2013 -0800 6.3 @@ -0,0 +1,37 @@ 6.4 +/* 6.5 + * @test /nodynamiccopyright/ 6.6 + * @bug 8025113 6.7 + * @author sogoel 6.8 + * @summary Redeclaration of resource variables 6.9 + * @compile/fail/ref=ResourceRedecl.out -XDrawDiagnostics ResourceRedecl.java 6.10 + */ 6.11 + 6.12 +import java.io.*; 6.13 + 6.14 +public class ResourceRedecl { 6.15 + 6.16 + public void test() { 6.17 + // compiler error if name of an exception param is redeclared within the Block of the catch clause as a local var; 6.18 + // or as an exception param of a catch clause in a try statement; 6.19 + // or as a resource in a try-with-resources statement 6.20 + try { 6.21 + } catch (Exception exParam1) { 6.22 + Object exParam1 = new Object(); 6.23 + try (java.io.FileInputStream exParam1 = new java.io.FileInputStream("foo.txt")) { 6.24 + Object exParam1 = new Object(); 6.25 + } catch (IOException exParam1) { 6.26 + } 6.27 + } 6.28 + 6.29 + // compiler error if resource is redeclared within the try Block as a local var 6.30 + // or as an exception param of a catch clause in a try statement 6.31 + try (java.io.FileInputStream exParam2 = new java.io.FileInputStream("bar.txt")) { 6.32 + Object exParam2 = new Object(); 6.33 + try (BufferedReader br = new BufferedReader(new FileReader("zee.txt"))) { 6.34 + } catch (IOException exParam2) { 6.35 + } 6.36 + } catch (Exception ex) { 6.37 + } 6.38 + } 6.39 +} 6.40 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/TryWithResources/ResourceRedecl.out Wed Nov 13 16:36:08 2013 -0800 7.3 @@ -0,0 +1,7 @@ 7.4 +ResourceRedecl.java:19:20: compiler.err.already.defined: kindname.variable, exParam1, kindname.method, test() 7.5 +ResourceRedecl.java:20:42: compiler.err.already.defined: kindname.variable, exParam1, kindname.method, test() 7.6 +ResourceRedecl.java:21:24: compiler.err.already.defined: kindname.variable, exParam1, kindname.method, test() 7.7 +ResourceRedecl.java:22:34: compiler.err.already.defined: kindname.variable, exParam1, kindname.method, test() 7.8 +ResourceRedecl.java:29:20: compiler.err.already.defined: kindname.variable, exParam2, kindname.method, test() 7.9 +ResourceRedecl.java:31:34: compiler.err.already.defined: kindname.variable, exParam2, kindname.method, test() 7.10 +6 errors
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/TryWithResources/ResourceShadow.java Wed Nov 13 16:36:08 2013 -0800 8.3 @@ -0,0 +1,119 @@ 8.4 +/* 8.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. 8.11 + * 8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.15 + * version 2 for more details (a copy is included in the LICENSE file that 8.16 + * accompanied this code). 8.17 + * 8.18 + * You should have received a copy of the GNU General Public License version 8.19 + * 2 along with this work; if not, write to the Free Software Foundation, 8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.21 + * 8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.23 + * or visit www.oracle.com if you need additional information or have any 8.24 + * questions. 8.25 + */ 8.26 + 8.27 +/* 8.28 + * @test 8.29 + * @bug 8025113 8.30 + * @author sogoel 8.31 + * @summary Test shadowing of resource variable 8.32 + */ 8.33 + 8.34 +/* 8.35 + * "...a variable declared in a resource specification 8.36 + * may be shadowed (6.3.1) anywhere inside a class declaration nested 8.37 + * within the Block of the try." 8.38 + */ 8.39 +public class ResourceShadow { 8.40 + 8.41 + static final String str = "asdf"; //this is okay 8.42 + 8.43 + /** 8.44 + * Resource variable shadows switch and case variables 8.45 + */ 8.46 + String test1() { 8.47 + String ret = null; 8.48 + switch (str) { 8.49 + case str: //this is okay 8.50 + try (SilentCloseable str = new SilentCloseable()) { 8.51 + SilentCloseable tr = new SilentCloseable(str); 8.52 + ret = str.getClass().getSimpleName(); 8.53 + } 8.54 + break; 8.55 + default: 8.56 + ret = ""; 8.57 + } 8.58 + return ret; 8.59 + } 8.60 + 8.61 + /** 8.62 + * Resource variable may be shadowed (6.3.1) anywhere inside a class 8.63 + * declaration nested within the Block of the try 8.64 + */ 8.65 + String test2() { 8.66 + String ret = null; 8.67 + try (SilentCloseable str = new SilentCloseable()) { 8.68 + class temp { 8.69 + 8.70 + String str = "I am not a SilentCloseable"; 8.71 + 8.72 + public void printSTR() { 8.73 + System.out.println(str); 8.74 + } 8.75 + 8.76 + public String getSTR() { 8.77 + return str; 8.78 + } 8.79 + } 8.80 + temp tmp = new temp(); 8.81 + SilentCloseable tr = new SilentCloseable(tmp.getSTR()); 8.82 + ret = tr.getMsg(); 8.83 + } 8.84 + return ret; 8.85 + } 8.86 + 8.87 + public static void main(String... args) { 8.88 + ResourceShadow t = new ResourceShadow(); 8.89 + if (t.test1().compareTo("SilentCloseable") != 0) { 8.90 + throw new RuntimeException("FAIL-test1"); 8.91 + } 8.92 + if (t.test2().compareTo("I am not a SilentCloseable") != 0) { 8.93 + throw new RuntimeException("FAIL-test2"); 8.94 + } 8.95 + } 8.96 +} 8.97 + 8.98 +class SilentCloseable implements AutoCloseable { 8.99 + 8.100 + SilentCloseable testres = null; 8.101 + String msg = "default"; 8.102 + 8.103 + @Override 8.104 + public void close() { 8.105 + } 8.106 + 8.107 + public SilentCloseable() { 8.108 + } 8.109 + 8.110 + public SilentCloseable(String s) { 8.111 + msg = s; 8.112 + } 8.113 + 8.114 + public SilentCloseable(SilentCloseable tr) { 8.115 + testres = tr; 8.116 + } 8.117 + 8.118 + public String getMsg() { 8.119 + return msg; 8.120 + } 8.121 +} 8.122 +
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/tools/javac/TryWithResources/TestTwr09.java Wed Nov 13 16:36:08 2013 -0800 9.3 @@ -0,0 +1,129 @@ 9.4 +/* 9.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 + * 9.8 + * This code is free software; you can redistribute it and/or modify it 9.9 + * under the terms of the GNU General Public License version 2 only, as 9.10 + * published by the Free Software Foundation. 9.11 + * 9.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 9.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.15 + * version 2 for more details (a copy is included in the LICENSE file that 9.16 + * accompanied this code). 9.17 + * 9.18 + * You should have received a copy of the GNU General Public License version 9.19 + * 2 along with this work; if not, write to the Free Software Foundation, 9.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.21 + * 9.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.23 + * or visit www.oracle.com if you need additional information or have any 9.24 + * questions. 9.25 + */ 9.26 + 9.27 +/* 9.28 + * @test 9.29 + * @bug 8025113 9.30 + * @author sogoel 9.31 + * @summary t-w-r completes abruptly if the initialization of resource completes abruptly 9.32 + */ 9.33 + 9.34 +import java.io.FileInputStream; 9.35 +import java.io.IOException; 9.36 +import java.io.File; 9.37 + 9.38 +/* 9.39 + * If the initialization of the resource completes abruptly because of a 9.40 + * throw of a value V ... and the automatic ->closing of the resource completes normally, 9.41 + * then the try-with-resources statement completes abruptly because of the throw of value V. 9.42 + */ 9.43 +public class TestTwr09 { 9.44 + 9.45 + /** 9.46 + * throw from ctor of nested resource 9.47 + * Check first resource is not open. 9.48 + */ 9.49 + String test1() { 9.50 + String ret = null; 9.51 + try (ResCloseable tr = new ResCloseable(new ResCloseable("throw from inner resource ctor",3))) { 9.52 + ret = "FAIL"; 9.53 + } catch (RuntimeException re) { 9.54 + ret = re.getMessage(); 9.55 + } 9.56 + return ret; 9.57 + } 9.58 + 9.59 + /** 9.60 + * throw from ctor of 2nd resource. 9.61 + * 1st resource, FileInputStream should be automatically closed. 9.62 + */ 9.63 + String test2() { 9.64 + String ret = null; 9.65 + byte[] buf = new byte[1]; 9.66 + try (java.io.ByteArrayInputStream tr = new java.io.ByteArrayInputStream(buf); 9.67 + ResCloseable str = new ResCloseable("throw from inner resource ctor",3)) { 9.68 + ret = "FAIL"; 9.69 + } catch (final IOException fe) { 9.70 + ret = "FAIL test2"; 9.71 + } catch (RuntimeException re) { 9.72 + ret = "PASS test2"; 9.73 + } 9.74 + System.out.println("Ret = " + ret); 9.75 + return ret; 9.76 + } 9.77 + 9.78 + public static void main(String... args) { 9.79 + TestTwr09 t = new TestTwr09(); 9.80 + if (t.test1().compareTo("throw from inner resource ctor") != 0) { 9.81 + throw new RuntimeException("FAIL-test1"); 9.82 + } 9.83 + if (t.test2().compareTo("PASS test2") != 0) { 9.84 + throw new RuntimeException("FAIL-test2"); 9.85 + } 9.86 + } 9.87 +} 9.88 + 9.89 +/** a simple resource the implements AutoCloseable so it can be used 9.90 + * in twr's resource specification block. 9.91 + */ 9.92 +class ResCloseable implements AutoCloseable { 9.93 + 9.94 + ResCloseable testres = null; 9.95 + String msg = "default"; 9.96 + boolean bOpen = false; 9.97 + 9.98 + public ResCloseable() { 9.99 + bOpen = true; 9.100 + } 9.101 + 9.102 + public ResCloseable(ResCloseable tr) { 9.103 + bOpen = true; 9.104 + msg = tr.getMsg(); 9.105 + } 9.106 + 9.107 + public ResCloseable(String s) { 9.108 + bOpen = true; 9.109 + msg = s; 9.110 + } 9.111 + 9.112 + public ResCloseable(String msg, int c) { 9.113 + bOpen = true; 9.114 + if (c == 3) { 9.115 + throw new RuntimeException(msg); 9.116 + } 9.117 + } 9.118 + 9.119 + @Override 9.120 + public void close() { 9.121 + bOpen = false; 9.122 + } 9.123 + 9.124 + public boolean isOpen() { 9.125 + return bOpen; 9.126 + } 9.127 + 9.128 + public String getMsg() { 9.129 + return msg; 9.130 + } 9.131 +} 9.132 +