DBZ-632 Removing methods from HistoryRecord which where only used in tests;

Also adding a new test for HistoryRecord (de-)serialization
This commit is contained in:
Gunnar Morling 2018-03-01 11:08:35 +01:00 committed by Jiri Pechanec
parent 6e29b82358
commit 401cbae852
2 changed files with 30 additions and 81 deletions

View File

@ -38,36 +38,20 @@ public Document document() {
return this.doc;
}
public boolean isAtOrBefore(HistoryRecord other) {
if (other == this) return true;
return this.position().compareToUsingSimilarFields(other.position()) <= 0
&& source().equals(other.source());
}
protected Document source() {
return doc.getDocument("source");
return doc.getDocument(Fields.SOURCE);
}
protected Document position() {
return doc.getDocument("position");
return doc.getDocument(Fields.POSITION);
}
protected String databaseName() {
return doc.getString("databaseName");
return doc.getString(Fields.DATABASE_NAME);
}
protected String ddl() {
return doc.getString("ddl");
}
protected boolean hasSameSource(HistoryRecord other) {
if (this == other) return true;
return other != null && source().equals(other.source());
}
protected boolean hasSameDatabase(HistoryRecord other) {
if (this == other) return true;
return other != null && databaseName().equals(other.databaseName());
return doc.getString(Fields.DDL_STATEMENTS);
}
@Override

View File

@ -5,13 +5,13 @@
*/
package io.debezium.relational.history;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;
import java.util.Map;
import org.junit.Test;
import io.debezium.document.DocumentReader;
import io.debezium.util.Collect;
/**
@ -19,67 +19,32 @@
*
*/
public class HistoryRecordTest {
private Map<String,Object> source1;
private Map<String,Object> position1;
private HistoryRecord record1;
private Map<String,Object> source2;
private Map<String,Object> position2;
private HistoryRecord record2;
private Map<String,Object> source3;
private Map<String,Object> position3;
private HistoryRecord record3;
@Before
public void beforeEach() {
source1 = Collect.linkMapOf("server", "abc");
position1 = Collect.linkMapOf("file", "x.log", "position", 100L, "entry", 1);
record1 = new HistoryRecord(source1, position1, "db", "CREATE TABLE foo ( first VARCHAR(22) NOT NULL );");
source2 = Collect.linkMapOf("server", "abc");
position2 = Collect.linkMapOf("file", "x.log", "position", 300L, "entry", 2);
record2 = new HistoryRecord(source2, position2, "db", "DROP TABLE foo;");
source3 = Collect.linkMapOf("server", "xyx");
position3 = Collect.linkMapOf("file", "y.log", "position", 10000L, "entry", 1);
record3 = new HistoryRecord(source3, position3, "other", "DROP TABLE foo;");
}
@Test
public void shouldConsiderOneSourceTheSame() {
assertThat(record1.hasSameSource(record1)).isTrue();
assertThat(record2.hasSameSource(record2)).isTrue();
assertThat(record3.hasSameSource(record3)).isTrue();
}
@Test
public void shouldConsiderTwoDifferentSourcesNotSame() {
assertThat(record1.hasSameSource(null)).isFalse();
assertThat(record1.hasSameSource(record3)).isFalse();
assertThat(record2.hasSameSource(record3)).isFalse();
}
public void canSerializeAndDeserializeHistoryRecord() throws Exception {
Map<String,Object> source = Collect.linkMapOf("server", "abc");
Map<String,Object> position = Collect.linkMapOf("file", "x.log", "positionInt", 100, "positionLong", Long.MAX_VALUE, "entry", 1);
String databaseName = "db";
String ddl = "CREATE TABLE foo ( first VARCHAR(22) NOT NULL );";
@Test
public void shouldConsiderTwoDifferentSourcesTheSame() {
assertThat(record1.hasSameSource(record2)).isTrue();
}
HistoryRecord record = new HistoryRecord(source, position, databaseName, ddl);
@Test
public void shouldConsiderOneDatabaseTheSame() {
assertThat(record1.hasSameDatabase(record1)).isTrue();
}
String serialized = record.toString();
DocumentReader reader = DocumentReader.defaultReader();
HistoryRecord deserialized = new HistoryRecord(reader.read(serialized));
@Test
public void shouldConsiderTwoDifferentDatabasesNotSame() {
assertThat(record1.hasSameDatabase(record3)).isFalse();
assertThat(record2.hasSameDatabase(record3)).isFalse();
}
assertThat(deserialized.source()).isNotNull();
assertThat(deserialized.source().get("server")).isEqualTo("abc");
@Test
public void shouldCorrectlyComparePositions() {
assertThat(record1.isAtOrBefore(record1)).isTrue();
assertThat(record2.isAtOrBefore(record2)).isTrue();
assertThat(record1.isAtOrBefore(record2)).isTrue();
assertThat(record2.isAtOrBefore(record1)).isFalse();
assertThat(deserialized.position()).isNotNull();
assertThat(deserialized.position().get("file")).isEqualTo("x.log");
assertThat(deserialized.position().get("positionInt")).isEqualTo(100);
assertThat(deserialized.position().get("positionLong")).isEqualTo(Long.MAX_VALUE);
assertThat(deserialized.position().get("entry")).isEqualTo(1);
assertThat(deserialized.databaseName()).isEqualTo(databaseName);
assertThat(deserialized.ddl()).isEqualTo(ddl);
System.out.println(record);
}
}