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