2020-08-06 11:07:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2020 dorkbox, llc
|
|
|
|
* Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
|
|
|
|
|
|
|
|
* 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
|
|
|
|
2021-01-03 20:25:26 +01:00
|
|
|
import dorkbox.executor.SuccessTest.ProcessListenerImpl
|
2020-08-06 11:07:25 +02:00
|
|
|
import dorkbox.executor.exceptions.InvalidOutputException
|
|
|
|
import dorkbox.executor.processResults.ProcessResult
|
|
|
|
import dorkbox.executor.processResults.SyncProcessResult
|
|
|
|
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.util.concurrent.TimeUnit
|
|
|
|
|
|
|
|
class ProcessListenerThrowTest {
|
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
@Test
|
2020-08-06 11:07:25 +02:00
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testJavaVersion() {
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertThrows(InvalidOutputException::class.java) {
|
|
|
|
runBlocking {
|
|
|
|
Executor("java", "-version")
|
|
|
|
.enableRead()
|
|
|
|
.addListener(ProcessListenerThrowImpl())
|
|
|
|
.start()
|
|
|
|
}
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
@Test
|
2020-08-06 11:07:25 +02:00
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testJavaVersionWithTimeout() {
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertThrows(InvalidOutputException::class.java) {
|
|
|
|
runBlocking {
|
|
|
|
Executor("java", "-version")
|
|
|
|
.enableRead()
|
|
|
|
.addListener(ProcessListenerThrowImpl())
|
|
|
|
.start(1, TimeUnit.MINUTES)
|
|
|
|
}
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ProcessListenerThrowImpl : ProcessListenerImpl() {
|
|
|
|
override fun afterFinish(process: Process, result: ProcessResult) {
|
|
|
|
super.afterFinish(process, result)
|
|
|
|
|
|
|
|
val string = (result as SyncProcessResult).output.string()
|
|
|
|
if (string.contains("java version") || string.contains("openjdk version")) {
|
|
|
|
throw InvalidOutputException("Test", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|