1
+ package org.utbot.examples.stdlib
2
+
3
+ import org.junit.jupiter.api.Tag
4
+ import org.junit.jupiter.api.Test
5
+ import org.utbot.examples.AbstractTestCaseGeneratorTest
6
+ import org.utbot.examples.eq
7
+ import org.utbot.examples.isException
8
+ import org.utbot.examples.withUsingReflectionForMaximizingCoverage
9
+ import java.util.Date
10
+
11
+ class DateExampleTest : AbstractTestCaseGeneratorTest (testClass = DateExample : :class) {
12
+ @Suppress(" SpellCheckingInspection" )
13
+ @Tag(" slow" )
14
+ @Test
15
+ fun testGetTimeWithNpeChecksForNonPublicFields () {
16
+ withUsingReflectionForMaximizingCoverage(maximizeCoverage = true ) {
17
+ checkWithException(
18
+ DateExample ::getTime,
19
+ eq(5 ),
20
+ * commonMatchers,
21
+ { date: Date ? , r: Result <Boolean > ->
22
+ val cdate = date!! .getDeclaredFieldValue(" cdate" )
23
+ val calendarDate = cdate!! .getDeclaredFieldValue(" date" )
24
+
25
+ calendarDate == null && r.isException<NullPointerException >()
26
+ },
27
+ { date: Date ? , r: Result <Boolean > ->
28
+ val cdate = date!! .getDeclaredFieldValue(" cdate" )
29
+ val calendarDate = cdate!! .getDeclaredFieldValue(" date" )
30
+
31
+ val gcal = date.getDeclaredFieldValue(" gcal" )
32
+
33
+ val normalized = calendarDate!! .getDeclaredFieldValue(" normalized" ) as Boolean
34
+ val gregorianYear = calendarDate.getDeclaredFieldValue(" gregorianYear" ) as Int
35
+
36
+ gcal == null && ! normalized && gregorianYear >= 1582 && r.isException<NullPointerException >()
37
+ }
38
+ )
39
+ }
40
+ }
41
+
42
+ @Test
43
+ fun testGetTimeWithoutReflection () {
44
+ withUsingReflectionForMaximizingCoverage(maximizeCoverage = false ) {
45
+ checkWithException(
46
+ DateExample ::getTime,
47
+ eq(3 ),
48
+ * commonMatchers
49
+ )
50
+ }
51
+ }
52
+
53
+ private val commonMatchers = arrayOf(
54
+ { date: Date ? , r: Result <Boolean > -> date == null && r.isException<NullPointerException >() },
55
+ { date: Date ? , r: Result <Boolean > -> date != null && date.time == 100L && r.getOrThrow() },
56
+ { date: Date ? , r: Result <Boolean > -> date != null && date.time != 100L && ! r.getOrThrow() }
57
+ )
58
+
59
+ private fun Any.getDeclaredFieldValue (fieldName : String ): Any? {
60
+ val declaredField = javaClass.getDeclaredField(fieldName)
61
+ declaredField.isAccessible = true
62
+
63
+ return declaredField.get(this )
64
+ }
65
+ }
0 commit comments