Skip to content

Commit

Permalink
Clone builder tests (#208)
Browse files Browse the repository at this point in the history
* Correcting plugin data clone builder tests to show that changes to the clone builder's state cause it to build a new instance of the plugin data.

* Correcting plugin data clone builder tests to show that changes to the clone builder's state cause it to build a new instance of the plugin data.

* Correcting plugin data clone builder tests to show that changes to the clone builder's state cause it to build a new instance of the plugin data.
  • Loading branch information
shawnhatch authored Feb 16, 2024
1 parent ed5cd8e commit ce38f76
Show file tree
Hide file tree
Showing 40 changed files with 4,878 additions and 4,183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ private Data(Data data) {
locked = data.locked;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((consumers == null) ? 0 : consumers.hashCode());
result = prime * result + (locked ? 1231 : 1237);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Data)) {
return false;
}
Data other = (Data) obj;
if (consumers == null) {
if (other.consumers != null) {
return false;
}
} else if (!consumers.equals(other.consumers)) {
return false;
}
if (locked != other.locked) {
return false;
}
return true;
}



}

/**
Expand Down Expand Up @@ -105,7 +138,7 @@ public List<Pair<Double, Consumer<ActorContext>>> getConsumers() {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand All @@ -117,4 +150,31 @@ public boolean allPlansComplete() {
return data.consumers.isEmpty();
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((data == null) ? 0 : data.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof RunContinuityPluginData)) {
return false;
}
RunContinuityPluginData other = (RunContinuityPluginData) obj;
if (data == null) {
if (other.data != null) {
return false;
}
} else if (!data.equals(other.data)) {
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private static void validateGlobalPropertyIdExists(final Data data, final Global
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ public Builder setNextGroupIdValue(int nextGroupIdValue) {
data.nextGroupIdValue = nextGroupIdValue;
return this;
}

public Builder resetNextGroupIdValue() {
ensureDataMutability();
data.nextGroupIdValue = -1;
return this;
}


private void validateData() {

Expand Down Expand Up @@ -1030,7 +1037,7 @@ public int getGroupCount() {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public enum GroupError implements ContractError {

NEGATIVE_GROUP_ID("group id is negative"), NEGATIVE_GROUP_COUNT("group count is negative"),
NEXT_GROUP_ID_TOO_SMALL("The next gropu id must exceed all extant group ids"),
NEXT_GROUP_ID_TOO_SMALL("The next group id must exceed all extant group ids"),
NULL_GROUP_INITIALIZATION_DATA("Null group initialization data"),
NULL_GROUP_DATA_MANAGER("Null group data manager"), NULL_GROUP_PLUGIN_DATA("null groupsplugin data"),
NULL_GROUP_POPULATION_REPORT_PLUGIN_DATA("Null group population report plugin data"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,7 @@ public Boolean isStageOffered(final StageId stageId) {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean supportsRunContinuity() {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(new Data(data));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ public <T extends AttributeId> Set<T> getAttributeIds() {
}

@Override
public PluginDataBuilder getCloneBuilder() {

public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ public Builder setPersonCount(int personCount) {
data.personCount = personCount;
return this;
}
/**
* Resets the person count
*/
public Builder resetPersonCount() {
ensureDataMutability();
data.personCount = -1;
return this;
}

/**
* Sets the time for the last person added to the population. Defaults to zero.
Expand Down Expand Up @@ -297,7 +305,7 @@ public List<PersonRange> getPersonRanges() {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public <T extends PersonPropertyId> Set<T> getPersonPropertyIds() {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Data(Data data) {
}
personRegions.addAll(data.personRegions);
personAdditionMode = data.personAdditionMode;

locked = data.locked;
personArrivalTimes.addAll(data.personArrivalTimes);
locked = data.locked;
}

@Override
Expand Down Expand Up @@ -327,7 +327,7 @@ private void setPersonAdditionMode(PersonAdditionMode personAdditionMode) {
public Builder addPerson(final PersonId personId, final RegionId regionId) {
ensureDataMutability();
validatePersonId(personId);
validateRegionIdNotNull(regionId);
validateRegionIdNotNull(regionId);
setPersonAdditionMode(PersonAdditionMode.REGION_ONLY);
int personIndex = personId.getValue();
while (personIndex >= data.personRegions.size()) {
Expand Down Expand Up @@ -568,7 +568,7 @@ public boolean getPersonRegionArrivalTrackingPolicy() {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
* periodicity of the report.
*/
public enum ReportPeriod {
HOURLY, DAILY, END_OF_SIMULATION
HOURLY, DAILY, END_OF_SIMULATION;

public ReportPeriod next() {
int index = (ordinal()+1) % ReportPeriod.values().length;
return ReportPeriod.values()[index];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ public Set<RegionId> getRegionIds() {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private StochasticsPluginData(Data data) {
}

@Override
public PluginDataBuilder getCloneBuilder() {
public Builder getCloneBuilder() {
return new Builder(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -64,10 +65,20 @@ public void testGetCloneBuilder() {
}
RunContinuityPluginData runContinuityPluginData = builder.build();

RunContinuityPluginData cloneRunContinuityPluginData = //
(RunContinuityPluginData) runContinuityPluginData.getCloneBuilder().build();
// show that the returned clone builder will build an identical instance if no
// mutations are made
RunContinuityPluginData.Builder cloneBuilder = runContinuityPluginData.getCloneBuilder();
assertNotNull(cloneBuilder);
assertEquals(runContinuityPluginData, cloneBuilder.build());

// show that the clone builder builds a distinct instance if any mutation is
// made

// addContextConsumer
cloneBuilder = runContinuityPluginData.getCloneBuilder();
cloneBuilder.addContextConsumer(2.5,(c)->{});
assertNotEquals(runContinuityPluginData, cloneBuilder.build());

assertEquals(runContinuityPluginData.getConsumers(), cloneRunContinuityPluginData.getConsumers());

}
}
Expand Down
Loading

0 comments on commit ce38f76

Please sign in to comment.