-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava14.java
More file actions
121 lines (105 loc) · 4.04 KB
/
Copy pathJava14.java
File metadata and controls
121 lines (105 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package pl.mperor.lab.java;
import jdk.jfr.Configuration;
import jdk.jfr.Event;
import jdk.jfr.Label;
import jdk.jfr.Name;
import jdk.jfr.consumer.RecordingStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.text.ParseException;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertTrue;
/// Java 14™ (March 2020)
/// [JDK 14](https://openjdk.org/projects/jdk/14)
///
/// - STANDARD FEATURES:
/// - 361: Switch Expressions (Standard)
/// - 358: Helpful NullPointerExceptions
/// - 367: Remove the Pack200 Tools and API
/// - 349: JFR Event Streaming (`-XX:StartFlightRecording:filename=events.jfr`) [JFAPI](https://docs.oracle.com/en/java/javase/23/jfapi)
/// - 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector (`-XX:+UseConcMarkSweepGC`)
/// - 345: NUMA-Aware Memory Allocation for G1 (`+XX:+UseNUMA`)
/// - 352: Non-Volatile Mapped Byte Buffers
/// - 364: ZGC on macOS
/// - 365: ZGC on Windows
/// - 362: Deprecate the Solaris and SPARC Ports
/// - 366: Deprecate the ParallelScavenge + SerialOld GC Combination (`-XX:-UseParallelOldGC`)
///
/// - PREVIEW & INCUBATOR:
/// - 359: Records (Preview)
/// - 305: Pattern Matching for instanceof (Preview)
/// - 368: Text Blocks (Second Preview)
/// - 343: Packaging Tool `jpackage` (Incubator)
/// - 370: Foreign-Memory Access API (Incubator)
public class Java14 {
@Test
public void testSwitchExpression() {
Assertions.assertEquals("Working Day", getTypeOfDayByNumber(1));
Assertions.assertEquals("Day Off", getTypeOfDayByNumber(6));
Assertions.assertThrows(IllegalStateException.class, () -> getTypeOfDayByNumber(0));
}
private String getTypeOfDayByNumber(int dayOfWeek) {
return switch (dayOfWeek) {
case 1, 2, 3, 4, 5 -> "Working Day";
case 6, 7 -> {
// code block example
yield "Day Off";
}
default -> throw new IllegalStateException("Unexpected value: " + dayOfWeek);
};
}
@Test
public void testSwitchStatementVsSwitchExpression() {
assertTrue(switchExpression(Switch.EXPRESSION));
assertTrue(switchStatement(Switch.STATEMENT));
}
enum Switch {EXPRESSION, STATEMENT}
private static boolean switchExpression(Switch s) {
return switch (s) {
case EXPRESSION -> true;
default -> false;
};
}
private static boolean switchStatement(Switch s) {
switch (s) {
case STATEMENT -> {
return true;
}
default -> {
return false;
}
}
}
@Test
public void testHelpfulNullPointerExceptions() {
NullPointerException exception = Assertions.assertThrows(NullPointerException.class, () -> {
String name = null;
name.length();
});
assertTrue(exception.getMessage().contains("Cannot invoke \"String.length()\" because \"name\" is null"));
}
@Test
public void testPack200ToolsAndAPIRemoved() {
Assertions.assertThrows(ClassNotFoundException.class, () -> {
Class.forName("java.util.jar.Pack200");
});
}
@Test
public void testThreadIsInterruptedAlwaysAvailable() {
Thread neverStartedThread = new Thread();
Assertions.assertFalse(neverStartedThread.isInterrupted());
neverStartedThread.interrupt();
Assertions.assertTrue(neverStartedThread.isInterrupted());
}
@Test
public void testJFREvent() throws IOException, ParseException, InterruptedException {
Configuration c = Configuration.getConfiguration("profile");
try (RecordingStream rs = new RecordingStream(c)) {
rs.onEvent("jdk.ThreadSleep", event -> Assertions.assertTrue(event.getDuration().equals(Duration.ofSeconds(100))));
rs.startAsync();
System.out.println("Sleeping for 100ms...");
Thread.sleep(100);
}
}
}