1 /**
   2  * @test /nodymaticcopyright/
   3  * @bug 8206986
   4  * @compile/fail/ref=SwitchStatementArrow-old.out -source 9 -Xlint:-options -XDrawDiagnostics SwitchStatementArrow.java
   5  * @compile --enable-preview -source 12 SwitchStatementArrow.java
   6  * @run main/othervm --enable-preview SwitchStatementArrow
   7  */
   8 
   9 import java.util.Objects;
  10 import java.util.function.Function;
  11 
  12 public class SwitchStatementArrow {
  13     public static void main(String... args) {
  14         new SwitchStatementArrow().run();
  15     }
  16 
  17     private void run() {
  18         runTest(this::statement1);
  19     }
  20 
  21     private void runTest(Function<T, String> print) {
  22         check(T.A,  print, "A");
  23         check(T.B,  print, "B-C");
  24         check(T.C,  print, "B-C");
  25         try {
  26             print.apply(T.D);
  27             throw new AssertionError();
  28         } catch (IllegalStateException ex) {
  29             if (!Objects.equals("D", ex.getMessage()))
  30                 throw new AssertionError(ex);
  31         }
  32         check(T.E,  print, "other");
  33     }
  34 
  35     private String statement1(T t) {
  36         String res;
  37 
  38         switch (t) {
  39             case A -> { res = "A"; }
  40             case B, C -> res = "B-C";
  41             case D -> throw new IllegalStateException("D");
  42             default -> { res = "other"; break; }
  43         }
  44 
  45         return res;
  46     }
  47 
  48     private void check(T t, Function<T, String> print, String expected) {
  49         String result = print.apply(t);
  50         if (!Objects.equals(result, expected)) {
  51             throw new AssertionError("Unexpected result: " + result);
  52         }
  53     }
  54 
  55     enum T {
  56         A, B, C, D, E;
  57     }
  58 }