Skip to content

Commit 231d9e0

Browse files
authored
Refactoring in Summary Module #2322 #2367 (#2425)
1 parent 4833af7 commit 231d9e0

File tree

27 files changed

+357
-206
lines changed

27 files changed

+357
-206
lines changed

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class UtBotSymbolicEngine(
393393
applicationContext.getBeansAssignableTo(methodUnderTest.classId).isEmpty()) {
394394
val fullConfigDisplayName = (applicationContext.springSettings as? SpringSettings.PresentSpringSettings)
395395
?.configuration?.fullDisplayName
396-
val errorDescription = "No beans of type ${methodUnderTest.classId.name} are found. " +
396+
val errorDescription = "No beans of type ${methodUnderTest.classId.name} were found. " +
397397
"Try choosing different Spring configuration or adding beans to $fullConfigDisplayName"
398398
emit(UtError(
399399
errorDescription,

utbot-sample/src/main/java/org/utbot/examples/controlflow/Conditions.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public void emptyBranches(boolean condition) {
3131
// do nothing
3232
}
3333
}
34+
35+
public int elseIf(int id) throws RuntimeException {
36+
if (id > 0) return 0;
37+
else if (id == 0) throw new RuntimeException("Exception message");
38+
else return 1;
39+
}
3440
}

utbot-sample/src/main/java/org/utbot/examples/controlflow/Switch.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ public int charToIntSwitch(char c) {
5959
}
6060
}
6161

62+
public int throwExceptionInSwitchArgument() {
63+
switch (getChar()) {
64+
case 'I':
65+
return 1;
66+
default:
67+
return 100;
68+
}
69+
}
70+
71+
private char getChar() throws RuntimeException {
72+
throw new RuntimeException("Exception message");
73+
}
74+
6275
//TODO: String switch
6376
// public int stringSwitch(String s) {
6477
// switch (s) {
@@ -72,4 +85,3 @@ public int charToIntSwitch(char c) {
7285
// }
7386
// }
7487
}
75-

utbot-sample/src/main/java/org/utbot/examples/exceptions/ExceptionExamples.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,8 @@ private int nestedWithThrow(int i) {
123123
}
124124
return i;
125125
}
126-
}
126+
127+
public int throwExceptionInMethodUnderTest() throws RuntimeException {
128+
throw new RuntimeException("Exception message");
129+
}
130+
}

