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