forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#6781 from terrymanu/dev
Add test cases for SQLBuilder's impl
- Loading branch information
Showing
7 changed files
with
151 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...va/org/apache/shardingsphere/infra/rewrite/sql/fixture/RouteUnitAwareSQLTokenFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shardingsphere.infra.rewrite.sql.fixture; | ||
|
||
import lombok.Getter; | ||
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.RouteUnitAware; | ||
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken; | ||
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.Substitutable; | ||
import org.apache.shardingsphere.infra.route.context.RouteUnit; | ||
|
||
@Getter | ||
public final class RouteUnitAwareSQLTokenFixture extends SQLToken implements Substitutable, RouteUnitAware { | ||
|
||
private final int stopIndex; | ||
|
||
public RouteUnitAwareSQLTokenFixture(final int startIndex, final int stopIndex) { | ||
super(startIndex); | ||
this.stopIndex = stopIndex; | ||
} | ||
|
||
@Override | ||
public String toString(final RouteUnit routeUnit) { | ||
return routeUnit.getTableMappers().iterator().next().getActualName(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ne/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/fixture/SQLTokenFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shardingsphere.infra.rewrite.sql.fixture; | ||
|
||
import lombok.Getter; | ||
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken; | ||
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.Substitutable; | ||
|
||
@Getter | ||
public final class SQLTokenFixture extends SQLToken implements Substitutable { | ||
|
||
private final int stopIndex; | ||
|
||
public SQLTokenFixture(final int startIndex, final int stopIndex) { | ||
super(startIndex); | ||
this.stopIndex = stopIndex; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "XXX"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...e/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/impl/RouteSQLBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shardingsphere.infra.rewrite.sql.impl; | ||
|
||
import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext; | ||
import org.apache.shardingsphere.infra.rewrite.sql.fixture.RouteUnitAwareSQLTokenFixture; | ||
import org.apache.shardingsphere.infra.rewrite.sql.fixture.SQLTokenFixture; | ||
import org.apache.shardingsphere.infra.route.context.RouteMapper; | ||
import org.apache.shardingsphere.infra.route.context.RouteUnit; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public final class RouteSQLBuilderTest { | ||
|
||
@Test | ||
public void assertToSQLWithNormalSQLToken() { | ||
SQLRewriteContext context = mock(SQLRewriteContext.class); | ||
when(context.getSql()).thenReturn("SELECT * FROM tbl WHERE id=?"); | ||
when(context.getSqlTokens()).thenReturn(Collections.singletonList(new SQLTokenFixture(14, 16))); | ||
assertThat(new RouteSQLBuilder(context, createRouteUnit()).toSQL(), is("SELECT * FROM XXX WHERE id=?")); | ||
} | ||
|
||
@Test | ||
public void assertToSQLWithRouteUnitAwaredSQLToken() { | ||
SQLRewriteContext context = mock(SQLRewriteContext.class); | ||
when(context.getSql()).thenReturn("SELECT * FROM tbl WHERE id=?"); | ||
when(context.getSqlTokens()).thenReturn(Collections.singletonList(new RouteUnitAwareSQLTokenFixture(14, 16))); | ||
assertThat(new RouteSQLBuilder(context, createRouteUnit()).toSQL(), is("SELECT * FROM tbl_0 WHERE id=?")); | ||
} | ||
|
||
private RouteUnit createRouteUnit() { | ||
return new RouteUnit(mock(RouteMapper.class), Collections.singletonList(new RouteMapper("tbl", "tbl_0"))); | ||
} | ||
} |