Skip to content

Commit 4370f8c

Browse files
committed
Update test class for Java 15 ...
- samples only for standard features - markdown javadoc for JEPs - organize classes related to Java 15 - improve code readability
1 parent 954e0de commit 4370f8c

File tree

5 files changed

+109
-25
lines changed

5 files changed

+109
-25
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pl.mperor.lab.java;
2+
3+
public interface Hidden {
4+
static int lookup() {
5+
return 1;
6+
}
7+
}

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,4 @@
99
* Java 13 (September 2019)
1010
*/
1111
public class Java13 {
12-
13-
@Test
14-
public void testTextBlock() {
15-
String json = """
16-
{
17-
"login": "admin",
18-
"password": "*****"
19-
}
20-
""";
21-
22-
Assertions.assertTrue(json.contains("\"login\": \"admin\""));
23-
}
24-
25-
@Test
26-
public void testStringFormatted() {
27-
Assertions.assertEquals("Hello World!", "Hello %s".formatted("World!"));
28-
Assertions.assertEquals("Value: 0.00", String.format(Locale.US, "Value: %.2f", 0d));
29-
}
30-
3112
}
Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,99 @@
11
package 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)
637
public 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
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
/// - 394: Pattern Matching for instanceof
3030
/// - 390: Warnings for Value-Based Classes
3131
/// - 392: Packaging Tool
32-
/// - 396: Strongly Encapsulate JDK Internals by Default
33-
/// - 376: ZGC: Concurrent Thread-Stack Processing
3432
/// - 380: Unix-Domain Socket Channels
33+
/// - 396: Strongly Encapsulate JDK Internals by Default [Java17#testStronglyEncapsulatedInternals()]
34+
/// - 376: ZGC: Concurrent Thread-Stack Processing
3535
/// - 387: Elastic Metaspace
3636
/// - 347: Enable C++14 Language Features
3737
/// - 386: Alpine Linux Port

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ public String call() {
7878
}
7979
}
8080

81+
/**
82+
* You may need this JVM argument:
83+
* --add-opens java.base/java.lang=ALL-UNNAMED
84+
*/
8185
@Test
8286
public void testStronglyEncapsulatedInternals() {
8387
Assertions.assertThrows(InaccessibleObjectException.class, () -> deepLookIntoStringBytes("Hello"));
88+
new ProcessBuilder("java", "--add-opens", "java.base/java.lang=ALL-UNNAMED");
8489
}
8590

8691
private static byte[] deepLookIntoStringBytes(String string) throws IllegalAccessException, NoSuchFieldException {
@@ -132,7 +137,7 @@ public void testDeprecated() {
132137
}
133138

134139
@Test
135-
public void testRmiActivation() {
140+
public void testRmiActivationRemoved() {
136141
Assertions.assertFalse(ModuleLayer.boot().findModule("java.rmi").stream()
137142
.map(Module::getPackages)
138143
.flatMap(Set::stream)

0 commit comments

Comments
 (0)