From e4dc9b4c3da604563a9e2c27f1caa5cc8603b2c9 Mon Sep 17 00:00:00 2001 From: mperor Date: Tue, 22 Oct 2024 22:16:09 +0200 Subject: [PATCH] Add Java 19 test class ... - Virtual Threads - Pattern Matching for Switch --- README.md | 1 + src/test/java/pl/mperor/lab/java/Java19.java | 40 ++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/test/java/pl/mperor/lab/java/Java19.java diff --git a/README.md b/README.md index eff0b23..3ea8d47 100644 --- a/README.md +++ b/README.md @@ -30,5 +30,6 @@ This project includes unit tests for key functionalities introduced in each Java - [Java 16](src/test/java/pl/mperor/lab/java/Java16.java) - [Java 17](src/test/java/pl/mperor/lab/java/Java17.java) - [Java 18](src/test/java/pl/mperor/lab/java/Java18.java) +- [Java 19](src/test/java/pl/mperor/lab/java/Java19.java) For detailed examples and tests of each feature, please refer to the individual source files linked above. \ No newline at end of file diff --git a/src/test/java/pl/mperor/lab/java/Java19.java b/src/test/java/pl/mperor/lab/java/Java19.java new file mode 100644 index 0000000..77a95de --- /dev/null +++ b/src/test/java/pl/mperor/lab/java/Java19.java @@ -0,0 +1,40 @@ +package pl.mperor.lab.java; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pl.mperor.lab.TestUtils; +import pl.mperor.lab.TestUtils.ReadableOut; + +/** + * Java 19 (September 2022) + */ +public class Java19 { + + @Test + public void testRecordPatternDeconstruct() { + record Point(int x, int y) {} + record LineSegment(Point start, Point end) {} + Object obj = new LineSegment(new Point(0,1), new Point(1, 2)); + + if(obj instanceof LineSegment(Point(int x, int y), Point end)) { + Assertions.assertEquals(0, x); + Assertions.assertEquals(1, y); + Assertions.assertEquals(new Point (1,2), end); + } + } + + @Test + public void testVirtualThreads() throws InterruptedException { + ReadableOut out = TestUtils.setTempSystemOut(); + Thread virtualThread = Thread.startVirtualThread(() -> + System.out.print("Hello from Virtual Thread!") + ); + virtualThread.join(); + TestUtils.resetSystemOut(); + + Assertions.assertTrue(virtualThread.isDaemon() && virtualThread.isVirtual()); + Assertions.assertEquals(Thread.NORM_PRIORITY, virtualThread.getPriority()); + Assertions.assertEquals("Hello from Virtual Thread!", out.all()); + } + +} \ No newline at end of file