test/tools/javac/TryWithResources/TestTwr09.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/TryWithResources/TestTwr09.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug     8025113
    1.30 + * @author  sogoel
    1.31 + * @summary t-w-r completes abruptly if the initialization of resource completes abruptly
    1.32 + */
    1.33 +
    1.34 +import java.io.FileInputStream;
    1.35 +import java.io.IOException;
    1.36 +import java.io.File;
    1.37 +
    1.38 +/*
    1.39 + * If the initialization of the resource completes abruptly because of a
    1.40 + * throw of a value V ... and the automatic ->closing of the resource completes normally,
    1.41 + * then the try-with-resources statement completes abruptly because of the throw of value V.
    1.42 + */
    1.43 +public class TestTwr09 {
    1.44 +
    1.45 +    /**
    1.46 +     * throw from ctor of nested resource
    1.47 +     * Check first resource is not open.
    1.48 +     */
    1.49 +    String test1() {
    1.50 +        String ret = null;
    1.51 +        try (ResCloseable tr = new ResCloseable(new ResCloseable("throw from inner resource ctor",3))) {
    1.52 +            ret = "FAIL";
    1.53 +        } catch (RuntimeException re) {
    1.54 +            ret = re.getMessage();
    1.55 +        }
    1.56 +        return ret;
    1.57 +    }
    1.58 +
    1.59 +    /**
    1.60 +     * throw from ctor of 2nd resource.
    1.61 +     * 1st resource, FileInputStream should be automatically closed.
    1.62 +     */
    1.63 +    String test2() {
    1.64 +        String ret = null;
    1.65 +        byte[] buf = new byte[1];
    1.66 +        try (java.io.ByteArrayInputStream tr = new java.io.ByteArrayInputStream(buf);
    1.67 +            ResCloseable str = new ResCloseable("throw from inner resource ctor",3)) {
    1.68 +            ret = "FAIL";
    1.69 +        } catch (final IOException fe) {
    1.70 +            ret = "FAIL test2";
    1.71 +        } catch (RuntimeException re) {
    1.72 +            ret = "PASS test2";
    1.73 +        }
    1.74 +        System.out.println("Ret = " + ret);
    1.75 +        return ret;
    1.76 +    }
    1.77 +
    1.78 +    public static void main(String... args) {
    1.79 +        TestTwr09 t = new TestTwr09();
    1.80 +        if (t.test1().compareTo("throw from inner resource ctor") != 0) {
    1.81 +            throw new RuntimeException("FAIL-test1");
    1.82 +        }
    1.83 +        if (t.test2().compareTo("PASS test2") != 0) {
    1.84 +            throw new RuntimeException("FAIL-test2");
    1.85 +        }
    1.86 +    }
    1.87 +}
    1.88 +
    1.89 +/** a simple resource the implements AutoCloseable so it can be used
    1.90 + * in twr's resource specification block.
    1.91 + */
    1.92 +class ResCloseable implements AutoCloseable {
    1.93 +
    1.94 +    ResCloseable testres = null;
    1.95 +    String msg = "default";
    1.96 +    boolean bOpen = false;
    1.97 +
    1.98 +    public ResCloseable() {
    1.99 +        bOpen = true;
   1.100 +    }
   1.101 +
   1.102 +    public ResCloseable(ResCloseable tr) {
   1.103 +        bOpen = true;
   1.104 +        msg = tr.getMsg();
   1.105 +    }
   1.106 +
   1.107 +    public ResCloseable(String s) {
   1.108 +        bOpen = true;
   1.109 +        msg = s;
   1.110 +    }
   1.111 +
   1.112 +    public ResCloseable(String msg, int c) {
   1.113 +        bOpen = true;
   1.114 +        if (c == 3) {
   1.115 +            throw new RuntimeException(msg);
   1.116 +        }
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    public void close() {
   1.121 +        bOpen = false;
   1.122 +    }
   1.123 +
   1.124 +    public boolean isOpen() {
   1.125 +        return bOpen;
   1.126 +    }
   1.127 +
   1.128 +    public String getMsg() {
   1.129 +        return msg;
   1.130 +    }
   1.131 +}
   1.132 +

mercurial