33import org .junit .jupiter .api .Assertions ;
44import org .junit .jupiter .api .Test ;
55
6+ import javax .net .ssl .SSLSocket ;
7+ import javax .net .ssl .SSLSocketFactory ;
68import java .io .ByteArrayInputStream ;
79import java .io .ByteArrayOutputStream ;
810import java .io .IOException ;
11+ import java .net .URI ;
12+ import java .net .URISyntaxException ;
13+ import java .net .http .HttpClient ;
14+ import java .net .http .HttpRequest ;
15+ import java .net .http .HttpResponse ;
916import java .nio .charset .StandardCharsets ;
1017import java .nio .file .Files ;
1118import java .nio .file .Path ;
12- import java .util .AbstractMap .SimpleEntry ;
1319import java .util .List ;
20+ import java .util .concurrent .CompletableFuture ;
21+ import java .util .concurrent .ExecutionException ;
1422import java .util .function .Predicate ;
1523import java .util .stream .Collectors ;
1624import java .util .stream .Stream ;
1725
18- /**
19- * Java 11 (September 2018)
20- */
26+ /// Java 11™ (September 2018)
27+ /// [JDK 11](https://openjdk.org/projects/jdk/11)
28+ ///
29+ /// - STANDARD FEATURES:
30+ /// - 321: HTTP Client (Standard)
31+ /// - 323: Local-Variable Syntax for Lambda Parameters
32+ /// - 332: Transport Layer Security (TLS) 1.3
33+ /// - 320: Remove the Java EE and CORBA Modules
34+ /// - 309: Dynamic Class-File Constants
35+ /// - 181: Nest-Based Access Control
36+ /// - 315: Improve Aarch64 Intrinsics
37+ /// - 318: Epsilon: A No-Op Garbage Collector
38+ /// - 327: Unicode 10
39+ /// - 324: Key Agreement with Curve25519 and Curve448
40+ /// - 328: Flight Recorder
41+ /// - 329: ChaCha20 and Poly1305 Cryptographic Algorithms
42+ /// - 330: Launch Single-File Source-Code Programs
43+ /// - 331: Low-Overhead Heap Profiling
44+ /// - 335: Deprecate the Nashorn JavaScript Engine
45+ /// - 336: Deprecate the Pack200 Tools and API
46+ ///
47+ /// - PREVIEW & INCUBATOR:
48+ /// - 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
2149public class Java11 {
2250
51+ @ Test
52+ public void testNewHTTPClientAPI_getMethodSyncVsAsync () throws URISyntaxException , IOException , InterruptedException , ExecutionException {
53+ HttpClient client = HttpClient .newHttpClient ();
54+ HttpRequest request = HttpRequest .newBuilder ()
55+ .uri (new URI ("https://jsonplaceholder.typicode.com/posts/1" ))
56+ .GET ()
57+ .build ();
58+
59+ // Synchronous
60+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
61+ Assertions .assertEquals (200 , response .statusCode ());
62+ Assertions .assertNotNull (response .body ());
63+
64+ // Asynchronous
65+ CompletableFuture <HttpResponse <String >> responseFuture =
66+ client .sendAsync (request , HttpResponse .BodyHandlers .ofString ());
67+
68+ responseFuture .thenAccept (result -> {
69+ Assertions .assertEquals (200 , result .statusCode ());
70+ Assertions .assertNotNull (result .body ());
71+ }).get ();
72+ }
73+
74+ @ Test
75+ public void testNewHTTPClientAPI_postMethod () throws URISyntaxException , IOException , InterruptedException {
76+ HttpClient client = HttpClient .newHttpClient ();
77+ HttpRequest request = HttpRequest .newBuilder ()
78+ .uri (new URI ("https://jsonplaceholder.typicode.com/posts" ))
79+ .header ("Content-Type" , "application/json" )
80+ .POST (HttpRequest .BodyPublishers .ofString ("""
81+ {
82+ "title": "foo",
83+ "body": "bar",
84+ "userId": 1
85+ }
86+ """ )
87+ ).build ();
88+
89+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
90+ Assertions .assertEquals (201 , response .statusCode ());
91+ Assertions .assertNotNull (response .body ());
92+ }
93+
2394 @ Test
2495 public void testStringAPIEnhancements () {
2596 Assertions .assertTrue (" " .isBlank ());
@@ -71,22 +142,24 @@ public void testTransferTo() throws Exception {
71142 }
72143
73144 @ Test
74- public void testTeeingCollector () {
75- List <String > words = List .of ("one" , "two" , "three" );
76-
77- SimpleEntry <Integer , Long > pair = words .stream ().collect (Collectors .teeing (
78- Collectors .summingInt (String ::length ),
79- Collectors .counting (),
80- SimpleEntry ::new )
81- );
145+ public void testCollectionToArray () {
146+ Assertions .assertArrayEquals (new String []{"x" , "y" , "z" }, List .of ("x" , "y" , "z" ).toArray (String []::new ));
147+ }
82148
83- Assertions .assertEquals (11 , pair .getKey ());
84- Assertions .assertEquals (3 , pair .getValue ());
149+ @ Test
150+ public void testTransportLayerSecurity13 () throws IOException {
151+ SSLSocketFactory factory = (SSLSocketFactory ) SSLSocketFactory .getDefault ();
152+ try (var socket = (SSLSocket ) factory .createSocket ("google.com" , 443 )) {
153+ socket .setEnabledProtocols (new String []{"TLSv1.3" });
154+ socket .setEnabledCipherSuites (new String []{"TLS_AES_128_GCM_SHA256" });
155+ Assertions .assertTrue (socket .isConnected ());
156+ }
85157 }
86158
87159 @ Test
88- public void testCollectionToArray () {
89- Assertions .assertArrayEquals (new String []{"x" , "y" , "z" }, List .of ("x" , "y" , "z" ).toArray (String []::new ));
160+ public void testJavaEERemoved () {
161+ Assertions .assertThrows (ClassNotFoundException .class , () -> Class .forName ("javax.activation.DataHandler" ));
162+ Assertions .assertThrows (ClassNotFoundException .class , () -> Class .forName ("javax.xml.bind.JAXBContext" ));
90163 }
91164
92165}
0 commit comments