You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2023/03/04 23:02:51 UTC
[shardingsphere] branch master updated: Refactor @Test(expected) to assert assertThrows on sharding modules (#24461)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 652f3e481c6 Refactor @Test(expected) to assert assertThrows on sharding modules (#24461)
652f3e481c6 is described below
commit 652f3e481c6456bcc3fb1e21de78d73b681ac05f
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Mar 5 07:02:36 2023 +0800
Refactor @Test(expected) to assert assertThrows on sharding modules (#24461)
---
...ardingConditionsShardingAuditAlgorithmTest.java | 5 +-
.../keygen/SnowflakeKeyGenerateAlgorithmTest.java | 32 +++++-----
.../ClassBasedShardingAlgorithmFactoryTest.java | 6 +-
.../ClassBasedShardingAlgorithmTest.java | 28 +++++----
.../inline/InlineShardingAlgorithmTest.java | 6 +-
.../sharding/mod/ModShardingAlgorithmTest.java | 11 ++--
.../ShardingRuleConfigurationCheckerTest.java | 5 +-
.../InsertClauseShardingConditionEngineTest.java | 5 +-
...ConditionValueBetweenOperatorGeneratorTest.java | 5 +-
.../complex/ShardingComplexRoutingEngineTest.java | 5 +-
.../ShardingStandardRoutingEngineTest.java | 5 +-
.../unicast/ShardingUnicastRoutingEngineTest.java | 12 ++--
.../ShardingAlterIndexStatementValidatorTest.java | 13 ++--
.../ShardingAlterTableStatementValidatorTest.java | 25 ++++----
.../ShardingAlterViewStatementValidatorTest.java | 11 ++--
...ardingCreateFunctionStatementValidatorTest.java | 20 ++++---
.../ShardingCreateIndexStatementValidatorTest.java | 25 +++++---
...rdingCreateProcedureStatementValidatorTest.java | 20 ++++---
.../ShardingCreateTableStatementValidatorTest.java | 33 ++++++-----
.../ShardingCreateViewStatementValidatorTest.java | 18 +++---
.../ShardingDropIndexStatementValidatorTest.java | 21 ++++---
.../ShardingDropTableStatementValidatorTest.java | 24 ++++----
.../ddl/ShardingPrepareStatementValidatorTest.java | 14 +++--
.../ShardingRenameTableStatementValidatorTest.java | 16 +++--
.../dml/ShardingCopyStatementValidatorTest.java | 9 +--
.../dml/ShardingDeleteStatementValidatorTest.java | 21 +++----
.../dml/ShardingInsertStatementValidatorTest.java | 31 +++++-----
.../dml/ShardingUpdateStatementValidatorTest.java | 24 ++++----
.../sharding/rule/BindingTableRuleTest.java | 9 +--
.../sharding/rule/ShardingRuleTest.java | 29 ++++-----
.../sharding/rule/TableRuleTest.java | 5 +-
.../rule/builder/ShardingRuleBuilderTest.java | 11 ++--
...rdingAutoTableRuleConfigurationSwapperTest.java | 5 +-
...lShardingTableRuleConfigurationSwapperTest.java | 5 +-
.../checker/ShardingRuleStatementCheckerTest.java | 69 +++++++++++-----------
...efaultShardingStrategyStatementUpdaterTest.java | 18 +++---
...dingTableReferenceRuleStatementUpdaterTest.java | 16 +++--
...eateBroadcastTableRuleStatementUpdaterTest.java | 5 +-
...efaultShardingStrategyStatementUpdaterTest.java | 30 +++++-----
...dingTableReferenceRuleStatementUpdaterTest.java | 9 +--
...reateShardingTableRuleStatementUpdaterTest.java | 5 +-
...DropBroadcastTableRuleStatementUpdaterTest.java | 20 +++----
...efaultShardingStrategyStatementUpdaterTest.java | 11 ++--
.../DropShardingAlgorithmStatementUpdaterTest.java | 17 +++---
.../DropShardingAuditorStatementUpdaterTest.java | 11 ++--
...opShardingKeyGeneratorStatementUpdaterTest.java | 11 ++--
...dingTableReferenceRuleStatementUpdaterTest.java | 31 ++++------
.../DropShardingTableRuleStatementUpdaterTest.java | 22 ++++---
.../rule/builder/ShardingCacheRuleBuilderTest.java | 5 +-
.../keygen/CosIdKeyGenerateAlgorithmTest.java | 5 +-
.../CosIdSnowflakeKeyGenerateAlgorithmTest.java | 19 +++---
51 files changed, 450 insertions(+), 368 deletions(-)
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/audit/DMLShardingConditionsShardingAuditAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/audit/DMLShardingConditionsShardingAuditAlgorithmTest.java
index 55d6e7d9cb6..37185986e3e 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/audit/DMLShardingConditionsShardingAuditAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/audit/DMLShardingConditionsShardingAuditAlgorithmTest.java
@@ -32,6 +32,7 @@ import org.junit.Test;
import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -81,12 +82,12 @@ public final class DMLShardingConditionsShardingAuditAlgorithmTest {
shardingAuditAlgorithm.check(sqlStatementContext, Collections.emptyList(), mock(Grantee.class), mock(ShardingSphereRuleMetaData.class), database);
}
- @Test(expected = SQLAuditException.class)
+ @Test
public void assertEmptyShardingConditionsCheck() {
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DMLStatement.class));
when(database.getRuleMetaData()).thenReturn(new ShardingSphereRuleMetaData(Collections.singletonList(rule)));
when(rule.isAllBroadcastTables(sqlStatementContext.getTablesContext().getTableNames())).thenReturn(false);
when(rule.isShardingTable("t_order")).thenReturn(true);
- shardingAuditAlgorithm.check(sqlStatementContext, Collections.emptyList(), mock(Grantee.class), mock(ShardingSphereRuleMetaData.class), database);
+ assertThrows(SQLAuditException.class, () -> shardingAuditAlgorithm.check(sqlStatementContext, Collections.emptyList(), mock(Grantee.class), mock(ShardingSphereRuleMetaData.class), database));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
index 7dcabf7160f..f7e73bbc129 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
@@ -49,8 +49,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -153,7 +153,7 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
assertThat(actual, is(expected));
}
- @Test(expected = SnowflakeClockMoveBackException.class)
+ @Test
public void assertGenerateKeyWithClockCallBackBeyondTolerateTime() {
TimeService timeService = new FixedTimeService(1);
SnowflakeKeyGenerateAlgorithm.setTimeService(timeService);
@@ -162,11 +162,13 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
((InstanceContextAware) algorithm).setInstanceContext(INSTANCE);
}
setLastMilliseconds(algorithm, timeService.getCurrentMillis() + 2);
- List<Comparable<?>> actual = new ArrayList<>(DEFAULT_KEY_AMOUNT);
+ assertThrows(SnowflakeClockMoveBackException.class, () -> batchGenerate(algorithm));
+ }
+
+ private static void batchGenerate(final KeyGenerateAlgorithm algorithm) {
for (int i = 0; i < DEFAULT_KEY_AMOUNT; i++) {
- actual.add(algorithm.generateKey());
+ algorithm.generateKey();
}
- assertThat(actual.size(), not(10));
}
@Test
@@ -197,32 +199,32 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
Plugins.getMemberAccessor().set(SnowflakeKeyGenerateAlgorithm.class.getDeclaredField("sequence"), algorithm, value);
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void assertSetWorkerIdFailureWhenNegative() {
SnowflakeKeyGenerateAlgorithm algorithm = (SnowflakeKeyGenerateAlgorithm) TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE");
InstanceContext instanceContext = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(-1),
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
- algorithm.setInstanceContext(instanceContext);
- algorithm.generateKey();
+ assertThrows(IllegalArgumentException.class, () -> algorithm.setInstanceContext(instanceContext));
}
- @Test(expected = KeyGenerateAlgorithmInitializationException.class)
+ @Test
public void assertSetMaxVibrationOffsetFailureWhenNegative() {
- TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE", PropertiesBuilder.build(new Property("max-vibration-offset", "-1"))).generateKey();
+ assertThrows(KeyGenerateAlgorithmInitializationException.class,
+ () -> TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE", PropertiesBuilder.build(new Property("max-vibration-offset", "-1"))).generateKey());
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void assertSetWorkerIdFailureWhenOutOfRange() {
SnowflakeKeyGenerateAlgorithm algorithm = (SnowflakeKeyGenerateAlgorithm) TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE");
InstanceContext instanceContext = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE),
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
- algorithm.setInstanceContext(instanceContext);
- algorithm.generateKey();
+ assertThrows(IllegalArgumentException.class, () -> algorithm.setInstanceContext(instanceContext));
}
- @Test(expected = KeyGenerateAlgorithmInitializationException.class)
+ @Test
public void assertSetMaxVibrationOffsetFailureWhenOutOfRange() {
- TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE", PropertiesBuilder.build(new Property("max-vibration-offset", "4096"))).generateKey();
+ assertThrows(KeyGenerateAlgorithmInitializationException.class,
+ () -> TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE", PropertiesBuilder.build(new Property("max-vibration-offset", "4096"))).generateKey());
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java
index 950d25f473d..8e4aea1a2bb 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java
@@ -27,12 +27,14 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public final class ClassBasedShardingAlgorithmFactoryTest {
- @Test(expected = ShardingAlgorithmClassImplementationException.class)
+ @Test
public void assertNewInstanceWithUnAssignableFrom() {
- ClassBasedShardingAlgorithmFactory.newInstance(ClassBasedHintShardingAlgorithmFixture.class.getName(), StandardShardingAlgorithm.class, new Properties());
+ assertThrows(ShardingAlgorithmClassImplementationException.class,
+ () -> ClassBasedShardingAlgorithmFactory.newInstance(ClassBasedHintShardingAlgorithmFixture.class.getName(), StandardShardingAlgorithm.class, new Properties()));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
index 03c2eec6175..43a170b23c9 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
@@ -39,34 +39,38 @@ import java.util.Collection;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public final class ClassBasedShardingAlgorithmTest {
- @Test(expected = ShardingAlgorithmInitializationException.class)
+ @Test
public void assertInitWithNullStrategy() {
- TypedSPILoader.getService(ShardingAlgorithm.class, "CLASS_BASED");
+ assertThrows(ShardingAlgorithmInitializationException.class, () -> TypedSPILoader.getService(ShardingAlgorithm.class, "CLASS_BASED"));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void assertInitWithWrongStrategy() {
- TypedSPILoader.getService(ShardingAlgorithm.class, "CLASS_BASED", PropertiesBuilder.build(new Property("strategy", "wrong")));
+ assertThrows(IllegalArgumentException.class, () -> TypedSPILoader.getService(ShardingAlgorithm.class, "CLASS_BASED", PropertiesBuilder.build(new Property("strategy", "wrong"))));
}
- @Test(expected = ShardingAlgorithmInitializationException.class)
+ @Test
public void assertInitWithNullClass() {
- TypedSPILoader.getService(ShardingAlgorithm.class, "CLASS_BASED", PropertiesBuilder.build(new Property("strategy", "standard")));
+ assertThrows(ShardingAlgorithmInitializationException.class,
+ () -> TypedSPILoader.getService(ShardingAlgorithm.class, "CLASS_BASED", PropertiesBuilder.build(new Property("strategy", "standard"))));
}
- @Test(expected = ClassNotFoundException.class)
+ @Test
public void assertInitWithUndefinedClass() {
- TypedSPILoader.getService(ShardingAlgorithm.class,
- "CLASS_BASED", PropertiesBuilder.build(new Property("strategy", "standard"), new Property("algorithmClassName", "org.apache.shardingsphere.sharding.UndefinedClass")));
+ assertThrows(ClassNotFoundException.class,
+ () -> TypedSPILoader.getService(ShardingAlgorithm.class,
+ "CLASS_BASED", PropertiesBuilder.build(new Property("strategy", "standard"), new Property("algorithmClassName", "org.apache.shardingsphere.sharding.UndefinedClass"))));
}
- @Test(expected = ShardingAlgorithmClassImplementationException.class)
+ @Test
public void assertInitWithMismatchStrategy() {
- TypedSPILoader.getService(ShardingAlgorithm.class,
- "CLASS_BASED", PropertiesBuilder.build(new Property("strategy", "standard"), new Property("algorithmClassName", ClassBasedComplexKeysShardingAlgorithmFixture.class.getName())));
+ assertThrows(ShardingAlgorithmClassImplementationException.class,
+ () -> TypedSPILoader.getService(ShardingAlgorithm.class, "CLASS_BASED",
+ PropertiesBuilder.build(new Property("strategy", "standard"), new Property("algorithmClassName", ClassBasedComplexKeysShardingAlgorithmFixture.class.getName()))));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/inline/InlineShardingAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/inline/InlineShardingAlgorithmTest.java
index 4fd2223acd7..11aefe9eac9 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/inline/InlineShardingAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/inline/InlineShardingAlgorithmTest.java
@@ -38,6 +38,7 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
public final class InlineShardingAlgorithmTest {
@@ -60,11 +61,12 @@ public final class InlineShardingAlgorithmTest {
PropertiesBuilder.build(new Property("algorithm-expression", "t_order_$->{(order_id % 4).abs()}"), new Property("allow-range-query-with-inline-sharding", Boolean.TRUE.toString())));
}
- @Test(expected = MismatchedInlineShardingAlgorithmExpressionAndColumnException.class)
+ @Test
public void assertDoSharding() {
List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
assertThat(inlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, 0)), is("t_order_0"));
- inlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "non_existent_column1", DATA_NODE_INFO, 0));
+ assertThrows(MismatchedInlineShardingAlgorithmExpressionAndColumnException.class,
+ () -> inlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "non_existent_column1", DATA_NODE_INFO, 0)));
}
@SuppressWarnings({"unchecked", "rawtypes"})
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/mod/ModShardingAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/mod/ModShardingAlgorithmTest.java
index 471b646e4e6..46966acd21b 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/mod/ModShardingAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/mod/ModShardingAlgorithmTest.java
@@ -35,6 +35,7 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public final class ModShardingAlgorithmTest {
@@ -88,20 +89,18 @@ public final class ModShardingAlgorithmTest {
assertThat(actual.size(), is(16));
}
- @Test(expected = ShardingAlgorithmInitializationException.class)
+ @Test
public void assertRangeDoShardingWithWrongArgumentForStartOffset() {
Properties props = createZeroPaddingProperties();
props.setProperty("start-offset", "-1");
- ModShardingAlgorithm algorithm = (ModShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "MOD", props);
- assertThat(algorithm.doSharding(createAvailableIncludeZeroTargetNames(), new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, "12345678910111213141516")), is("t_order_07"));
+ assertThrows(ShardingAlgorithmInitializationException.class, () -> TypedSPILoader.getService(ShardingAlgorithm.class, "MOD", props));
}
- @Test(expected = ShardingAlgorithmInitializationException.class)
+ @Test
public void assertRangeDoShardingWithWrongArgumentForStopOffset() {
Properties props = createZeroPaddingProperties();
props.setProperty("stop-offset", "-1");
- ModShardingAlgorithm algorithm = (ModShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "MOD", props);
- assertThat(algorithm.doSharding(createAvailableIncludeZeroTargetNames(), new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, "12345678910111213141516")), is("t_order_07"));
+ assertThrows(ShardingAlgorithmInitializationException.class, () -> TypedSPILoader.getService(ShardingAlgorithm.class, "MOD", props));
}
private Properties createZeroPaddingProperties() {
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
index 01f1257bfd5..7be702aa3c6 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
@@ -31,6 +31,7 @@ import org.junit.Test;
import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -50,14 +51,14 @@ public final class ShardingRuleConfigurationCheckerTest {
}
@SuppressWarnings("unchecked")
- @Test(expected = MissingRequiredShardingConfigurationException.class)
+ @Test
public void assertCheckTableConfigurationFailed() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ruleConfig.setTables(Collections.singletonList(createShardingTableRuleConfiguration(null, null, null)));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(null, null, null)));
RuleConfigurationChecker<ShardingRuleConfiguration> checker = OrderedSPILoader.getServicesByClass(
RuleConfigurationChecker.class, Collections.singleton(ruleConfig.getClass())).get(ruleConfig.getClass());
- checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList());
+ assertThrows(MissingRequiredShardingConfigurationException.class, () -> checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList()));
}
private ShardingRuleConfiguration createRuleConfiguration() {
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
index d8c48dcabac..a7f60d5bd68 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
@@ -51,6 +51,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -113,13 +114,13 @@ public final class InsertClauseShardingConditionEngineTest {
assertThat(shardingConditions.get(0).getValues().size(), is(1));
}
- @Test(expected = InsertColumnsAndValuesMismatchedException.class)
+ @Test
public void assertCreateShardingConditionsInsertStatementWithMismatchColumns() {
InsertValueContext insertValueContext = new InsertValueContext(Arrays.asList(new LiteralExpressionSegment(0, 10, "1"), new LiteralExpressionSegment(0, 10, "1")), Collections.emptyList(), 0);
when(insertStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(insertValueContext));
when(shardingRule.findShardingColumn(any(), any())).thenReturn(Optional.of("foo_sharding_col"));
when(insertStatementContext.getColumnNames()).thenReturn(Collections.singletonList("foo_col1"));
- shardingConditionEngine.createShardingConditions(insertStatementContext, Collections.emptyList());
+ assertThrows(InsertColumnsAndValuesMismatchedException.class, () -> shardingConditionEngine.createShardingConditions(insertStatementContext, Collections.emptyList()));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
index 2f869f20fe2..96eae7c09ad 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
@@ -47,6 +47,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
@RunWith(MockitoJUnitRunner.class)
public final class ConditionValueBetweenOperatorGeneratorTest {
@@ -93,13 +94,13 @@ public final class ConditionValueBetweenOperatorGeneratorTest {
assertTrue(rangeShardingConditionValue.getParameterMarkerIndexes().isEmpty());
}
- @Test(expected = ClassCastException.class)
+ @Test
public void assertGenerateErrorConditionValue() {
int between = 1;
ExpressionSegment betweenSegment = new LiteralExpressionSegment(0, 0, between);
ExpressionSegment andSegment = new CommonExpressionSegment(0, 0, "now()");
BetweenExpression value = new BetweenExpression(0, 0, null, betweenSegment, andSegment, false);
- generator.generate(value, column, new LinkedList<>(), timeServiceRule);
+ assertThrows(ClassCastException.class, () -> generator.generate(value, column, new LinkedList<>(), timeServiceRule));
}
@SuppressWarnings("unchecked")
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java
index 396eac55b42..7959ed1a071 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java
@@ -35,6 +35,7 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
public final class ShardingComplexRoutingEngineTest extends AbstractRoutingEngineTest {
@@ -65,10 +66,10 @@ public final class ShardingComplexRoutingEngineTest extends AbstractRoutingEngin
assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_order"));
}
- @Test(expected = ShardingTableRuleNotFoundException.class)
+ @Test
public void assertRoutingForNonLogicTable() {
ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(
createShardingConditions("t_order"), mock(SQLStatementContext.class), new HintValueContext(), new ConfigurationProperties(new Properties()), Collections.emptyList());
- complexRoutingEngine.route(mock(ShardingRule.class));
+ assertThrows(ShardingTableRuleNotFoundException.class, () -> complexRoutingEngine.route(mock(ShardingRule.class)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java
index a87767d40e2..d99088291e6 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java
@@ -37,6 +37,7 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -86,11 +87,11 @@ public final class ShardingStandardRoutingEngineTest extends AbstractRoutingEngi
assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_order"));
}
- @Test(expected = ShardingRouteAlgorithmException.class)
+ @Test
public void assertRouteByErrorShardingTableStrategy() {
ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_order", createErrorShardingConditions("t_order"),
mock(SQLStatementContext.class), new HintValueContext());
- standardRoutingEngine.route(createErrorShardingRule());
+ assertThrows(ShardingRouteAlgorithmException.class, () -> standardRoutingEngine.route(createErrorShardingRule()));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java
index bb03f8c0ada..561d88ae34a 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java
@@ -36,8 +36,9 @@ import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
public final class ShardingUnicastRoutingEngineTest {
@@ -80,13 +81,10 @@ public final class ShardingUnicastRoutingEngineTest {
assertThat(actual.getRouteUnits().size(), is(1));
}
- @Test(expected = ShardingTableRuleNotFoundException.class)
+ @Test
public void assertRouteForWithNoIntersection() {
- Set<String> tables = new HashSet<>(3, 1);
- tables.add("t_order");
- tables.add("t_config");
- tables.add("t_product");
- new ShardingUnicastRoutingEngine(mock(SQLStatementContext.class), tables, new ConnectionContext()).route(shardingRule);
+ assertThrows(ShardingTableRuleNotFoundException.class,
+ () -> new ShardingUnicastRoutingEngine(mock(SQLStatementContext.class), Arrays.asList("t_order", "t_config", "t_product"), new ConnectionContext()).route(shardingRule));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java
index cd2c41a80f0..416df3a8bef 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java
@@ -40,6 +40,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -67,7 +68,7 @@ public final class ShardingAlterIndexStatementValidatorTest {
new ShardingAlterIndexStatementValidator().preValidate(shardingRule, new AlterIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = IndexNotExistedException.class)
+ @Test
public void assertPreValidateAlterIndexWhenIndexNotExistRenameIndexNotExistForPostgreSQL() {
PostgreSQLAlterIndexStatement sqlStatement = new PostgreSQLAlterIndexStatement();
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
@@ -78,10 +79,12 @@ public final class ShardingAlterIndexStatementValidatorTest {
when(database.getSchema("public").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
when(indexes.containsKey("t_order_index")).thenReturn(false);
- new ShardingAlterIndexStatementValidator().preValidate(shardingRule, new AlterIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(IndexNotExistedException.class,
+ () -> new ShardingAlterIndexStatementValidator().preValidate(
+ shardingRule, new AlterIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = DuplicatedIndexException.class)
+ @Test
public void assertPreValidateAlterIndexWhenIndexExistRenameIndexExistForPostgreSQL() {
PostgreSQLAlterIndexStatement sqlStatement = new PostgreSQLAlterIndexStatement();
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
@@ -93,6 +96,8 @@ public final class ShardingAlterIndexStatementValidatorTest {
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
when(indexes.containsKey("t_order_index")).thenReturn(true);
when(indexes.containsKey("t_order_index_new")).thenReturn(true);
- new ShardingAlterIndexStatementValidator().preValidate(shardingRule, new AlterIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(DuplicatedIndexException.class,
+ () -> new ShardingAlterIndexStatementValidator().preValidate(
+ shardingRule, new AlterIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java
index e7f20d2e3f0..55dec557cbf 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java
@@ -46,6 +46,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -61,17 +62,18 @@ public final class ShardingAlterTableStatementValidatorTest {
@Mock
private RouteContext routeContext;
- @Test(expected = UnsupportedShardingOperationException.class)
+ @Test
public void assertPreValidateAlterTableWithRenameTableWithShardingTableForPostgreSQL() {
PostgreSQLAlterTableStatement sqlStatement = new PostgreSQLAlterTableStatement();
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new"))));
SQLStatementContext<AlterTableStatement> sqlStatementContext = new AlterTableStatementContext(sqlStatement);
when(shardingRule.tableRuleExists(Arrays.asList("t_order", "t_order_new"))).thenReturn(true);
- new ShardingAlterTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(UnsupportedShardingOperationException.class,
+ () -> new ShardingAlterTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = UnsupportedShardingOperationException.class)
+ @Test
public void assertPreValidateAlterTableWithRenameTableWithBroadcastTableForPostgreSQL() {
PostgreSQLAlterTableStatement sqlStatement = new PostgreSQLAlterTableStatement();
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -79,7 +81,8 @@ public final class ShardingAlterTableStatementValidatorTest {
SQLStatementContext<AlterTableStatement> sqlStatementContext = new AlterTableStatementContext(sqlStatement);
when(shardingRule.tableRuleExists(Arrays.asList("t_order", "t_order_new"))).thenReturn(false);
when(shardingRule.isBroadcastTable("t_order")).thenReturn(true);
- new ShardingAlterTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(UnsupportedShardingOperationException.class,
+ () -> new ShardingAlterTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
@Test
@@ -96,7 +99,7 @@ public final class ShardingAlterTableStatementValidatorTest {
new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateAlterTableWithDifferentRouteResultShardingTableForPostgreSQL() {
PostgreSQLAlterTableStatement sqlStatement = new PostgreSQLAlterTableStatement();
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -105,8 +108,9 @@ public final class ShardingAlterTableStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingAlterTableStatementValidator().postValidate(shardingRule, new AlterTableStatementContext(sqlStatement),
- new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class,
+ () -> new ShardingAlterTableStatementValidator().postValidate(shardingRule, new AlterTableStatementContext(sqlStatement),
+ new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
@Test
@@ -123,7 +127,7 @@ public final class ShardingAlterTableStatementValidatorTest {
new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateAlterTableWithDifferentRouteResultBroadcastTableForPostgreSQL() {
PostgreSQLAlterTableStatement sqlStatement = new PostgreSQLAlterTableStatement();
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_config"))));
@@ -132,7 +136,8 @@ public final class ShardingAlterTableStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingAlterTableStatementValidator().postValidate(shardingRule, new AlterTableStatementContext(sqlStatement),
- new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class,
+ () -> new ShardingAlterTableStatementValidator().postValidate(shardingRule, new AlterTableStatementContext(sqlStatement),
+ new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
index de48898f4f7..45ba90f9053 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
@@ -39,6 +39,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -60,7 +61,7 @@ public final class ShardingAlterViewStatementValidatorTest {
new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = UnsupportedShardingOperationException.class)
+ @Test
public void assertPreValidateAlterViewWithShardingTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -69,7 +70,8 @@ public final class ShardingAlterViewStatementValidatorTest {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
SQLStatementContext<AlterViewStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
- new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(UnsupportedShardingOperationException.class,
+ () -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
@Test
@@ -84,7 +86,7 @@ public final class ShardingAlterViewStatementValidatorTest {
new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = RenamedViewWithoutSameConfigurationException.class)
+ @Test
public void assertPreValidateAlterRenamedViewWithoutSameConfig() {
OpenGaussAlterViewStatement selectStatement = new OpenGaussAlterViewStatement();
selectStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -93,6 +95,7 @@ public final class ShardingAlterViewStatementValidatorTest {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
when(shardingRule.isBroadcastTable("t_order")).thenReturn(true);
when(shardingRule.isBroadcastTable("t_order_new")).thenReturn(false);
- new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(RenamedViewWithoutSameConfigurationException.class,
+ () -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java
index ce40d2f6f0e..9d0a7c090c8 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java
@@ -17,12 +17,12 @@
package org.apache.shardingsphere.sharding.route.engine.validator.ddl;
+import org.apache.shardingsphere.dialect.exception.syntax.table.NoSuchTableException;
+import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
-import org.apache.shardingsphere.dialect.exception.syntax.table.NoSuchTableException;
-import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateFunctionStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -42,6 +42,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -74,7 +75,7 @@ public final class ShardingCreateFunctionStatementValidatorTest {
new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = NoSuchTableException.class)
+ @Test
public void assertPreValidateCreateFunctionWithShardingTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -87,10 +88,11 @@ public final class ShardingCreateFunctionStatementValidatorTest {
SQLStatementContext<CreateFunctionStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("db_schema");
- new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = NoSuchTableException.class)
+ @Test
public void assertPreValidateCreateFunctionWithNoSuchTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -103,10 +105,11 @@ public final class ShardingCreateFunctionStatementValidatorTest {
SQLStatementContext<CreateFunctionStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("db_schema");
- new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = TableExistsException.class)
+ @Test
public void assertPreValidateCreateFunctionWithTableExistsForMySQL() {
MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement(false);
createTableStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -120,6 +123,7 @@ public final class ShardingCreateFunctionStatementValidatorTest {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order")).thenReturn(true);
- new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(TableExistsException.class,
+ () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java
index 3b7354ad76d..f125621d3e0 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java
@@ -40,6 +40,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -63,16 +64,18 @@ public final class ShardingCreateIndexStatementValidatorTest {
new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = NoSuchTableException.class)
+ @Test
public void assertPreValidateCreateIndexWhenTableNotExistIndexNotExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
when(database.getSchema("public").containsTable("t_order")).thenReturn(false);
- new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateIndexStatementValidator().preValidate(
+ shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = DuplicatedIndexException.class)
+ @Test
public void assertPreValidateCreateIndexWhenTableExistIndexExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -81,7 +84,9 @@ public final class ShardingCreateIndexStatementValidatorTest {
ShardingSphereTable table = mock(ShardingSphereTable.class);
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
when(database.getSchema("public").containsIndex("t_order", "t_order_index")).thenReturn(true);
- new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(DuplicatedIndexException.class,
+ () -> new ShardingCreateIndexStatementValidator().preValidate(
+ shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
@Test
@@ -96,17 +101,19 @@ public final class ShardingCreateIndexStatementValidatorTest {
new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = NoSuchTableException.class)
+ @Test
public void assertPreValidateCreateIndexWithoutIndexNameWhenTableNotExistIndexNotExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.getColumns().add(new ColumnSegment(0, 0, new IdentifierValue("content")));
sqlStatement.setGeneratedIndexStartIndex(10);
when(database.getSchema("public").containsTable("t_order")).thenReturn(false);
- new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateIndexStatementValidator().preValidate(
+ shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = DuplicatedIndexException.class)
+ @Test
public void assertPreValidateCreateIndexWithoutIndexNameWhenTableExistIndexExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -116,6 +123,8 @@ public final class ShardingCreateIndexStatementValidatorTest {
ShardingSphereTable table = mock(ShardingSphereTable.class);
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
when(database.getSchema("public").containsIndex("t_order", "content_idx")).thenReturn(true);
- new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(DuplicatedIndexException.class,
+ () -> new ShardingCreateIndexStatementValidator().preValidate(
+ shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java
index a690a66b777..50bbb00fc00 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java
@@ -17,12 +17,12 @@
package org.apache.shardingsphere.sharding.route.engine.validator.ddl;
+import org.apache.shardingsphere.dialect.exception.syntax.table.NoSuchTableException;
+import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
-import org.apache.shardingsphere.dialect.exception.syntax.table.NoSuchTableException;
-import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateProcedureStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -42,6 +42,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -75,7 +76,7 @@ public final class ShardingCreateProcedureStatementValidatorTest {
new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = NoSuchTableException.class)
+ @Test
public void assertPreValidateCreateProcedureWithShardingTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -88,10 +89,11 @@ public final class ShardingCreateProcedureStatementValidatorTest {
SQLStatementContext<CreateProcedureStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("db_schema");
- new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = NoSuchTableException.class)
+ @Test
public void assertPreValidateCreateProcedureWithNoSuchTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -104,10 +106,11 @@ public final class ShardingCreateProcedureStatementValidatorTest {
SQLStatementContext<CreateProcedureStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("db_schema");
- new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = TableExistsException.class)
+ @Test
public void assertPreValidateCreateProcedureWithTableExistsForMySQL() {
MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement(false);
createTableStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -121,6 +124,7 @@ public final class ShardingCreateProcedureStatementValidatorTest {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order")).thenReturn(true);
- new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(TableExistsException.class,
+ () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java
index 4489eec9a22..afd31300d5a 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java
@@ -50,6 +50,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -66,39 +67,39 @@ public final class ShardingCreateTableStatementValidatorTest {
@Mock
private RouteContext routeContext;
- @Test(expected = TableExistsException.class)
+ @Test
public void assertPreValidateCreateTableForMySQL() {
MySQLCreateTableStatement sqlStatement = new MySQLCreateTableStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertPreValidateCreateTable(sqlStatement, "sharding_db");
+ assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "sharding_db"));
}
- @Test(expected = TableExistsException.class)
+ @Test
public void assertPreValidateCreateTableForOracle() {
OracleCreateTableStatement sqlStatement = new OracleCreateTableStatement();
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertPreValidateCreateTable(sqlStatement, "sharding_db");
+ assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "sharding_db"));
}
- @Test(expected = TableExistsException.class)
+ @Test
public void assertPreValidateCreateTableForPostgreSQL() {
PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertPreValidateCreateTable(sqlStatement, "public");
+ assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "public"));
}
- @Test(expected = TableExistsException.class)
+ @Test
public void assertPreValidateCreateTableForSQL92() {
SQL92CreateTableStatement sqlStatement = new SQL92CreateTableStatement();
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertPreValidateCreateTable(sqlStatement, "sharding_db");
+ assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "sharding_db"));
}
- @Test(expected = TableExistsException.class)
+ @Test
public void assertPreValidateCreateTableForSQLServer() {
SQLServerCreateTableStatement sqlStatement = new SQLServerCreateTableStatement();
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertPreValidateCreateTable(sqlStatement, "sharding_db");
+ assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "sharding_db"));
}
private void assertPreValidateCreateTable(final CreateTableStatement sqlStatement, final String schemaName) {
@@ -143,7 +144,7 @@ public final class ShardingCreateTableStatementValidatorTest {
new CreateTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateCreateTableWithDifferentRouteResultShardingTableForPostgreSQL() {
PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -152,8 +153,8 @@ public final class ShardingCreateTableStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingCreateTableStatementValidator().postValidate(shardingRule,
- new CreateTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class, () -> new ShardingCreateTableStatementValidator().postValidate(shardingRule,
+ new CreateTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
@Test
@@ -170,7 +171,7 @@ public final class ShardingCreateTableStatementValidatorTest {
new CreateTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateCreateTableWithDifferentRouteResultBroadcastTableForPostgreSQL() {
PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_config"))));
@@ -179,7 +180,7 @@ public final class ShardingCreateTableStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingCreateTableStatementValidator().postValidate(shardingRule,
- new CreateTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class, () -> new ShardingCreateTableStatementValidator().postValidate(shardingRule,
+ new CreateTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
index 38dc399844a..3bf3bf233f0 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
@@ -42,6 +42,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -79,13 +80,14 @@ public final class ShardingCreateViewStatementValidatorTest {
mock(ConfigurationProperties.class));
}
- @Test(expected = EngagedViewException.class)
+ @Test
public void assertPreValidateCreateViewWithException() {
when(shardingRule.isShardingTable(any())).thenReturn(true);
when(shardingRule.isAllBindingTables(any())).thenReturn(false);
ConfigurationProperties props = mock(ConfigurationProperties.class);
when(props.getValue(ConfigurationPropertyKey.SQL_FEDERATION_TYPE)).thenReturn("NONE");
- new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), props);
+ assertThrows(EngagedViewException.class,
+ () -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), props));
}
@Test
@@ -96,21 +98,23 @@ public final class ShardingCreateViewStatementValidatorTest {
mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = UnsupportedCreateViewException.class)
+ @Test
public void assertPostValidateCreateViewWithException() {
ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
when(projectionsSegment.isDistinctRow()).thenReturn(true);
when(selectStatement.getProjections()).thenReturn(projectionsSegment);
- new ShardingCreateViewStatementValidator().postValidate(
- shardingRule, createViewStatementContext, new HintValueContext(), Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class), routeContext);
+ assertThrows(UnsupportedCreateViewException.class,
+ () -> new ShardingCreateViewStatementValidator().postValidate(shardingRule,
+ createViewStatementContext, new HintValueContext(), Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class), routeContext));
}
- @Test(expected = EngagedViewException.class)
+ @Test
public void assertPreValidateCreateViewWithBroadcastTable() {
when(shardingRule.isAllBroadcastTables(any())).thenReturn(true);
when(shardingRule.isBroadcastTable("order_view")).thenReturn(false);
ConfigurationProperties props = mock(ConfigurationProperties.class);
when(props.getValue(ConfigurationPropertyKey.SQL_FEDERATION_TYPE)).thenReturn("NONE");
- new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), props);
+ assertThrows(EngagedViewException.class,
+ () -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), props));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java
index b3f32f198c6..ce5d9bce29e 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java
@@ -26,8 +26,8 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
+import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingDropIndexStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
@@ -48,6 +48,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -78,7 +79,7 @@ public final class ShardingDropIndexStatementValidatorTest {
new ShardingDropIndexStatementValidator().preValidate(shardingRule, new DropIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = IndexNotExistedException.class)
+ @Test
public void assertPreValidateDropIndexWhenIndexNotExistForPostgreSQL() {
PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
@@ -89,7 +90,9 @@ public final class ShardingDropIndexStatementValidatorTest {
when(database.getSchema("public").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
when(indexes.containsKey("t_order_index")).thenReturn(false);
- new ShardingDropIndexStatementValidator().preValidate(shardingRule, new DropIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(IndexNotExistedException.class,
+ () -> new ShardingDropIndexStatementValidator().preValidate(
+ shardingRule, new DropIndexStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
@Test
@@ -113,7 +116,7 @@ public final class ShardingDropIndexStatementValidatorTest {
Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateDropIndexWithDifferentRouteResultShardingTableIndexForPostgreSQL() {
PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
@@ -129,8 +132,8 @@ public final class ShardingDropIndexStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingDropIndexStatementValidator().postValidate(shardingRule, new DropIndexStatementContext(sqlStatement), new HintValueContext(),
- Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class, () -> new ShardingDropIndexStatementValidator().postValidate(shardingRule, new DropIndexStatementContext(sqlStatement), new HintValueContext(),
+ Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
@Test
@@ -154,7 +157,7 @@ public final class ShardingDropIndexStatementValidatorTest {
Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateDropIndexWithDifferentRouteResultBroadcastTableIndexForPostgreSQL() {
PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_config_index"))));
@@ -170,7 +173,7 @@ public final class ShardingDropIndexStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingDropIndexStatementValidator().postValidate(shardingRule,
- new DropIndexStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class, () -> new ShardingDropIndexStatementValidator().postValidate(shardingRule,
+ new DropIndexStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
index 6dfd653c90a..f8af834b767 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
@@ -27,8 +27,8 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.sharding.exception.metadata.DropInUsedTablesException;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
+import org.apache.shardingsphere.sharding.exception.metadata.DropInUsedTablesException;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingDropTableStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
@@ -53,6 +53,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -74,7 +75,7 @@ public final class ShardingDropTableStatementValidatorTest {
when(shardingRule.getTableRules()).thenReturn(tableRules);
}
- @Test(expected = DropInUsedTablesException.class)
+ @Test
public void assertPreValidateDropTableForMySQL() {
MySQLDropTableStatement sqlStatement = new MySQLDropTableStatement(false);
sqlStatement.getTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_item"))));
@@ -92,8 +93,9 @@ public final class ShardingDropTableStatementValidatorTest {
routeUnits.add(routeUnit);
RouteContext routeContext = mock(RouteContext.class);
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- validator.postValidate(shardingRule, new DropTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database,
- mock(ConfigurationProperties.class), routeContext);
+ assertThrows(DropInUsedTablesException.class,
+ () -> validator.postValidate(shardingRule, new DropTableStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database,
+ mock(ConfigurationProperties.class), routeContext));
}
private TableRule generateShardingRule(final String tableName) {
@@ -129,7 +131,7 @@ public final class ShardingDropTableStatementValidatorTest {
Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateDropTableWithDifferentRouteResultShardingTableForPostgreSQL() {
PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement(false, false);
sqlStatement.getTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
@@ -138,8 +140,9 @@ public final class ShardingDropTableStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingDropTableStatementValidator().postValidate(shardingRule, new DropTableStatementContext(sqlStatement), new HintValueContext(),
- Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class,
+ () -> new ShardingDropTableStatementValidator().postValidate(shardingRule, new DropTableStatementContext(sqlStatement), new HintValueContext(),
+ Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext));
}
@Test
@@ -156,7 +159,7 @@ public final class ShardingDropTableStatementValidatorTest {
Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateDropTableWithDifferentRouteResultBroadcastTableForPostgreSQL() {
PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement(false, false);
sqlStatement.getTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_config"))));
@@ -165,7 +168,8 @@ public final class ShardingDropTableStatementValidatorTest {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
- new ShardingDropTableStatementValidator().postValidate(shardingRule, new DropTableStatementContext(sqlStatement), new HintValueContext(),
- Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext);
+ assertThrows(ShardingDDLRouteException.class,
+ () -> new ShardingDropTableStatementValidator().postValidate(shardingRule, new DropTableStatementContext(sqlStatement), new HintValueContext(),
+ Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingPrepareStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingPrepareStatementValidatorTest.java
index f6443b1ff4d..30f64f726ea 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingPrepareStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingPrepareStatementValidatorTest.java
@@ -40,6 +40,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -55,12 +56,13 @@ public final class ShardingPrepareStatementValidatorTest {
@Mock
private RouteContext routeContext;
- @Test(expected = EmptyShardingRouteResultException.class)
+ @Test
public void assertPostValidatePrepareWithEmptyRouteResultForPostgreSQL() {
PrepareStatement sqlStatement = new PostgreSQLPrepareStatement();
when(routeContext.getRouteUnits()).thenReturn(Collections.emptyList());
- new ShardingPrepareStatementValidator().postValidate(shardingRule, new PrepareStatementContext(sqlStatement), new HintValueContext(),
- Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(EmptyShardingRouteResultException.class,
+ () -> new ShardingPrepareStatementValidator().postValidate(shardingRule, new PrepareStatementContext(sqlStatement), new HintValueContext(),
+ Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
@Test
@@ -74,7 +76,7 @@ public final class ShardingPrepareStatementValidatorTest {
Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = UnsupportedPrepareRouteToSameDataSourceException.class)
+ @Test
public void assertPostValidatePrepareWithSameDataSourceForPostgreSQL() {
Collection<RouteUnit> routeUnits = new LinkedList<>();
routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"),
@@ -83,7 +85,7 @@ public final class ShardingPrepareStatementValidatorTest {
Arrays.asList(new RouteMapper("t_order", "t_order_0"), new RouteMapper("t_order_item", "t_order_item_1"))));
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
PrepareStatement sqlStatement = new PostgreSQLPrepareStatement();
- new ShardingPrepareStatementValidator().postValidate(shardingRule, new PrepareStatementContext(sqlStatement), new HintValueContext(),
- Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext);
+ assertThrows(UnsupportedPrepareRouteToSameDataSourceException.class, () -> new ShardingPrepareStatementValidator().postValidate(
+ shardingRule, new PrepareStatementContext(sqlStatement), new HintValueContext(), Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java
index b8d169f62e4..fd92b11f5f9 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java
@@ -44,6 +44,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
+import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -55,20 +56,22 @@ public final class ShardingRenameTableStatementValidatorTest {
@Mock
private ShardingRule shardingRule;
- @Test(expected = UnsupportedShardingOperationException.class)
+ @Test
public void assertPreValidateShardingTable() {
SQLStatementContext<RenameTableStatement> sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
when(shardingRule.tableRuleExists(argThat(tableNames -> tableNames.contains("t_order") || tableNames.contains("t_user_order")))).thenReturn(true);
- new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(UnsupportedShardingOperationException.class,
+ () -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
- @Test(expected = UnsupportedShardingOperationException.class)
+ @Test
public void assertPreValidateBroadcastTable() {
SQLStatementContext<RenameTableStatement> sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
when(shardingRule.isBroadcastTable(eq("t_order"))).thenReturn(true);
- new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ assertThrows(UnsupportedShardingOperationException.class,
+ () -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)));
}
@Test
@@ -78,7 +81,7 @@ public final class ShardingRenameTableStatementValidatorTest {
new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = ShardingDDLRouteException.class)
+ @Test
public void assertPostValidateDifferentRouteUnitsAndDataNodesSize() {
RouteContext routeContext = new RouteContext();
routeContext.getRouteUnits().add(mock(RouteUnit.class));
@@ -89,7 +92,8 @@ public final class ShardingRenameTableStatementValidatorTest {
SQLStatementContext<RenameTableStatement> sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
ConfigurationProperties props = mock(ConfigurationProperties.class);
- new ShardingRenameTableStatementValidator().postValidate(shardingRule, sqlStatementContext, new HintValueContext(), Collections.emptyList(), database, props, routeContext);
+ assertThrows(ShardingDDLRouteException.class,
+ () -> new ShardingRenameTableStatementValidator().postValidate(shardingRule, sqlStatementContext, new HintValueContext(), Collections.emptyList(), database, props, routeContext));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java
index 87a6f3069f7..9b00712c1b2 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java
@@ -37,6 +37,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -63,14 +64,14 @@ public final class ShardingCopyStatementValidatorTest {
new ShardingCopyStatementValidator().preValidate(shardingRule, new CopyStatementContext(sqlStatement), Collections.emptyList(), database, mock(ConfigurationProperties.class));
}
- @Test(expected = UnsupportedShardingOperationException.class)
+ @Test
public void assertPreValidateCopyWithShardingTableForPostgreSQL() {
- assertPreValidateCopyTable(new PostgreSQLCopyStatement());
+ assertThrows(UnsupportedShardingOperationException.class, () -> assertPreValidateCopyTable(new PostgreSQLCopyStatement()));
}
- @Test(expected = UnsupportedShardingOperationException.class)
+ @Test
public void assertPreValidateCopyWithShardingTableForOpenGauss() {
- assertPreValidateCopyTable(new OpenGaussCopyStatement());
+ assertThrows(UnsupportedShardingOperationException.class, () -> assertPreValidateCopyTable(new OpenGaussCopyStatement()));
}
private void assertPreValidateCopyTable(final CopyStatement sqlStatement) {
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java
index 58eea06a610..dfd53650ddc 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java
@@ -43,6 +43,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -52,29 +53,29 @@ public final class ShardingDeleteStatementValidatorTest {
@Mock
private ShardingRule shardingRule;
- @Test(expected = DMLWithMultipleShardingTablesException.class)
+ @Test
public void assertPreValidateWhenDeleteMultiTablesForMySQL() {
- assertPreValidateWhenDeleteMultiTables(new MySQLDeleteStatement());
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new MySQLDeleteStatement()));
}
- @Test(expected = DMLWithMultipleShardingTablesException.class)
+ @Test
public void assertPreValidateWhenDeleteMultiTablesForOracle() {
- assertPreValidateWhenDeleteMultiTables(new OracleDeleteStatement());
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new OracleDeleteStatement()));
}
- @Test(expected = DMLWithMultipleShardingTablesException.class)
+ @Test
public void assertPreValidateWhenDeleteMultiTablesForPostgreSQL() {
- assertPreValidateWhenDeleteMultiTables(new PostgreSQLDeleteStatement());
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new PostgreSQLDeleteStatement()));
}
- @Test(expected = DMLWithMultipleShardingTablesException.class)
+ @Test
public void assertPreValidateWhenDeleteMultiTablesForSQL92() {
- assertPreValidateWhenDeleteMultiTables(new SQL92DeleteStatement());
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new SQL92DeleteStatement()));
}
- @Test(expected = DMLWithMultipleShardingTablesException.class)
+ @Test
public void assertPreValidateWhenDeleteMultiTablesForSQLServer() {
- assertPreValidateWhenDeleteMultiTables(new SQLServerDeleteStatement());
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new SQLServerDeleteStatement()));
}
private void assertPreValidateWhenDeleteMultiTables(final DeleteStatement sqlStatement) {
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java
index 11c53d9b2f7..d1c5bc86720 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java
@@ -73,6 +73,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -89,14 +90,14 @@ public final class ShardingInsertStatementValidatorTest {
@Mock
private ShardingConditions shardingConditions;
- @Test(expected = DMLWithMultipleShardingTablesException.class)
+ @Test
public void assertPreValidateWhenInsertMultiTables() {
SQLStatementContext<InsertStatement> sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
when(shardingRule.tableRuleExists(tableNames)).thenReturn(true);
- new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule,
- sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class));
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule,
+ sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class)));
}
private InsertStatementContext createInsertStatementContext(final List<Object> params, final InsertStatement insertStatement) {
@@ -107,14 +108,14 @@ public final class ShardingInsertStatementValidatorTest {
return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME);
}
- @Test(expected = MissingGenerateKeyColumnWithInsertSelectException.class)
+ @Test
public void assertPreValidateWhenInsertSelectWithoutKeyGenerateColumn() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false);
SQLStatementContext<InsertStatement> sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames());
- new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule,
- sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class));
+ assertThrows(MissingGenerateKeyColumnWithInsertSelectException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule,
+ sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class)));
}
@Test
@@ -127,7 +128,7 @@ public final class ShardingInsertStatementValidatorTest {
mock(ConfigurationProperties.class));
}
- @Test(expected = InsertSelectTableViolationException.class)
+ @Test
public void assertPreValidateWhenInsertSelectWithoutBindingTables() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
@@ -136,8 +137,8 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.tableRuleExists(multiTablesContext.getTableNames())).thenReturn(true);
SQLStatementContext<InsertStatement> sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames());
- new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS),
- mock(ConfigurationProperties.class));
+ assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(
+ shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class)));
}
@Test
@@ -178,14 +179,14 @@ public final class ShardingInsertStatementValidatorTest {
Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext);
}
- @Test(expected = DuplicateInsertDataRecordException.class)
+ @Test
public void assertPostValidateWhenInsertWithRoutingToMultipleDataNodes() {
SQLStatementContext<InsertStatement> sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
when(routeContext.isSingleRouting()).thenReturn(false);
when(shardingRule.isBroadcastTable(sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue())).thenReturn(false);
when(routeContext.getOriginalDataNodes()).thenReturn(getMultipleRouteDataNodes());
- new ShardingInsertStatementValidator(shardingConditions).postValidate(shardingRule, sqlStatementContext, new HintValueContext(),
- Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext);
+ assertThrows(DuplicateInsertDataRecordException.class, () -> new ShardingInsertStatementValidator(shardingConditions).postValidate(shardingRule, sqlStatementContext, new HintValueContext(),
+ Collections.emptyList(), mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), routeContext));
}
@Test
@@ -207,13 +208,13 @@ public final class ShardingInsertStatementValidatorTest {
insertStatementContext, new HintValueContext(), params, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), createSingleRouteContext());
}
- @Test(expected = UnsupportedUpdatingShardingValueException.class)
+ @Test
public void assertPostValidateWhenOnDuplicateKeyUpdateShardingColumnWithDifferentRouteContext() {
mockShardingRuleForUpdateShardingColumn();
List<Object> params = Collections.singletonList(1);
InsertStatementContext insertStatementContext = createInsertStatementContext(params, createInsertStatement());
- new ShardingInsertStatementValidator(mock(ShardingConditions.class)).postValidate(shardingRule,
- insertStatementContext, new HintValueContext(), params, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), createFullRouteContext());
+ assertThrows(UnsupportedUpdatingShardingValueException.class, () -> new ShardingInsertStatementValidator(mock(ShardingConditions.class)).postValidate(shardingRule,
+ insertStatementContext, new HintValueContext(), params, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), mock(ConfigurationProperties.class), createFullRouteContext()));
}
private void mockShardingRuleForUpdateShardingColumn() {
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java
index 596f5f8ec2f..f1c0e711a3d 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java
@@ -59,6 +59,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -79,7 +80,7 @@ public final class ShardingUpdateStatementValidatorTest {
new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class));
}
- @Test(expected = DMLWithMultipleShardingTablesException.class)
+ @Test
public void assertPreValidateWhenUpdateMultipleTables() {
UpdateStatement updateStatement = createUpdateStatement();
JoinTableSegment joinTableSegment = new JoinTableSegment();
@@ -90,7 +91,8 @@ public final class ShardingUpdateStatementValidatorTest {
Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
when(shardingRule.tableRuleExists(tableNames)).thenReturn(true);
- new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class));
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingUpdateStatementValidator().preValidate(
+ shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class)));
}
@Test
@@ -115,17 +117,18 @@ public final class ShardingUpdateStatementValidatorTest {
Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class), createSingleRouteContext());
}
- @Test(expected = UnsupportedUpdatingShardingValueException.class)
+ @Test
public void assertPostValidateWhenUpdateShardingColumnWithDifferentRouteContext() {
mockShardingRuleForUpdateShardingColumn();
- new ShardingUpdateStatementValidator().postValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), new HintValueContext(),
- Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class), createFullRouteContext());
+ assertThrows(UnsupportedUpdatingShardingValueException.class,
+ () -> new ShardingUpdateStatementValidator().postValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), new HintValueContext(),
+ Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class), createFullRouteContext()));
}
private void mockShardingRuleForUpdateShardingColumn() {
TableRule tableRule = mock(TableRule.class);
when(tableRule.getActualDataSourceNames()).thenReturn(Arrays.asList("ds_0", "ds_1"));
- when(tableRule.getActualTableNames("ds_1")).thenReturn(Collections.singletonList("user"));
+ when(tableRule.getActualTableNames("ds_1")).thenReturn(Collections.singleton("user"));
when(shardingRule.findShardingColumn("id", "user")).thenReturn(Optional.of("id"));
when(shardingRule.getTableRule("user")).thenReturn(tableRule);
StandardShardingStrategyConfiguration databaseStrategyConfig = mock(StandardShardingStrategyConfiguration.class);
@@ -141,14 +144,14 @@ public final class ShardingUpdateStatementValidatorTest {
private RouteContext createSingleRouteContext() {
RouteContext result = new RouteContext();
- result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("user", "user"))));
+ result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singleton(new RouteMapper("user", "user"))));
return result;
}
private RouteContext createFullRouteContext() {
RouteContext result = new RouteContext();
- result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("user", "user"))));
- result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("user", "user"))));
+ result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singleton(new RouteMapper("user", "user"))));
+ result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singleton(new RouteMapper("user", "user"))));
return result;
}
@@ -158,8 +161,7 @@ public final class ShardingUpdateStatementValidatorTest {
List<ColumnSegment> columns = new LinkedList<>();
columns.add(new ColumnSegment(0, 0, new IdentifierValue("id")));
AssignmentSegment assignment = new ColumnAssignmentSegment(0, 0, columns, new LiteralExpressionSegment(0, 0, 1));
- result.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(assignment)));
+ result.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singleton(assignment)));
return result;
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java
index 5131641d16d..2024c9ae3be 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java
@@ -36,6 +36,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public final class BindingTableRuleTest {
@@ -54,14 +55,14 @@ public final class BindingTableRuleTest {
assertThat(createBindingTableRule().getBindingActualTable("ds1", "Sub_Logic_Table", "LOGIC_TABLE", "table_1"), is("SUB_TABLE_1"));
}
- @Test(expected = ActualTableNotFoundException.class)
+ @Test
public void assertGetBindingActualTablesFailureWhenNotFound() {
- createBindingTableRule().getBindingActualTable("no_ds", "Sub_Logic_Table", "LOGIC_TABLE", "table_1");
+ assertThrows(ActualTableNotFoundException.class, () -> createBindingTableRule().getBindingActualTable("no_ds", "Sub_Logic_Table", "LOGIC_TABLE", "table_1"));
}
- @Test(expected = BindingTableNotFoundException.class)
+ @Test
public void assertGetBindingActualTablesFailureWhenLogicTableNotFound() {
- createBindingTableRule().getBindingActualTable("ds0", "No_Logic_Table", "LOGIC_TABLE", "table_1");
+ assertThrows(BindingTableNotFoundException.class, () -> createBindingTableRule().getBindingActualTable("ds0", "No_Logic_Table", "LOGIC_TABLE", "table_1"));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index 4d49a374eaa..66acde48593 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -74,6 +74,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -156,9 +157,9 @@ public final class ShardingRuleTest {
assertThat(actual.getLogicTable(), is("Broadcast_Table"));
}
- @Test(expected = ShardingTableRuleNotFoundException.class)
+ @Test
public void assertGetTableRuleFailure() {
- createMinimumShardingRule().getTableRule("New_Table");
+ assertThrows(ShardingTableRuleNotFoundException.class, () -> createMinimumShardingRule().getTableRule("New_Table"));
}
@Test
@@ -297,12 +298,12 @@ public final class ShardingRuleTest {
assertFalse(createMinimumShardingRule().findGenerateKeyColumnName("sub_logic_table").isPresent());
}
- @Test(expected = GenerateKeyStrategyNotFoundException.class)
+ @Test
public void assertGenerateKeyFailure() {
- createMaximumShardingRule().generateKey("table_0");
+ assertThrows(GenerateKeyStrategyNotFoundException.class, () -> createMaximumShardingRule().generateKey("table_0"));
}
- @Test(expected = InvalidBindingTablesException.class)
+ @Test
public void assertCreateInconsistentActualDataSourceNamesFailure() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
ShardingTableRuleConfiguration shardingTableRuleConfig = createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..2}.table_${0..2}");
@@ -311,10 +312,10 @@ public final class ShardingRuleTest {
shardingRuleConfig.getTables().add(shardingTableRuleConfig);
shardingRuleConfig.getTables().add(subTableRuleConfig);
shardingRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", shardingTableRuleConfig.getLogicTable() + "," + subTableRuleConfig.getLogicTable()));
- new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class));
+ assertThrows(InvalidBindingTablesException.class, () -> new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class)));
}
- @Test(expected = InvalidBindingTablesException.class)
+ @Test
public void assertCreateInconsistentActualTableNamesFailure() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
ShardingTableRuleConfiguration shardingTableRuleConfig = createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..3}");
@@ -323,10 +324,10 @@ public final class ShardingRuleTest {
shardingRuleConfig.getTables().add(shardingTableRuleConfig);
shardingRuleConfig.getTables().add(subTableRuleConfig);
shardingRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", shardingTableRuleConfig.getLogicTable() + "," + subTableRuleConfig.getLogicTable()));
- new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class));
+ assertThrows(InvalidBindingTablesException.class, () -> new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class)));
}
- @Test(expected = InvalidBindingTablesException.class)
+ @Test
public void assertCreateInconsistentAlgorithmExpressionOnDatabaseShardingStrategyFailure() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
ShardingTableRuleConfiguration shardingTableRuleConfig = createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");
@@ -338,10 +339,10 @@ public final class ShardingRuleTest {
shardingRuleConfig.getShardingAlgorithms().put("shardingAlgorithmDB", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_%{ds_id % 2}"))));
subTableRuleConfig.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("ds_id", "subAlgorithmDB"));
shardingRuleConfig.getShardingAlgorithms().put("subAlgorithmDB", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_%{ds_id % 3}"))));
- new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class));
+ assertThrows(InvalidBindingTablesException.class, () -> new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class)));
}
- @Test(expected = InvalidBindingTablesException.class)
+ @Test
public void assertCreateInconsistentAlgorithmExpressionOnTableShardingStrategyFailure() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
ShardingTableRuleConfiguration shardingTableRuleConfig = createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");
@@ -355,10 +356,10 @@ public final class ShardingRuleTest {
subTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("table_id", "subAlgorithmTBL"));
shardingRuleConfig.getShardingAlgorithms().put(
"subAlgorithmTBL", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "table_%{table_id % 3}"))));
- new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class));
+ assertThrows(InvalidBindingTablesException.class, () -> new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class)));
}
- @Test(expected = InvalidBindingTablesException.class)
+ @Test
public void assertCreateInconsistentAlgorithmExpressionWithDefaultAndSpecifiedTableShardingStrategyFailure() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
ShardingTableRuleConfiguration shardingTableRuleConfig = createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");
@@ -372,7 +373,7 @@ public final class ShardingRuleTest {
shardingRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("table_id", "table_inline"));
shardingRuleConfig.setDefaultShardingColumn("table_id");
shardingRuleConfig.getShardingAlgorithms().put("table_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "table_%{table_id % 3}"))));
- new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class));
+ assertThrows(InvalidBindingTablesException.class, () -> new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class)));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/TableRuleTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/TableRuleTest.java
index 35e9fe8bc3e..7a8bee39b71 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/TableRuleTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/TableRuleTest.java
@@ -43,6 +43,7 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public final class TableRuleTest {
@@ -143,11 +144,11 @@ public final class TableRuleTest {
assertFalse(actual.isExisted("table_3"));
}
- @Test(expected = DataNodesMissedWithShardingTableException.class)
+ @Test
public void assertActualDataNodesNotConfigured() {
ShardingTableRuleConfiguration shardingTableRuleConfig = new ShardingTableRuleConfiguration("LOGIC_TABLE", "");
shardingTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("shardingColumn", "INLINE"));
- new TableRule(shardingTableRuleConfig, Arrays.asList("ds0", "ds1"), null);
+ assertThrows(DataNodesMissedWithShardingTableException.class, () -> new TableRule(shardingTableRuleConfig, Arrays.asList("ds0", "ds1"), null));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java
index 863027aee82..6ad857ecff6 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java
@@ -31,6 +31,7 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -55,14 +56,16 @@ public final class ShardingRuleBuilderTest {
}
@SuppressWarnings("unchecked")
- @Test(expected = MissingRequiredShardingConfigurationException.class)
+ @Test
public void assertBuildWithNullDataSourceMap() {
- assertThat(builder.build(ruleConfig, "sharding_db", null, Collections.emptyList(), mock(InstanceContext.class)), instanceOf(ShardingRule.class));
+ assertThrows(MissingRequiredShardingConfigurationException.class,
+ () -> builder.build(ruleConfig, "sharding_db", null, Collections.emptyList(), mock(InstanceContext.class)));
}
@SuppressWarnings("unchecked")
- @Test(expected = MissingRequiredShardingConfigurationException.class)
+ @Test
public void assertBuildWithEmptyDataSourceMap() {
- assertThat(builder.build(ruleConfig, "sharding_db", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(ShardingRule.class));
+ assertThrows(MissingRequiredShardingConfigurationException.class,
+ () -> builder.build(ruleConfig, "sharding_db", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java
index f055f0f4785..8dc1e3381e9 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java
@@ -37,6 +37,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -74,9 +75,9 @@ public final class YamlShardingAutoTableRuleConfigurationSwapperTest {
assertNotNull(actual.getAuditStrategy());
}
- @Test(expected = MissingRequiredShardingConfigurationException.class)
+ @Test
public void assertSwapToObjectWithoutLogicTable() {
- new YamlShardingAutoTableRuleConfigurationSwapper().swapToObject(new YamlShardingAutoTableRuleConfiguration());
+ assertThrows(MissingRequiredShardingConfigurationException.class, () -> new YamlShardingAutoTableRuleConfigurationSwapper().swapToObject(new YamlShardingAutoTableRuleConfiguration()));
}
@Test
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingTableRuleConfigurationSwapperTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingTableRuleConfigurationSwapperTest.java
index 66040b62a65..f17bbe470da 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingTableRuleConfigurationSwapperTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/YamlShardingTableRuleConfigurationSwapperTest.java
@@ -34,6 +34,7 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
public final class YamlShardingTableRuleConfigurationSwapperTest {
@@ -60,9 +61,9 @@ public final class YamlShardingTableRuleConfigurationSwapperTest {
return result;
}
- @Test(expected = MissingRequiredShardingConfigurationException.class)
+ @Test
public void assertSwapToObjectWithoutLogicTable() {
- new YamlShardingTableRuleConfigurationSwapper().swapToObject(new YamlTableRuleConfiguration());
+ assertThrows(MissingRequiredShardingConfigurationException.class, () -> new YamlShardingTableRuleConfigurationSwapper().swapToObject(new YamlTableRuleConfiguration()));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
index 8f31237dc47..57e0240405d 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
@@ -61,6 +61,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -107,120 +108,120 @@ public final class ShardingRuleStatementCheckerTest {
ShardingTableRuleStatementChecker.checkAlteration(database, rules, shardingRuleConfig);
}
- @Test(expected = DuplicateRuleException.class)
+ @Test
public void assertCheckCreationWithDuplicated() {
List<AbstractTableRuleSegment> rules = Arrays.asList(
new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1")),
new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1")));
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(DuplicateRuleException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = DuplicateRuleException.class)
+ @Test
public void assertCheckCreationWithIdentical() {
List<AbstractTableRuleSegment> rules = Collections.singletonList(new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1")));
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(DuplicateRuleException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckAlterationWithRuleRequiredMissed() {
List<AbstractTableRuleSegment> rules = Collections.singletonList(new AutoTableRuleSegment("t_order_required_missed", Arrays.asList("ds_0", "ds_1")));
- ShardingTableRuleStatementChecker.checkAlteration(database, rules, shardingRuleConfig);
+ assertThrows(MissingRequiredRuleException.class, () -> ShardingTableRuleStatementChecker.checkAlteration(database, rules, shardingRuleConfig));
}
- @Test(expected = MissingRequiredStorageUnitsException.class)
+ @Test
public void assertCheckCreationWithResourceRequiredMissed() {
List<AbstractTableRuleSegment> rules = Collections.singletonList(new AutoTableRuleSegment("t_product", Arrays.asList("ds_required_missed", "ds_1")));
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(MissingRequiredStorageUnitsException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckCreationWithInvalidKeyGenerateAlgorithm() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
autoTableRuleSegment.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("invalid", PropertiesBuilder.build(new Property("invalid", "invalid")))));
- ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class,
+ () -> ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckCreationWithInvalidAuditAlgorithm() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
autoTableRuleSegment.setAuditStrategySegment(new AuditStrategySegment(Collections.singletonList(new ShardingAuditorSegment("sharding_key_required_auditor",
new AlgorithmSegment("invalid", new Properties()))), true));
- ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class,
+ () -> ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckAutoTableWithNotExistShardingAlgorithms() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
autoTableRuleSegment.setShardingColumn("product_id");
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("not_exist", PropertiesBuilder.build(new Property("", ""))));
List<AbstractTableRuleSegment> rules = Collections.singletonList(autoTableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
- autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("complex", PropertiesBuilder.build(new Property("", ""))));
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckAutoTableWithComplexShardingAlgorithms() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
autoTableRuleSegment.setShardingColumn("product_id");
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("complex", PropertiesBuilder.build(new Property("", ""))));
List<AbstractTableRuleSegment> rules = Collections.singletonList(autoTableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = UnsupportedSQLOperationException.class)
+ @Test
public void assertCheckTableWithInvalidShardingStrategyType() {
KeyGenerateStrategySegment keyGenerateStrategy = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("invalid", "product_id", null));
List<AbstractTableRuleSegment> rules = Collections.singletonList(tableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(UnsupportedSQLOperationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckTableWithUnmatchedShardingStrategyType1() {
KeyGenerateStrategySegment keyGenerateStrategy = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("complex", "product_id", null));
List<AbstractTableRuleSegment> rules = Collections.singletonList(tableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckTableWithUnmatchedShardingStrategyType2() {
KeyGenerateStrategySegment keyGenerateStrategy = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("standard", "product_id,user_id", null));
List<AbstractTableRuleSegment> rules = Collections.singletonList(tableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckTableWithUnmatchedShardingStrategyType3() {
AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "4")));
KeyGenerateStrategySegment keyGenerateStrategy = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("standard", "user_id", databaseAlgorithmSegment));
List<AbstractTableRuleSegment> rules = Collections.singletonList(tableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckTableWithInvalidAlgorithmName() {
KeyGenerateStrategySegment keyGenerateStrategy = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("hint", "product_id", null));
List<AbstractTableRuleSegment> rules = Collections.singletonList(tableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckTableWithInvalidAlgorithmNameWhenCurrentRuleConfigIsNull() {
KeyGenerateStrategySegment keyGenerateStrategy = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("hint", "product_id", null));
List<AbstractTableRuleSegment> rules = Collections.singletonList(tableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, null);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, null));
}
@Test
@@ -233,21 +234,21 @@ public final class ShardingRuleStatementCheckerTest {
ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckNullAlgorithmNameAndNullAlgorithmSegment() {
KeyGenerateStrategySegment keyGenerateStrategy = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("standard", "product_id", null));
List<AbstractTableRuleSegment> rules = Collections.singletonList(tableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertCheckAutoTableRuleWithStandardShardingAlgorithm() {
AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${product_id % 2}"))));
List<AbstractTableRuleSegment> rules = Collections.singletonList(autoTableRuleSegment);
- ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java
index e43932982bb..1cae82a8d78 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java
@@ -40,6 +40,7 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -55,33 +56,34 @@ public final class AlterDefaultShardingStrategyStatementUpdaterTest {
when(database.getName()).thenReturn("test");
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertExecuteWithInvalidStrategyType() {
- updater.checkSQLStatement(database, new AlterDefaultShardingStrategyStatement("TABLE", "invalidType", null, null), new ShardingRuleConfiguration());
+ assertThrows(InvalidAlgorithmConfigurationException.class,
+ () -> updater.checkSQLStatement(database, new AlterDefaultShardingStrategyStatement("TABLE", "invalidType", null, null), new ShardingRuleConfiguration()));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertExecuteWithoutCurrentConfiguration() {
AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id", null);
- updater.checkSQLStatement(database, statement, null);
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, statement, null));
}
- @Test(expected = MissingRequiredAlgorithmException.class)
+ @Test
public void assertExecuteWithNotExist() {
AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id", null);
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm"));
currentRuleConfig.getShardingAlgorithms().put("not_exist_algorithm", null);
- updater.checkSQLStatement(database, statement, currentRuleConfig);
+ assertThrows(MissingRequiredAlgorithmException.class, () -> updater.checkSQLStatement(database, statement, currentRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertExecuteWithUnmatchedStrategy() {
AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id,user_id", null);
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm"));
currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null);
- updater.checkSQLStatement(database, statement, currentRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> updater.checkSQLStatement(database, statement, currentRuleConfig));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java
index 6d072009101..8cc307298e4 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java
@@ -35,6 +35,7 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
@RunWith(MockitoJUnitRunner.class)
public final class AlterShardingTableReferenceRuleStatementUpdaterTest {
@@ -42,19 +43,22 @@ public final class AlterShardingTableReferenceRuleStatementUpdaterTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckWithoutCurrentRuleConfig() {
- new AlterShardingTableReferenceRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("foo", "t_order,t_order_item"), null);
+ assertThrows(MissingRequiredRuleException.class,
+ () -> new AlterShardingTableReferenceRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("foo", "t_order,t_order_item"), null));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckWithNotExistedRule() {
- new AlterShardingTableReferenceRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("notExisted", "t_1,t_2"), createCurrentRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class,
+ () -> new AlterShardingTableReferenceRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("notExisted", "t_1,t_2"), createCurrentRuleConfiguration()));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckWithNotExistedTables() {
- new AlterShardingTableReferenceRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("reference_0", "t_3,t_4"), createCurrentRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class,
+ () -> new AlterShardingTableReferenceRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("reference_0", "t_3,t_4"), createCurrentRuleConfiguration()));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
index acb2898aca6..01f39453468 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
@@ -35,6 +35,7 @@ import java.util.LinkedList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
@RunWith(MockitoJUnitRunner.class)
public final class CreateBroadcastTableRuleStatementUpdaterTest {
@@ -56,11 +57,11 @@ public final class CreateBroadcastTableRuleStatementUpdaterTest {
assertTrue(currentRuleConfig.getBroadcastTables().contains("t_2"));
}
- @Test(expected = DuplicateRuleException.class)
+ @Test
public void assertCheckDuplicatedBroadcastTable() {
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Arrays.asList("t_order", "t_address"));
- updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
+ assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(database, sqlStatement, currentRuleConfig));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java
index f36404caff5..010be43dc26 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java
@@ -40,6 +40,7 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -52,37 +53,38 @@ public final class CreateDefaultShardingStrategyStatementUpdaterTest {
@Before
public void before() {
- when(database.getName()).thenReturn("test");
+ when(database.getName()).thenReturn("foo_db");
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertExecuteWithInvalidStrategyType() {
- updater.checkSQLStatement(database, new CreateDefaultShardingStrategyStatement(false, "TABLE", "invalidType", null, null), new ShardingRuleConfiguration());
+ CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "invalidType", null, null);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> updater.checkSQLStatement(database, statement, new ShardingRuleConfiguration()));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertExecuteWithoutCurrentConfiguration() {
CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", null);
- updater.checkSQLStatement(database, statement, null);
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, statement, null));
}
- @Test(expected = DuplicateRuleException.class)
+ @Test
public void assertExecuteWithExist() {
AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties());
CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm);
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm"));
currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null);
- updater.checkSQLStatement(database, statement, currentRuleConfig);
+ assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(database, statement, currentRuleConfig));
}
- @Test(expected = InvalidAlgorithmConfigurationException.class)
+ @Test
public void assertExecuteWithUnmatchedStrategy() {
CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id,user_id", null);
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm"));
currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null);
- updater.checkSQLStatement(database, statement, currentRuleConfig);
+ assertThrows(InvalidAlgorithmConfigurationException.class, () -> updater.checkSQLStatement(database, statement, currentRuleConfig));
}
@Test
@@ -102,7 +104,7 @@ public final class CreateDefaultShardingStrategyStatementUpdaterTest {
@Test
public void assertCreateDefaultDatabaseShardingStrategy() {
- AlgorithmSegment databaseAlgorithmSegment = getAutoCreativeAlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")));
+ AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")));
CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "standard", "user_id", databaseAlgorithmSegment);
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
updater.checkSQLStatement(database, statement, currentRuleConfig);
@@ -135,13 +137,13 @@ public final class CreateDefaultShardingStrategyStatementUpdaterTest {
@Test
public void assertCreateDefaultDatabaseShardingStrategyWithIfNotExists() {
- AlgorithmSegment databaseAlgorithmSegment = getAutoCreativeAlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")));
+ AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")));
CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "standard", "user_id", databaseAlgorithmSegment);
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
updater.checkSQLStatement(database, statement, currentRuleConfig);
ShardingRuleConfiguration toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement);
updater.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig);
- databaseAlgorithmSegment = getAutoCreativeAlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${order_id % 2}")));
+ databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${order_id % 2}")));
CreateDefaultShardingStrategyStatement statementWithIfNotExists = new CreateDefaultShardingStrategyStatement(true, "TABLE", "standard", "order_id", databaseAlgorithmSegment);
updater.checkSQLStatement(database, statementWithIfNotExists, currentRuleConfig);
toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(currentRuleConfig, statementWithIfNotExists);
@@ -176,8 +178,4 @@ public final class CreateDefaultShardingStrategyStatementUpdaterTest {
assertThat(defaultDatabaseShardingStrategy.getType(), is(""));
assertThat(defaultDatabaseShardingStrategy.getShardingAlgorithmName(), is(""));
}
-
- private AlgorithmSegment getAutoCreativeAlgorithmSegment(final String name, final Properties props) {
- return new AlgorithmSegment(name, props);
- }
}
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java
index 70852125ef2..8ddb48fc04e 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java
@@ -38,6 +38,7 @@ import java.util.LinkedList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
@RunWith(MockitoJUnitRunner.class)
public final class CreateShardingTableReferenceRuleStatementUpdaterTest {
@@ -47,9 +48,9 @@ public final class CreateShardingTableReferenceRuleStatementUpdaterTest {
private final CreateShardingTableReferenceRuleStatementUpdater updater = new CreateShardingTableReferenceRuleStatementUpdater();
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithoutCurrentTableRule() {
- updater.checkSQLStatement(database, createSQLStatement(false, "foo", "t_order,t_order_item"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement(false, "foo", "t_order,t_order_item"), new ShardingRuleConfiguration()));
}
private CreateShardingTableReferenceRuleStatement createSQLStatement(final boolean ifNotExists, final String name, final String reference) {
@@ -58,9 +59,9 @@ public final class CreateShardingTableReferenceRuleStatementUpdaterTest {
return new CreateShardingTableReferenceRuleStatement(ifNotExists, segments);
}
- @Test(expected = DuplicateRuleException.class)
+ @Test
public void assertCheckSQLStatementWithDuplicateTables() {
- updater.checkSQLStatement(database, createSQLStatement(false, "foo", "t_order,t_order_item"), getCurrentRuleConfig());
+ assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement(false, "foo", "t_order,t_order_item"), getCurrentRuleConfig()));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java
index 925ff082fc7..1828e83376a 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java
@@ -69,6 +69,7 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -141,7 +142,7 @@ public final class CreateShardingTableRuleStatementUpdaterTest {
updater.checkSQLStatement(database, distSQLStatement, null);
}
- @Test(expected = DistSQLException.class)
+ @Test
public void assertCheckCreateShardingStatementThrows() {
String sql = "CREATE SHARDING TABLE RULE t_order("
+ "STORAGE_UNITS(ds_0,ds_1),"
@@ -149,7 +150,7 @@ public final class CreateShardingTableRuleStatementUpdaterTest {
+ "TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_item_${order_id % 4}')),"
+ "KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME='snowflake')))";
CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql);
- updater.checkSQLStatement(database, distSQLStatement, null);
+ assertThrows(DistSQLException.class, () -> updater.checkSQLStatement(database, distSQLStatement, null));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropBroadcastTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropBroadcastTableRuleStatementUpdaterTest.java
index 5d284bbb885..6860471c0ff 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropBroadcastTableRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropBroadcastTableRuleStatementUpdaterTest.java
@@ -33,6 +33,7 @@ import java.util.Collections;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
@RunWith(MockitoJUnitRunner.class)
public final class DropBroadcastTableRuleStatementUpdaterTest {
@@ -42,14 +43,14 @@ public final class DropBroadcastTableRuleStatementUpdaterTest {
private final DropBroadcastTableRuleStatementUpdater updater = new DropBroadcastTableRuleStatementUpdater();
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithoutCurrentRule() {
- updater.checkSQLStatement(database, createSQLStatement("t_order"), null);
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement("t_order"), null));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithoutExistBroadcastTableRule() {
- updater.checkSQLStatement(database, createSQLStatement("t_order"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement("t_order"), new ShardingRuleConfiguration()));
}
@Test
@@ -65,9 +66,9 @@ public final class DropBroadcastTableRuleStatementUpdaterTest {
assertTrue(updater.hasAnyOneToBeDropped(createSQLStatement(true, "t_order"), createCurrentRuleConfiguration()));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithBroadcastTableRuleAreNotTheSame() {
- updater.checkSQLStatement(database, createSQLStatement("t_order_item"), createCurrentRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement("t_order_item"), createCurrentRuleConfiguration()));
}
@Test
@@ -94,14 +95,11 @@ public final class DropBroadcastTableRuleStatementUpdaterTest {
}
private DropBroadcastTableRuleStatement createSQLStatement(final String tableName) {
- return null == tableName ? new DropBroadcastTableRuleStatement(false, Collections.emptyList())
- : new DropBroadcastTableRuleStatement(false, Collections.singleton(tableName));
+ return null == tableName ? new DropBroadcastTableRuleStatement(false, Collections.emptyList()) : new DropBroadcastTableRuleStatement(false, Collections.singleton(tableName));
}
private DropBroadcastTableRuleStatement createSQLStatement(final boolean ifExists, final String tableName) {
- return null == tableName
- ? new DropBroadcastTableRuleStatement(false, Collections.emptyList())
- : new DropBroadcastTableRuleStatement(ifExists, Collections.singleton(tableName));
+ return null == tableName ? new DropBroadcastTableRuleStatement(false, Collections.emptyList()) : new DropBroadcastTableRuleStatement(ifExists, Collections.singleton(tableName));
}
private ShardingRuleConfiguration createCurrentRuleConfiguration() {
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java
index 3f5a471496e..9d60d77601c 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java
@@ -17,8 +17,8 @@
package org.apache.shardingsphere.sharding.distsql.update;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
@@ -38,6 +38,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
@RunWith(MockitoJUnitRunner.class)
@@ -48,14 +49,14 @@ public final class DropDefaultShardingStrategyStatementUpdaterTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithoutCurrentRule() {
- updater.checkSQLStatement(database, new DropDefaultShardingStrategyStatement(false, "TABLE"), null);
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, new DropDefaultShardingStrategyStatement(false, "TABLE"), null));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithoutExistedAlgorithm() {
- updater.checkSQLStatement(database, createSQLStatement("table"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement("table"), new ShardingRuleConfiguration()));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java
index 8737b26db2b..40c1de65e35 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java
@@ -40,6 +40,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
@RunWith(MockitoJUnitRunner.class)
public final class DropShardingAlgorithmStatementUpdaterTest {
@@ -49,9 +50,9 @@ public final class DropShardingAlgorithmStatementUpdaterTest {
private final DropShardingAlgorithmStatementUpdater updater = new DropShardingAlgorithmStatementUpdater();
- @Test(expected = MissingRequiredAlgorithmException.class)
+ @Test
public void assertCheckSQLStatementWithoutCurrentRule() throws RuleDefinitionViolationException {
- updater.checkSQLStatement(database, new DropShardingAlgorithmStatement(false, Collections.emptyList()), null);
+ assertThrows(MissingRequiredAlgorithmException.class, () -> updater.checkSQLStatement(database, new DropShardingAlgorithmStatement(false, Collections.emptyList()), null));
}
@Test
@@ -60,9 +61,9 @@ public final class DropShardingAlgorithmStatementUpdaterTest {
updater.checkSQLStatement(database, dropShardingAlgorithmStatement, null);
}
- @Test(expected = MissingRequiredAlgorithmException.class)
+ @Test
public void assertCheckSQLStatementWithoutExistedAlgorithm() throws RuleDefinitionViolationException {
- updater.checkSQLStatement(database, createSQLStatement("t_order"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredAlgorithmException.class, () -> updater.checkSQLStatement(database, createSQLStatement("t_order"), new ShardingRuleConfiguration()));
}
@Test
@@ -70,14 +71,14 @@ public final class DropShardingAlgorithmStatementUpdaterTest {
updater.checkSQLStatement(database, createSQLStatementWithIfExists("t_order"), new ShardingRuleConfiguration());
}
- @Test(expected = AlgorithmInUsedException.class)
+ @Test
public void assertCheckSQLStatementWithBindingTableRule() throws RuleDefinitionViolationException {
- updater.checkSQLStatement(database, createSQLStatement("t_order_tb_inline"), createCurrentRuleConfiguration());
+ assertThrows(AlgorithmInUsedException.class, () -> updater.checkSQLStatement(database, createSQLStatement("t_order_tb_inline"), createCurrentRuleConfiguration()));
}
- @Test(expected = AlgorithmInUsedException.class)
+ @Test
public void assertCheckSQLStatementWithBindingTableRuleWithIfExists() throws RuleDefinitionViolationException {
- updater.checkSQLStatement(database, createSQLStatementWithIfExists("t_order_tb_inline"), createCurrentRuleConfiguration());
+ assertThrows(AlgorithmInUsedException.class, () -> updater.checkSQLStatement(database, createSQLStatementWithIfExists("t_order_tb_inline"), createCurrentRuleConfiguration()));
}
@Test
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java
index 1d60480571c..f710b4396e7 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java
@@ -17,9 +17,9 @@
package org.apache.shardingsphere.sharding.distsql.update;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException;
import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
@@ -38,6 +38,7 @@ import java.util.Collections;
import java.util.Properties;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -53,9 +54,9 @@ public final class DropShardingAuditorStatementUpdaterTest {
when(database.getName()).thenReturn("test");
}
- @Test(expected = MissingRequiredAlgorithmException.class)
+ @Test
public void assertExecuteWithNotExist() {
- updater.checkSQLStatement(database, createSQLStatement("sharding_key_required_auditor"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredAlgorithmException.class, () -> updater.checkSQLStatement(database, createSQLStatement("sharding_key_required_auditor"), new ShardingRuleConfiguration()));
}
@Test
@@ -72,12 +73,12 @@ public final class DropShardingAuditorStatementUpdaterTest {
assertTrue(currentRuleConfig.getAuditors().isEmpty());
}
- @Test(expected = AlgorithmInUsedException.class)
+ @Test
public void assertExecuteWithUsed() {
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", null));
currentRuleConfig.getAutoTables().add(createShardingAutoTableRuleConfiguration());
- updater.checkSQLStatement(database, createSQLStatement("sharding_key_required_auditor"), currentRuleConfig);
+ assertThrows(AlgorithmInUsedException.class, () -> updater.checkSQLStatement(database, createSQLStatement("sharding_key_required_auditor"), currentRuleConfig));
}
private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() {
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java
index 103d391beb4..13973f1fa17 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java
@@ -17,9 +17,9 @@
package org.apache.shardingsphere.sharding.distsql.update;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException;
import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
@@ -38,6 +38,7 @@ import java.util.Collections;
import java.util.Properties;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -53,9 +54,9 @@ public final class DropShardingKeyGeneratorStatementUpdaterTest {
when(database.getName()).thenReturn("test");
}
- @Test(expected = MissingRequiredAlgorithmException.class)
+ @Test
public void assertExecuteWithNotExist() {
- updater.checkSQLStatement(database, createSQLStatement("uuid_key_generator"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredAlgorithmException.class, () -> updater.checkSQLStatement(database, createSQLStatement("uuid_key_generator"), new ShardingRuleConfiguration()));
}
@Test
@@ -72,12 +73,12 @@ public final class DropShardingKeyGeneratorStatementUpdaterTest {
assertTrue(currentRuleConfig.getKeyGenerators().isEmpty());
}
- @Test(expected = AlgorithmInUsedException.class)
+ @Test
public void assertExecuteWithUsed() {
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.getKeyGenerators().put("uuid_key_generator", new AlgorithmConfiguration("UUID", null));
currentRuleConfig.getAutoTables().add(createShardingAutoTableRuleConfiguration());
- updater.checkSQLStatement(database, createSQLStatement("uuid_key_generator"), currentRuleConfig);
+ assertThrows(AlgorithmInUsedException.class, () -> updater.checkSQLStatement(database, createSQLStatement("uuid_key_generator"), currentRuleConfig));
}
private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() {
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java
index 29116892e7f..19335fd9f2e 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java
@@ -32,12 +32,12 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
-import java.util.Collection;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
@RunWith(MockitoJUnitRunner.class)
@@ -48,37 +48,30 @@ public final class DropShardingTableReferenceRuleStatementUpdaterTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckWithoutCurrentRule() {
- updater.checkSQLStatement(database, createSQLStatement("notExisted"), null);
- }
-
- private DropShardingTableReferenceRuleStatement createSQLStatement(final String ruleName) {
- return createSQLStatement(false, Collections.singletonList(ruleName));
+ assertThrows(MissingRequiredRuleException.class, () -> updater.checkSQLStatement(database, new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")), null));
}
- private DropShardingTableReferenceRuleStatement createSQLStatement(final boolean ifExists, final Collection<String> ruleNames) {
- return new DropShardingTableReferenceRuleStatement(ifExists, ruleNames);
- }
-
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckWithNotExistedShardingTableReferenceRule() {
- updater.checkSQLStatement(database, createSQLStatement("notExisted"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class,
+ () -> updater.checkSQLStatement(database, new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")), new ShardingRuleConfiguration()));
}
@Test
public void assertCheckWithIfExists() {
- DropShardingTableReferenceRuleStatement statement = createSQLStatement(true, Collections.singletonList("notExisted"));
+ DropShardingTableReferenceRuleStatement statement = new DropShardingTableReferenceRuleStatement(true, Collections.singleton("notExisted"));
updater.checkSQLStatement(database, statement, null);
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
- shardingRuleConfig.setBindingTableGroups(Collections.singletonList(new ShardingTableReferenceRuleConfiguration("foo", "t_3,t_4")));
+ shardingRuleConfig.setBindingTableGroups(Collections.singleton(new ShardingTableReferenceRuleConfiguration("foo", "t_3,t_4")));
updater.checkSQLStatement(database, statement, shardingRuleConfig);
}
@Test
public void assertHasAnyOneToBeDropped() {
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
- DropShardingTableReferenceRuleStatement sqlStatement = createSQLStatement(true, Arrays.asList("reference_0", "reference_1"));
+ DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(true, Arrays.asList("reference_0", "reference_1"));
assertTrue(updater.hasAnyOneToBeDropped(sqlStatement, currentRuleConfig));
}
@@ -93,7 +86,7 @@ public final class DropShardingTableReferenceRuleStatementUpdaterTest {
@Test
public void assertHasNotAnyOneToBeDropped() {
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
- DropShardingTableReferenceRuleStatement sqlStatement = createSQLStatement("foo");
+ DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Collections.singleton("foo"));
assertFalse(updater.hasAnyOneToBeDropped(sqlStatement, currentRuleConfig));
}
@@ -101,7 +94,7 @@ public final class DropShardingTableReferenceRuleStatementUpdaterTest {
public void assertDropSpecifiedReferenceRuleConfiguration() {
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
currentRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_1", "t_1,t_2"));
- DropShardingTableReferenceRuleStatement sqlStatement = createSQLStatement("reference_1");
+ DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Collections.singleton("reference_1"));
updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
updater.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig);
assertThat(currentRuleConfig.getBindingTableGroups().size(), is(1));
@@ -112,7 +105,7 @@ public final class DropShardingTableReferenceRuleStatementUpdaterTest {
public void assertDropMultipleReferenceRules() {
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
currentRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_1", "t_1,t_2,t_3"));
- DropShardingTableReferenceRuleStatement sqlStatement = createSQLStatement(false, Arrays.asList("reference_0", "reference_1"));
+ DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Arrays.asList("reference_0", "reference_1"));
updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
updater.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig);
assertTrue(currentRuleConfig.getBindingTableGroups().isEmpty());
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
index d5c5e53fb0e..5a802b74595 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
@@ -45,21 +45,25 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
public final class DropShardingTableRuleStatementUpdaterTest {
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithoutCurrentRule() throws RuleDefinitionViolationException {
- new DropShardingTableRuleStatementUpdater().checkSQLStatement(
- mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), new DropShardingTableRuleStatement(false, Collections.emptyList()), null);
+ assertThrows(MissingRequiredRuleException.class,
+ () -> new DropShardingTableRuleStatementUpdater().checkSQLStatement(
+ mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), new DropShardingTableRuleStatement(false, Collections.emptyList()), null));
}
- @Test(expected = MissingRequiredRuleException.class)
+ @Test
public void assertCheckSQLStatementWithoutExistedTableRule() throws RuleDefinitionViolationException {
- new DropShardingTableRuleStatementUpdater().checkSQLStatement(
- mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order"), new ShardingRuleConfiguration());
+ assertThrows(MissingRequiredRuleException.class,
+ () -> new DropShardingTableRuleStatementUpdater().checkSQLStatement(
+ mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order"), new ShardingRuleConfiguration()));
+
}
@Test
@@ -74,10 +78,10 @@ public final class DropShardingTableRuleStatementUpdaterTest {
new DropShardingTableRuleStatementUpdater().checkSQLStatement(mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), statement, new ShardingRuleConfiguration());
}
- @Test(expected = RuleInUsedException.class)
+ @Test
public void assertCheckSQLStatementWithBindingTableRule() throws RuleDefinitionViolationException {
- new DropShardingTableRuleStatementUpdater().checkSQLStatement(
- mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order_item"), createCurrentRuleConfiguration());
+ assertThrows(RuleInUsedException.class, () -> new DropShardingTableRuleStatementUpdater().checkSQLStatement(
+ mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order_item"), createCurrentRuleConfiguration()));
}
@Test
diff --git a/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/rule/builder/ShardingCacheRuleBuilderTest.java b/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/rule/builder/ShardingCacheRuleBuilderTest.java
index afdcaf0d97b..b49b5f23319 100644
--- a/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/rule/builder/ShardingCacheRuleBuilderTest.java
+++ b/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/rule/builder/ShardingCacheRuleBuilderTest.java
@@ -32,6 +32,7 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
public final class ShardingCacheRuleBuilderTest {
@@ -48,9 +49,9 @@ public final class ShardingCacheRuleBuilderTest {
assertThat(actualShardingCacheRule.getShardingRule(), is(expectedShardingRule));
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void assertBuildShardingCacheRuleWithoutShardingRule() {
- new ShardingCacheRuleBuilder().build(null, "", Collections.emptyMap(), Collections.emptyList(), null);
+ assertThrows(IllegalStateException.class, () -> new ShardingCacheRuleBuilder().build(null, "", Collections.emptyMap(), Collections.emptyList(), null));
}
@Test
diff --git a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
index 8f5f5ab70c2..5756fff606e 100644
--- a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
+++ b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
@@ -40,6 +40,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
+import static org.junit.Assert.assertThrows;
public final class CosIdKeyGenerateAlgorithmTest {
@@ -62,10 +63,10 @@ public final class CosIdKeyGenerateAlgorithmTest {
assertThat(algorithm.generateKey(), is(2L));
}
- @Test(expected = NotFoundIdGeneratorException.class)
+ @Test
public void assertGenerateKeyWhenIdProviderIsEmpty() {
DefaultIdGeneratorProvider.INSTANCE.clear();
- TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID").generateKey();
+ assertThrows(NotFoundIdGeneratorException.class, () -> TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID").generateKey());
}
@Test
diff --git a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
index 73e764fc076..eee618f4d81 100644
--- a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
+++ b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
@@ -45,6 +45,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
+import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
@@ -133,24 +134,22 @@ public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
assertThat(actualState.getSequence(), is(1L));
}
- @Test(expected = ShardingPluginException.class)
+ @Test
public void assertGenerateKeyWhenNoneInstanceContext() {
- TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID_SNOWFLAKE").generateKey();
+ assertThrows(ShardingPluginException.class, () -> TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID_SNOWFLAKE").generateKey());
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void assertGenerateKeyWhenNegative() {
CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID_SNOWFLAKE");
- algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(-1),
- new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext));
- algorithm.generateKey();
+ assertThrows(IllegalArgumentException.class, () -> algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(-1),
+ new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext)));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void assertGenerateKeyWhenGreaterThen1023() {
CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID_SNOWFLAKE");
- algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(1024),
- new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext));
- algorithm.generateKey();
+ assertThrows(IllegalArgumentException.class, () -> algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(1024),
+ new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext)));
}
}