Skip to content

Commit

Permalink
Add arguments not null check when creating RouteUnit (#33382)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FlyingZC authored Oct 24, 2024
1 parent 98c0fb6 commit ec63205
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
1. Proxy: Add query parameters and check for mysql kill processId - [#33274](https://github.com/apache/shardingsphere/pull/33274)
1. SQL Parser: Support parsing Doris INSTR - [#33289](https://github.com/apache/shardingsphere/pull/33289)
1. Agent: Simplify the use of Agent's Docker Image - [#33356](https://github.com/apache/shardingsphere/pull/33356)
1. Add arguments not null check when creating RouteUnit - [#33382](https://github.com/apache/shardingsphere/pull/33382)

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,6 @@ void assertBuildGenericSQLRewriteResultWithTableAvailableSQLStatement() {
assertThat(actual, is(expected));
}

@Test
void assertBuildRouteSQLRewriteResultWithNullTableMappers() {
RouteUnit routeUnit = new RouteUnit(new RouteMapper("foo_db", "actual_db"), null);
SQLRewriteUnit sqlRewriteUnit = new SQLRewriteUnit("sql", Collections.singletonList("parameter"));
Map<RouteUnit, SQLRewriteUnit> sqlRewriteUnits = Collections.singletonMap(routeUnit, sqlRewriteUnit);
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap());
RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList());
ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, ruleMetaData, buildDatabase());
Collection<ExecutionUnit> actual = ExecutionContextBuilder.build(database, new RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class));
ExecutionUnit expectedUnit = new ExecutionUnit("actual_db", new SQLUnit("sql", Collections.singletonList("parameter")));
assertThat(actual, is(Collections.singleton(expectedUnit)));
}

@Test
void assertBuildRouteSQLRewriteResult() {
RouteUnit routeUnit1 = new RouteUnit(new RouteMapper("foo_db_1", "actual_db_1"), Collections.singletonList(new RouteMapper("foo_tbl", "actual_tbl")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;

import java.util.Collection;
import java.util.HashSet;
Expand All @@ -31,7 +31,6 @@
/**
* Route unit.
*/
@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
Expand All @@ -41,6 +40,13 @@ public final class RouteUnit {

private final Collection<RouteMapper> tableMappers;

public RouteUnit(final RouteMapper dataSourceMapper, final Collection<RouteMapper> tableMappers) {
ShardingSpherePreconditions.checkNotNull(dataSourceMapper, () -> new IllegalArgumentException("`dataSourceMapper` is required"));
ShardingSpherePreconditions.checkNotNull(tableMappers, () -> new IllegalArgumentException("`tableMappers` is required"));
this.dataSourceMapper = dataSourceMapper;
this.tableMappers = tableMappers;
}

/**
* Get logic table names.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class RouteUnitTest {
Expand Down Expand Up @@ -70,4 +71,14 @@ void assertFindTableMapper() {
void assertTableMapperNotFound() {
assertFalse(routeUnit.findTableMapper("invalid_ds", "invalid_tbl").isPresent());
}

@Test
void assertTableMapperIsNull() {
assertThrows(IllegalArgumentException.class, () -> new RouteUnit(new RouteMapper(LOGIC_DATA_SOURCE, ACTUAL_DATA_SOURCE), null));
}

@Test
void assertDataSourceMapperIsNull() {
assertThrows(IllegalArgumentException.class, () -> new RouteUnit(null, Arrays.asList(new RouteMapper(LOGIC_TABLE, ACTUAL_TABLE_0), new RouteMapper(LOGIC_TABLE, ACTUAL_TABLE_1))));
}
}

0 comments on commit ec63205

Please sign in to comment.