Skip to content

Commit ec63205

Browse files
authored
Add arguments not null check when creating RouteUnit (#33382)
* Add arguments not null check when creating RouteUnit * Add arguments not null check when creating RouteUnit * Add release note for #33357 * Add release note for #33357
1 parent 98c0fb6 commit ec63205

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1. Proxy: Add query parameters and check for mysql kill processId - [#33274](https://github.com/apache/shardingsphere/pull/33274)
1212
1. SQL Parser: Support parsing Doris INSTR - [#33289](https://github.com/apache/shardingsphere/pull/33289)
1313
1. Agent: Simplify the use of Agent's Docker Image - [#33356](https://github.com/apache/shardingsphere/pull/33356)
14+
1. Add arguments not null check when creating RouteUnit - [#33382](https://github.com/apache/shardingsphere/pull/33382)
1415

1516
### Bug Fixes
1617

infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,6 @@ void assertBuildGenericSQLRewriteResultWithTableAvailableSQLStatement() {
8989
assertThat(actual, is(expected));
9090
}
9191

92-
@Test
93-
void assertBuildRouteSQLRewriteResultWithNullTableMappers() {
94-
RouteUnit routeUnit = new RouteUnit(new RouteMapper("foo_db", "actual_db"), null);
95-
SQLRewriteUnit sqlRewriteUnit = new SQLRewriteUnit("sql", Collections.singletonList("parameter"));
96-
Map<RouteUnit, SQLRewriteUnit> sqlRewriteUnits = Collections.singletonMap(routeUnit, sqlRewriteUnit);
97-
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap());
98-
RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList());
99-
ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, ruleMetaData, buildDatabase());
100-
Collection<ExecutionUnit> actual = ExecutionContextBuilder.build(database, new RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class));
101-
ExecutionUnit expectedUnit = new ExecutionUnit("actual_db", new SQLUnit("sql", Collections.singletonList("parameter")));
102-
assertThat(actual, is(Collections.singleton(expectedUnit)));
103-
}
104-
10592
@Test
10693
void assertBuildRouteSQLRewriteResult() {
10794
RouteUnit routeUnit1 = new RouteUnit(new RouteMapper("foo_db_1", "actual_db_1"), Collections.singletonList(new RouteMapper("foo_tbl", "actual_tbl")));

infra/route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteUnit.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import lombok.EqualsAndHashCode;
2121
import lombok.Getter;
22-
import lombok.RequiredArgsConstructor;
2322
import lombok.ToString;
23+
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
2424

2525
import java.util.Collection;
2626
import java.util.HashSet;
@@ -31,7 +31,6 @@
3131
/**
3232
* Route unit.
3333
*/
34-
@RequiredArgsConstructor
3534
@Getter
3635
@EqualsAndHashCode
3736
@ToString
@@ -41,6 +40,13 @@ public final class RouteUnit {
4140

4241
private final Collection<RouteMapper> tableMappers;
4342

43+
public RouteUnit(final RouteMapper dataSourceMapper, final Collection<RouteMapper> tableMappers) {
44+
ShardingSpherePreconditions.checkNotNull(dataSourceMapper, () -> new IllegalArgumentException("`dataSourceMapper` is required"));
45+
ShardingSpherePreconditions.checkNotNull(tableMappers, () -> new IllegalArgumentException("`tableMappers` is required"));
46+
this.dataSourceMapper = dataSourceMapper;
47+
this.tableMappers = tableMappers;
48+
}
49+
4450
/**
4551
* Get logic table names.
4652
*

infra/route/src/test/java/org/apache/shardingsphere/infra/route/context/RouteUnitTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.hamcrest.CoreMatchers.is;
2727
import static org.junit.jupiter.api.Assertions.assertFalse;
2828
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.junit.jupiter.api.Assertions.assertThrows;
2930
import static org.junit.jupiter.api.Assertions.assertTrue;
3031

3132
class RouteUnitTest {
@@ -70,4 +71,14 @@ void assertFindTableMapper() {
7071
void assertTableMapperNotFound() {
7172
assertFalse(routeUnit.findTableMapper("invalid_ds", "invalid_tbl").isPresent());
7273
}
74+
75+
@Test
76+
void assertTableMapperIsNull() {
77+
assertThrows(IllegalArgumentException.class, () -> new RouteUnit(new RouteMapper(LOGIC_DATA_SOURCE, ACTUAL_DATA_SOURCE), null));
78+
}
79+
80+
@Test
81+
void assertDataSourceMapperIsNull() {
82+
assertThrows(IllegalArgumentException.class, () -> new RouteUnit(null, Arrays.asList(new RouteMapper(LOGIC_TABLE, ACTUAL_TABLE_0), new RouteMapper(LOGIC_TABLE, ACTUAL_TABLE_1))));
83+
}
7384
}

0 commit comments

Comments
 (0)