33import org .junit .jupiter .api .Assertions ;
44import org .junit .jupiter .api .Test ;
55
6- import java . util . Objects ;
6+ import static org . junit . jupiter . api . Assertions . assertTrue ;
77
8- /**
9- * Java 14 (March 2020)
10- */
8+ /// Java 14™ (March 2020)
9+ /// [JDK 14](https://openjdk.org/projects/jdk/14)
10+ ///
11+ /// - STANDARD FEATURES:
12+ /// - 361: Switch Expressions (Standard)
13+ /// - 358: Helpful NullPointerExceptions
14+ /// - 367: Remove the Pack200 Tools and API
15+ /// - 345: NUMA-Aware Memory Allocation for G1 (+XX:+UseNUMA)
16+ /// - 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector (-XX:+UseConcMarkSweepGC)
17+ /// - 349: JFR Event Streaming
18+ /// - 352: Non-Volatile Mapped Byte Buffers
19+ /// - 364: ZGC on macOS
20+ /// - 365: ZGC on Windows
21+ /// - 362: Deprecate the Solaris and SPARC Ports
22+ /// - 366: Deprecate the ParallelScavenge + SerialOld GC Combination (-XX:-UseParallelOldGC)
23+ ///
24+ /// - PREVIEW & INCUBATOR:
25+ /// - 359: Records (Preview)
26+ /// - 305: Pattern Matching for instanceof (Preview)
27+ /// - 368: Text Blocks (Second Preview)
28+ /// - 343: Packaging Tool 'jpackage' (Incubator)
29+ /// - 370: Foreign-Memory Access API (Incubator)
1130public class Java14 {
1231
32+ @ Test
33+ public void testSwitchExpression () {
34+ Assertions .assertEquals ("Working Day" , getTypeOfDayByNumber (1 ));
35+ Assertions .assertEquals ("Day Off" , getTypeOfDayByNumber (6 ));
36+ Assertions .assertThrows (IllegalStateException .class , () -> getTypeOfDayByNumber (0 ));
37+ }
38+
39+ private String getTypeOfDayByNumber (int dayOfWeek ) {
40+ return switch (dayOfWeek ) {
41+ case 1 , 2 , 3 , 4 , 5 -> "Working Day" ;
42+ case 6 , 7 -> {
43+ // code block example
44+ yield "Day Off" ;
45+ }
46+ default -> throw new IllegalStateException ("Unexpected value: " + dayOfWeek );
47+ };
48+ }
49+
50+ @ Test
51+ public void testSwitchStatementVsSwitchExpression () {
52+ assertTrue (switchExpression (Switch .EXPRESSION ));
53+ assertTrue (switchStatement (Switch .STATEMENT ));
54+ }
55+
56+ enum Switch {EXPRESSION , STATEMENT }
57+
58+ private static boolean switchExpression (Switch s ) {
59+ return switch (s ) {
60+ case EXPRESSION -> true ;
61+ default -> false ;
62+ };
63+ }
64+
65+ private static boolean switchStatement (Switch s ) {
66+ switch (s ) {
67+ case STATEMENT -> {
68+ return true ;
69+ }
70+ default -> {
71+ return false ;
72+ }
73+ }
74+ }
75+
1376 @ Test
1477 public void testHelpfulNullPointerExceptions () {
1578 NullPointerException exception = Assertions .assertThrows (NullPointerException .class , () -> {
1679 String name = null ;
17- int length = name .length ();
80+ name .length ();
1881 });
19- Assertions . assertTrue (exception .getMessage ().contains ("Cannot invoke \" String.length()\" because \" name\" is null" ));
82+ assertTrue (exception .getMessage ().contains ("Cannot invoke \" String.length()\" because \" name\" is null" ));
2083 }
84+
85+ @ Test
86+ public void testPack200ToolsAndAPIRemoved () {
87+ Assertions .assertThrows (ClassNotFoundException .class , () -> {
88+ Class .forName ("java.util.jar.Pack200" );
89+ });
90+ }
91+
2192}
0 commit comments