Skip to content

Commit

Permalink
Add test cases on PipelineContextKey (#33323)
Browse files Browse the repository at this point in the history
* Add test cases on PipelineElasticJobListener

* Add test cases on PipelineContextKey
  • Loading branch information
terrymanu authored Oct 19, 2024
1 parent f15303c commit aa6da27
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ public PipelineContextKey(final InstanceType instanceType) {
}

@Override
public boolean equals(final Object o) {
if (this == o) {
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (null == o || getClass() != o.getClass()) {
if (null == obj || getClass() != obj.getClass()) {
return false;
}
final PipelineContextKey that = (PipelineContextKey) o;
PipelineContextKey that = (PipelineContextKey) obj;
return instanceType == that.instanceType && Objects.equals(filterDatabaseName(this), filterDatabaseName(that));
}

private String filterDatabaseName(final PipelineContextKey contextKey) {
return InstanceType.PROXY == contextKey.getInstanceType() ? "" : contextKey.getDatabaseName();
}

@Override
public int hashCode() {
return Objects.hash(instanceType, filterDatabaseName(this));
}

private String filterDatabaseName(final PipelineContextKey contextKey) {
return InstanceType.PROXY == contextKey.getInstanceType() ? "" : contextKey.getDatabaseName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,53 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class PipelineContextKeyTest {

@Test
void assertHashCodeEqualsForProxyMode() {
PipelineContextKey contextKey1 = new PipelineContextKey(null, InstanceType.PROXY);
PipelineContextKey contextKey2 = new PipelineContextKey("sharding_db", InstanceType.PROXY);
assertThat(contextKey1.hashCode(), is(contextKey2.hashCode()));
assertThat(contextKey1, is(contextKey2));
void assertEqualsWithSameObject() {
PipelineContextKey pipelineContextKey = new PipelineContextKey(InstanceType.JDBC);
assertThat(pipelineContextKey, is(pipelineContextKey));
}

@Test
void assertHashCodeEqualsForJdbcMode() {
PipelineContextKey contextKey1 = new PipelineContextKey("logic_db", InstanceType.JDBC);
PipelineContextKey contextKey2 = new PipelineContextKey("sharding_db", InstanceType.JDBC);
assertThat(contextKey1.hashCode(), not(contextKey2.hashCode()));
assertThat(contextKey1, not(contextKey2));
void assertEqualsWithNull() {
assertNotEquals(null, new PipelineContextKey(InstanceType.JDBC));
}

@Test
void assertEqualsWithDifferentClassTypes() {
assertThat(new PipelineContextKey(InstanceType.JDBC), not(new Object()));
}

@Test
void assertEqualsWithDifferentInstanceTypes() {
assertThat(new PipelineContextKey(InstanceType.JDBC), not(new PipelineContextKey(InstanceType.PROXY)));
}

@Test
void assertEqualsWithProxyMode() {
assertThat(new PipelineContextKey(null, InstanceType.PROXY), is(new PipelineContextKey("foo_db", InstanceType.PROXY)));
}

@Test
void assertEqualsWithJDBCMode() {
assertThat(new PipelineContextKey("foo_db", InstanceType.JDBC), is(new PipelineContextKey("foo_db", InstanceType.JDBC)));
}

@Test
void assertNotEqualsWithJDBCMode() {
assertThat(new PipelineContextKey("foo_db", InstanceType.JDBC), not(new PipelineContextKey("bar_db", InstanceType.JDBC)));
}

@Test
void assertHashCodeWithProxyMode() {
assertThat(new PipelineContextKey(InstanceType.PROXY).hashCode(), is(new PipelineContextKey("foo_db", InstanceType.PROXY).hashCode()));
}

@Test
void assertHashCodeWithJDBCMode() {
assertThat(new PipelineContextKey("foo_db", InstanceType.JDBC).hashCode(), not(new PipelineContextKey("bar_db", InstanceType.JDBC).hashCode()));
}
}

0 comments on commit aa6da27

Please sign in to comment.