File: TwrClose.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (140 lines) | stat: -rw-r--r-- 5,978 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 7020499
 * @summary Verify that the close resource code works properly in all cases
 * @library /tools/lib
 * @modules jdk.compiler/com.sun.tools.javac.api
 *          jdk.compiler/com.sun.tools.javac.comp
 *          jdk.compiler/com.sun.tools.javac.main
 * @build toolbox.ToolBox TwrClose
 * @run main TwrClose
 */

import javax.tools.JavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;

import com.sun.tools.javac.comp.Lower;

import toolbox.JavacTask;
import toolbox.ToolBox;

public class TwrClose {

    public static void main(String... args) throws Exception {
        for (int i = 1; i < Lower.USE_CLOSE_RESOURCE_METHOD_THRESHOLD * 2; i++) {
            new TwrClose().compile(i);
        }
    }

    ToolBox tb = new ToolBox();
    JavaFileManager fm = ToolProvider.getSystemJavaCompiler()
                                     .getStandardFileManager(null, null, null);

    void compile(int trysCount) throws Exception {
        StringBuilder testInvocations = new StringBuilder();
        StringBuilder testMethods = new StringBuilder();

        for (int i = 0; i < trysCount; i++) {
            testInvocations.append(TEST_INVOCATIONS_TEMPLATE.replace("#N", Integer.toString(i)));
            testMethods.append(TEST_METHOD_TEMPLATE.replace("#N", Integer.toString(i)));
        }

        String sourceCode = FILE_TEMPLATE.replace("#TEST_INVOCATIONS", testInvocations.toString())
                                         .replace("#TEST_METHODS", testMethods.toString());

        System.err.println("analyzing:");
        System.err.println(sourceCode);

        try (ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager()) {
            new JavacTask(tb).fileManager(mfm)
                             .sources(sourceCode)
                             .run()
                             .writeAll();
            ClassLoader cl = new ClassLoader(TwrClose.class.getClassLoader()) {
                @Override
                protected Class<?> findClass(String name) throws ClassNotFoundException {
                    byte[] data = mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, name);
                    if (data != null) {
                        return defineClass(name, data, 0, data.length);
                    }
                    return super.findClass(name);
                }
            };

            ((Runnable) cl.loadClass("Test").newInstance()).run();
        }
    }

    final String TEST_INVOCATIONS_TEMPLATE =
        "        test#N(false, false, Arrays.asList(\"close\"));\n" +
        "        test#N(false, true, Arrays.asList(\"close\", \"close-exception\"));\n" +
        "        test#N(true, false, Arrays.asList(\"close\", \"inTwr\"));\n" +
        "        test#N(true, true, Arrays.asList(\"close\", \"inTwr\", \"close-exception\"));\n";

    final String TEST_METHOD_TEMPLATE =
        "    private void test#N(boolean failInTwr, boolean failOnClose,\n" +
        "                        List<String> expectedMessages) {\n" +
        "        List<String> messages = new ArrayList<>();\n" +
        "        try {\n" +
        "            try (CloseableImpl c = new CloseableImpl(messages, failOnClose)) {\n" +
        "                if (failInTwr)\n" +
        "                    throw new IllegalStateException(\"inTwr\");\n" +
        "            }\n" +
        "        } catch (IllegalStateException ex) {\n" +
        "            messages.add(ex.getMessage());\n" +
        "            for (Throwable t : ex.getSuppressed()) {\n" +
        "                messages.add(t.getMessage());\n" +
        "            }\n" +
        "        }\n" +
        "        if (!expectedMessages.equals(messages))\n" +
        "            throw new AssertionError(\"Expected and actual messages differ; expectedMessages=\" +\n" +
        "                                     expectedMessages + \"; actual=\" + messages);\n" +
        "    }\n";

    final String FILE_TEMPLATE =
        "import java.util.*;\n" +
        "public class Test implements Runnable {\n" +
        "    public void run() {\n" +
        "#TEST_INVOCATIONS" +
        "    }\n" +
        "#TEST_METHODS" +
        "    static class CloseableImpl implements AutoCloseable {\n" +
        "        private final List<String> messages;\n" +
        "        private final boolean failOnClose;\n" +
        "        public CloseableImpl(List<String> messages, boolean failOnClose) {\n" +
        "            this.messages = messages;\n" +
        "            this.failOnClose = failOnClose;\n" +
        "        }\n" +
        "        @Override\n" +
        "        public void close() {\n" +
        "            messages.add(\"close\");\n" +
        "            if (failOnClose)\n" +
        "                throw new IllegalStateException(\"close-exception\");\n" +
        "        }\n" +
        "    }\n" +
        "}\n";
}