11package pl .mperor .lab .java ;
22
3- /**
4- * Java 15 (September 2020)
5- */
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .lang .invoke .MethodHandle ;
7+ import java .lang .invoke .MethodHandles ;
8+ import java .lang .invoke .MethodType ;
9+ import java .net .DatagramSocket ;
10+ import java .net .SocketException ;
11+ import java .nio .file .Files ;
12+ import java .nio .file .Path ;
13+ import java .nio .file .Paths ;
14+ import java .security .*;
15+ import java .util .Locale ;
16+
17+ /// Java 15™ (September 2020)
18+ /// [JDK 15](https://openjdk.org/projects/jdk/15)
19+ ///
20+ /// - STANDARD FEATURES:
21+ /// - 378: Text Blocks
22+ /// - 339: Edwards-Curve Digital Signature Algorithm (EdDSA)
23+ /// - 371: Hidden Classes
24+ /// - 372: Remove the Nashorn JavaScript Engine
25+ /// - 373: Reimplement the Legacy DatagramSocket API
26+ /// - 374: Disable and Deprecate Biased Locking
27+ /// - 377: ZGC: A Scalable Low-Latency Garbage Collector
28+ /// - 379: Shenandoah: A Low-Pause-Time Garbage Collector
29+ /// - 381: Remove the Solaris and SPARC Ports
30+ /// - 385: Deprecate RMI Activation for Removal [Java17#testRmiActivationRemoved()]
31+ ///
32+ /// - PREVIEW & INCUBATOR:
33+ /// - 360: Sealed Classes (Preview)
34+ /// - 375: Pattern Matching for instanceof (Second Preview)
35+ /// - 384: Records (Second Preview)
36+ /// - 383: Foreign-Memory Access API (Second Incubator)
637public class Java15 {
738
39+ @ Test
40+ public void testTextBlock () {
41+ String json = """
42+ {
43+ "login": "admin",
44+ "password": "*****"
45+ }
46+ """ ;
47+
48+ Assertions .assertTrue (json .contains ("\" login\" : \" admin\" " ));
49+ }
50+
51+ @ Test
52+ public void testStringFormatted () {
53+ Assertions .assertEquals ("Hello World!" , "Hello %s" .formatted ("World!" ));
54+ Assertions .assertEquals ("Value: 0.00" , String .format (Locale .US , "Value: %.2f" , 0d ));
55+ }
56+
57+ @ Test
58+ public void testEdwardsCurveDigitalSignatureAlgorithm () throws NoSuchAlgorithmException , InvalidKeyException , SignatureException {
59+ String edwardsAlgorithm = "Ed25519" ;
60+ KeyPairGenerator generator = KeyPairGenerator .getInstance (edwardsAlgorithm );
61+ KeyPair keyPair = generator .generateKeyPair ();
62+ byte [] bytes = "secret" .getBytes ();
63+
64+ Signature sig = Signature .getInstance (edwardsAlgorithm );
65+ sig .initSign (keyPair .getPrivate ());
66+ sig .update (bytes );
67+ byte [] signed = sig .sign ();
68+
69+ Signature verificationSignature = Signature .getInstance (edwardsAlgorithm );
70+ verificationSignature .initVerify (keyPair .getPublic ());
71+ verificationSignature .update (bytes );
72+ Assertions .assertTrue (verificationSignature .verify (signed ));
73+ }
74+
75+ @ Test
76+ public void testHiddenClass () throws Throwable {
77+ Path path = Paths .get ("build/classes/java/main/pl/mperor/lab/java/Hidden.class" );
78+
79+ MethodHandles .Lookup lookup = MethodHandles .lookup ();
80+ Class <?> hiddenClass = lookup .defineHiddenClass (Files .readAllBytes (path ), true , MethodHandles .Lookup .ClassOption .NESTMATE )
81+ .lookupClass ();
82+ MethodHandle methodHandle = lookup .findStatic (hiddenClass , "lookup" , MethodType .methodType (int .class ));
83+ Assertions .assertEquals (1 , methodHandle .invoke ());
84+ }
85+
86+ @ Test
87+ public void testNashornEngineRemoved () {
88+ Assertions .assertThrows (ClassNotFoundException .class , () -> {
89+ Class .forName ("jdk.nashorn.api.scripting.NashornScriptEngineFactory" );
90+ });
91+ }
92+
93+ @ Test
94+ public void testDatagramSocket () throws SocketException {
95+ DatagramSocket datagramSocket = new DatagramSocket ();
96+ Assertions .assertNotNull (datagramSocket );
97+ }
98+
899}
0 commit comments