1 /*
  2  * Copyright (c) 2021, 2022, 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 8269146 8290709
 27  * @summary Check compilation outcomes for various combinations of case label element.
 28  * @library /tools/lib /tools/javac/lib
 29  * @modules
 30  *      jdk.compiler/com.sun.tools.javac.api
 31  *      jdk.compiler/com.sun.tools.javac.file
 32  *      jdk.compiler/com.sun.tools.javac.main
 33  *      jdk.compiler/com.sun.tools.javac.util
 34  * @build toolbox.ToolBox toolbox.JavacTask
 35  * @build combo.ComboTestHelper
 36  * @compile CaseStructureTest.java
 37  * @run main CaseStructureTest
 38  */
 39 
 40 import combo.ComboInstance;
 41 import combo.ComboParameter;
 42 import combo.ComboTask;
 43 import combo.ComboTestHelper;
 44 import java.util.Arrays;
 45 import java.util.stream.Collectors;
 46 import toolbox.ToolBox;
 47 
 48 public class CaseStructureTest extends ComboInstance<CaseStructureTest> {
 49     private static final String JAVA_VERSION = System.getProperty("java.specification.version");
 50 
 51     protected ToolBox tb;
 52 
 53     CaseStructureTest() {
 54         super();
 55         tb = new ToolBox();
 56     }
 57 
 58     public static void main(String... args) throws Exception {
 59         new ComboTestHelper<CaseStructureTest>()
 60                 .withDimension("AS_CASE_LABEL_ELEMENTS", (x, asCaseLabelElements) -> x.asCaseLabelElements = asCaseLabelElements, true, false)
 61                 .withArrayDimension("CASE_LABELS", (x, caseLabels, idx) -> x.caseLabels[idx] = caseLabels, DIMENSIONS, CaseLabel.values())
 62                 .withFilter(t -> Arrays.stream(t.caseLabels).anyMatch(l -> l != CaseLabel.NONE))
 63                 .withFailMode(ComboTestHelper.FailMode.FAIL_FAST)
 64                 .run(CaseStructureTest::new);
 65     }
 66 
 67     private static final int DIMENSIONS = 4;
 68     private boolean asCaseLabelElements;
 69     private CaseLabel[] caseLabels = new CaseLabel[DIMENSIONS];
 70 
 71     private static final String MAIN_TEMPLATE =
 72             """
 73             public class Test {
 74                 public static void doTest(Integer in) {
 75                     switch (in) {
 76                         case -1: break;
 77                         #{CASES}
 78                         #{DEFAULT}
 79                     }
 80                 }
 81             }
 82             """;
 83 
 84     @Override
 85     protected void doWork() throws Throwable {
 86         String labelSeparator = asCaseLabelElements ? ", " : ": case ";
 87         String labels = Arrays.stream(caseLabels).filter(l -> l != CaseLabel.NONE).map(l -> l.code).collect(Collectors.joining(labelSeparator, "case ", ": break;"));
 88         boolean hasDefault = Arrays.stream(caseLabels).anyMatch(l -> l == CaseLabel.DEFAULT || l == CaseLabel.TYPE_PATTERN);
 89 
 90         ComboTask task = newCompilationTask()
 91                 .withSourceFromTemplate(MAIN_TEMPLATE.replace("#{CASES}", labels).replace("#{DEFAULT}", hasDefault ? "" : "default: break;"));
 92 
 93         task.generate(result -> {
 94             boolean shouldPass = true;
 95             long patternCases = Arrays.stream(caseLabels).filter(l -> l == CaseLabel.TYPE_PATTERN).count();
 96             long constantCases = Arrays.stream(caseLabels).filter(l -> l == CaseLabel.CONSTANT).count();
 97             long nullCases = Arrays.stream(caseLabels).filter(l -> l == CaseLabel.NULL).count();
 98             long defaultCases = Arrays.stream(caseLabels).filter(l -> l == CaseLabel.DEFAULT).count();
 99             if (constantCases > 1) {
100                 shouldPass &= false;
101             }
102             if (constantCases > 0) {
103                 shouldPass &= patternCases == 0;
104             }
105             if (defaultCases > 0) {
106                 shouldPass &= asCaseLabelElements && nullCases > 0;
107             }
108             if (defaultCases > 1) {
109                 shouldPass &= false;
110             }
111             if (nullCases > 1) {
112                 shouldPass &= false;
113             }
114             if (nullCases > 0) {
115                 shouldPass &= patternCases == 0 && (constantCases == 0 || !asCaseLabelElements);
116                 if (defaultCases > 0 && asCaseLabelElements) {
117                     int nullIndex = Arrays.asList(caseLabels).indexOf(CaseLabel.NULL);
118                     int defaultIndex = Arrays.asList(caseLabels).indexOf(CaseLabel.DEFAULT);
119                     shouldPass &= nullIndex < defaultIndex;
120                 }
121             }
122             if (patternCases > 1) {
123                 shouldPass &= false;
124             }
125             if (patternCases > 0 && defaultCases > 0) {
126                 shouldPass &= false;
127             }
128             if (!(shouldPass ^ result.hasErrors())) {
129                 throw new AssertionError("Unexpected result: shouldPass=" + shouldPass + ", actual: " + !result.hasErrors() + ", info: " + result.compilationInfo());
130             }
131         });
132     }
133 
134     public enum CaseLabel implements ComboParameter {
135         NONE(""),
136         TYPE_PATTERN("Integer i"),
137         CONSTANT("1"),
138         NULL("null"),
139         DEFAULT("default");
140 
141         private final String code;
142 
143         private CaseLabel(String code) {
144             this.code = code;
145         }
146 
147         @Override
148         public String expand(String optParameter) {
149             throw new UnsupportedOperationException("Not supported.");
150         }
151     }
152 
153 }