Skip to content

Commit 1c527a8

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

File tree

5 files changed

+124
-32
lines changed

5 files changed

+124
-32
lines changed

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@
1616
*/
1717
public class Java12 {
1818

19-
@Test
20-
public void testSwitchExpression() {
21-
Assertions.assertEquals("Working Day", getTypeOfDayByNumber(1));
22-
Assertions.assertEquals("Day Off", getTypeOfDayByNumber(6));
23-
Assertions.assertThrows(IllegalStateException.class, () -> getTypeOfDayByNumber(0));
24-
}
25-
26-
private String getTypeOfDayByNumber(int dayOfWeek) {
27-
return switch (dayOfWeek) {
28-
case 1, 2, 3, 4, 5 -> "Working Day";
29-
case 6, 7 -> {
30-
// code block example
31-
yield "Day Off";
32-
}
33-
default -> throw new IllegalStateException("Unexpected value: " + dayOfWeek);
34-
};
35-
}
36-
3719
@Test
3820
public void testStringIndentAndTransformMethods() {
3921
String text = "Hello\nJava 12\n";
Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,121 @@
11
package pl.mperor.lab.java;
22

3+
import jdk.jfr.Configuration;
4+
import jdk.jfr.Event;
5+
import jdk.jfr.Label;
6+
import jdk.jfr.Name;
7+
import jdk.jfr.consumer.RecordingStream;
38
import org.junit.jupiter.api.Assertions;
49
import org.junit.jupiter.api.Test;
510

6-
import java.util.Objects;
11+
import java.io.IOException;
12+
import java.text.ParseException;
13+
import java.time.Duration;
714

8-
/**
9-
* Java 14 (March 2020)
10-
*/
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
/// Java 14™ (March 2020)
18+
/// [JDK 14](https://openjdk.org/projects/jdk/14)
19+
///
20+
/// - STANDARD FEATURES:
21+
/// - 361: Switch Expressions (Standard)
22+
/// - 358: Helpful NullPointerExceptions
23+
/// - 367: Remove the Pack200 Tools and API
24+
/// - 349: JFR Event Streaming (`-XX:StartFlightRecording:filename=events.jfr`) [JFAPI](https://docs.oracle.com/en/java/javase/23/jfapi)
25+
/// - 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector (`-XX:+UseConcMarkSweepGC`)
26+
/// - 345: NUMA-Aware Memory Allocation for G1 (`+XX:+UseNUMA`)
27+
/// - 352: Non-Volatile Mapped Byte Buffers
28+
/// - 364: ZGC on macOS
29+
/// - 365: ZGC on Windows
30+
/// - 362: Deprecate the Solaris and SPARC Ports
31+
/// - 366: Deprecate the ParallelScavenge + SerialOld GC Combination (`-XX:-UseParallelOldGC`)
32+
///
33+
/// - PREVIEW & INCUBATOR:
34+
/// - 359: Records (Preview)
35+
/// - 305: Pattern Matching for instanceof (Preview)
36+
/// - 368: Text Blocks (Second Preview)
37+
/// - 343: Packaging Tool `jpackage` (Incubator)
38+
/// - 370: Foreign-Memory Access API (Incubator)
1139
public class Java14 {
1240

41+
@Test
42+
public void testSwitchExpression() {
43+
Assertions.assertEquals("Working Day", getTypeOfDayByNumber(1));
44+
Assertions.assertEquals("Day Off", getTypeOfDayByNumber(6));
45+
Assertions.assertThrows(IllegalStateException.class, () -> getTypeOfDayByNumber(0));
46+
}
47+
48+
private String getTypeOfDayByNumber(int dayOfWeek) {
49+
return switch (dayOfWeek) {
50+
case 1, 2, 3, 4, 5 -> "Working Day";
51+
case 6, 7 -> {
52+
// code block example
53+
yield "Day Off";
54+
}
55+
default -> throw new IllegalStateException("Unexpected value: " + dayOfWeek);
56+
};
57+
}
58+
59+
@Test
60+
public void testSwitchStatementVsSwitchExpression() {
61+
assertTrue(switchExpression(Switch.EXPRESSION));
62+
assertTrue(switchStatement(Switch.STATEMENT));
63+
}
64+
65+
enum Switch {EXPRESSION, STATEMENT}
66+
67+
private static boolean switchExpression(Switch s) {
68+
return switch (s) {
69+
case EXPRESSION -> true;
70+
default -> false;
71+
};
72+
}
73+
74+
private static boolean switchStatement(Switch s) {
75+
switch (s) {
76+
case STATEMENT -> {
77+
return true;
78+
}
79+
default -> {
80+
return false;
81+
}
82+
}
83+
}
84+
1385
@Test
1486
public void testHelpfulNullPointerExceptions() {
1587
NullPointerException exception = Assertions.assertThrows(NullPointerException.class, () -> {
1688
String name = null;
17-
int length = name.length();
89+
name.length();
1890
});
19-
Assertions.assertTrue(exception.getMessage().contains("Cannot invoke \"String.length()\" because \"name\" is null"));
91+
assertTrue(exception.getMessage().contains("Cannot invoke \"String.length()\" because \"name\" is null"));
2092
}
93+
94+
@Test
95+
public void testPack200ToolsAndAPIRemoved() {
96+
Assertions.assertThrows(ClassNotFoundException.class, () -> {
97+
Class.forName("java.util.jar.Pack200");
98+
});
99+
}
100+
101+
@Test
102+
public void testThreadIsInterruptedAlwaysAvailable() {
103+
Thread neverStartedThread = new Thread();
104+
Assertions.assertFalse(neverStartedThread.isInterrupted());
105+
neverStartedThread.interrupt();
106+
Assertions.assertTrue(neverStartedThread.isInterrupted());
107+
}
108+
109+
@Test
110+
public void testJFREvent() throws IOException, ParseException, InterruptedException {
111+
Configuration c = Configuration.getConfiguration("profile");
112+
try (RecordingStream rs = new RecordingStream(c)) {
113+
rs.onEvent("jdk.ThreadSleep", event -> event.getDuration().equals(Duration.ofSeconds(100)));
114+
rs.startAsync();
115+
116+
System.out.println("Sleeping for 100ms...");
117+
Thread.sleep(100);
118+
}
119+
}
120+
21121
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
/// - 371: Hidden Classes
2424
/// - 372: Remove the Nashorn JavaScript Engine
2525
/// - 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
26+
/// - 374: Disable and Deprecate Biased Locking (`-XX:+UseBiasedLocking`)
27+
/// - 377: ZGC: A Scalable Low-Latency Garbage Collector (`-XX:+UseZGC`)
28+
/// - 379: Shenandoah: A Low-Pause-Time Garbage Collector (`-XX:+UseShenandoahGC`)
2929
/// - 381: Remove the Solaris and SPARC Ports
3030
/// - 385: Deprecate RMI Activation for Removal [Java17#testRmiActivationRemoved()]
3131
///
@@ -44,8 +44,20 @@ public void testTextBlock() {
4444
"password": "*****"
4545
}
4646
""";
47-
4847
Assertions.assertTrue(json.contains("\"login\": \"admin\""));
48+
49+
String skippedLines = """
50+
a\
51+
b\
52+
c\
53+
""";
54+
Assertions.assertEquals("abc", skippedLines);
55+
56+
String notSkippedSpaces = """
57+
a \s
58+
b\s
59+
c""";
60+
Assertions.assertEquals("a \nb \nc", notSkippedSpaces);
4961
}
5062

5163
@Test

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/// - 395: Records
2929
/// - 394: Pattern Matching for instanceof
3030
/// - 390: Warnings for Value-Based Classes
31-
/// - 392: Packaging Tool
31+
/// - 392: Packaging Tool `jpackage`
3232
/// - 380: Unix-Domain Socket Channels
3333
/// - 396: Strongly Encapsulate JDK Internals by Default [Java17#testStronglyEncapsulatedInternals()]
3434
/// - 376: ZGC: Concurrent Thread-Stack Processing
@@ -46,7 +46,7 @@
4646
/// - 338: Vector API (Incubator)
4747
public class Java16 {
4848

49-
// POJOs
49+
// POJOs with accessors (without getters)
5050
@Test
5151
public void testPairRecord() {
5252
var pair = Pair.of("Boy", "Girl");

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
5-
import org.junit.jupiter.api.condition.DisabledOnJre;
6-
import org.junit.jupiter.api.condition.JRE;
75

86
import java.io.File;
97
import java.io.FileWriter;

0 commit comments

Comments
 (0)