|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.cdc.connectors.mysql.source.config; |
| 19 | + |
| 20 | +import io.debezium.relational.TableId; |
| 21 | +import io.debezium.relational.Tables; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import java.util.function.Predicate; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** Tests for {@link MySqlSourceConfig}. */ |
| 30 | +class MySqlSourceConfigTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void testCachesTableFilterResults() { |
| 34 | + AtomicInteger filterInvocationCount = new AtomicInteger(); |
| 35 | + Tables.TableFilter cachedTableFilter = |
| 36 | + MySqlSourceConfig.createCachedTableFilter( |
| 37 | + tableId -> { |
| 38 | + filterInvocationCount.incrementAndGet(); |
| 39 | + return tableId.table().startsWith("orders"); |
| 40 | + }, |
| 41 | + null); |
| 42 | + |
| 43 | + TableId includedTable = new TableId("test_db", null, "orders_1"); |
| 44 | + TableId unmatchedTable = new TableId("test_db", null, "customers"); |
| 45 | + |
| 46 | + assertThat(cachedTableFilter.isIncluded(includedTable)).isTrue(); |
| 47 | + assertThat(cachedTableFilter.isIncluded(includedTable)).isTrue(); |
| 48 | + assertThat(cachedTableFilter.isIncluded(unmatchedTable)).isFalse(); |
| 49 | + assertThat(cachedTableFilter.isIncluded(unmatchedTable)).isFalse(); |
| 50 | + assertThat(filterInvocationCount).hasValue(2); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void testTableFilterWithExcludeTableList() { |
| 55 | + MySqlSourceConfig config = |
| 56 | + new MySqlSourceConfigFactory() |
| 57 | + .hostname("localhost") |
| 58 | + .username("user") |
| 59 | + .password("password") |
| 60 | + .databaseList("test_db") |
| 61 | + .tableList("test_db\\.orders_.*") |
| 62 | + .excludeTableList("test_db.orders_skip") |
| 63 | + .createConfig(0); |
| 64 | + |
| 65 | + Predicate<TableId> tableFilter = config.getTableFilter(); |
| 66 | + TableId includedTable = new TableId("test_db", null, "orders_1"); |
| 67 | + TableId excludedTable = new TableId("test_db", null, "orders_skip"); |
| 68 | + TableId unmatchedTable = new TableId("test_db", null, "customers"); |
| 69 | + |
| 70 | + assertThat(tableFilter.test(includedTable)).isTrue(); |
| 71 | + assertThat(tableFilter.test(excludedTable)).isFalse(); |
| 72 | + assertThat(tableFilter.test(unmatchedTable)).isFalse(); |
| 73 | + } |
| 74 | +} |
0 commit comments