Skip to content

Commit c9be3f1

Browse files
committed
Update test class for Java 10 & 11 ...
- samples only for standard features - markdown javadoc for JEPs - organize classes related to Java 10 & 11 ... - improve code readability
1 parent 1c527a8 commit c9be3f1

File tree

8 files changed

+177
-83
lines changed

8 files changed

+177
-83
lines changed

src/test/java/pl/mperor/lab/java/Java10.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,22 @@
99
import java.util.concurrent.TimeUnit;
1010
import java.util.stream.Collectors;
1111

12-
/**
13-
* Java 10 (March 2018)
14-
*/
12+
/// Java 10™ (March 2018)
13+
/// [JDK 10](https://openjdk.org/projects/jdk/10)
14+
///
15+
/// - STANDARD FEATURES:
16+
/// - 286: Local-Variable Type Inference
17+
/// - 296: Consolidate the JDK Forest into a Single Repository
18+
/// - 304: Garbage-Collector Interface
19+
/// - 307: Parallel Full GC for G1
20+
/// - 310: Application Class-Data Sharing
21+
/// - 312: Thread-Local Handshakes
22+
/// - 314: Additional Unicode Language-Tag Extensions
23+
/// - 316: Heap Allocation on Alternative Memory Devices
24+
/// - 317: Experimental Java-Based JIT Compiler
25+
/// - 319: Root Certificates
26+
/// - 322: Time-Based Release Versioning
27+
/// - 313: Remove the Native-Header Generation Tool (javah)
1528
public class Java10 {
1629

1730
@Test

src/test/java/pl/mperor/lab/java/Java11.java

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,94 @@
33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
55

6+
import javax.net.ssl.SSLSocket;
7+
import javax.net.ssl.SSLSocketFactory;
68
import java.io.ByteArrayInputStream;
79
import java.io.ByteArrayOutputStream;
810
import 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;
916
import java.nio.charset.StandardCharsets;
1017
import java.nio.file.Files;
1118
import java.nio.file.Path;
12-
import java.util.AbstractMap.SimpleEntry;
1319
import java.util.List;
20+
import java.util.concurrent.CompletableFuture;
21+
import java.util.concurrent.ExecutionException;
1422
import java.util.function.Predicate;
1523
import java.util.stream.Collectors;
1624
import 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)
2149
public 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
}

src/test/java/pl/mperor/lab/java/Java12.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,26 @@
99
import java.security.MessageDigest;
1010
import java.security.NoSuchAlgorithmException;
1111
import java.text.NumberFormat;
12+
import java.util.AbstractMap;
13+
import java.util.List;
1214
import java.util.Locale;
15+
import java.util.concurrent.CompletableFuture;
16+
import java.util.stream.Collectors;
1317

