DBZ-1118 Adding JUnit rule to skip tests depending on Postgres version

This commit is contained in:
Gunnar Morling 2019-02-04 12:32:18 +01:00 committed by Jiri Pechanec
parent c414a8b942
commit 7076deba5a
4 changed files with 99 additions and 10 deletions

View File

@ -8,12 +8,13 @@
import static io.debezium.connector.postgresql.TestHelper.PK_FIELD;
import static io.debezium.connector.postgresql.TestHelper.topicName;
import static io.debezium.connector.postgresql.junit.SkipWhenDatabaseVersionLessThan.PostgresVersion.POSTGRES_10;
import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -24,8 +25,12 @@
import org.apache.kafka.connect.source.SourceRecord;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import io.debezium.connector.postgresql.junit.SkipTestDependingOnDatabaseVersionRule;
import io.debezium.connector.postgresql.junit.SkipWhenDatabaseVersionLessThan;
import io.debezium.data.Envelope;
import io.debezium.data.VerifyRecord;
import io.debezium.doc.FixFor;
@ -43,6 +48,9 @@
*/
public class RecordsSnapshotProducerIT extends AbstractRecordsProducerTest {
@Rule
public final TestRule skip = new SkipTestDependingOnDatabaseVersionRule();
private RecordsSnapshotProducer snapshotProducer;
private PostgresTaskContext context;
@ -337,7 +345,8 @@ public void shouldGenerateSnapshotsForDecimalDatatypesUsingStringEncoding() thro
@Test
@FixFor("DBZ-1118")
public void shouldGenerateSnapshotsForParitionedTables() throws Exception {
@SkipWhenDatabaseVersionLessThan(POSTGRES_10)
public void shouldGenerateSnapshotsForPartitionedTables() throws Exception {
TestHelper.dropAllSchemas();
String ddl = "CREATE TABLE first_table (pk integer, user_id integer, PRIMARY KEY(pk));" +

View File

@ -0,0 +1,46 @@
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.connector.postgresql.junit;
import java.sql.SQLException;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import io.debezium.connector.postgresql.TestHelper;
import io.debezium.junit.AnnotationBasedTestRule;
/**
* JUnit rule that skips a test based on the {@link SkipWhenDatabaseVersionLessThan} annotation either on a test method
* or on a test class.
*
* @author Gunnar Morling
*/
public class SkipTestDependingOnDatabaseVersionRule extends AnnotationBasedTestRule {
private static final int majorDbVersion = determineDbVersion();
@Override
public Statement apply(Statement base, Description description) {
SkipWhenDatabaseVersionLessThan skip = hasAnnotation(description, SkipWhenDatabaseVersionLessThan.class);
if (skip != null && skip.value().isLargerThan(majorDbVersion)) {
String reasonForSkipping = "Database version less than " + skip.value();
return emptyStatement(reasonForSkipping, description);
}
return base;
}
public static int determineDbVersion() {
try {
return TestHelper.create().connection().getMetaData().getDatabaseMajorVersion();
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.connector.postgresql.junit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marker annotation used together with the {@link SkipTestDependingOnDatabaseVersionRule} JUnit rule, that allows
* tests to be skipped based on the Postgres version used for testing.
*
* @author Gunnar Morling
*/
@Retention( RetentionPolicy.RUNTIME)
@Target( {ElementType.METHOD, ElementType.TYPE})
public @interface SkipWhenDatabaseVersionLessThan {
PostgresVersion value();
public enum PostgresVersion {
POSTGRES_10 {
@Override
boolean isLargerThan(int otherMajor) {
return otherMajor < 10;
}
};
abstract boolean isLargerThan(int otherMajor);
}
}

View File

@ -13,8 +13,8 @@
/**
* A base {@link TestRule} that allows easy writing of test rules based on method annotations.
* @author Jiri Pechanec
*
* @author Jiri Pechanec
*/
public abstract class AnnotationBasedTestRule implements TestRule {
@ -27,6 +27,7 @@ public void evaluate() throws Throwable {
if (reason != null && !reason.trim().isEmpty()) {
messageBuilder.append(" because: ").append(reason);
}
System.out.println(messageBuilder.toString());
}
};
@ -34,16 +35,14 @@ public void evaluate() throws Throwable {
protected <T extends Annotation> T hasAnnotation(Description description, Class<T> annotationClass) {
T annotation = description.getAnnotation(annotationClass);
if (annotation != null) {
return annotation;
} else if (description.isTest() && description.getTestClass().isAnnotationPresent(annotationClass)) {
}
else if (description.isTest() && description.getTestClass().isAnnotationPresent(annotationClass)) {
return description.getTestClass().getAnnotation(annotationClass);
}
return null;
}
public AnnotationBasedTestRule() {
super();
}
}
}