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.file
 32  *          jdk.compiler/com.sun.tools.javac.util
 33  * @build combo.ComboTestHelper
 34  * @run main DisjunctiveTypeWellFormednessTest
 35  */
 36 
 37 import java.io.IOException;
 38 
 39 import combo.ComboInstance;
 40 import combo.ComboParameter;
 41 import combo.ComboTask.Result;
 42 import combo.ComboTestHelper;
 43 
 44 
 45 public class DisjunctiveTypeWellFormednessTest extends ComboInstance<DisjunctiveTypeWellFormednessTest> {
 46 
 47     enum Alternative implements ComboParameter {
 48         EXCEPTION("Exception"),
 49         RUNTIME_EXCEPTION("RuntimeException"),
 50         IO_EXCEPTION("java.io.IOException"),
 51         FILE_NOT_FOUND_EXCEPTION("java.io.FileNotFoundException"),
 52         ILLEGAL_ARGUMENT_EXCEPTION("IllegalArgumentException");
 53 
 54         String exceptionStr;
 55 
 56         Alternative(String exceptionStr) {
 57             this.exceptionStr = exceptionStr;
 58         }
 59 
 60         boolean disjoint(Alternative that) {
 61             return disjoint[this.ordinal()][that.ordinal()];
 62         }
 63 
 64         static boolean[][] disjoint = {
 65             //                              Exception    RuntimeException    IOException    FileNotFoundException    IllegalArgumentException
 66             /*Exception*/                {  false,       false,              false,         false,                   false },
 67             /*RuntimeException*/         {  false,       false,              true,          true,                    false },
 68             /*IOException*/              {  false,       true,               false,         false,                   true },
 69             /*FileNotFoundException*/    {  false,       true,               false,         false,                   true },
 70             /*IllegalArgumentException*/ {  false,       false,              true,          true,                    false }
 71         };
 72 
 73         @Override
 74         public String expand(String optParameter) {
 75             return exceptionStr;
 76         }
 77     }
 78 
 79     enum Arity implements ComboParameter {
 80         ONE(1, "#{TYPE[0]}"),
 81         TWO(2, "#{TYPE[0]} | #{TYPE[1]}"),
 82         THREE(3, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]}"),
 83         FOUR(4, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]} | #{TYPE[3]}"),
 84         FIVE(5, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]} | #{TYPE[3]} | #{TYPE[4]}");
 85 
 86         int n;
 87         String arityTemplate;
 88 
 89         Arity(int n, String arityTemplate) {
 90             this.n = n;
 91             this.arityTemplate = arityTemplate;
 92         }
 93 
 94         @Override
 95         public String expand(String optParameter) {
 96             return arityTemplate;
 97         }
 98     }
 99 
100     public static void main(String... args) throws Exception {
101         new ComboTestHelper<DisjunctiveTypeWellFormednessTest>()
102                 .withFilter(DisjunctiveTypeWellFormednessTest::arityFilter)
103                 .withDimension("CTYPE", (x, arity) -> x.arity = arity, Arity.values())
104                 .withArrayDimension("TYPE", (x, type, idx) -> x.alternatives[idx] = type, 5, Alternative.values())
105                 .run(DisjunctiveTypeWellFormednessTest::new);
106     }
107 
108     Arity arity;
109     Alternative[] alternatives = new Alternative[5];
110 
111     boolean arityFilter() {
112         for (int i = arity.n; i < alternatives.length ; i++) {
113             if (alternatives[i].ordinal() != 0) {
114                 return false;
115             }
116         }
117         return true;
118     }
119 
120     String template = "class Test {\n" +
121                       "void test() {\n" +
122                       "try {} catch (#{CTYPE} e) {}\n" +
123                       "}\n" +
124                       "}\n";
125 
126     @Override
127     public void doWork() throws IOException {
128         newCompilationTask()
129                 .withSourceFromTemplate(template)
130                 .analyze(this::check);
131     }
132 
133     void check(Result<?> res) {
134 
135         int non_disjoint = 0;
136         for (int i = 0 ; i < arity.n ; i++) {
137             for (int j = 0 ; j < i ; j++) {
138                 if (!alternatives[i].disjoint(alternatives[j])) {
139                     non_disjoint++;
140                     break;
141                 }
142             }
143         }
144 
145         int foundErrs = res.diagnosticsForKey("compiler.err.multicatch.types.must.be.disjoint").size();
146         if (non_disjoint != foundErrs) {
147             fail("invalid diagnostics for source:\n" +
148                     res.compilationInfo() +
149                     "\nFound errors: " + foundErrs +
150                     "\nExpected errors: " + non_disjoint);
151         }
152     }
153 }