utbot-summary-tests/src/test/kotlin/examples/algorithms/SummaryBinarySearchTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ class SummaryBinarySearchTest : SummaryTestCaseGeneratorTest(
3737
"returns from: return right + 1;\n"
3838
val summary5 = "Test invokes:\n" +
3939
" org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[]) once\n" +
40-
"throws NullPointerException in: isUnsorted(array)\n"
40+
"throws NullPointerException when: isUnsorted(array)\n"
4141
val summary6 = "Test invokes:\n" +
4242
" org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[]) once\n" +
4343
"executes conditions:\n" +
4444
" (isUnsorted(array)): True\n" +
45-
"throws IllegalArgumentException after condition: isUnsorted(array)\n"
45+
"throws IllegalArgumentException when: isUnsorted(array)\n"
4646

4747
val methodName1 = "testLeftBinSearch_NotFound"
4848
val methodName2 = "testLeftBinSearch_MiddleOfArrayLessThanKey"
4949
val methodName3 = "testLeftBinSearch_Found"
5050
val methodName4 = "testLeftBinSearch_Found_1"
51-
val methodName5 = "testLeftBinSearch_BinarySearchIsUnsorted"
52-
val methodName6 = "testLeftBinSearch_IsUnsorted"
51+
val methodName5 = "testLeftBinSearch_ThrowNullPointerException"
52+
val methodName6 = "testLeftBinSearch_ThrowIllegalArgumentException"
5353

5454
val displayName1 = "found : False -> return -1"
5555
val displayName2 = "array[middle] == key : False -> return -1"

utbot-summary-tests/src/test/kotlin/examples/algorithms/SummarySortTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ class SummarySortTest : SummaryTestCaseGeneratorTest(
1212
@Test
1313
fun testDefaultSort() {
1414
val summary1 = "Test \n" +
15-
"throws NullPointerException in: array.length < 4\n"
15+
"throws NullPointerException when: array.length < 4\n"
1616
val summary2 = "Test executes conditions:\n" +
1717
" (array.length < 4): True\n" +
18-
"throws IllegalArgumentException after condition: array.length < 4\n"
18+
"throws IllegalArgumentException when: array.length < 4\n"
1919
val summary3 = "Test executes conditions:\n" +
2020
" (array.length < 4): False\n" +
2121
"invokes:\n" +
2222
" {@link java.util.Arrays#sort(int[])} once\n" +
2323
"returns from: return array;\n"
2424

2525
val methodName1 = "testDefaultSort_ThrowNullPointerException"
26-
val methodName2 = "testDefaultSort_ArrayLengthLessThan4"
26+
val methodName2 = "testDefaultSort_ThrowIllegalArgumentException"
2727
val methodName3 = "testDefaultSort_ArrayLengthGreaterOrEqual4"
2828

2929
val displayName1 = "array.length < 4 -> ThrowNullPointerException"

utbot-summary-tests/src/test/kotlin/examples/collections/SummaryListWrapperReturnsVoidTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SummaryListWrapperReturnsVoidTest : SummaryTestCaseGeneratorTest(
2626
val summary3 = "Test returns from: return i[0];"
2727
val summary4 = "Test returns from: return i[0];"
2828

29-
val methodName1 = "testRunForEach_ListForEach"
29+
val methodName1 = "testRunForEach_ThrowNullPointerException"
3030
val methodName2 = "testRunForEach_Return0OfI"
3131
val methodName3 = "testRunForEach_Return0OfI_1"
3232
val methodName4 = "testRunForEach_Return0OfI_2"
@@ -88,7 +88,7 @@ class SummaryListWrapperReturnsVoidTest : SummaryTestCaseGeneratorTest(
8888
"returns from: return sum[0];"
8989

9090
val methodName1 = "testSumPositiveForEach_ThrowNullPointerException"
91-
val methodName2 = "testSumPositiveForEach_ListForEach"
91+
val methodName2 = "testSumPositiveForEach_ThrowNullPointerException_1"
9292
val methodName3 = "testSumPositiveForEach_0OfSumEqualsZero"
9393
val methodName4 = "testSumPositiveForEach_0OfSumEqualsZero_1"
9494
val methodName5 = "testSumPositiveForEach_0OfSumNotEqualsZero"

utbot-summary-tests/src/test/kotlin/examples/controlflow/SummaryConditionsTest.kt

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,56 @@ class SummaryConditionsTest : SummaryTestCaseGeneratorTest(
9696

9797
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
9898
}
99-
}
99+
100+
@Test
101+
fun testElseIf() {
102+
val summary1 = "@utbot.classUnderTest {@link Conditions}\n" +
103+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#elseIf(int)}\n" +
104+
"@utbot.executesCondition {@code (id > 0): True}\n" +
105+
"@utbot.returnsFrom {@code return 0;}"
106+
107+
val summary2 = "@utbot.classUnderTest {@link Conditions}\n" +
108+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#elseIf(int)}\n" +
109+
"@utbot.executesCondition {@code (id > 0): False}\n" +
110+
"@utbot.executesCondition {@code (id == 0): False}\n" +
111+
"@utbot.returnsFrom {@code return 1;}"
112+
113+
val summary3 = "@utbot.classUnderTest {@link Conditions}\n" +
114+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#elseIf(int)}\n" +
115+
"@utbot.executesCondition {@code (id > 0): False}\n" +
116+
"@utbot.executesCondition {@code (id == 0): True}\n" +
117+
"@utbot.throwsException {@link java.lang.RuntimeException} when: id == 0"
118+
119+
val methodName1 = "testElseIf_IdGreaterThanZero"
120+
val methodName2 = "testElseIf_IdNotEqualsZero"
121+
val methodName3 = "testElseIf_ThrowRuntimeException"
122+
123+
val displayName1 = "id > 0 : True -> id > 0"
124+
val displayName2 = "id > 0 : False -> return 1"
125+
val displayName3 = "id == 0 -> ThrowRuntimeException"
126+
127+
val summaryKeys = listOf(
128+
summary1,
129+
summary2,
130+
summary3
131+
)
132+
133+
val displayNames = listOf(
134+
displayName1,
135+
displayName2,
136+
displayName3
137+
)
138+
139+
val methodNames = listOf(
140+
methodName1,
141+
methodName2,
142+
methodName3
143+
)
144+
145+
val method = Conditions::elseIf
146+
val mockStrategy = MockStrategyApi.NO_MOCKS
147+
val coverage = DoNotCalculate
148+
149+
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
150+
}
151+
}

utbot-summary-tests/src/test/kotlin/examples/controlflow/SummarySwitchTest.kt

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import examples.SummaryTestCaseGeneratorTest
55
import org.junit.jupiter.api.Test
66
import org.junit.jupiter.api.extension.ExtendWith
77
import org.utbot.examples.controlflow.Switch
8+
import org.utbot.examples.exceptions.ExceptionExamples
89
import org.utbot.framework.plugin.api.MockStrategyApi
910
import org.utbot.testing.DoNotCalculate
1011

@@ -16,19 +17,19 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
1617
fun testSimpleSwitch() {
1718
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
1819
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
19-
"@utbot.activatesSwitch {@code case 10}\n" +
20+
"@utbot.activatesSwitch {@code switch(x) case: 10}\n" +
2021
"@utbot.returnsFrom {@code return 10;}"
2122
val summary2 = "@utbot.classUnderTest {@link Switch}\n" +
2223
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
23-
"@utbot.activatesSwitch {@code case default}\n" +
24+
"@utbot.activatesSwitch {@code switch(x) case: default}\n" +
2425
"@utbot.returnsFrom {@code return -1;}"
2526
val summary3 = "@utbot.classUnderTest {@link Switch}\n" +
2627
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
27-
"@utbot.activatesSwitch {@code case 12}\n" +
28+
"@utbot.activatesSwitch {@code switch(x) case: 12}\n" +
2829
"@utbot.returnsFrom {@code return 12;}"
2930
val summary4 = "@utbot.classUnderTest {@link Switch}\n" +
3031
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
31-
"@utbot.activatesSwitch {@code case 13}\n" +
32+
"@utbot.activatesSwitch {@code switch(x) case: 13}\n" +
3233
"@utbot.returnsFrom {@code return 13;}"
3334

3435
val methodName1 = "testSimpleSwitch_Return10"
@@ -37,7 +38,7 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
3738
val methodName4 = "testSimpleSwitch_Return13"
3839

3940
val displayName1 = "switch(x) case: 10 -> return 10"
40-
val displayName2 = "switch(x) case: Default -> return -1"
41+
val displayName2 = "switch(x) case: default -> return -1"
4142
val displayName3 = "switch(x) case: 12 -> return 12"
4243
val displayName4 = "switch(x) case: 13 -> return 13"
4344

@@ -73,40 +74,39 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
7374
fun testCharToIntSwitch() {
7475
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
7576
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
76-
"@utbot.activatesSwitch {@code case 'C'}\n" +
77+
"@utbot.activatesSwitch {@code switch(c) case: 'C'}\n" +
7778
"@utbot.returnsFrom {@code return 100;}\n"
7879
val summary2 = "@utbot.classUnderTest {@link Switch}\n" +
7980
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
80-
"@utbot.activatesSwitch {@code case 'V'}\n" +
81+
"@utbot.activatesSwitch {@code switch(c) case: 'V'}\n" +
8182
"@utbot.returnsFrom {@code return 5;}\n"
8283
val summary3 = "@utbot.classUnderTest {@link Switch}\n" +
8384
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
84-
"@utbot.activatesSwitch {@code case 'I'}\n" +
85+
"@utbot.activatesSwitch {@code switch(c) case: 'I'}\n" +
8586
"@utbot.returnsFrom {@code return 1;}\n"
8687
val summary4 = "@utbot.classUnderTest {@link Switch}\n" +
8788
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
88-
"@utbot.activatesSwitch {@code case 'X'}\n" +
89+
"@utbot.activatesSwitch {@code switch(c) case: 'X'}\n" +
8990
"@utbot.returnsFrom {@code return 10;}\n"
9091
val summary5 = "@utbot.classUnderTest {@link Switch}\n" +
9192
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
92-
"@utbot.activatesSwitch {@code case 'M'}\n" +
93+
"@utbot.activatesSwitch {@code switch(c) case: 'M'}\n" +
9394
"@utbot.returnsFrom {@code return 1000;}\n"
9495
val summary6 = "@utbot.classUnderTest {@link Switch}\n" +
9596
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
96-
"@utbot.activatesSwitch {@code case 'D'}\n" +
97+
"@utbot.activatesSwitch {@code switch(c) case: 'D'}\n" +
9798
"@utbot.returnsFrom {@code return 500;}\n"
9899
val summary7 = "@utbot.classUnderTest {@link Switch}\n" +
99100
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
100-
"@utbot.activatesSwitch {@code case 'L'}\n" +
101+
"@utbot.activatesSwitch {@code switch(c) case: 'L'}\n" +
101102
"@utbot.returnsFrom {@code return 50;}\n"
102103
val summary8 = "@utbot.classUnderTest {@link Switch}\n" +
103104
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
104105
"@utbot.invokes {@link java.lang.StringBuilder#append(java.lang.String)}\n" +
105106
"@utbot.invokes {@link java.lang.StringBuilder#append(char)}\n" +
106107
"@utbot.invokes {@link java.lang.StringBuilder#toString()}\n" +
107-
"@utbot.activatesSwitch {@code case default}\n" +
108-
"@utbot.throwsException {@link java.lang.IllegalArgumentException} in: default:\n" +
109-
" throw new IllegalArgumentException(\"Unrecognized symbol: \" + c);\n"
108+
"@utbot.activatesSwitch {@code switch(c) case: default}\n" +
109+
"@utbot.throwsException {@link java.lang.IllegalArgumentException} when: switch(c) case: default\n"
110110

111111
val methodName1 = "testCharToIntSwitch_Return100"
112112
val methodName2 = "testCharToIntSwitch_Return5"
@@ -115,7 +115,7 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
115115
val methodName5 = "testCharToIntSwitch_Return1000"
116116
val methodName6 = "testCharToIntSwitch_Return500"
117117
val methodName7 = "testCharToIntSwitch_Return50"
118-
val methodName8 = "testCharToIntSwitch_StringBuilderToString"
118+
val methodName8 = "testCharToIntSwitch_ThrowIllegalArgumentException"
119119

120120
val displayName1 = "switch(c) case: 'C' -> return 100"
121121
val displayName2 = "switch(c) case: 'V' -> return 5"
@@ -124,7 +124,7 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
124124
val displayName5 = "switch(c) case: 'M' -> return 1000"
125125
val displayName6 = "switch(c) case: 'D' -> return 500"
126126
val displayName7 = "switch(c) case: 'L' -> return 50"
127-
val displayName8 = """default: throw new IllegalArgumentException("Unrecognized symbol: " + c) -> ThrowIllegalArgumentException"""
127+
val displayName8 = "switch(c) case: default -> ThrowIllegalArgumentException"
128128

129129
val summaryKeys = listOf(
130130
summary1,
@@ -165,4 +165,34 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
165165

166166
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
167167
}
168-
}
168+
169+
@Test
170+
fun testThrowExceptionInSwitchArgument() {
171+
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
172+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#throwExceptionInSwitchArgument()}\n" +
173+
"@utbot.invokes org.utbot.examples.controlflow.Switch#getChar()\n" +
174+
"@utbot.throwsException {@link java.lang.RuntimeException} in: switch(getChar())\n"
175+
176+
val methodName1 = "testThrowExceptionInSwitchArgument_ThrowRuntimeException"
177+
178+
val displayName1 = "switch(getChar()) -> ThrowRuntimeException"
179+
180+
val summaryKeys = listOf(
181+
summary1,
182+
)
183+
184+
val displayNames = listOf(
185+
displayName1,
186+
)
187+
188+
val methodNames = listOf(
189+
methodName1,
190+
)
191+
192+
val method = Switch::throwExceptionInSwitchArgument
193+
val mockStrategy = MockStrategyApi.NO_MOCKS
194+
val coverage = DoNotCalculate
195+
196+
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
197+
}
198+
}

utbot-summary-tests/src/test/kotlin/examples/exceptions/SummaryExceptionClusteringExamplesTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ class SummaryExceptionClusteringExamplesTest : SummaryTestCaseGeneratorTest(
2323
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
2424
"@utbot.executesCondition {@code (i == 0): False}\n" +
2525
"@utbot.executesCondition {@code (i == 1): True}\n" +
26-
"@utbot.throwsException {@link org.utbot.examples.exceptions.MyCheckedException} after condition: i == 1"
26+
"@utbot.throwsException {@link org.utbot.examples.exceptions.MyCheckedException} when: i == 1"
2727
val summary3 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
2828
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
2929
"@utbot.executesCondition {@code (i == 0): False}\n" +
3030
"@utbot.executesCondition {@code (i == 1): False}\n" +
3131
"@utbot.executesCondition {@code (i == 2): True}\n" +
32-
"@utbot.throwsException {@link java.lang.IllegalArgumentException} after condition: i == 2"
32+
"@utbot.throwsException {@link java.lang.IllegalArgumentException} when: i == 2"
3333
val summary4 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
3434
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
3535
"@utbot.executesCondition {@code (i == 0): False}\n" +
3636
"@utbot.executesCondition {@code (i == 1): False}\n" +
3737
"@utbot.executesCondition {@code (i == 2): False}\n" +
3838
"@utbot.returnsFrom {@code return i * 2;}\n"
3939

40-
val methodName1 = "testDifferentExceptions_IEqualsZero"
41-
val methodName2 = "testDifferentExceptions_IEquals1"
42-
val methodName3 = "testDifferentExceptions_IEquals2"
40+
val methodName1 = "testDifferentExceptions_ThrowArithmeticException"
41+
val methodName2 = "testDifferentExceptions_ThrowMyCheckedException"
42+
val methodName3 = "testDifferentExceptions_ThrowIllegalArgumentException"
4343
val methodName4 = "testDifferentExceptions_INotEquals2"
4444

4545
val displayName1 = "return 100 / i : True -> ThrowArithmeticException"

utbot-summary-tests/src/test/kotlin/examples/exceptions/SummaryExceptionExampleTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,33 @@ class SummaryExceptionExampleTest : SummaryTestCaseGeneratorTest(
9797

9898
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
9999
}
100+
101+
@Test
102+
fun testThrowExceptionInMethodUnderTest() {
103+
val summary1 = "@utbot.classUnderTest {@link ExceptionExamples}\n" +
104+
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionExamples#throwExceptionInMethodUnderTest()}\n" +
105+
"@utbot.throwsException {@link java.lang.RuntimeException} in: throw new RuntimeException(\"Exception message\");\n"
106+
107+
val methodName1 = "testThrowExceptionInMethodUnderTest_ThrowRuntimeException"
108+
109+
val displayName1 = " -> ThrowRuntimeException"
110+
111+
val summaryKeys = listOf(
112+
summary1,
113+
)
114+
115+
val displayNames = listOf(
116+
displayName1,
117+
)
118+
119+
val methodNames = listOf(
120+
methodName1,
121+
)
122+
123+
val method = ExceptionExamples::throwExceptionInMethodUnderTest
124+
val mockStrategy = MockStrategyApi.NO_MOCKS
125+
val coverage = DoNotCalculate
126+
127+
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
128+
}
100129
}

0 commit comments

Comments
 (0)