14-
/**
15-
* Java 12 (March 2019)
16-
*/
18+
/// Java 12™ (March 2019)
19+
/// [JDK 12](https://openjdk.org/projects/jdk/12)
20+
///
21+
/// - STANDARD FEATURES:
22+
/// - 230: Microbenchmark Suite
23+
/// - 334: JVM Constants API
24+
/// - 340: One AArch64 Port, Not Two
25+
/// - 341: Default CDS Archives (`-Xshare:dump`)
26+
/// - 344: Abortable Mixed Collections for G1
27+
/// - 346: Promptly Return Unused Committed Memory from G1 (`-XX:SoftMaxHeapSize`)
28+
///
29+
/// - PREVIEW & INCUBATOR:
30+
/// - 189: Shenandoah: A Low-Pause-Time Garbage Collector "Experimental" (`-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC`)
31+
/// - 325: Switch Expressions (Preview)
1732
public class Java12 {
1833

1934
@Test
@@ -40,7 +55,7 @@ public void testFilesMismatch() throws Exception {
4055
Assertions.assertEquals(generateFileHashMD5(file1), generateFileHashMD5(file2));
4156
}
4257

43-
private String generateFileHashMD5(Path file) throws IOException, NoSuchAlgorithmException, NoSuchAlgorithmException, IOException {
58+
private String generateFileHashMD5(Path file) throws NoSuchAlgorithmException, IOException {
4459
MessageDigest md = MessageDigest.getInstance("MD5");
4560
byte[] fileBytes = Files.readAllBytes(file);
4661
byte[] hashBytes = md.digest(fileBytes);
@@ -60,4 +75,29 @@ public void testCompactNumberFormat() {
6075
Assertions.assertEquals("1.02M", shortNumberFormat.format(1_020_000));
6176
}
6277

78+
@Test
79+
public void testTeeingCollector() {
80+
List<String> words = List.of("one", "two", "three");
81+
82+
AbstractMap.SimpleEntry<Integer, Long> pair = words.stream().collect(Collectors.teeing(
83+
Collectors.summingInt(String::length),
84+
Collectors.counting(),
85+
AbstractMap.SimpleEntry::new)
86+
);
87+
88+
Assertions.assertEquals(11, pair.getKey());
89+
Assertions.assertEquals(3, pair.getValue());
90+
}
91+
92+
@Test
93+
public void testCompletableExceptionallyCompose() {
94+
var errorThrowingPlanA = CompletableFuture.supplyAsync(() -> 1 / 0);
95+
var noErrorPlanB = CompletableFuture.supplyAsync(() -> 0);
96+
97+
errorThrowingPlanA.exceptionallyComposeAsync(ex -> {
98+
Assertions.assertInstanceOf(ArithmeticException.class, ex);
99+
return noErrorPlanB;
100+
}).thenAccept(result -> Assertions.assertEquals(0, result));
101+
}
102+
63103
}

src/test/java/pl/mperor/lab/java/Java13.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@
33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
55

6-
import java.util.Locale;
6+
import java.io.IOException;
7+
import java.net.ServerSocket;
78

8-
/**
9-
* Java 13 (September 2019)
10-
*/
9+
/// Java 13™ (September 2019)
10+
/// [JDK 13](https://openjdk.org/projects/jdk/13)
11+
///
12+
/// - STANDARD FEATURES:
13+
/// - 353: Reimplement the Legacy Socket API (`-Djdk.net.usePlainSocketImpl`)
14+
/// - 350: Dynamic CDS (Class-Data Sharing) Archives (`-XX:ArchiveClassesAtExit=hello.jsa`)
15+
/// - 351: ZGC: Uncommit Unused Memory (`-XX:+UseZGC`)
16+
///
17+
/// - PREVIEW & INCUBATOR:
18+
/// - 354: Switch Expressions (Preview)
19+
/// - 355: Text Blocks (Preview)
1120
public class Java13 {
21+
22+
@Test
23+
public void testServerSocketImpl() throws IOException {
24+
try (ServerSocket serverSocket = new ServerSocket(8013)) {
25+
Assertions.assertNotNull(serverSocket);
26+
}
27+
}
28+
1229
}

src/test/java/pl/mperor/lab/java/Java15.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ public void testNashornEngineRemoved() {
103103
}
104104

105105
@Test
106-
public void testDatagramSocket() throws SocketException {
107-
DatagramSocket datagramSocket = new DatagramSocket();
108-
Assertions.assertNotNull(datagramSocket);
106+
public void testDatagramSocketImpl() throws SocketException {
107+
try (DatagramSocket datagramSocket = new DatagramSocket(8015)) {
108+
Assertions.assertNotNull(datagramSocket);
109+
}
109110
}
110111

111112
}

src/test/java/pl/mperor/lab/java/Java18.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testDefaultCharsetIsUTF8() throws IOException {
5252

5353
@Test
5454
public void testSimpleWebServer() throws IOException, InterruptedException {
55-
HttpServer server = HttpServer.create(new InetSocketAddress(9000), 0);
55+
HttpServer server = HttpServer.create(new InetSocketAddress(8018), 0);
5656
server.createContext("/hello", exchange -> {
5757
String response = "Hello World!";
5858
exchange.sendResponseHeaders(200, response.getBytes().length);
@@ -62,7 +62,7 @@ public void testSimpleWebServer() throws IOException, InterruptedException {
6262
});
6363
server.start();
6464
Assertions.assertNotNull(server);
65-
Assertions.assertEquals("Hello World!", curlResponse("http://127.0.0.1:9000/hello"));
65+
Assertions.assertEquals("Hello World!", curlResponse("http://127.0.0.1:8018/hello"));
6666

6767
server.stop(0);
6868
}

src/test/java/pl/mperor/lab/java/Java4.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void testImageIO() throws IOException {
9292
}
9393
@Test
9494
public void testServerClientSocketChannel() throws IOException, InterruptedException {
95-
int port = 8888;
95+
int port = 8004;
9696
CountDownLatch serverReadyLatch = new CountDownLatch(1);
9797

9898
var executorService = Executors.newSingleThreadExecutor();

src/test/java/pl/mperor/lab/java/Java9.java

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,16 @@
77
import java.io.FileWriter;
88
import java.io.IOException;
99
import java.io.PrintWriter;
10-
import java.net.URI;
11-
import java.net.URISyntaxException;
12-
import java.net.http.HttpClient;
13-
import java.net.http.HttpRequest;
14-
import java.net.http.HttpResponse;
1510
import java.nio.file.Files;
1611
import java.util.Collections;
1712
import java.util.HashMap;
1813
import java.util.Map;
19-
import java.util.concurrent.CompletableFuture;
20-
import java.util.concurrent.ExecutionException;
2114

2215
/**
2316
* Java 1.9 (September 2017)
2417
*/
2518
public class Java9 {
2619

27-
@Test
28-
public void testNewHTTPClientAPI_getMethodSyncVsAsync() throws URISyntaxException, IOException, InterruptedException, ExecutionException {
29-
HttpClient client = HttpClient.newHttpClient();
30-
HttpRequest request = HttpRequest.newBuilder()
31-
.uri(new URI("https://jsonplaceholder.typicode.com/posts/1"))
32-
.GET()
33-
.build();
34-
35-
// Synchronous
36-
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
37-
Assertions.assertEquals(200, response.statusCode());
38-
Assertions.assertNotNull(response.body());
39-
40-
// Asynchronous
41-
CompletableFuture<HttpResponse<String>> responseFuture =
42-
client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
43-
44-
responseFuture.thenAccept(result -> {
45-
Assertions.assertEquals(200, result.statusCode());
46-
Assertions.assertNotNull(result.body());
47-
}).get();
48-
}
49-
50-
@Test
51-
public void testNewHTTPClientAPI_postMethod() throws URISyntaxException, IOException, InterruptedException {
52-
HttpClient client = HttpClient.newHttpClient();
53-
HttpRequest request = HttpRequest.newBuilder()
54-
.uri(new URI("https://jsonplaceholder.typicode.com/posts"))
55-
.header("Content-Type", "application/json")
56-
.POST(HttpRequest.BodyPublishers.ofString("""
57-
{
58-
"title": "foo",
59-
"body": "bar",
60-
"userId": 1
61-
}
62-
""")
63-
).build();
64-
65-
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
66-
Assertions.assertEquals(201, response.statusCode());
67-
Assertions.assertNotNull(response.body());
68-
}
69-
7020
@Test
7121
public void testProcessAPI() throws IOException {
7222
ProcessHandle self = ProcessHandle.current();

0 commit comments

Comments
 (0)