|
1 |
| -package org.utbot.framework.plugin.api |
2 |
| - |
3 |
| -import org.utbot.framework.plugin.api.visible.UtStreamConsumingException |
4 |
| -import java.io.File |
5 |
| -import java.util.LinkedList |
6 |
| - |
7 |
| -sealed class UtExecutionResult |
8 |
| - |
9 |
| -data class UtExecutionSuccess(val model: UtModel) : UtExecutionResult() { |
10 |
| - override fun toString() = "$model" |
11 |
| -} |
12 |
| - |
13 |
| -sealed class UtExecutionFailure : UtExecutionResult() { |
14 |
| - abstract val exception: Throwable |
15 |
| - |
16 |
| - /** |
17 |
| - * Represents the most inner exception in the failure. |
18 |
| - * Often equals to [exception], but is wrapped exception in [UtStreamConsumingException]. |
19 |
| - */ |
20 |
| - open val rootCauseException: Throwable |
21 |
| - get() = exception |
22 |
| -} |
23 |
| - |
24 |
| -data class UtOverflowFailure( |
25 |
| - override val exception: Throwable, |
26 |
| -) : UtExecutionFailure() |
27 |
| - |
28 |
| -data class UtSandboxFailure( |
29 |
| - override val exception: Throwable |
30 |
| -) : UtExecutionFailure() |
31 |
| - |
32 |
| -data class UtStreamConsumingFailure( |
33 |
| - override val exception: UtStreamConsumingException, |
34 |
| -) : UtExecutionFailure() { |
35 |
| - override val rootCauseException: Throwable |
36 |
| - get() = exception.innerExceptionOrAny |
37 |
| -} |
38 |
| - |
39 |
| -/** |
40 |
| - * unexpectedFail (when exceptions such as NPE, IOBE, etc. appear, but not thrown by a user, applies both for function under test and nested calls ) |
41 |
| - * expectedCheckedThrow (when function under test or nested call explicitly says that checked exception could be thrown and throws it) |
42 |
| - * expectedUncheckedThrow (when there is a throw statement for unchecked exception inside of function under test) |
43 |
| - * unexpectedUncheckedThrow (in case when there is unchecked exception thrown from nested call) |
44 |
| - */ |
45 |
| -data class UtExplicitlyThrownException( |
46 |
| - override val exception: Throwable, |
47 |
| - val fromNestedMethod: Boolean |
48 |
| -) : UtExecutionFailure() |
49 |
| - |
50 |
| -data class UtImplicitlyThrownException( |
51 |
| - override val exception: Throwable, |
52 |
| - val fromNestedMethod: Boolean |
53 |
| -) : UtExecutionFailure() |
54 |
| - |
55 |
| -class TimeoutException(s: String) : Exception(s) |
56 |
| - |
57 |
| -data class UtTimeoutException(override val exception: TimeoutException) : UtExecutionFailure() |
58 |
| - |
59 |
| -/** |
60 |
| - * Indicates failure in concrete execution. |
61 |
| - * For now it is explicitly throwing by ConcreteExecutor in case instrumented process death. |
62 |
| - */ |
63 |
| -class ConcreteExecutionFailureException(cause: Throwable, errorFile: File, val processStdout: List<String>) : |
64 |
| - Exception( |
65 |
| - buildString { |
66 |
| - appendLine() |
67 |
| - appendLine("----------------------------------------") |
68 |
| - appendLine("The instrumented process is dead") |
69 |
| - appendLine("Cause:\n${cause.message}") |
70 |
| - appendLine("Last 1000 lines of the error log ${errorFile.absolutePath}:") |
71 |
| - appendLine("----------------------------------------") |
72 |
| - errorFile.useLines { lines -> |
73 |
| - val lastLines = LinkedList<String>() |
74 |
| - for (line in lines) { |
75 |
| - lastLines.add(line) |
76 |
| - if (lastLines.size > 1000) { |
77 |
| - lastLines.removeFirst() |
78 |
| - } |
79 |
| - } |
80 |
| - lastLines.forEach { appendLine(it) } |
81 |
| - } |
82 |
| - appendLine("----------------------------------------") |
83 |
| - }, |
84 |
| - cause |
85 |
| - ) |
86 |
| - |
87 |
| -data class UtConcreteExecutionFailure(override val exception: ConcreteExecutionFailureException) : UtExecutionFailure() |
88 |
| - |
89 |
| -val UtExecutionResult.isSuccess: Boolean |
90 |
| - get() = this is UtExecutionSuccess |
91 |
| - |
92 |
| -val UtExecutionResult.isFailure: Boolean |
93 |
| - get() = this is UtExecutionFailure |
94 |
| - |
95 |
| -inline fun UtExecutionResult.onSuccess(action: (model: UtModel) -> Unit): UtExecutionResult { |
96 |
| - if (this is UtExecutionSuccess) action(model) |
97 |
| - return this |
98 |
| -} |
99 |
| - |
100 |
| -inline fun UtExecutionResult.onFailure(action: (exception: Throwable) -> Unit): UtExecutionResult { |
101 |
| - if (this is UtExecutionFailure) action(rootCauseException) |
102 |
| - return this |
103 |
| -} |
104 |
| - |
105 |
| -fun UtExecutionResult.exceptionOrNull(): Throwable? = when (this) { |
106 |
| - is UtExecutionFailure -> rootCauseException |
107 |
| - is UtExecutionSuccess -> null |
108 |
| -} |
| 1 | +package org.utbot.framework.plugin.api |
| 2 | + |
| 3 | +import org.utbot.framework.plugin.api.visible.UtStreamConsumingException |
| 4 | +import java.io.File |
| 5 | +import java.util.LinkedList |
| 6 | + |
| 7 | +sealed class UtExecutionResult |
| 8 | + |
| 9 | +data class UtExecutionSuccess(val model: UtModel) : UtExecutionResult() { |
| 10 | + override fun toString() = "$model" |
| 11 | +} |
| 12 | + |
| 13 | +sealed class UtExecutionFailure : UtExecutionResult() { |
| 14 | + abstract val exception: Throwable |
| 15 | + |
| 16 | + /** |
| 17 | + * Represents the most inner exception in the failure. |
| 18 | + * Often equals to [exception], but is wrapped exception in [UtStreamConsumingException]. |
| 19 | + */ |
| 20 | + open val rootCauseException: Throwable |
| 21 | + get() = exception |
| 22 | +} |
| 23 | + |
| 24 | +data class UtOverflowFailure( |
| 25 | + override val exception: Throwable, |
| 26 | +) : UtExecutionFailure() |
| 27 | + |
| 28 | +data class UtSandboxFailure( |
| 29 | + override val exception: Throwable |
| 30 | +) : UtExecutionFailure() |
| 31 | + |
| 32 | +data class UtStreamConsumingFailure( |
| 33 | + override val exception: UtStreamConsumingException, |
| 34 | +) : UtExecutionFailure() { |
| 35 | + override val rootCauseException: Throwable |
| 36 | + get() = exception.innerExceptionOrAny |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * unexpectedFail (when exceptions such as NPE, IOBE, etc. appear, but not thrown by a user, applies both for function under test and nested calls ) |
| 41 | + * expectedCheckedThrow (when function under test or nested call explicitly says that checked exception could be thrown and throws it) |
| 42 | + * expectedUncheckedThrow (when there is a throw statement for unchecked exception inside of function under test) |
| 43 | + * unexpectedUncheckedThrow (in case when there is unchecked exception thrown from nested call) |
| 44 | + */ |
| 45 | +data class UtExplicitlyThrownException( |
| 46 | + override val exception: Throwable, |
| 47 | + val fromNestedMethod: Boolean |
| 48 | +) : UtExecutionFailure() |
| 49 | + |
| 50 | +data class UtImplicitlyThrownException( |
| 51 | + override val exception: Throwable, |
| 52 | + val fromNestedMethod: Boolean |
| 53 | +) : UtExecutionFailure() |
| 54 | + |
| 55 | +class TimeoutException(s: String) : Exception(s) |
| 56 | + |
| 57 | +data class UtTimeoutException(override val exception: TimeoutException) : UtExecutionFailure() |
| 58 | + |
| 59 | +/** |
| 60 | + * Indicates failure in concrete execution. |
| 61 | + * For now it is explicitly throwing by ConcreteExecutor in case instrumented process death. |
| 62 | + */ |
| 63 | +class InstrumentedProcessDeathException(cause: Throwable, errorFile: File, val processStdout: List<String>) : |
| 64 | + Exception( |
| 65 | + buildString { |
| 66 | + appendLine() |
| 67 | + appendLine("----------------------------------------") |
| 68 | + appendLine("The instrumented process is dead") |
| 69 | + appendLine("Cause:\n${cause.message}") |
| 70 | + appendLine("Last 1000 lines of the error log ${errorFile.absolutePath}:") |
| 71 | + appendLine("----------------------------------------") |
| 72 | + errorFile.useLines { lines -> |
| 73 | + val lastLines = LinkedList<String>() |
| 74 | + for (line in lines) { |
| 75 | + lastLines.add(line) |
| 76 | + if (lastLines.size > 1000) { |
| 77 | + lastLines.removeFirst() |
| 78 | + } |
| 79 | + } |
| 80 | + lastLines.forEach { appendLine(it) } |
| 81 | + } |
| 82 | + appendLine("----------------------------------------") |
| 83 | + }, |
| 84 | + cause |
| 85 | + ) |
| 86 | + |
| 87 | +data class UtConcreteExecutionFailure(override val exception: InstrumentedProcessDeathException) : UtExecutionFailure() |
| 88 | + |
| 89 | +val UtExecutionResult.isSuccess: Boolean |
| 90 | + get() = this is UtExecutionSuccess |
| 91 | + |
| 92 | +val UtExecutionResult.isFailure: Boolean |
| 93 | + get() = this is UtExecutionFailure |
| 94 | + |
| 95 | +inline fun UtExecutionResult.onSuccess(action: (model: UtModel) -> Unit): UtExecutionResult { |
| 96 | + if (this is UtExecutionSuccess) action(model) |
| 97 | + return this |
| 98 | +} |
| 99 | + |
| 100 | +inline fun UtExecutionResult.onFailure(action: (exception: Throwable) -> Unit): UtExecutionResult { |
| 101 | + if (this is UtExecutionFailure) action(rootCauseException) |
| 102 | + return this |
| 103 | +} |
| 104 | + |
| 105 | +fun UtExecutionResult.exceptionOrNull(): Throwable? = when (this) { |
| 106 | + is UtExecutionFailure -> rootCauseException |
| 107 | + is UtExecutionSuccess -> null |
| 108 | +} |
0 commit comments