Skip to content

Commit

Permalink
Merge pull request apache#6781 from terrymanu/dev
Browse files Browse the repository at this point in the history
Add test cases for SQLBuilder's impl
  • Loading branch information
menghaoranss authored Aug 11, 2020
2 parents 0a68286 + c912aae commit 0be4e04
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.shardingsphere.infra.rewrite.sql.impl;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
import org.apache.shardingsphere.infra.rewrite.sql.SQLBuilder;
Expand All @@ -30,7 +29,6 @@
* Abstract SQL builder.
*/
@RequiredArgsConstructor
@Getter
public abstract class AbstractSQLBuilder implements SQLBuilder {

private final SQLRewriteContext context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.rewrite.impl;
package org.apache.shardingsphere.infra.rewrite.parameter.builder.impl;

import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
import org.junit.Test;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.rewrite.impl;
package org.apache.shardingsphere.infra.rewrite.parameter.builder.impl;

import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
import org.junit.Before;
import org.junit.Test;

Expand Down
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();
}
}
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,34 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.rewrite.impl;
package org.apache.shardingsphere.infra.rewrite.sql.impl;

import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
import org.apache.shardingsphere.infra.rewrite.sql.SQLBuilder;
import org.apache.shardingsphere.infra.rewrite.sql.impl.DefaultSQLBuilder;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.rewrite.sql.fixture.SQLTokenFixture;
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 DefaultSQLBuilderTest {

@Test
public void assertToSQL() {
SQLRewriteContext context = new SQLRewriteContext(mock(SchemaMetaData.class), mock(SQLStatementContext.class), "SELECT * FROM t_config", Collections.emptyList());
SQLBuilder sqlBuilderWithoutTokens = new DefaultSQLBuilder(context);
assertThat(sqlBuilderWithoutTokens.toSQL(), is("SELECT * FROM t_config"));
public void assertToSQLWithEmptySQLToken() {
SQLRewriteContext context = mock(SQLRewriteContext.class);
when(context.getSql()).thenReturn("SELECT * FROM tbl WHERE id=?");
when(context.getSqlTokens()).thenReturn(Collections.emptyList());
assertThat(new DefaultSQLBuilder(context).toSQL(), is("SELECT * FROM tbl WHERE id=?"));
}

@Test
public void assertToSQLWithSQLToken() {
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 DefaultSQLBuilder(context).toSQL(), is("SELECT * FROM XXX WHERE id=?"));
}
}
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")));
}
}

0 comments on commit 0be4e04

Please sign in to comment.