Skip to content
Permalink
Browse files
[hotfix][table][tests] Replace ExpectedException with assertJ
Replace deprecated ExpectedException with assertJ's
`assertThatThrownBy` and improve certain assertions by asserting
the Exception class or the Exception message where missing.

This closes #19262.
  • Loading branch information
matriv authored and twalthr committed May 9, 2022
1 parent 8a7a613 commit 2ade17dc29716b04b0f59e59608316ca980de2e0
Show file tree
Hide file tree
Showing 31 changed files with 901 additions and 814 deletions.
@@ -27,7 +27,6 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.Timeout;

@@ -50,12 +49,11 @@

import static org.apache.flink.configuration.ConfigConstants.ENV_FLINK_CONF_DIR;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Tests for {@link SqlClient}. */
public class SqlClientTest {

@Rule public ExpectedException thrown = ExpectedException.none();

@Rule public TemporaryFolder tempFolder = new TemporaryFolder();

private Map<String, String> originalEnv;
@@ -119,9 +117,9 @@ public void testEmptyOptions() throws Exception {
@Test
public void testUnsupportedGatewayMode() {
String[] args = new String[] {"gateway"};
thrown.expect(SqlClientException.class);
thrown.expectMessage("Gateway mode is not supported yet.");
SqlClient.main(args);
assertThatThrownBy(() -> SqlClient.main(args))
.isInstanceOf(SqlClientException.class)
.hasMessage("Gateway mode is not supported yet.");
}

@Test
@@ -203,11 +201,11 @@ public void testExecuteSqlFile() throws Exception {
}

@Test
public void testExecuteSqlWithHDFSFile() throws Exception {
public void testExecuteSqlWithHDFSFile() {
String[] args = new String[] {"-f", "hdfs://path/to/file/test.sql"};
thrown.expect(SqlClientException.class);
thrown.expectMessage("SQL Client only supports to load files in local.");
runSqlClient(args);
assertThatThrownBy(() -> runSqlClient(args))
.isInstanceOf(SqlClientException.class)
.hasMessage("SQL Client only supports to load files in local.");
}

private String runSqlClient(String[] args) throws Exception {
@@ -50,9 +50,7 @@

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

import java.io.File;
@@ -120,8 +118,6 @@ private static Configuration getConfig() {
return config;
}

@Rule public ExpectedException exception = ExpectedException.none();

@Test
public void testCompleteStatement() {
final Executor executor = createLocalExecutor();
@@ -113,7 +113,7 @@ void testCreateTableLikeCannotDuplicateOptions() throws Exception {

assertThatThrownBy(extendedSqlNode::validate)
.isInstanceOf(SqlValidateException.class)
.hasMessageContaining("Each like option feature can be declared only once.");
.hasMessage("Each like option feature can be declared only once.");
}

@Test
@@ -131,8 +131,7 @@ void testInvalidOverwritingForPartition() throws Exception {

assertThatThrownBy(extendedSqlNode::validate)
.isInstanceOf(SqlValidateException.class)
.hasMessageContaining(
"Illegal merging strategy 'OVERWRITING' for 'PARTITIONS' option.");
.hasMessage("Illegal merging strategy 'OVERWRITING' for 'PARTITIONS' option.");
}

@Test
@@ -150,7 +149,7 @@ void testInvalidOverwritingForAll() throws Exception {

assertThatThrownBy(extendedSqlNode::validate)
.isInstanceOf(SqlValidateException.class)
.hasMessageContaining("Illegal merging strategy 'OVERWRITING' for 'ALL' option.");
.hasMessage("Illegal merging strategy 'OVERWRITING' for 'ALL' option.");
}

@Test
@@ -174,16 +173,11 @@ void testInvalidOverwritingForConstraints() throws Exception {

@Test
void testInvalidNoOptions() {
assertThatThrownBy(
() ->
createFlinkParser(
"CREATE TABLE t (\n"
+ " a STRING\n"
+ ")\n"
+ "LIKE b ()")
.parseStmt())
SqlParser parser =
createFlinkParser("CREATE TABLE t (\n" + " a STRING\n" + ")\n" + "LIKE b ()");
assertThatThrownBy(parser::parseStmt)
.isInstanceOf(SqlParseException.class)
.hasMessageContaining(
.hasMessageStartingWith(
"Encountered \")\" at line 4, column 9.\n"
+ "Was expecting one of:\n"
+ " \"EXCLUDING\" ...\n"
@@ -193,18 +187,18 @@ void testInvalidNoOptions() {

@Test
void testInvalidNoSourceTable() {
assertThatThrownBy(
() ->
createFlinkParser(
"CREATE TABLE t (\n"
+ " a STRING\n"
+ ")\n"
+ "LIKE ("
+ " INCLUDING ALL"
+ ")")
.parseStmt())
SqlParser parser =
createFlinkParser(
"CREATE TABLE t (\n"
+ " a STRING\n"
+ ")\n"
+ "LIKE ("
+ " INCLUDING ALL"
+ ")");

assertThatThrownBy(parser::parseStmt)
.isInstanceOf(SqlParseException.class)
.hasMessageContaining(
.hasMessageStartingWith(
"Encountered \"(\" at line 4, column 6.\n"
+ "Was expecting one of:\n"
+ " <BRACKET_QUOTED_IDENTIFIER> ...\n"
@@ -20,103 +20,106 @@

import org.apache.flink.configuration.Configuration;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.time.DateTimeException;
import java.time.Duration;
import java.time.ZoneId;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Tests for {@link TableConfig}. */
public class TableConfigTest {

@Rule public ExpectedException expectedException = ExpectedException.none();

private static TableConfig configByMethod = TableConfig.getDefault();
private static TableConfig configByConfiguration = TableConfig.getDefault();
private static Configuration configuration = new Configuration();
private static final TableConfig CONFIG_BY_METHOD = TableConfig.getDefault();
private static final TableConfig CONFIG_BY_CONFIGURATION = TableConfig.getDefault();
private static final Configuration configuration = new Configuration();

@Test
public void testSetAndGetSqlDialect() {
configuration.setString("table.sql-dialect", "HIVE");
configByConfiguration.addConfiguration(configuration);
configByMethod.setSqlDialect(SqlDialect.HIVE);
CONFIG_BY_CONFIGURATION.addConfiguration(configuration);
CONFIG_BY_METHOD.setSqlDialect(SqlDialect.HIVE);

assertThat(configByMethod.getSqlDialect()).isEqualTo(SqlDialect.HIVE);
assertThat(configByConfiguration.getSqlDialect()).isEqualTo(SqlDialect.HIVE);
assertThat(CONFIG_BY_METHOD.getSqlDialect()).isEqualTo(SqlDialect.HIVE);
assertThat(CONFIG_BY_CONFIGURATION.getSqlDialect()).isEqualTo(SqlDialect.HIVE);
}

@Test
public void testSetAndGetMaxGeneratedCodeLength() {
configuration.setString("table.generated-code.max-length", "5000");
configByConfiguration.addConfiguration(configuration);
configByMethod.setMaxGeneratedCodeLength(5000);
CONFIG_BY_CONFIGURATION.addConfiguration(configuration);
CONFIG_BY_METHOD.setMaxGeneratedCodeLength(5000);

assertThat(configByMethod.getMaxGeneratedCodeLength()).isEqualTo(Integer.valueOf(5000));
assertThat(configByConfiguration.getMaxGeneratedCodeLength())
assertThat(CONFIG_BY_METHOD.getMaxGeneratedCodeLength()).isEqualTo(Integer.valueOf(5000));
assertThat(CONFIG_BY_CONFIGURATION.getMaxGeneratedCodeLength())
.isEqualTo(Integer.valueOf(5000));
}

@Test
public void testSetAndGetLocalTimeZone() {
configuration.setString("table.local-time-zone", "Asia/Shanghai");
configByConfiguration.addConfiguration(configuration);
configByMethod.setLocalTimeZone(ZoneId.of("Asia/Shanghai"));
CONFIG_BY_CONFIGURATION.addConfiguration(configuration);
CONFIG_BY_METHOD.setLocalTimeZone(ZoneId.of("Asia/Shanghai"));

assertThat(configByMethod.getLocalTimeZone()).isEqualTo(ZoneId.of("Asia/Shanghai"));
assertThat(configByConfiguration.getLocalTimeZone()).isEqualTo(ZoneId.of("Asia/Shanghai"));
assertThat(CONFIG_BY_METHOD.getLocalTimeZone()).isEqualTo(ZoneId.of("Asia/Shanghai"));
assertThat(CONFIG_BY_CONFIGURATION.getLocalTimeZone())
.isEqualTo(ZoneId.of("Asia/Shanghai"));

configuration.setString("table.local-time-zone", "GMT-08:00");
configByConfiguration.addConfiguration(configuration);
configByMethod.setLocalTimeZone(ZoneId.of("GMT-08:00"));
CONFIG_BY_CONFIGURATION.addConfiguration(configuration);
CONFIG_BY_METHOD.setLocalTimeZone(ZoneId.of("GMT-08:00"));

assertThat(configByMethod.getLocalTimeZone()).isEqualTo(ZoneId.of("GMT-08:00"));
assertThat(configByConfiguration.getLocalTimeZone()).isEqualTo(ZoneId.of("GMT-08:00"));
assertThat(CONFIG_BY_METHOD.getLocalTimeZone()).isEqualTo(ZoneId.of("GMT-08:00"));
assertThat(CONFIG_BY_CONFIGURATION.getLocalTimeZone()).isEqualTo(ZoneId.of("GMT-08:00"));
}

@Test
public void testSetInvalidLocalTimeZone() {
expectedException.expectMessage(
"The supported Zone ID is either a full name such as 'America/Los_Angeles',"
+ " or a custom timezone id such as 'GMT-08:00', but configured Zone ID is 'UTC-10:00'.");
configByMethod.setLocalTimeZone(ZoneId.of("UTC-10:00"));
assertThatThrownBy(() -> CONFIG_BY_METHOD.setLocalTimeZone(ZoneId.of("UTC-10:00")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"The supported Zone ID is either a full name such as 'America/Los_Angeles',"
+ " or a custom timezone id such as 'GMT-08:00', but configured Zone ID is 'UTC-10:00'.");
}

@Test
public void testInvalidGmtLocalTimeZone() {
expectedException.expectMessage("Invalid ID for offset-based ZoneId: GMT-8:00");
configByMethod.setLocalTimeZone(ZoneId.of("GMT-8:00"));
assertThatThrownBy(() -> CONFIG_BY_METHOD.setLocalTimeZone(ZoneId.of("GMT-8:00")))
.isInstanceOf(DateTimeException.class)
.hasMessage("Invalid ID for offset-based ZoneId: GMT-8:00");
}

@Test
public void testGetInvalidLocalTimeZone() {
configuration.setString("table.local-time-zone", "UTC+8");
configByConfiguration.addConfiguration(configuration);
expectedException.expectMessage(
"The supported Zone ID is either a full name such as 'America/Los_Angeles',"
+ " or a custom timezone id such as 'GMT-08:00', but configured Zone ID is 'UTC+8'.");
configByConfiguration.getLocalTimeZone();
CONFIG_BY_CONFIGURATION.addConfiguration(configuration);
assertThatThrownBy(CONFIG_BY_CONFIGURATION::getLocalTimeZone)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"The supported Zone ID is either a full name such as 'America/Los_Angeles',"
+ " or a custom timezone id such as 'GMT-08:00', but configured Zone ID is 'UTC+8'.");
}

@Test
public void testGetInvalidAbbreviationLocalTimeZone() {
configuration.setString("table.local-time-zone", "PST");
configByConfiguration.addConfiguration(configuration);
expectedException.expectMessage(
"The supported Zone ID is either a full name such as 'America/Los_Angeles',"
+ " or a custom timezone id such as 'GMT-08:00', but configured Zone ID is 'PST'.");
configByConfiguration.getLocalTimeZone();
CONFIG_BY_CONFIGURATION.addConfiguration(configuration);
assertThatThrownBy(CONFIG_BY_CONFIGURATION::getLocalTimeZone)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"The supported Zone ID is either a full name such as 'America/Los_Angeles',"
+ " or a custom timezone id such as 'GMT-08:00', but configured Zone ID is 'PST'.");
}

@Test
public void testSetAndGetIdleStateRetention() {
configuration.setString("table.exec.state.ttl", "1 h");
configByConfiguration.addConfiguration(configuration);
configByMethod.setIdleStateRetention(Duration.ofHours(1));
CONFIG_BY_CONFIGURATION.addConfiguration(configuration);
CONFIG_BY_METHOD.setIdleStateRetention(Duration.ofHours(1));

assertThat(configByMethod.getIdleStateRetention()).isEqualTo(Duration.ofHours(1));
assertThat(configByConfiguration.getIdleStateRetention()).isEqualTo(Duration.ofHours(1));
assertThat(CONFIG_BY_METHOD.getIdleStateRetention()).isEqualTo(Duration.ofHours(1));
assertThat(CONFIG_BY_CONFIGURATION.getIdleStateRetention()).isEqualTo(Duration.ofHours(1));
}
}
@@ -23,9 +23,7 @@
import org.apache.flink.types.Row;
import org.apache.flink.types.RowKind;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.HashMap;
import java.util.List;
@@ -41,15 +39,14 @@
import static org.apache.flink.table.expressions.ApiExpressionUtils.objectToExpression;
import static org.apache.flink.table.expressions.ApiExpressionUtils.unwrapFromApi;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* Tests for converting an object to a {@link Expression} via {@link
* ApiExpressionUtils#objectToExpression(Object)}.
*/
public class ObjectToExpressionTest {

@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void testListConversion() {
Expression expr = objectToExpression(asList(1, 2));
@@ -96,29 +93,32 @@ public void testRowConversion() {

@Test
public void testRowWithDeleteKindConversion() {
thrown.expect(ValidationException.class);
thrown.expectMessage(
"Unsupported kind 'DELETE' of a row [-D[1]]. "
+ "Only rows with 'INSERT' kind are supported when converting to an expression.");
objectToExpression(Row.ofKind(RowKind.DELETE, 1));
assertThatThrownBy(() -> objectToExpression(Row.ofKind(RowKind.DELETE, 1)))
.isInstanceOf(ValidationException.class)
.hasMessage(
"Unsupported kind 'DELETE' of a row [-D[1]]. "
+ "Only rows with 'INSERT' kind are supported when converting "
+ "to an expression.");
}

@Test
public void testRowWithUpdateBeforeKindConversion() {
thrown.expect(ValidationException.class);
thrown.expectMessage(
"Unsupported kind 'UPDATE_BEFORE' of a row [-U[1]]. "
+ "Only rows with 'INSERT' kind are supported when converting to an expression.");
objectToExpression(Row.ofKind(RowKind.UPDATE_BEFORE, 1));
assertThatThrownBy(() -> objectToExpression(Row.ofKind(RowKind.UPDATE_BEFORE, 1)))
.isInstanceOf(ValidationException.class)
.hasMessage(
"Unsupported kind 'UPDATE_BEFORE' of a row [-U[1]]. "
+ "Only rows with 'INSERT' kind are supported when converting "
+ "to an expression.");
}

@Test
public void testRowWithUpdateAfterKindConversion() {
thrown.expect(ValidationException.class);
thrown.expectMessage(
"Unsupported kind 'UPDATE_AFTER' of a row [+U[1]]. "
+ "Only rows with 'INSERT' kind are supported when converting to an expression.");
objectToExpression(Row.ofKind(RowKind.UPDATE_AFTER, 1));
assertThatThrownBy(() -> objectToExpression(Row.ofKind(RowKind.UPDATE_AFTER, 1)))
.isInstanceOf(ValidationException.class)
.hasMessage(
"Unsupported kind 'UPDATE_AFTER' of a row [+U[1]]. "
+ "Only rows with 'INSERT' kind are supported when converting "
+ "to an expression.");
}

private static void assertThatEquals(Expression actual, Expression expected) {

0 comments on commit 2ade17d

Please sign in to comment.