Skip to content

Do not set final fields with direct accessors in UtAssembleModel #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
if (fieldId.isStatic) {
throw AssembleException("Static field $fieldId can't be set in an object of the class $classId")
}
if (fieldId.isFinal) {
throw AssembleException("Final field $fieldId can't be set in an object of the class $classId")
}
//fill field value if it hasn't been filled by constructor, and it is not default
if (fieldId in constructorInfo.affectedFields ||
(fieldId !in constructorInfo.setFields && !fieldModel.hasDefaultValue())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.utbot.examples.assemble;

/**
* A class with a FINAL field allowing direct access.
*/
public class DirectAccessFinal {
public final int[] array = {1, 2};
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package org.utbot.framework.assemble

import org.utbot.examples.assemble.AssembleTestUtils
import org.utbot.examples.assemble.ComplexField
import org.utbot.examples.assemble.DirectAccess
import org.utbot.examples.assemble.DirectAccessAndSetter
import org.utbot.examples.assemble.InheritedField
import org.utbot.examples.assemble.ListItem
import org.utbot.examples.assemble.NoModifier
import org.utbot.examples.assemble.PackagePrivateFields
import org.utbot.examples.assemble.PrimitiveFields
import org.utbot.examples.assemble.arrays.ArrayOfComplexArrays
import org.utbot.examples.assemble.arrays.ArrayOfPrimitiveArrays
import org.utbot.examples.assemble.arrays.AssignedArray
Expand Down Expand Up @@ -54,6 +45,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.utbot.examples.assemble.*

/**
* Test classes must be located in the same folder as [AssembleTestUtils] class.
Expand Down Expand Up @@ -337,6 +329,26 @@ class AssembleModelGeneratorTests {
createModelAndAssert(compositeModel, expectedRepresentation)
}

@Test
fun testOnObjectWithFinalFields() {
val testClassId = DirectAccessFinal::class.id

val arrayObjectFields = fields(
testClassId,
"array" to UtArrayModel(
modelIdCounter.incrementAndGet(),
intArrayClassId,
length = 2,
UtPrimitiveModel(0),
mutableMapOf(0 to UtPrimitiveModel(1), 1 to UtPrimitiveModel(2)),
),
)
val compositeModel =
UtCompositeModel(modelIdCounter.incrementAndGet(), testClassId, isMock = false, arrayObjectFields)

createModelAndAssert(compositeModel, null)
}

//region inheritance_tests

@Test
Expand Down