11package pl .mperor .lab .java ;
22
3+ import jdk .jfr .Configuration ;
4+ import jdk .jfr .Event ;
5+ import jdk .jfr .Label ;
6+ import jdk .jfr .Name ;
7+ import jdk .jfr .consumer .RecordingStream ;
38import org .junit .jupiter .api .Assertions ;
49import org .junit .jupiter .api .Test ;
510
6- import java .util .Objects ;
11+ import java .io .IOException ;
12+ import java .text .ParseException ;
13+ import java .time .Duration ;
714
8- /**
9- * Java 14 (March 2020)
10- */
15+ import static org .junit .jupiter .api .Assertions .assertTrue ;
16+
17+ /// Java 14™ (March 2020)
18+ /// [JDK 14](https://openjdk.org/projects/jdk/14)
19+ ///
20+ /// - STANDARD FEATURES:
21+ /// - 361: Switch Expressions (Standard)
22+ /// - 358: Helpful NullPointerExceptions
23+ /// - 367: Remove the Pack200 Tools and API
24+ /// - 345: NUMA-Aware Memory Allocation for G1 (`+XX:+UseNUMA`)
25+ /// - 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector (`-XX:+UseConcMarkSweepGC`)
26+ /// - 349: JFR Event Streaming (`-XX:StartFlightRecording:filename=events.jfr`) [JFAPI](https://docs.oracle.com/en/java/javase/23/jfapi/)
27+ /// - 352: Non-Volatile Mapped Byte Buffers
28+ /// - 364: ZGC on macOS
29+ /// - 365: ZGC on Windows
30+ /// - 362: Deprecate the Solaris and SPARC Ports
31+ /// - 366: Deprecate the ParallelScavenge + SerialOld GC Combination (`-XX:-UseParallelOldGC`)
32+ ///
33+ /// - PREVIEW & INCUBATOR:
34+ /// - 359: Records (Preview)
35+ /// - 305: Pattern Matching for instanceof (Preview)
36+ /// - 368: Text Blocks (Second Preview)
37+ /// - 343: Packaging Tool `jpackage` (Incubator)
38+ /// - 370: Foreign-Memory Access API (Incubator)
1139public class Java14 {
1240
41+ @ Test
42+ public void testSwitchExpression () {
43+ Assertions .assertEquals ("Working Day" , getTypeOfDayByNumber (1 ));
44+ Assertions .assertEquals ("Day Off" , getTypeOfDayByNumber (6 ));
45+ Assertions .assertThrows (IllegalStateException .class , () -> getTypeOfDayByNumber (0 ));
46+ }
47+
48+ private String getTypeOfDayByNumber (int dayOfWeek ) {
49+ return switch (dayOfWeek ) {
50+ case 1 , 2 , 3 , 4 , 5 -> "Working Day" ;
51+ case 6 , 7 -> {
52+ // code block example
53+ yield "Day Off" ;
54+ }
55+ default -> throw new IllegalStateException ("Unexpected value: " + dayOfWeek );
56+ };
57+ }
58+
59+ @ Test
60+ public void testSwitchStatementVsSwitchExpression () {
61+ assertTrue (switchExpression (Switch .EXPRESSION ));
62+ assertTrue (switchStatement (Switch .STATEMENT ));
63+ }
64+
65+ enum Switch {EXPRESSION , STATEMENT }
66+
67+ private static boolean switchExpression (Switch s ) {
68+ return switch (s ) {
69+ case EXPRESSION -> true ;
70+ default -> false ;
71+ };
72+ }
73+
74+ private static boolean switchStatement (Switch s ) {
75+ switch (s ) {
76+ case STATEMENT -> {
77+ return true ;
78+ }
79+ default -> {
80+ return false ;
81+ }
82+ }
83+ }
84+
1385 @ Test
1486 public void testHelpfulNullPointerExceptions () {
1587 NullPointerException exception = Assertions .assertThrows (NullPointerException .class , () -> {
1688 String name = null ;
17- int length = name .length ();
89+ name .length ();
1890 });
19- Assertions . assertTrue (exception .getMessage ().contains ("Cannot invoke \" String.length()\" because \" name\" is null" ));
91+ assertTrue (exception .getMessage ().contains ("Cannot invoke \" String.length()\" because \" name\" is null" ));
2092 }
93+
94+ @ Test
95+ public void testPack200ToolsAndAPIRemoved () {
96+ Assertions .assertThrows (ClassNotFoundException .class , () -> {
97+ Class .forName ("java.util.jar.Pack200" );
98+ });
99+ }
100+
101+ @ Test
102+ public void testThreadIsInterruptedAlwaysAvailable () {
103+ Thread neverStartedThread = new Thread ();
104+ Assertions .assertFalse (neverStartedThread .isInterrupted ());
105+ neverStartedThread .interrupt ();
106+ Assertions .assertTrue (neverStartedThread .isInterrupted ());
107+ }
108+
109+ @ Test
110+ public void testJFREvent () throws IOException , ParseException , InterruptedException {
111+ Configuration c = Configuration .getConfiguration ("profile" );
112+ try (RecordingStream rs = new RecordingStream (c )) {
113+ rs .onEvent ("jdk.ThreadSleep" , event -> event .getDuration ().equals (Duration .ofSeconds (100 )));
114+ rs .startAsync ();
115+
116+ System .out .println ("Sleeping for 100ms..." );
117+ Thread .sleep (100 );
118+ }
119+ }
120+
21121}
0 commit comments