2020-08-06 11:07:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-08-07 23:22:31 +02:00
|
|
|
package dorkbox.executor
|
2020-08-06 11:07:25 +02:00
|
|
|
|
2020-08-07 23:22:31 +02:00
|
|
|
import dorkbox.executor.samples.PrintArguments
|
|
|
|
import dorkbox.executor.samples.TestSetup
|
2020-08-06 11:07:25 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2022-01-20 00:04:59 +01:00
|
|
|
import org.junit.jupiter.api.Assertions
|
|
|
|
import org.junit.jupiter.api.Test
|
2020-08-06 11:07:25 +02:00
|
|
|
import java.io.IOException
|
|
|
|
import java.util.*
|
2021-01-03 20:25:26 +01:00
|
|
|
import java.util.concurrent.*
|
2020-08-06 11:07:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests passing command line arguments to a Java process.
|
|
|
|
*/
|
2021-01-03 20:25:26 +01:00
|
|
|
class CommandLineTest {
|
2020-08-06 11:07:25 +02:00
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testOneArg() {
|
|
|
|
testArguments("foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testTwoArgs() {
|
|
|
|
testArguments("foo", "bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testSpaces() {
|
|
|
|
testArguments("foo foo", "bar bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testQuotes() {
|
|
|
|
val args = arrayOf("\"a\"", "b \"c\" d", "f \"e\"", "\"g\" h")
|
|
|
|
var expected = listOf("\"a\"", "b \"c\" d", "f \"e\"", "\"g\" h")
|
|
|
|
if (Executor.IS_OS_WINDOWS) expected = listOf("a", "b c d", "f e", "g h")
|
|
|
|
testArguments(expected, *args)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testSlashes() {
|
|
|
|
testArguments("/o\\", "\\/.*")
|
|
|
|
}
|
|
|
|
|
|
|
|
@Throws(IOException::class, InterruptedException::class, TimeoutException::class)
|
|
|
|
private fun testArguments(vararg args: String) {
|
|
|
|
val actual = runBlocking {
|
2022-01-20 00:04:59 +01:00
|
|
|
printArguments(*args)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val expected = listOf(*args)
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertEquals(expected, actual)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Throws(IOException::class, InterruptedException::class, TimeoutException::class)
|
|
|
|
private fun testArguments(expected: List<String>, vararg args: String) {
|
|
|
|
val actual = runBlocking {
|
2022-01-20 00:04:59 +01:00
|
|
|
printArguments(*args)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertEquals(expected, actual)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
private suspend fun printArguments(vararg args: String): List<String> {
|
2020-08-06 11:07:25 +02:00
|
|
|
val command: MutableList<String> = ArrayList()
|
|
|
|
command.addAll(listOf("java", TestSetup.getFile(PrintArguments::class.java)))
|
|
|
|
command.addAll(listOf(*args))
|
2022-01-20 00:04:59 +01:00
|
|
|
|
|
|
|
// explicitly making this a direct invocation.
|
2020-08-06 11:07:25 +02:00
|
|
|
return Executor(command)
|
2020-08-10 23:35:09 +02:00
|
|
|
.enableRead()
|
2022-01-20 00:04:59 +01:00
|
|
|
.start()
|
|
|
|
.output
|
|
|
|
.linesAsUtf8()
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
}
|