Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
8742ca45
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8742ca45
编写于
3月 19, 2019
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
for #1864, add BaseShardingEngineTest
上级
e22101d0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
98 addition
and
95 deletion
+98
-95
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/BaseShardingEngineTest.java
...rg/apache/shardingsphere/core/BaseShardingEngineTest.java
+81
-0
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/PreparedQueryShardingEngineTest.java
.../shardingsphere/core/PreparedQueryShardingEngineTest.java
+9
-48
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/SimpleQueryShardingEngineTest.java
...he/shardingsphere/core/SimpleQueryShardingEngineTest.java
+8
-47
未找到文件。
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/BaseShardingEngineTest.java
0 → 100644
浏览文件 @
8742ca45
/*
* 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.core
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.api.hint.HintManager
;
import
org.apache.shardingsphere.core.constant.properties.ShardingProperties
;
import
org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant
;
import
org.apache.shardingsphere.core.parse.parser.sql.dql.select.SelectStatement
;
import
org.apache.shardingsphere.core.route.RouteUnit
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.core.route.type.RoutingResult
;
import
org.apache.shardingsphere.core.route.type.TableUnit
;
import
org.junit.Test
;
import
java.util.List
;
import
java.util.Properties
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
@RequiredArgsConstructor
@Getter
public
abstract
class
BaseShardingEngineTest
{
private
final
String
sql
;
private
final
List
<
Object
>
parameters
;
protected
final
ShardingProperties
getShardingProperties
()
{
Properties
result
=
new
Properties
();
result
.
setProperty
(
ShardingPropertiesConstant
.
SQL_SHOW
.
getKey
(),
Boolean
.
TRUE
.
toString
());
return
new
ShardingProperties
(
result
);
}
protected
final
SQLRouteResult
createSQLRouteResult
()
{
SQLRouteResult
result
=
new
SQLRouteResult
(
new
SelectStatement
());
RoutingResult
routingResult
=
new
RoutingResult
();
routingResult
.
getTableUnits
().
getTableUnits
().
add
(
new
TableUnit
(
"ds"
));
result
.
setRoutingResult
(
routingResult
);
return
result
;
}
protected
final
void
assertSQLRouteResult
(
final
SQLRouteResult
actual
)
{
assertThat
(
actual
.
getRouteUnits
().
size
(),
is
(
1
));
RouteUnit
actualRouteUnit
=
actual
.
getRouteUnits
().
iterator
().
next
();
assertThat
(
actualRouteUnit
.
getDataSourceName
(),
is
(
"ds"
));
assertThat
(
actualRouteUnit
.
getSqlUnit
().
getSql
(),
is
(
sql
));
assertThat
(
actualRouteUnit
.
getSqlUnit
().
getParameters
(),
is
(
parameters
));
}
@Test
public
void
assertShardWithHintDatabaseShardingOnly
()
{
HintManager
.
getInstance
().
setDatabaseShardingValue
(
"1"
);
assertShard
();
HintManager
.
clear
();
}
@Test
public
void
assertShardWithoutHint
()
{
assertShard
();
}
protected
abstract
void
assertShard
();
}
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/PreparedQueryShardingEngineTest.java
浏览文件 @
8742ca45
...
...
@@ -18,54 +18,40 @@
package
org.apache.shardingsphere.core
;
import
lombok.SneakyThrows
;
import
org.apache.shardingsphere.api.hint.HintManager
;
import
org.apache.shardingsphere.core.constant.DatabaseType
;
import
org.apache.shardingsphere.core.constant.properties.ShardingProperties
;
import
org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant
;
import
org.apache.shardingsphere.core.metadata.ShardingMetaData
;
import
org.apache.shardingsphere.core.parse.cache.ParsingResultCache
;
import
org.apache.shardingsphere.core.parse.parser.sql.dql.select.SelectStatement
;
import
org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine
;
import
org.apache.shardingsphere.core.route.RouteUnit
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.core.route.type.RoutingResult
;
import
org.apache.shardingsphere.core.route.type.TableUnit
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
java.lang.reflect.Field
;
import
java.util.Collections
;
import
java.util.Properties
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
final
class
PreparedQueryShardingEngineTest
{
public
final
class
PreparedQueryShardingEngineTest
extends
BaseShardingEngineTest
{
@Mock
private
PreparedStatementRoutingEngine
routingEngine
;
private
PreparedQueryShardingEngine
shardingEngine
;
public
PreparedQueryShardingEngineTest
()
{
super
(
"SELECT ?"
,
Collections
.<
Object
>
singletonList
(
1
));
}
@Before
public
void
setUp
()
{
shardingEngine
=
new
PreparedQueryShardingEngine
(
"SELECT 1"
,
mock
(
ShardingRule
.
class
),
getShardingProperties
(),
mock
(
ShardingMetaData
.
class
),
DatabaseType
.
MySQL
,
new
ParsingResultCache
());
shardingEngine
=
new
PreparedQueryShardingEngine
(
getSql
()
,
mock
(
ShardingRule
.
class
),
getShardingProperties
(),
mock
(
ShardingMetaData
.
class
),
DatabaseType
.
MySQL
,
new
ParsingResultCache
());
setRoutingEngine
();
}
private
ShardingProperties
getShardingProperties
()
{
Properties
result
=
new
Properties
();
result
.
setProperty
(
ShardingPropertiesConstant
.
SQL_SHOW
.
getKey
(),
Boolean
.
TRUE
.
toString
());
return
new
ShardingProperties
(
result
);
}
@SneakyThrows
private
void
setRoutingEngine
()
{
Field
field
=
PreparedQueryShardingEngine
.
class
.
getDeclaredField
(
"routingEngine"
);
...
...
@@ -73,33 +59,8 @@ public final class PreparedQueryShardingEngineTest {
field
.
set
(
shardingEngine
,
routingEngine
);
}
@Test
public
void
assertShardWithHintDatabaseShardingOnly
()
{
HintManager
.
getInstance
().
setDatabaseShardingValue
(
"1"
);
when
(
routingEngine
.
route
(
Collections
.<
Object
>
singletonList
(
1
))).
thenReturn
(
createSQLRouteResult
());
assertSQLRouteResult
(
shardingEngine
.
shard
(
"SELECT ?"
,
Collections
.<
Object
>
singletonList
(
1
)));
HintManager
.
clear
();
}
@Test
public
void
assertShardWithoutHint
()
{
when
(
routingEngine
.
route
(
Collections
.<
Object
>
singletonList
(
1
))).
thenReturn
(
createSQLRouteResult
());
assertSQLRouteResult
(
shardingEngine
.
shard
(
"SELECT ?"
,
Collections
.<
Object
>
singletonList
(
1
)));
}
private
SQLRouteResult
createSQLRouteResult
()
{
SQLRouteResult
result
=
new
SQLRouteResult
(
new
SelectStatement
());
RoutingResult
routingResult
=
new
RoutingResult
();
routingResult
.
getTableUnits
().
getTableUnits
().
add
(
new
TableUnit
(
"ds"
));
result
.
setRoutingResult
(
routingResult
);
return
result
;
}
private
void
assertSQLRouteResult
(
final
SQLRouteResult
actual
)
{
assertThat
(
actual
.
getRouteUnits
().
size
(),
is
(
1
));
RouteUnit
actualRouteUnit
=
actual
.
getRouteUnits
().
iterator
().
next
();
assertThat
(
actualRouteUnit
.
getDataSourceName
(),
is
(
"ds"
));
assertThat
(
actualRouteUnit
.
getSqlUnit
().
getSql
(),
is
(
"SELECT ?"
));
assertThat
(
actualRouteUnit
.
getSqlUnit
().
getParameters
(),
is
(
Collections
.<
Object
>
singletonList
(
1
)));
protected
void
assertShard
()
{
when
(
routingEngine
.
route
(
getParameters
())).
thenReturn
(
createSQLRouteResult
());
assertSQLRouteResult
(
shardingEngine
.
shard
(
getSql
(),
getParameters
()));
}
}
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/SimpleQueryShardingEngineTest.java
浏览文件 @
8742ca45
...
...
@@ -18,54 +18,40 @@
package
org.apache.shardingsphere.core
;
import
lombok.SneakyThrows
;
import
org.apache.shardingsphere.api.hint.HintManager
;
import
org.apache.shardingsphere.core.constant.DatabaseType
;
import
org.apache.shardingsphere.core.constant.properties.ShardingProperties
;
import
org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant
;
import
org.apache.shardingsphere.core.metadata.ShardingMetaData
;
import
org.apache.shardingsphere.core.parse.cache.ParsingResultCache
;
import
org.apache.shardingsphere.core.parse.parser.sql.dql.select.SelectStatement
;
import
org.apache.shardingsphere.core.route.RouteUnit
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.core.route.StatementRoutingEngine
;
import
org.apache.shardingsphere.core.route.type.RoutingResult
;
import
org.apache.shardingsphere.core.route.type.TableUnit
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
java.lang.reflect.Field
;
import
java.util.Collections
;
import
java.util.Properties
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
final
class
SimpleQueryShardingEngineTest
{
public
final
class
SimpleQueryShardingEngineTest
extends
BaseShardingEngineTest
{
@Mock
private
StatementRoutingEngine
routingEngine
;
private
SimpleQueryShardingEngine
shardingEngine
;
public
SimpleQueryShardingEngineTest
()
{
super
(
"SELECT 1"
,
Collections
.
emptyList
());
}
@Before
public
void
setUp
()
{
shardingEngine
=
new
SimpleQueryShardingEngine
(
mock
(
ShardingRule
.
class
),
getShardingProperties
(),
mock
(
ShardingMetaData
.
class
),
DatabaseType
.
MySQL
,
new
ParsingResultCache
());
setRoutingEngine
();
}
private
ShardingProperties
getShardingProperties
()
{
Properties
result
=
new
Properties
();
result
.
setProperty
(
ShardingPropertiesConstant
.
SQL_SHOW
.
getKey
(),
Boolean
.
TRUE
.
toString
());
return
new
ShardingProperties
(
result
);
}
@SneakyThrows
private
void
setRoutingEngine
()
{
Field
field
=
SimpleQueryShardingEngine
.
class
.
getDeclaredField
(
"routingEngine"
);
...
...
@@ -73,33 +59,8 @@ public final class SimpleQueryShardingEngineTest {
field
.
set
(
shardingEngine
,
routingEngine
);
}
@Test
public
void
assertShardWithHintDatabaseShardingOnly
()
{
HintManager
.
getInstance
().
setDatabaseShardingValue
(
"1"
);
when
(
routingEngine
.
route
(
"SELECT 1"
)).
thenReturn
(
createSQLRouteResult
());
assertSQLRouteResult
(
shardingEngine
.
shard
(
"SELECT 1"
,
Collections
.
emptyList
()));
HintManager
.
clear
();
}
@Test
public
void
assertShardWithoutHint
()
{
when
(
routingEngine
.
route
(
"SELECT 1"
)).
thenReturn
(
createSQLRouteResult
());
assertSQLRouteResult
(
shardingEngine
.
shard
(
"SELECT 1"
,
Collections
.
emptyList
()));
}
private
SQLRouteResult
createSQLRouteResult
()
{
SQLRouteResult
result
=
new
SQLRouteResult
(
new
SelectStatement
());
RoutingResult
routingResult
=
new
RoutingResult
();
routingResult
.
getTableUnits
().
getTableUnits
().
add
(
new
TableUnit
(
"ds"
));
result
.
setRoutingResult
(
routingResult
);
return
result
;
}
private
void
assertSQLRouteResult
(
final
SQLRouteResult
actual
)
{
assertThat
(
actual
.
getRouteUnits
().
size
(),
is
(
1
));
RouteUnit
actualRouteUnit
=
actual
.
getRouteUnits
().
iterator
().
next
();
assertThat
(
actualRouteUnit
.
getDataSourceName
(),
is
(
"ds"
));
assertThat
(
actualRouteUnit
.
getSqlUnit
().
getSql
(),
is
(
"SELECT 1"
));
assertThat
(
actualRouteUnit
.
getSqlUnit
().
getParameters
(),
is
(
Collections
.
emptyList
()));
protected
void
assertShard
()
{
when
(
routingEngine
.
route
(
getSql
())).
thenReturn
(
createSQLRouteResult
());
assertSQLRouteResult
(
shardingEngine
.
shard
(
getSql
(),
getParameters
()));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录