Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tepa46 committed Jul 24, 2023
1 parent f89cedf commit 89a8400
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ class SpringTestClassModelBuilder(val context: CgContext): TestClassModelBuilder

private fun collectByModel(model: UtModel): Set<UtModelWrapper> {
val dependentModels = mutableSetOf<UtModelWrapper>()
collectRecursively(model, dependentModels)

with(context){
collectRecursively(model.wrap(), dependentModels)
}

return dependentModels
}
Expand All @@ -98,51 +101,54 @@ class SpringTestClassModelBuilder(val context: CgContext): TestClassModelBuilder
return classModels
}

private fun collectRecursively(currentModel: UtModel, allModels: MutableSet<UtModelWrapper>, modelTagName: String? = null) {
val cgModel = with(context) { currentModel.wrap(if(currentModel.isMockModel()) modelTagName else null) }
if (!allModels.add(cgModel)) {
private fun collectRecursively(currentModelWrapper: UtModelWrapper, allModels: MutableSet<UtModelWrapper>) {
if (!allModels.add(currentModelWrapper)) {
return
}

when (currentModel) {
is UtNullModel,
is UtPrimitiveModel,
is UtClassRefModel,
is UtVoidModel,
is UtEnumConstantModel,
is UtSpringContextModel -> {}
is UtLambdaModel -> {
currentModel.capturedValues.forEach { collectRecursively(it, allModels) }
}
is UtArrayModel -> {
currentModel.stores.values.forEach { collectRecursively(it, allModels) }
if (currentModel.stores.count() < currentModel.length) {
collectRecursively(currentModel.constModel, allModels)
with(context) {
when (val currentModel = currentModelWrapper.model) {
is UtNullModel,
is UtPrimitiveModel,
is UtClassRefModel,
is UtVoidModel,
is UtEnumConstantModel,
is UtSpringContextModel -> {}
is UtLambdaModel -> {
currentModel.capturedValues.forEach { collectRecursively(it.wrap(), allModels) }
}
}
is UtCompositeModel -> {
// Here we traverse fields only.
// Traversing mocks as well will result in wrong models playing
// a role of class fields with @Mock annotation.
currentModel.fields.forEach { (key, value) ->
collectRecursively(value, allModels, key.name)
is UtArrayModel -> {
currentModel.stores.values.forEach { collectRecursively(it.wrap(), allModels) }
if (currentModel.stores.count() < currentModel.length) {
collectRecursively(currentModel.constModel.wrap(), allModels)
}
}
}
is UtAssembleModel -> {
currentModel.origin?.let { collectRecursively(it, allModels) }
is UtCompositeModel -> {
// Here we traverse fields only.
// Traversing mocks as well will result in wrong models playing
// a role of class fields with @Mock annotation.
currentModel.fields.forEach { (fieldId, model) ->
// We use `modelTagName` in order to distinguish mock models
val modeTagName = if(model.isMockModel()) fieldId.name else null
collectRecursively(model.wrap(modeTagName), allModels)
}
}
is UtAssembleModel -> {
currentModel.origin?.let { collectRecursively(it.wrap(), allModels) }

currentModel.instantiationCall.instance?.let { collectRecursively(it, allModels) }
currentModel.instantiationCall.params.forEach { collectRecursively(it, allModels) }
currentModel.instantiationCall.instance?.let { collectRecursively(it.wrap(), allModels) }
currentModel.instantiationCall.params.forEach { collectRecursively(it.wrap(), allModels) }

currentModel.modificationsChain.forEach { stmt ->
stmt.instance?.let { collectRecursively(it, allModels) }
when (stmt) {
is UtStatementCallModel -> stmt.params.forEach { collectRecursively(it, allModels) }
is UtDirectSetFieldModel -> collectRecursively(stmt.fieldModel, allModels)
currentModel.modificationsChain.forEach { stmt ->
stmt.instance?.let { collectRecursively(it.wrap(), allModels) }
when (stmt) {
is UtStatementCallModel -> stmt.params.forEach { collectRecursively(it.wrap(), allModels) }
is UtDirectSetFieldModel -> collectRecursively(stmt.fieldModel.wrap(), allModels)
}
}
}
//Python, JavaScript, Go models are not required in Spring
}
//Python, JavaScript, Go models are not required in Spring
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,18 @@ abstract class CgAbstractSpringTestClassConstructor(context: CgContext) :

modelWrappers
.forEach { modelWrapper ->
val modelWrapperWithEmptyTagName = UtModelWrapper(

val modelWrapperWithNullTagName = UtModelWrapper(
testSetId = modelWrapper.testSetId,
executionId = modelWrapper.executionId,
model = modelWrapper.model,
modelTagName = null,
)

valueByUtModelWrapper[modelWrapperWithEmptyTagName] = createdVariable
valueByUtModelWrapper[modelWrapperWithNullTagName] = createdVariable

variableConstructor.annotatedModelGroups
.getOrPut(annotationClassId) { mutableSetOf() } += modelWrapperWithEmptyTagName
.getOrPut(annotationClassId) { mutableSetOf() } += modelWrapperWithNullTagName
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.utbot.examples.spring.autowiring.twoAndMoreBeansForOneType;

public class Person {
private String firstName;
private String lastName;

private Integer age;

private Integer weight;

public Person(String firstName, String secondName, Integer age, Integer weight) {
this.firstName = firstName;
this.lastName = secondName;
public Person(Integer age, Integer weight) {
this.age = age;
this.weight = weight;
}
Expand All @@ -19,10 +14,6 @@ public Integer getAge(){
return age;
}

public String getName() {
return firstName + " " + lastName;
}

public Integer getWeight() {
return weight;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

@Service
public class PersonService {
public class ServiceOfBeansWithSameType {
@Autowired
private Person personOne;

Expand All @@ -16,12 +16,12 @@ public class PersonService {

public final List<String> baseOrders = new ArrayList<>();

// a method for testing the case when the Engine reproduces one model on @Autowired variables of the same type
// a method for testing the case when the Engine produces one model on @Autowired variables of the same type
public Integer ageSum(){
return personOne.getAge() + personTwo.getAge();
}

// a method for testing the case when the Engine reproduces two models on @Autowired variables of the same type
// a method for testing the case when the Engine produces two models on @Autowired variables of the same type
public Integer joinInfo(){
return personOne.getWeight() + personTwo.getAge() + baseOrders.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import org.utbot.framework.plugin.api.UtConcreteValue
import org.utbot.testcheckers.eq
import org.utbot.testing.*

class PersonServiceTest : SpringNoConfigUtValueTestCaseChecker(
testClass = PersonService::class,
class ServiceOfBeansWithSameTypeTest : SpringNoConfigUtValueTestCaseChecker(
testClass = ServiceOfBeansWithSameType::class,
) {


/**
* In this test, we check the case when the Engine reproduces two models on two @Autowired variables of the same type.
*
Expand All @@ -20,16 +19,16 @@ class PersonServiceTest : SpringNoConfigUtValueTestCaseChecker(
@Test
fun testJoin() {
checkThisMocksAndExceptions(
method = PersonService::joinInfo,
method = ServiceOfBeansWithSameType::joinInfo,
branches = eq(3),
{ _, mocks, r ->
val personOne = mocks.singleMock("personOne", weightPersonCall)

val personOneWeight = personOne.value<Int?>()

val r1 = personOneWeight == null
val isPersonOneWeightNull = personOneWeight == null

r1 && r.isException<NullPointerException>()
isPersonOneWeightNull && r.isException<NullPointerException>()
},
{ _, mocks, r ->
val personOne = mocks.singleMock("personOne", weightPersonCall)
Expand All @@ -38,10 +37,10 @@ class PersonServiceTest : SpringNoConfigUtValueTestCaseChecker(
val personOneWeight = personOne.value<Int?>()
val personTwoAge = personTwo.value<Int?>()

val r1 = personOneWeight != null
val r2 = personTwoAge == null
val isPersonOneWeightNotNull = personOneWeight != null
val isPersonTwoAgeNull = personTwoAge == null

r1 && r2 && r.isException<NullPointerException>()
isPersonOneWeightNotNull && isPersonTwoAgeNull && r.isException<NullPointerException>()
},
{ thisInstance, mocks, r ->
val personOne = mocks.singleMock("personOne", weightPersonCall)
Expand All @@ -59,7 +58,6 @@ class PersonServiceTest : SpringNoConfigUtValueTestCaseChecker(
)
}


/**
* In this test, we check the case when the Engine reproduces one model on two @Autowired variables of the same type.
*
Expand All @@ -68,7 +66,7 @@ class PersonServiceTest : SpringNoConfigUtValueTestCaseChecker(
@Test
fun testAgeSum(){
checkThisMocksAndExceptions(
method = PersonService::ageSum,
method = ServiceOfBeansWithSameType::ageSum,
branches = ignoreExecutionsNumber,
{ _, mocks, r ->
val personOne = mocks.singleMock("personOne", agePersonCall)
Expand Down Expand Up @@ -102,5 +100,4 @@ class PersonServiceTest : SpringNoConfigUtValueTestCaseChecker(
additionalDependencies = springAdditionalDependencies,
)
}

}
}

0 comments on commit 89a8400

Please sign in to comment.