1/*
2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 7030606 8006694 8129962
27 * @summary Project-coin: multi-catch types should be pairwise disjoint
28 *  temporarily workaround combo tests are causing time out in several platforms
29 * @library /tools/javac/lib
30 * @modules jdk.compiler/com.sun.tools.javac.api
31 *          jdk.compiler/com.sun.tools.javac.code
32 *          jdk.compiler/com.sun.tools.javac.comp
33 *          jdk.compiler/com.sun.tools.javac.main
34 *          jdk.compiler/com.sun.tools.javac.tree
35 *          jdk.compiler/com.sun.tools.javac.util
36 * @build combo.ComboTestHelper
37 * @run main DisjunctiveTypeWellFormednessTest
38 */
39
40import java.io.IOException;
41
42import combo.ComboInstance;
43import combo.ComboParameter;
44import combo.ComboTask.Result;
45import combo.ComboTestHelper;
46
47
48public class DisjunctiveTypeWellFormednessTest extends ComboInstance<DisjunctiveTypeWellFormednessTest> {
49
50    enum Alternative implements ComboParameter {
51        EXCEPTION("Exception"),
52        RUNTIME_EXCEPTION("RuntimeException"),
53        IO_EXCEPTION("java.io.IOException"),
54        FILE_NOT_FOUND_EXCEPTION("java.io.FileNotFoundException"),
55        ILLEGAL_ARGUMENT_EXCEPTION("IllegalArgumentException");
56
57        String exceptionStr;
58
59        Alternative(String exceptionStr) {
60            this.exceptionStr = exceptionStr;
61        }
62
63        boolean disjoint(Alternative that) {
64            return disjoint[this.ordinal()][that.ordinal()];
65        }
66
67        static boolean[][] disjoint = {
68            //                              Exception    RuntimeException    IOException    FileNotFoundException    IllegalArgumentException
69            /*Exception*/                {  false,       false,              false,         false,                   false },
70            /*RuntimeException*/         {  false,       false,              true,          true,                    false },
71            /*IOException*/              {  false,       true,               false,         false,                   true },
72            /*FileNotFoundException*/    {  false,       true,               false,         false,                   true },
73            /*IllegalArgumentException*/ {  false,       false,              true,          true,                    false }
74        };
75
76        @Override
77        public String expand(String optParameter) {
78            return exceptionStr;
79        }
80    }
81
82    enum Arity implements ComboParameter {
83        ONE(1, "#{TYPE[0]}"),
84        TWO(2, "#{TYPE[0]} | #{TYPE[1]}"),
85        THREE(3, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]}"),
86        FOUR(4, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]} | #{TYPE[3]}"),
87        FIVE(5, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]} | #{TYPE[3]} | #{TYPE[4]}");
88
89        int n;
90        String arityTemplate;
91
92        Arity(int n, String arityTemplate) {
93            this.n = n;
94            this.arityTemplate = arityTemplate;
95        }
96
97        @Override
98        public String expand(String optParameter) {
99            return arityTemplate;
100        }
101    }
102
103    public static void main(String... args) throws Exception {
104        new ComboTestHelper<DisjunctiveTypeWellFormednessTest>()
105                .withFilter(DisjunctiveTypeWellFormednessTest::arityFilter)
106                .withDimension("CTYPE", (x, arity) -> x.arity = arity, Arity.values())
107                .withArrayDimension("TYPE", (x, type, idx) -> x.alternatives[idx] = type, 5, Alternative.values())
108                .run(DisjunctiveTypeWellFormednessTest::new);
109    }
110
111    Arity arity;
112    Alternative[] alternatives = new Alternative[5];
113
114    boolean arityFilter() {
115        for (int i = arity.n; i < alternatives.length ; i++) {
116            if (alternatives[i].ordinal() != 0) {
117                return false;
118            }
119        }
120        return true;
121    }
122
123    String template = "class Test {\n" +
124                      "void test() {\n" +
125                      "try {} catch (#{CTYPE} e) {}\n" +
126                      "}\n" +
127                      "}\n";
128
129    @Override
130    public void doWork() throws IOException {
131        check(newCompilationTask()
132                .withSourceFromTemplate(template)
133                .analyze());
134    }
135
136    void check(Result<?> res) {
137
138        int non_disjoint = 0;
139        for (int i = 0 ; i < arity.n ; i++) {
140            for (int j = 0 ; j < i ; j++) {
141                if (!alternatives[i].disjoint(alternatives[j])) {
142                    non_disjoint++;
143                    break;
144                }
145            }
146        }
147
148        int foundErrs = res.diagnosticsForKey("compiler.err.multicatch.types.must.be.disjoint").size();
149        if (non_disjoint != foundErrs) {
150            fail("invalid diagnostics for source:\n" +
151                    res.compilationInfo() +
152                    "\nFound errors: " + foundErrs +
153                    "\nExpected errors: " + non_disjoint);
154        }
155    }
156}
157