From f6515341a53cbc76018c3963d4c975926ffaf049 Mon Sep 17 00:00:00 2001 From: Robinson Date: Thu, 20 Jan 2022 00:03:28 +0100 Subject: [PATCH] Converted some files back to java, made sure they work properly with java11 single file execution --- .../ProcessExecutorShutdownHookTest.kt | 21 +++--- ...xit.kt => WriterLoopStarterAfterExit.java} | 47 +++++++------ .../shutdown/WriterLoopStarterBeforeExit.java | 70 +++++++++++++++++++ .../shutdown/WriterLoopStarterBeforeExit.kt | 47 ------------- 4 files changed, 108 insertions(+), 77 deletions(-) rename test/dorkbox/executor/shutdown/{WriterLoopStarterAfterExit.kt => WriterLoopStarterAfterExit.java} (50%) create mode 100644 test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.java delete mode 100644 test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.kt diff --git a/test/dorkbox/executor/shutdown/ProcessExecutorShutdownHookTest.kt b/test/dorkbox/executor/shutdown/ProcessExecutorShutdownHookTest.kt index c3e510c..56ad18b 100644 --- a/test/dorkbox/executor/shutdown/ProcessExecutorShutdownHookTest.kt +++ b/test/dorkbox/executor/shutdown/ProcessExecutorShutdownHookTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2020 dorkbox, llc + * Copyright 2022 dorkbox, llc * Copyright (C) 2014 ZeroTurnaround * Contains fragments of code from Apache Commons Exec, rights owned * by Apache Software Foundation (ASF). @@ -20,8 +20,9 @@ package dorkbox.executor.shutdown import dorkbox.executor.Executor -import org.junit.Assert -import org.junit.Test +import dorkbox.executor.samples.TestSetup +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test import java.io.File /** @@ -43,14 +44,18 @@ class ProcessExecutorShutdownHookTest { @Throws(Exception::class) private fun testDestroyOnExit(loopStarterClassFile: Class<*>, fileIsAlwaysCreated: Boolean) { val file = WriterLoop.getFile() - println(file) + println("Data loop file: $file") + if (file.exists()) file.delete() Executor() .redirectOutputAsInfo() + .redirectErrorAsInfo() + .enableRead() + .defaultLogger() .asJvmProcess() .cloneClasspath() - .setMainClass(loopStarterClassFile.name) + .setMainClass(TestSetup.getFile(loopStarterClassFile)) .startBlocking() // After WriterLoopStarter has finished we expect that WriterLoop is also finished - no-one is updating the file @@ -61,15 +66,15 @@ class ProcessExecutorShutdownHookTest { } companion object { - private const val SLEEP_FOR_RECHECKING_FILE: Long = 4000 + private const val SLEEP_FOR_RECHECKING_FILE: Long = 4000L @Throws(InterruptedException::class) private fun checkFileStaysTheSame(file: File) { - Assert.assertTrue(file.exists()) + Assertions.assertTrue(file.exists()) val length = file.length() Thread.sleep(SLEEP_FOR_RECHECKING_FILE) - Assert.assertEquals("File '$file' was still updated.", length, file.length()) + Assertions.assertEquals(length, file.length(), "File '$file' was still updated.",) } } } diff --git a/test/dorkbox/executor/shutdown/WriterLoopStarterAfterExit.kt b/test/dorkbox/executor/shutdown/WriterLoopStarterAfterExit.java similarity index 50% rename from test/dorkbox/executor/shutdown/WriterLoopStarterAfterExit.kt rename to test/dorkbox/executor/shutdown/WriterLoopStarterAfterExit.java index 6fe9975..bb911cb 100644 --- a/test/dorkbox/executor/shutdown/WriterLoopStarterAfterExit.kt +++ b/test/dorkbox/executor/shutdown/WriterLoopStarterAfterExit.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 dorkbox, llc + * Copyright 2022 dorkbox, llc * Copyright (C) 2014 ZeroTurnaround * Contains fragments of code from Apache Commons Exec, rights owned * by Apache Software Foundation (ASF). @@ -17,37 +17,40 @@ * limitations under the License. */ -package dorkbox.executor.shutdown - -import dorkbox.executor.Executor -import dorkbox.executor.samples.TestSetup -import kotlin.system.exitProcess +package dorkbox.executor.shutdown; +import dorkbox.executor.Executor; +import dorkbox.executor.samples.TestSetup; /** * Starts [WriterLoop] inside shutdown hook and destroys it. */ -class WriterLoopStarterAfterExit : Runnable { - companion object { - private const val SLEEP_AFTER_START: Long = 2000 +public +class WriterLoopStarterAfterExit implements Runnable { + private static Long SLEEP_AFTER_START = 2000L; - @Throws(Exception::class) - @JvmStatic - fun main(args: Array) { - Runtime.getRuntime().addShutdownHook(Thread(WriterLoopStarterAfterExit())) - // Launch the process and also destroy it - exitProcess(0) - } + public static + void main(String[] args) { + System.out.println("Starting output: " + WriterLoop.getFile()); + + Runtime.getRuntime().addShutdownHook(new Thread(new WriterLoopStarterAfterExit())); + + // Launch the process and also destroy it + System.exit(0); } - override fun run() { + @Override + public + void run() { try { - Executor("java", TestSetup.getFile(WriterLoop::class.java)) - .destroyOnExit().startBlocking() - Thread.sleep(SLEEP_AFTER_START) - } catch (e: Exception) { - e.printStackTrace() + new Executor("java", TestSetup.INSTANCE.getFile(WriterLoop.class)) + .destroyOnExit() + .startBlocking(); + + Thread.sleep(SLEEP_AFTER_START); + } catch (Exception e) { + e.printStackTrace(); } } } diff --git a/test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.java b/test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.java new file mode 100644 index 0000000..868261e --- /dev/null +++ b/test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.java @@ -0,0 +1,70 @@ +/* + * Copyright 2022 dorkbox, llc + * Copyright (C) 2014 ZeroTurnaround + * Contains fragments of code from Apache Commons Exec, rights owned + * by Apache Software Foundation (ASF). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dorkbox.executor.shutdown; + +import java.io.File; +import java.io.IOException; + +import dorkbox.executor.Executor; + +/** + * Starts [WriterLoop] and destroys it on JVM exit. + */ +public +class WriterLoopStarterBeforeExit { + public static + void main(String[] args) { + try { + System.out.println("Starting output: " + new File("writeLoop.data").getAbsoluteFile()); + + // silly workarounds, because executing java files from CLI, require SINGLE-FILE access! + String path = getFile(WriterLoopStarterBeforeExit.class) + .replace(WriterLoopStarterBeforeExit.class.getSimpleName(), "WriterLoop"); + + System.out.println("Starting output: " + path); + + new Executor() + .redirectOutputAsInfo() + .redirectErrorAsInfo() + .asJvmProcess() + .cloneClasspath() + .setMainClass(path) + .startBlocking(); + } catch (Exception e) { + e.printStackTrace(); + } + + // Launch the process and also destroy it + System.exit(0); + } + + private static String getFile(Class javaClass) throws IOException { + String properName; + + if (Executor.Companion.getIS_OS_WINDOWS()) { + properName = javaClass.getName().replace('.', '\\'); + } else { + properName = javaClass.getName().replace('.', '/'); + } + + File file = new File("test", properName + ".java").getAbsoluteFile().getCanonicalFile(); + return file.getPath(); + } +} diff --git a/test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.kt b/test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.kt deleted file mode 100644 index df5abf4..0000000 --- a/test/dorkbox/executor/shutdown/WriterLoopStarterBeforeExit.kt +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2020 dorkbox, llc - * Copyright (C) 2014 ZeroTurnaround - * Contains fragments of code from Apache Commons Exec, rights owned - * by Apache Software Foundation (ASF). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package dorkbox.executor.shutdown - -import dorkbox.executor.Executor -import dorkbox.executor.samples.TestSetup -import kotlinx.coroutines.delay -import kotlinx.coroutines.runBlocking -import kotlin.system.exitProcess - -/** - * Starts [WriterLoop] and destroys it on JVM exit. - */ -object WriterLoopStarterBeforeExit { - private const val SLEEP_AFTER_START: Long = 2000 - - @Throws(Exception::class) - @JvmStatic - fun main(args: Array) { - Executor("java", TestSetup.getFile(WriterLoop::class.java)) - .destroyOnExit().redirectOutputAsInfo().startAsync() - - runBlocking { - delay(SLEEP_AFTER_START) - } - - // Cause the launched process to be destroyed - exitProcess(0) - } -}