From 7c0af736049d5c57a6fe4c6b04ce45a67dab47de Mon Sep 17 00:00:00 2001 From: i032048 Date: Thu, 28 Jul 2016 18:03:48 +0300 Subject: [PATCH] initial push for core-java-9 tutorials --- core-java-9/.gitignore | 13 ++ core-java-9/README.md | 5 + core-java-9/pom.xml | 138 +++++++++++++++++ core-java-9/src/main/java/.gitignore | 13 ++ .../baeldung/java9/process/ProcessUtils.java | 48 ++++++ .../baeldung/java9/process/ServiceMain.java | 22 +++ core-java-9/src/main/resources/logback.xml | 16 ++ core-java-9/src/test/build.bat | 10 ++ .../test/java/com/baeldung/java9/Main.java | 22 +++ .../java9/MultiResultionImageTest.java | 48 ++++++ .../com/baeldung/java9/httpclient/Main.java | 14 ++ .../java9/httpclient/SimpleHttpRequests.java | 146 ++++++++++++++++++ .../baeldung/java9/language/DiamondTest.java | 36 +++++ .../java9/language/PrivateInterface.java | 23 +++ .../java9/language/TryWithResourcesTest.java | 70 +++++++++ .../java/com/baeldung/java9/process/Main.java | 18 +++ .../baeldung/java9/process/ProcessApi.java | 112 ++++++++++++++ core-java-9/src/test/resources/.gitignore | 13 ++ 18 files changed, 767 insertions(+) create mode 100644 core-java-9/.gitignore create mode 100644 core-java-9/README.md create mode 100644 core-java-9/pom.xml create mode 100644 core-java-9/src/main/java/.gitignore create mode 100644 core-java-9/src/main/java/com/baeldung/java9/process/ProcessUtils.java create mode 100644 core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java create mode 100644 core-java-9/src/main/resources/logback.xml create mode 100644 core-java-9/src/test/build.bat create mode 100644 core-java-9/src/test/java/com/baeldung/java9/Main.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/MultiResultionImageTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/httpclient/Main.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequests.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/DiamondTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/PrivateInterface.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/TryWithResourcesTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/process/Main.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/process/ProcessApi.java create mode 100644 core-java-9/src/test/resources/.gitignore diff --git a/core-java-9/.gitignore b/core-java-9/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/core-java-9/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/core-java-9/README.md b/core-java-9/README.md new file mode 100644 index 0000000000..b5d4dbef95 --- /dev/null +++ b/core-java-9/README.md @@ -0,0 +1,5 @@ +========= + +## Core Java 9 Examples + +http://inprogress.baeldung.com/java-9-new-features/ \ No newline at end of file diff --git a/core-java-9/pom.xml b/core-java-9/pom.xml new file mode 100644 index 0000000000..06421915a3 --- /dev/null +++ b/core-java-9/pom.xml @@ -0,0 +1,138 @@ + + 4.0.0 + com.baeldung + core-java9 + 0.1-SNAPSHOT + + core-java9 + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + junit + junit + ${junit.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + core-java-9 + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + true + C:\develop\jdks\jdk-9_ea122\bin\javac + 1.9 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + 1.7.13 + 1.0.13 + + + 5.1.3.Final + + + 19.0 + 3.4 + + + 1.3 + 4.12 + 1.10.19 + + + 3.5.1 + + + 2.6 + 2.19.1 + 2.7 + + + + diff --git a/core-java-9/src/main/java/.gitignore b/core-java-9/src/main/java/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/core-java-9/src/main/java/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/core-java-9/src/main/java/com/baeldung/java9/process/ProcessUtils.java b/core-java-9/src/main/java/com/baeldung/java9/process/ProcessUtils.java new file mode 100644 index 0000000000..b9c1cf1880 --- /dev/null +++ b/core-java-9/src/main/java/com/baeldung/java9/process/ProcessUtils.java @@ -0,0 +1,48 @@ +package com.baeldung.java9.process; + +import java.io.File; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.time.Duration; +import java.time.Instant; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +import junit.framework.Assert; + +public class ProcessUtils { + + public static String getClassPath(){ + String cp = System.getProperty("java.class.path"); + System.out.println("ClassPath is "+cp); + return cp; + } + + public static File getJavaCmd() throws IOException{ + String javaHome = System.getProperty("java.home"); + File javaCmd; + if(System.getProperty("os.name").startsWith("Win")){ + javaCmd = new File(javaHome, "bin/java.exe"); + }else{ + javaCmd = new File(javaHome, "bin/java"); + } + if(javaCmd.canExecute()){ + return javaCmd; + }else{ + throw new UnsupportedOperationException(javaCmd.getCanonicalPath() + " is not executable"); + } + } + + public static String getMainClass(){ + return System.getProperty("sun.java.command"); + } + + public static String getSystemProperties(){ + StringBuilder sb = new StringBuilder(); + System.getProperties().forEach((s1, s2) -> sb.append(s1 +" - "+ s2) ); + return sb.toString(); + } +} diff --git a/core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java b/core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java new file mode 100644 index 0000000000..458f746496 --- /dev/null +++ b/core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java @@ -0,0 +1,22 @@ +package com.baeldung.java9.process; + +import java.util.Optional; + +public class ServiceMain { + + public static void main(String[] args) throws InterruptedException { + ProcessHandle thisProcess = ProcessHandle.current(); + long pid = thisProcess.getPid(); + + + Optional opArgs = Optional.ofNullable(args); + String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command")); + + for (int i = 0; i < 10000; i++) { + System.out.println("Process " + procName + " with ID " + pid + " is running!"); + Thread.sleep(10000); + } + + } + +} diff --git a/core-java-9/src/main/resources/logback.xml b/core-java-9/src/main/resources/logback.xml new file mode 100644 index 0000000000..eefdc7a337 --- /dev/null +++ b/core-java-9/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + \ No newline at end of file diff --git a/core-java-9/src/test/build.bat b/core-java-9/src/test/build.bat new file mode 100644 index 0000000000..c6208b89d2 --- /dev/null +++ b/core-java-9/src/test/build.bat @@ -0,0 +1,10 @@ +@echo off +REM C:\develop\jdks\jdk-9_ea123\bin\javac +REM +REM C:\develop\jdks\jdk1.8.0_45\bin\javac +@echo on +C:\develop\jdks\jdk-9_ea123\bin\javac -Werror -cp C:\Users\i032048\.m2\repository\org\hamcrest\hamcrest-library\1.3\hamcrest-library-1.3.jar;C:\Users\i032048\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\i032048\.m2\repository\junit\junit\4.4\junit-4.4.jar;C:\develop\git-repos\baeldung-tutorials\core-java-9\target\test-classes;C:\develop\git-repos\baeldung-tutorials\core-java-9\src\test %1 + + +@echo off +REM com\baeldung\java9\language\TryWithResourcesTest.java diff --git a/core-java-9/src/test/java/com/baeldung/java9/Main.java b/core-java-9/src/test/java/com/baeldung/java9/Main.java new file mode 100644 index 0000000000..a41541f000 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/Main.java @@ -0,0 +1,22 @@ +package com.baeldung.java9; + +import com.baeldung.java9.language.PrivateInterface; +import com.baeldung.java9.language.TryWithResourcesTest; + +public class Main { + + public static void main(String args[]){ + PrivateInterface pi =new PrivateInterface() { + }; + pi.check(); + } + +// public static void main(String[] args) throws Exception { +// MultiResultionImageTest mri = new MultiResultionImageTest(); +// mri.baseMultiResImageTest(); +// +// TryWithResourcesTest tt = new TryWithResourcesTest(); +// // tt.test1(); +// } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/MultiResultionImageTest.java b/core-java-9/src/test/java/com/baeldung/java9/MultiResultionImageTest.java new file mode 100644 index 0000000000..d6c16b91bc --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/MultiResultionImageTest.java @@ -0,0 +1,48 @@ +package com.baeldung.java9; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +import java.awt.Image; +import java.awt.image.BaseMultiResolutionImage; +import java.awt.image.BufferedImage; +import java.awt.image.MultiResolutionImage; +import java.util.List; + +import org.junit.Test; + +public class MultiResultionImageTest { + + + @Test + public void baseMultiResImageTest() { + int baseIndex = 1; + int length = 4; + BufferedImage[] resolutionVariants = new BufferedImage[length]; + for (int i = 0; i < length; i++) { + resolutionVariants[i] = createImage(i); + } + MultiResolutionImage bmrImage = new BaseMultiResolutionImage(baseIndex, resolutionVariants); + List rvImageList = bmrImage.getResolutionVariants(); + assertEquals("MultiResoltion Image shoudl contain the same number of resolution variants!", rvImageList.size(), length); + + for (int i = 0; i < length; i++) { + int imageSize = getSize(i); + Image testRVImage = bmrImage.getResolutionVariant(imageSize, imageSize); + assertSame("Images should be the same", testRVImage, resolutionVariants[i]); + } + + } + + private static int getSize(int i) { + return 8 * (i + 1); + } + + + private static BufferedImage createImage(int i) { + return new BufferedImage(getSize(i), getSize(i), + BufferedImage.TYPE_INT_RGB); + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/Main.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/Main.java new file mode 100644 index 0000000000..c7d2f43800 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/Main.java @@ -0,0 +1,14 @@ +package com.baeldung.java9.httpclient; + +public class Main { + + public static void main(String[] args) throws Exception { + SimpleHttpRequests shr = new SimpleHttpRequests(); + shr.quickGet(); + shr.PostMehtod(); + + shr.configureHttpClient(); + shr.asyncGet(); + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequests.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequests.java new file mode 100644 index 0000000000..78a38f3357 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequests.java @@ -0,0 +1,146 @@ +package com.baeldung.java9.httpclient; + +import static java.net.HttpURLConnection.HTTP_OK; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpHeaders; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.security.NoSuchAlgorithmException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLParameters; + +import org.junit.Before; +import org.junit.Test; + +public class SimpleHttpRequests { + + // private URI httpURI = + + @Before + public void init() { + + } + + @Test + public void quickGet() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.create(new URI("http://localhost:8080")).GET(); + HttpResponse response = request.response(); + System.out.println(printHeaders(response.headers())); + String responseBody = response.body(HttpResponse.asString()); + assertTrue("Get response body size", responseBody.length() > 10); + } + + @Test + public void asyncGet() throws URISyntaxException, IOException, InterruptedException, ExecutionException{ + HttpRequest request = HttpRequest.create(new URI("http://localhost:8080")).GET(); + long before = System.currentTimeMillis(); + CompletableFuture futureResponse = request.responseAsync(); + futureResponse.thenAccept( response -> { + HttpHeaders hs = response.headers(); + System.out.println(Thread.currentThread()+"\nHeaders:----------------------\n"+ printHeaders(hs)); + String responseBody = response.body(HttpResponse.asString()); + + + //System.out.println(responseBody); + }); + + + + + long after = System.currentTimeMillis(); + System.out.println(Thread.currentThread()+" waits "+ (after - before)); + assertTrue("Thread waits", (after - before) < 1500); + + futureResponse.join(); + + // Calculate some other thing in this Thread + //HttpResponse response = futureResponse.get(); + long afterAfter = System.currentTimeMillis(); + System.out.println(Thread.currentThread()+ "(afterAfter - before)"+ (afterAfter - before)); + + //String responseBody = response.body(HttpResponse.asString()); + //HttpHeaders hs = response.headers(); + //System.out.println(responseBody); + // assertTrue("Get response body size", responseBody.length() > 10); + } + + @Test + public void PostMehtod() throws URISyntaxException, IOException, InterruptedException { + HttpRequest.Builder requestBuilder = HttpRequest.create(new URI("http://localhost:8080")); + requestBuilder.body(HttpRequest.fromString("param1=foo,param2=bar")).followRedirects(HttpClient.Redirect.SECURE); + HttpRequest request = requestBuilder.POST(); + HttpResponse response = request.response(); + int statusCode = response.statusCode(); + assertTrue("HTTP return code", statusCode == HTTP_OK); + } + + @Test + public void configureHttpClient() throws NoSuchAlgorithmException, URISyntaxException, IOException, InterruptedException{ + CookieManager cManager = new CookieManager(); + cManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); + + SSLParameters sslParam = new SSLParameters (new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String[] { "TLSv1.2" }); + + HttpClient.Builder hcBuilder = HttpClient.create(); + hcBuilder.cookieManager(cManager).sslContext(SSLContext.getDefault()).sslParameters(sslParam); + HttpClient httpClient = hcBuilder.build(); + HttpRequest.Builder reqBuilder = httpClient.request(new URI("https://localhost:8443")); + + HttpRequest request = reqBuilder.followRedirects(HttpClient.Redirect.ALWAYS).GET(); + HttpResponse response = request.response(); + int statusCode = response.statusCode(); + System.out.println(response.body(HttpResponse.asString())); + assertTrue("HTTP return code", statusCode == HTTP_OK); + } + + SSLParameters getDefaultSSLParameters() throws NoSuchAlgorithmException{ + SSLParameters sslP1 = SSLContext.getDefault().getSupportedSSLParameters(); + String [] proto = sslP1.getApplicationProtocols(); + String [] cifers = sslP1.getCipherSuites(); + System.out.println(printStringArr(proto)); + System.out.println(printStringArr(cifers)); + return sslP1; + } + + String printStringArr(String ... args ){ + if(args == null){ + return null; + } + StringBuilder sb = new StringBuilder(); + for (String s : args){ + sb.append(s); + sb.append("\n"); + } + return sb.toString(); + } + + String printHeaders(HttpHeaders h){ + if(h == null){ + return null; + } + + StringBuilder sb = new StringBuilder(); + Map> hMap = h.map(); + for(String k : hMap.keySet()){ + sb.append(k).append(":"); + List l = hMap.get(k); + if( l != null ){ + l.forEach( s -> { sb.append(s).append(","); } ); + } + sb.append("\n"); + } + return sb.toString(); + } +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/DiamondTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/DiamondTest.java new file mode 100644 index 0000000000..33da6486f4 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/DiamondTest.java @@ -0,0 +1,36 @@ +package com.baeldung.java9.language; + +import org.junit.Test; + +public class DiamondTest { + + static class FooClass { + FooClass(X x) { + } + + FooClass(X x, Z z) { + } + } + + @Test + public void diamondTest() { + FooClass fc = new FooClass<>(1) { + }; + FooClass fc0 = new FooClass<>(1) { + }; + FooClass fc1 = new FooClass<>(1) { + }; + FooClass fc2 = new FooClass<>(1) { + }; + + FooClass fc3 = new FooClass<>(1, "") { + }; + FooClass fc4 = new FooClass<>(1, "") { + }; + FooClass fc5 = new FooClass<>(1, "") { + }; + FooClass fc6 = new FooClass<>(1, "") { + }; + + } +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/PrivateInterface.java b/core-java-9/src/test/java/com/baeldung/java9/language/PrivateInterface.java new file mode 100644 index 0000000000..fd6a496b18 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/PrivateInterface.java @@ -0,0 +1,23 @@ +package com.baeldung.java9.language; + +public interface PrivateInterface { + + private static String staticPrivate() { + return "static private"; + } + + private String instancePrivate() { + return "instance private"; + } + + public default void check(){ + String result = staticPrivate(); + if (!result.equals("static private")) + throw new AssertionError("Incorrect result for static private interface method"); + PrivateInterface pvt = new PrivateInterface() { + }; + result = pvt.instancePrivate(); + if (!result.equals("instance private")) + throw new AssertionError("Incorrect result for instance private interface method"); + } +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/TryWithResourcesTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/TryWithResourcesTest.java new file mode 100644 index 0000000000..687dfbc390 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/TryWithResourcesTest.java @@ -0,0 +1,70 @@ +package com.baeldung.java9.language; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class TryWithResourcesTest { + + static int closeCount = 0; + + static class MyAutoCloseable implements AutoCloseable{ + final FinalWrapper finalWrapper = new FinalWrapper(); + + public void close() { + closeCount++; + } + + static class FinalWrapper { + public final AutoCloseable finalCloseable = new AutoCloseable() { + @Override + public void close() throws Exception { + closeCount++; + } + }; + } + } + + @Test + public void tryWithResourcesTest() { + MyAutoCloseable mac = new MyAutoCloseable(); + + try (mac) { + assertEquals("Expected and Actual does not match", 0, closeCount); + } + + try (mac.finalWrapper.finalCloseable) { + assertEquals("Expected and Actual does not match", 1, closeCount); + } catch (Exception ex) { + } + + try (new MyAutoCloseable() { }.finalWrapper.finalCloseable) { + assertEquals("Expected and Actual does not match", 2, closeCount); + } catch (Exception ex) { + } + + try ((closeCount > 0 ? mac : new MyAutoCloseable()).finalWrapper.finalCloseable) { + assertEquals("Expected and Actual does not match", 3, closeCount); + } catch (Exception ex) { + } + + try { + throw new CloseableException(); + } catch (CloseableException ex) { + try (ex) { + assertEquals("Expected and Actual does not match", 4, closeCount); + } + } + assertEquals("Expected and Actual does not match", 5, closeCount); + } + + + static class CloseableException extends Exception implements AutoCloseable { + @Override + public void close() { + closeCount++; + } + } + +} + + diff --git a/core-java-9/src/test/java/com/baeldung/java9/process/Main.java b/core-java-9/src/test/java/com/baeldung/java9/process/Main.java new file mode 100644 index 0000000000..b16baf4676 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/process/Main.java @@ -0,0 +1,18 @@ +package com.baeldung.java9.process; + +import java.security.NoSuchAlgorithmException; + +public class Main { + + public static void main(String[] args) throws Exception { + ProcessApi procApi = new ProcessApi(); + procApi.createAndDestroyProcess(); + + procApi.processInfoExample(); + + Thread.sleep(40200); + System.out.println("_______END!___________"); + + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/process/ProcessApi.java b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessApi.java new file mode 100644 index 0000000000..419516cb64 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessApi.java @@ -0,0 +1,112 @@ +package com.baeldung.java9.process; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +import junit.framework.Assert; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ProcessApi { + + + @Before + public void init() { + + } + + @Test + public void processInfoExample()throws NoSuchAlgorithmException{ + ProcessHandle self = ProcessHandle.current(); + long PID = self.getPid(); + ProcessHandle.Info procInfo = self.info(); + Optional args = procInfo.arguments(); + Optional cmd = procInfo.commandLine(); + Optional startTime = procInfo.startInstant(); + Optional cpuUsage = procInfo.totalCpuDuration(); + + waistCPU(); + System.out.println("Args "+ args); + System.out.println("Command " +cmd.orElse("EmptyCmd")); + System.out.println("Start time: "+ startTime.get().toString()); + System.out.println(cpuUsage.get().toMillis()); + + Stream allProc = ProcessHandle.current().children(); + allProc.forEach(p -> { + System.out.println("Proc "+ p.getPid()); + }); + + } + + @Test + public void createAndDestroyProcess() throws IOException, InterruptedException{ + int numberOfChildProcesses = 5; + for(int i=0; i < numberOfChildProcesses; i++){ + createNewJVM(ServiceMain.class, i).getPid(); + } + + Stream childProc = ProcessHandle.current().children(); + assertEquals( childProc.count(), numberOfChildProcesses); + + childProc = ProcessHandle.current().children(); + childProc.forEach(processHandle -> { + assertTrue("Process "+ processHandle.getPid() +" should be alive!", processHandle.isAlive()); + CompletableFuture onProcExit = processHandle.onExit(); + onProcExit.thenAccept(procHandle -> { + System.out.println("Process with PID "+ procHandle.getPid() + " has stopped"); + }); + }); + + Thread.sleep(10000); + + childProc = ProcessHandle.current().children(); + childProc.forEach(procHandle -> { + assertTrue("Could not kill process "+procHandle.getPid(), procHandle.destroy()); + }); + + Thread.sleep(5000); + + childProc = ProcessHandle.current().children(); + childProc.forEach(procHandle -> { + assertFalse("Process "+ procHandle.getPid() +" should not be alive!", procHandle.isAlive()); + }); + + } + + private Process createNewJVM(Class mainClass, int number) throws IOException{ + ArrayList cmdParams = new ArrayList(5); + cmdParams.add(ProcessUtils.getJavaCmd().getAbsolutePath()); + cmdParams.add("-cp"); + cmdParams.add(ProcessUtils.getClassPath()); + cmdParams.add(mainClass.getName()); + cmdParams.add("Service "+ number); + ProcessBuilder myService = new ProcessBuilder(cmdParams); + myService.inheritIO(); + return myService.start(); + } + + private void waistCPU() throws NoSuchAlgorithmException{ + ArrayList randArr = new ArrayList(4096); + SecureRandom sr = SecureRandom.getInstanceStrong(); + Duration somecpu = Duration.ofMillis(4200L); + Instant end = Instant.now().plus(somecpu); + while (Instant.now().isBefore(end)) { + //System.out.println(sr.nextInt()); + randArr.add( sr.nextInt() ); + } + } + +} diff --git a/core-java-9/src/test/resources/.gitignore b/core-java-9/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/core-java-9/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file