test/tools/javac/TryWithResources/ResInNestedExpr.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/TryWithResources/ResInNestedExpr.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,195 @@
     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 Resource creation in nested expressions
    1.32 + */
    1.33 +
    1.34 +/**
    1.35 + * This test checks for resource creation in nested expressions.
    1.36 + * test1() - Create 3 resource in nested new expressions, style 1
    1.37 + * test2() - Create 3 resource in nested new expressions, style 2
    1.38 + * test3() - Create 4 resources with resources as parameters: new expression; typeid & new expression
    1.39 + */
    1.40 +
    1.41 +public class ResInNestedExpr {
    1.42 +
    1.43 +    static final int expected = 5;
    1.44 +    static int closed = 0;
    1.45 +
    1.46 +    static void closing(String clazz) {
    1.47 +        closed++;
    1.48 +    }
    1.49 +
    1.50 +    static void checkClosedCount() {
    1.51 +        if (expected != closed) {
    1.52 +            throw new RuntimeException("Did not find enough closed resources."
    1.53 +               + "Expected " + expected + ", but found " + closed);
    1.54 +        }
    1.55 +    }
    1.56 +    /**
    1.57 +     * The "expected output" is each class name gotten with getSimpleName() to unclutter things.
    1.58 +     * Each test method returns a classname of the resource and that is compared with
    1.59 +     * values in this array.
    1.60 +     */
    1.61 +    static String[] expectedOutput = {
    1.62 +        "aResource::bResource::cResource", //test1
    1.63 +        "aResource::bResource::cResource&aResource::cResource", //test3
    1.64 +        "aResource::bResource::cResource&aResource::cResource"}; //test2
    1.65 +
    1.66 +    static void compare(String s1, String s2) {
    1.67 +        if (s1.compareTo(s2) != 0) {
    1.68 +            throw new RuntimeException(s1 + "!=" + s2);
    1.69 +        }
    1.70 +    }
    1.71 +
    1.72 +    String test1() {
    1.73 +        String ret = null;
    1.74 +        try (bResource br = new bResource(new cResource());
    1.75 +                aResource ar = new aResource(br)) {
    1.76 +            ret = ar.getClass().getSimpleName() + "::" +
    1.77 +                  ar.getB().getClass().getSimpleName() + "::" +
    1.78 +                  ar.getB().getC().getClass().getSimpleName();
    1.79 +        }
    1.80 +        return ret;
    1.81 +    }
    1.82 +
    1.83 +    String test2() {
    1.84 +        String ret = null;
    1.85 +        try (aResource ar = new aResource(new bResource(new cResource()), new cResource())) {
    1.86 +            String abc = ar.getClass().getSimpleName() + "::" +
    1.87 +                         ar.getB().getClass().getSimpleName() + "::" +
    1.88 +                         ar.getB().getC().getClass().getSimpleName();
    1.89 +            String ac = ar.getClass().getSimpleName() + "::" +
    1.90 +                        ar.getC().getClass().getSimpleName();
    1.91 +            ret = abc + "&" + ac;
    1.92 +        }
    1.93 +        return ret;
    1.94 +    }
    1.95 +
    1.96 +    String test3() {
    1.97 +        String ret = null;
    1.98 +        try (bResource br = new bResource(new cResource());
    1.99 +                aResource ar = new aResource(br, new cResource())) {
   1.100 +            String abc = ar.getClass().getSimpleName() + "::" +
   1.101 +                         ar.getB().getClass().getSimpleName() + "::" +
   1.102 +                         ar.getB().getC().getClass().getSimpleName();
   1.103 +            String ac = ar.getClass().getSimpleName() + "::" +
   1.104 +                        ar.getC().getClass().getSimpleName();
   1.105 +            ret = abc + "&" + ac;
   1.106 +        }
   1.107 +        return ret;
   1.108 +    }
   1.109 +
   1.110 +    public static void main(String... args) {
   1.111 +        ResInNestedExpr t = new ResInNestedExpr();
   1.112 +        int eo = 0;
   1.113 +        compare(expectedOutput[eo++], t.test1());
   1.114 +        compare(expectedOutput[eo++], t.test3());
   1.115 +        compare(expectedOutput[eo++], t.test2());
   1.116 +        ResInNestedExpr.checkClosedCount();
   1.117 +    }
   1.118 +
   1.119 +    /**
   1.120 +     * A resource to implement AutoCloseable
   1.121 +     * Contains two other resources as data items.
   1.122 +     */
   1.123 +    static class aResource implements AutoCloseable {
   1.124 +
   1.125 +        bResource bR;
   1.126 +        cResource cR;
   1.127 +
   1.128 +        public aResource() {
   1.129 +            bR = null;
   1.130 +            cR = null;
   1.131 +        }
   1.132 +
   1.133 +        public aResource(bResource br) {
   1.134 +            bR = br;
   1.135 +        }
   1.136 +
   1.137 +        public aResource(cResource cr) {
   1.138 +            cR = cr;
   1.139 +        }
   1.140 +
   1.141 +        public aResource(bResource br, cResource cr) {
   1.142 +            bR = br;
   1.143 +            cR = cr;
   1.144 +        }
   1.145 +
   1.146 +        public bResource getB() {
   1.147 +            return bR;
   1.148 +        }
   1.149 +
   1.150 +        public cResource getC() {
   1.151 +            return cR;
   1.152 +        }
   1.153 +
   1.154 +        @Override
   1.155 +        public void close() {
   1.156 +            ResInNestedExpr.closing(this.getClass().getName());
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * A resource to implement AutoCloseable
   1.162 +     * Contains one other resources as a data item.
   1.163 +     */
   1.164 +    static class bResource implements AutoCloseable {
   1.165 +
   1.166 +        cResource cR;
   1.167 +
   1.168 +        public bResource() {
   1.169 +            cR = null;
   1.170 +        }
   1.171 +
   1.172 +        public bResource(cResource cr) {
   1.173 +            cR = cr;
   1.174 +        }
   1.175 +
   1.176 +        public cResource getC() {
   1.177 +            return cR;
   1.178 +        }
   1.179 +
   1.180 +        @Override
   1.181 +        public void close() {
   1.182 +            ResInNestedExpr.closing(this.getClass().getName());
   1.183 +        }
   1.184 +    }
   1.185 +
   1.186 +    /** A resource to implement AutoCloseable */
   1.187 +    static class cResource implements AutoCloseable {
   1.188 +
   1.189 +        public cResource() {
   1.190 +        }
   1.191 +
   1.192 +        @Override
   1.193 +        public void close() {
   1.194 +            ResInNestedExpr.closing(this.getClass().getName());
   1.195 +        }
   1.196 +    }
   1.197 +}
   1.198 +

mercurial