test hashcodes are consistent (fixed 3 that weren't)

This commit is contained in:
Ken Stevens 2019-11-02 16:56:39 -04:00
parent f20fee297a
commit 5577680a85
7 changed files with 121 additions and 2 deletions

View File

@ -80,5 +80,4 @@ public class AddColumnTask extends BaseTableColumnTypeTask<AddColumnTask> {
}
return type + " " + nullable;
}
}

View File

@ -22,6 +22,8 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -99,4 +101,23 @@ public class AddIdGeneratorTask extends BaseTask<AddIdGeneratorTask> {
}
@Override
public boolean equals(Object theO) {
if (this == theO) return true;
if (!(theO instanceof AddIdGeneratorTask)) return false;
AddIdGeneratorTask that = (AddIdGeneratorTask) theO;
return new EqualsBuilder()
.append(myGeneratorName, that.myGeneratorName)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(myGeneratorName)
.toHashCode();
}
}

View File

@ -23,6 +23,8 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import ca.uhn.fhir.util.VersionEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.ColumnMapRowMapper;
@ -165,4 +167,26 @@ public class ArbitrarySqlTask extends BaseTask<ArbitrarySqlTask> {
return myColumn;
}
}
@Override
public boolean equals(Object theO) {
if (this == theO) return true;
if (!(theO instanceof ArbitrarySqlTask)) return false;
ArbitrarySqlTask that = (ArbitrarySqlTask) theO;
return new EqualsBuilder()
.append(myTableName, that.myTableName)
.append(myExecuteOnlyIfTableExists, that.myExecuteOnlyIfTableExists)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(myTableName)
.append(myExecuteOnlyIfTableExists)
.toHashCode();
}
}

View File

@ -40,6 +40,7 @@ import java.util.Map;
import java.util.concurrent.*;
import java.util.function.Function;
// FIXME KHS special case this task
public class CalculateHashesTask extends BaseTableColumnTask<CalculateHashesTask> {
private static final Logger ourLog = LoggerFactory.getLogger(CalculateHashesTask.class);

View File

@ -22,6 +22,8 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -99,4 +101,23 @@ public class DropIdGeneratorTask extends BaseTask<DropIdGeneratorTask> {
}
@Override
public boolean equals(Object theO) {
if (this == theO) return true;
if (!(theO instanceof DropIdGeneratorTask)) return false;
DropIdGeneratorTask that = (DropIdGeneratorTask) theO;
return new EqualsBuilder()
.append(myGeneratorName, that.myGeneratorName)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(myGeneratorName)
.toHashCode();
}
}

View File

@ -1,6 +1,9 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import ca.uhn.fhir.jpa.migrate.tasks.api.BaseMigrationTasks;
import ca.uhn.fhir.jpa.migrate.tasks.api.Builder;
import ca.uhn.fhir.util.VersionEnum;
import org.junit.Test;
import java.sql.SQLException;
@ -58,5 +61,4 @@ public class AddColumnTest extends BaseTest {
assertThat(JdbcUtils.getColumnNames(getConnectionProperties(), "SOMETABLE"), containsInAnyOrder("PID", "TEXTCOL", "NEWCOL"));
}
}

View File

@ -0,0 +1,51 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.tasks.HapiFhirJpaMigrationTasks;
import ca.uhn.fhir.util.VersionEnum;
import org.junit.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class HashTest {
@Test
public void testHash() {
AddColumnTask task1 = buildTask();
AddColumnTask task2 = buildTask();
assertEquals(task1.hashCode(), task2.hashCode());
}
private AddColumnTask buildTask() {
AddColumnTask task = new AddColumnTask("1", "1");
task.setTableName("TRM_CODESYSTEM_VER");
task.setColumnName("CS_VERSION_ID");
task.setNullable(true);
task.setColumnType(BaseTableColumnTypeTask.ColumnTypeEnum.STRING);
task.setColumnLength(255);
return task;
}
@Test
public void checkAllHashes() {
List<BaseTask<?>> tasks1 = new HapiFhirJpaMigrationTasks(Collections.emptySet()).getAllTasks(VersionEnum.values());
Map<String, Integer> hashesByVersion = new HashMap<>();
for (BaseTask task : tasks1) {
String version = task.getFlywayVersion();
assertNull("Duplicate flyway version " + version + " in " + HapiFhirJpaMigrationTasks.class.getName(), hashesByVersion.get(version));
hashesByVersion.put(version, task.hashCode());
}
List<BaseTask<?>> tasks2 = new HapiFhirJpaMigrationTasks(Collections.emptySet()).getAllTasks(VersionEnum.values());
for (BaseTask task : tasks2) {
String version = task.getFlywayVersion();
int origHash = hashesByVersion.get(version);
assertEquals("Hashes differ for task " + version, origHash, task.hashCode());
}
}
}