55import org .junit .jupiter .api .Test ;
66import pl .mperor .lab .TestUtils ;
77
8+ import java .io .BufferedReader ;
89import java .io .IOException ;
10+ import java .io .InputStreamReader ;
11+ import java .io .PrintWriter ;
12+ import java .net .InetSocketAddress ;
13+ import java .net .ServerSocket ;
14+ import java .net .StandardProtocolFamily ;
15+ import java .net .UnixDomainSocketAddress ;
16+ import java .nio .channels .ServerSocketChannel ;
17+ import java .nio .channels .SocketChannel ;
18+ import java .nio .file .Files ;
19+ import java .nio .file .Path ;
20+ import java .nio .file .Paths ;
921import java .time .LocalTime ;
1022import java .time .format .DateTimeFormatter ;
1123import java .util .List ;
1224import java .util .Locale ;
25+ import java .util .Objects ;
26+ import java .util .concurrent .CountDownLatch ;
27+ import java .util .concurrent .Executors ;
1328import java .util .stream .Collectors ;
1429
15- /**
16- * Java 16 (March 2021)
17- */
30+ /// Java 16™ (March 2021)
31+ /// [JDK 16](https://openjdk.org/projects/jdk/16)
32+ ///
33+ /// - STANDARD FEATURES:
34+ /// - 395: Records
35+ /// - 394: Pattern Matching for instanceof
36+ /// - 390: Warnings for Value-Based Classes
37+ /// - 392: Packaging Tool
38+ /// - 396: Strongly Encapsulate JDK Internals by Default
39+ /// - 376: ZGC: Concurrent Thread-Stack Processing
40+ /// - 380: Unix-Domain Socket Channels
41+ /// - 387: Elastic Metaspace
42+ /// - 347: Enable C++14 Language Features
43+ /// - 386: Alpine Linux Port
44+ /// - 388: Windows/AArch64 Port
45+ /// - 357: Migrate from Mercurial to Git
46+ /// - 369: Migrate to GitHub
47+ ///
48+ /// - PREVIEW & INCUBATOR:
49+ /// - 389: Foreign Linker API (Incubator)
50+ /// - 397: Sealed Classes (Second Preview)
51+ /// - 393: Foreign-Memory Access API (Third Incubator)
52+ /// - 338: Vector API (Incubator)
1853public class Java16 {
1954
55+ // POJOs
56+ @ Test
57+ public void testPairRecord () {
58+ var pair = Pair .of ("Boy" , "Girl" );
59+ Assertions .assertEquals ("Boy" , pair .left );
60+ Assertions .assertEquals ("Girl" , pair .right );
61+ Assertions .assertEquals ("Pair[left=Boy, right=Girl]" , pair .toString ());
62+ Assertions .assertEquals (Pair .of (1 , 1 ), Pair .of (1 , 1 ));
63+ Assertions .assertNotEquals (Pair .of (0 , 0 ), Pair .of (1 , 1 ));
64+ Assertions .assertThrows (NullPointerException .class , () -> Pair .of (0 , null ));
65+ }
66+
67+ record Pair <L , R >(L left , R right ) {
68+ Pair {
69+ Objects .requireNonNull (left );
70+ Objects .requireNonNull (right );
71+ }
72+
73+ public static <L , R > Pair <L , R > of (L left , R right ) {
74+ return new Pair <>(left , right );
75+ }
76+ }
77+
78+ @ Test
79+ public void testPatternMatchingInstanceOfAkaSmartCasting () {
80+ Object o = "Hello String!" ;
81+ if (o instanceof String s ) {
82+ Assertions .assertNotNull (s );
83+ Assertions .assertInstanceOf (String .class , s );
84+ }
85+ Assertions .assertTrue (nonEmptyString ("Hello World!" ));
86+ Assertions .assertFalse (nonEmptyString (null ));
87+ }
88+
89+ private static boolean nonEmptyString (Object obj ) {
90+ return (obj instanceof String str ) && !str .isEmpty ();
91+ }
92+
93+ @ SuppressWarnings ({"removal" , "synchronization" })
94+ @ Test
95+ public void testValueBasedClasses () {
96+ // warning: [removal] Long(long) in Long has been deprecated and marked for removal
97+ Long longByConstructor = new Long (13 );
98+
99+ // warning: [synchronization] attempt to synchronize on an instance of a value-based class
100+ synchronized (longByConstructor ) {
101+ System .out .println ("From the synchronized block!" );
102+ }
103+
104+ Long longByFactoryMethod = Long .valueOf (13L );
105+ Assertions .assertFalse (longByConstructor == longByFactoryMethod );
106+ Assertions .assertTrue (longByConstructor .equals (longByFactoryMethod ));
107+ Assertions .assertTrue (Long .valueOf (13L ) == longByFactoryMethod );
108+ }
109+
20110 @ Test
21111 public void testPeriodSupport () {
22112 LocalTime date = LocalTime .parse ("16:00:00" );
@@ -70,23 +160,6 @@ public void call() {
70160 TestUtils .resetSystemOut ();
71161 }
72162
73- @ SuppressWarnings ({"removal" , "synchronization" })
74- @ Test
75- public void testValueBasedClasses () {
76- // warning: new Long(...) has been deprecated and marked for removal
77- Long longByConstructor = new Long (13 );
78-
79- // warning: attempt to synchronize on an instance of a value-based class
80- synchronized (longByConstructor ) {
81- System .out .println ("From the synchronized block!" );
82- }
83-
84- Long longByFactoryMethod = Long .valueOf (13L );
85- Assertions .assertFalse (longByConstructor == longByFactoryMethod );
86- Assertions .assertTrue (longByConstructor .equals (longByFactoryMethod ));
87- Assertions .assertTrue (Long .valueOf (13L ) == longByFactoryMethod );
88- }
89-
90163 @ Disabled ("Dependent on the OS and additional libraries, besides having a long execution time." )
91164 @ Test
92165 public void testJPackage () throws IOException , InterruptedException {
@@ -102,4 +175,18 @@ public void testJPackage() throws IOException, InterruptedException {
102175 process .destroy ();
103176 }
104177
178+ @ Test
179+ public void testUnixDomainSocketChannels () throws IOException {
180+ Path socketPath = Paths .get ("/tmp/unix_socket" );
181+ Files .deleteIfExists (socketPath );
182+ UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress .of (socketPath );
183+ Assertions .assertNotNull (socketAddress );
184+ Assertions .assertNotNull (StandardProtocolFamily .UNIX );
185+
186+ try (ServerSocket serverSocket = ServerSocketChannel .open (StandardProtocolFamily .UNIX )
187+ .bind (socketAddress )
188+ .socket ()) {
189+ }
190+ }
191+
105192}
0 commit comments