diff --git a/README.md b/README.md index a948c15..f081428 100644 --- a/README.md +++ b/README.md @@ -20,5 +20,6 @@ This project includes unit tests for key functionalities introduced in each Java - [Java 1.6 (Java 6)](src/test/java/pl/mperor/lab/java/Java6.java) - [Java 1.7 (Java 7)](src/test/java/pl/mperor/lab/java/Java7.java) - [Java 1.8 (Java 8)](src/test/java/pl/mperor/lab/java/Java8.java) +- [Java 1.9 (Java 9)](src/test/java/pl/mperor/lab/java/Java9.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/Java2.java b/src/test/java/pl/mperor/lab/java/Java2.java index 640f90a..76a6186 100644 --- a/src/test/java/pl/mperor/lab/java/Java2.java +++ b/src/test/java/pl/mperor/lab/java/Java2.java @@ -6,9 +6,9 @@ import javax.swing.*; import java.awt.*; import java.lang.reflect.InvocationTargetException; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; /** * Java 1.2 (December 1998) @@ -17,9 +17,9 @@ public class Java2 { @Test public void testCollectionsApiBasics() { - var list = List.of(1, 2, 3); - var set = Set.of(1, 2, 3); - var map = Map.of(1, "One", 2, "Two", 3, "Three"); + var list = Arrays.asList(1, 2, 3); + var set = new HashSet<>(list); + var map = Collections.singletonMap(2, "Two"); Assertions.assertEquals(1, list.getFirst()); Assertions.assertTrue(set.contains(1)); Assertions.assertEquals("Two", map.get(2)); diff --git a/src/test/java/pl/mperor/lab/java/Java9.java b/src/test/java/pl/mperor/lab/java/Java9.java new file mode 100644 index 0000000..3b99997 --- /dev/null +++ b/src/test/java/pl/mperor/lab/java/Java9.java @@ -0,0 +1,159 @@ +package pl.mperor.lab.java; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +/** + * Java 1.9 (September 2017) + */ +public class Java9 { + + @Test + public void testNewHTTPClientAPI_getMethodSyncVsAsync() throws URISyntaxException, IOException, InterruptedException, ExecutionException { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://jsonplaceholder.typicode.com/posts/1")) + .GET() + .build(); + + // Synchronous + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + Assertions.assertEquals(200, response.statusCode()); + Assertions.assertNotNull(response.body()); + + // Asynchronous + CompletableFuture> responseFuture = + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); + + responseFuture.thenAccept(result -> { + Assertions.assertEquals(200, result.statusCode()); + Assertions.assertNotNull(result.body()); + }).get(); + } + + @Test + public void testNewHTTPClientAPI_postMethod() throws URISyntaxException, IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://jsonplaceholder.typicode.com/posts")) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(""" + { + "title": "foo", + "body": "bar", + "userId": 1 + } + """) + ).build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + Assertions.assertEquals(201, response.statusCode()); + Assertions.assertNotNull(response.body()); + } + + @Test + public void testProcessAPI() throws IOException { + ProcessHandle self = ProcessHandle.current(); + ProcessHandle.Info info = self.info(); + Assertions.assertNotNull(info); + Assertions.assertTrue(info.command().isPresent()); + + Process process = new ProcessBuilder("java", "-version").start(); + Assertions.assertTrue(process.isAlive()); + Assertions.assertTrue(process.pid() != -1); + process.destroy(); + } + + @Test + public void testJShell() throws IOException, InterruptedException { + Process process = new ProcessBuilder("jshell") + .start(); + Assertions.assertTrue(process.isAlive()); + + try (var reader = process.inputReader(); + var writer = new PrintWriter(process.outputWriter())) { + writer.println("System.out.println(\"Hello from JShell!\");"); + writer.println("/exit"); + writer.flush(); + + String lastLineBeforeExit = reader.lines() + .takeWhile(s -> !s.contains("Goodbye")) + .reduce("", (first, second) -> second); + Assertions.assertEquals("Hello from JShell!", lastLineBeforeExit); + } + + Assertions.assertEquals(0, process.waitFor()); + process.destroy(); + } + + @Test + public void testPrivateInterfaceMethod() { + InterfaceJava9 myInterface = new InterfaceJava9() {}; + Assertions.assertEquals("Private logic in public method!", myInterface.publicMethod()); + } + + public interface InterfaceJava9 { + default String publicMethod() { + return privateMethod() + " in public method!"; + } + + private String privateMethod() { + return "Private logic"; + } + } + + @Test + public void testCollectionFactoryMethods() { + Map byFactoryMethodMap = Map.of("one", 1, "two", 2, "three", 3); + + // Old style combination of `new HashMap<>()` and `put()` + Map classicalMap = new HashMap<>(); + classicalMap.put("one", 1); + classicalMap.put("two", 2); + classicalMap.put("three", 3); + Assertions.assertEquals(byFactoryMethodMap, classicalMap); + + // Double Brace Initialization + Map doubleBraceInitializedMap = new HashMap<>() {{ + put("one", 1); + put("two", 2); + put("three", 3); + }}; + Assertions.assertEquals(byFactoryMethodMap, doubleBraceInitializedMap); + + // Single entry map + Map singleEntryMap = Collections.singletonMap("one", 1); + Assertions.assertEquals(byFactoryMethodMap.get("one"), singleEntryMap.get("one")); + } + + @Test + public void testTryWithResourcesWithJava9Syntax() throws IOException { + File tempFile = File.createTempFile("Java9", "txt"); + // Declare and initialize the resource before the try block + FileWriter writer = new FileWriter(tempFile); + try (writer) { + writer.write("Java 9 syntax for try-with-resources!"); + } + IOException flushException = Assertions.assertThrows(IOException.class, () -> writer.flush()); + Assertions.assertEquals("Stream closed", flushException.getMessage()); + Assertions.assertEquals("Java 9 syntax for try-with-resources!", Files.readString(tempFile.toPath())); + Assertions.assertTrue(Files.deleteIfExists(tempFile.toPath())); + } + +}