Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
71c4014a
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,发现更多精彩内容 >>
未验证
提交
71c4014a
编写于
5月 12, 2020
作者:
Z
zhyee
提交者:
GitHub
5月 12, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add unit test (#5543)
上级
d4c7440b
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
162 addition
and
3 deletion
+162
-3
shadow-core/shadow-core-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java
...rdingsphere/shadow/route/engine/ShadowRouteDecorator.java
+3
-3
shadow-core/shadow-core-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecoratorTest.java
...gsphere/shadow/route/engine/ShadowRouteDecoratorTest.java
+159
-0
未找到文件。
shadow-core/shadow-core-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java
浏览文件 @
71c4014a
...
...
@@ -59,9 +59,9 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
return
new
RouteContext
(
sqlStatementContext
,
parameters
,
routeResult
);
}
if
(
isShadowSQL
(
routeContext
,
shadowRule
))
{
shadowRule
.
getShadowMappings
().
keySet
().
forEach
(
each
->
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
())));
}
else
{
shadowRule
.
getShadowMappings
().
values
().
forEach
(
each
->
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
())));
}
else
{
shadowRule
.
getShadowMappings
().
keySet
().
forEach
(
each
->
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
())));
}
return
new
RouteContext
(
sqlStatementContext
,
parameters
,
routeResult
);
}
...
...
@@ -90,7 +90,7 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
private
boolean
isShadowSQL
(
final
RouteContext
routeContext
,
final
ShadowRule
shadowRule
)
{
List
<
Object
>
parameters
=
routeContext
.
getParameters
();
SQLStatementContext
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
ShadowDataSourceRouter
shadowDataSourceRouter
=
parameters
==
null
?
new
SimpleShadowDataSourceRouter
(
shadowRule
,
sqlStatementContext
)
ShadowDataSourceRouter
shadowDataSourceRouter
=
parameters
.
size
()
==
0
?
new
SimpleShadowDataSourceRouter
(
shadowRule
,
sqlStatementContext
)
:
new
PreparedShadowDataSourceRouter
(
shadowRule
,
sqlStatementContext
,
parameters
);
return
shadowDataSourceRouter
.
isShadowSQL
();
}
...
...
shadow-core/shadow-core-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecoratorTest.java
0 → 100644
浏览文件 @
71c4014a
/*
* 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.shadow.route.engine
;
import
org.apache.shardingsphere.api.config.shadow.ShadowRuleConfiguration
;
import
org.apache.shardingsphere.core.rule.ShadowRule
;
import
org.apache.shardingsphere.sql.parser.binder.segment.insert.values.InsertValueContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateTableStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext
;
import
org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateTableStatement
;
import
org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement
;
import
org.apache.shardingsphere.underlying.common.config.properties.ConfigurationProperties
;
import
org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData
;
import
org.apache.shardingsphere.underlying.route.context.RouteContext
;
import
org.apache.shardingsphere.underlying.route.context.RouteMapper
;
import
org.apache.shardingsphere.underlying.route.context.RouteResult
;
import
org.apache.shardingsphere.underlying.route.context.RouteUnit
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
java.util.Collections
;
import
java.util.Iterator
;
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
class
ShadowRouteDecoratorTest
{
private
static
final
String
SHADOW_COLUMN
=
"is_shadow"
;
private
static
final
String
ACTUAL_DATASOURCE
=
"ds"
;
private
static
final
String
SHADOW_DATASOURCE
=
"shadow_ds"
;
@Mock
private
InsertStatementContext
sqlStatementContext
;
@Mock
private
InsertStatement
insertStatement
;
@Mock
private
CreateTableStatementContext
createTableStatementContext
;
@Mock
private
CreateTableStatement
createTableStatement
;
private
ShadowRouteDecorator
routeDecorator
;
private
ShadowRule
shadowRule
;
@Before
public
void
setUp
()
{
routeDecorator
=
new
ShadowRouteDecorator
();
ShadowRuleConfiguration
shadowRuleConfiguration
=
new
ShadowRuleConfiguration
();
shadowRuleConfiguration
.
setColumn
(
SHADOW_COLUMN
);
shadowRuleConfiguration
.
setShadowMappings
(
Collections
.
singletonMap
(
ACTUAL_DATASOURCE
,
SHADOW_DATASOURCE
));
shadowRule
=
new
ShadowRule
(
shadowRuleConfiguration
);
}
@Test
public
void
assertDecorateToShadowWithOutRouteUnit
()
{
RouteContext
routeContext
=
mockSQLRouteContextForShadow
();
RouteContext
actual
=
routeDecorator
.
decorate
(
routeContext
,
mock
(
ShardingSphereMetaData
.
class
),
shadowRule
,
new
ConfigurationProperties
(
new
Properties
()));
Iterator
<
String
>
routedDataSourceNames
=
actual
.
getRouteResult
().
getActualDataSourceNames
().
iterator
();
assertThat
(
routedDataSourceNames
.
next
(),
is
(
SHADOW_DATASOURCE
));
}
@Test
public
void
assertDecorateToActualWithOutRouteUnit
()
{
RouteContext
routeContext
=
mockSQLRouteContext
();
RouteContext
actual
=
routeDecorator
.
decorate
(
routeContext
,
mock
(
ShardingSphereMetaData
.
class
),
shadowRule
,
new
ConfigurationProperties
(
new
Properties
()));
Iterator
<
String
>
routedDataSourceNames
=
actual
.
getRouteResult
().
getActualDataSourceNames
().
iterator
();
assertThat
(
routedDataSourceNames
.
next
(),
is
(
ACTUAL_DATASOURCE
));
}
@Test
public
void
assertNonDMLStatementWithOutRouteUnit
()
{
RouteContext
routeContext
=
mockNonDMLSQLRouteContext
();
RouteContext
actual
=
routeDecorator
.
decorate
(
routeContext
,
mock
(
ShardingSphereMetaData
.
class
),
shadowRule
,
new
ConfigurationProperties
(
new
Properties
()));
assertThat
(
actual
.
getRouteResult
().
getRouteUnits
().
size
(),
is
(
2
));
assertThat
(
actual
.
getRouteResult
().
getActualDataSourceNames
().
contains
(
SHADOW_DATASOURCE
),
is
(
true
));
assertThat
(
actual
.
getRouteResult
().
getActualDataSourceNames
().
contains
(
ACTUAL_DATASOURCE
),
is
(
true
));
}
@Test
public
void
assertDecorateToShadowWithRouteUnit
()
{
RouteContext
routeContext
=
mockSQLRouteContextForShadow
();
routeContext
.
getRouteResult
().
getRouteUnits
().
add
(
mockRouteUnit
());
RouteContext
actual
=
routeDecorator
.
decorate
(
routeContext
,
mock
(
ShardingSphereMetaData
.
class
),
shadowRule
,
new
ConfigurationProperties
(
new
Properties
()));
assertThat
(
actual
.
getRouteResult
().
getRouteUnits
().
size
(),
is
(
1
));
assertThat
(
actual
.
getRouteResult
().
getActualDataSourceNames
().
contains
(
SHADOW_DATASOURCE
),
is
(
true
));
}
@Test
public
void
assertDecorateToActualWithRouteUnit
()
{
RouteContext
routeContext
=
mockSQLRouteContext
();
routeContext
.
getRouteResult
().
getRouteUnits
().
add
(
mockRouteUnit
());
RouteContext
actual
=
routeDecorator
.
decorate
(
routeContext
,
mock
(
ShardingSphereMetaData
.
class
),
shadowRule
,
new
ConfigurationProperties
(
new
Properties
()));
Iterator
<
String
>
routedDataSourceNames
=
actual
.
getRouteResult
().
getActualDataSourceNames
().
iterator
();
assertThat
(
routedDataSourceNames
.
next
(),
is
(
ACTUAL_DATASOURCE
));
}
@Test
public
void
assertNonDMLStatementWithRouteUnit
()
{
RouteContext
routeContext
=
mockNonDMLSQLRouteContext
();
routeContext
.
getRouteResult
().
getRouteUnits
().
add
(
mockRouteUnit
());
RouteContext
actual
=
routeDecorator
.
decorate
(
routeContext
,
mock
(
ShardingSphereMetaData
.
class
),
shadowRule
,
new
ConfigurationProperties
(
new
Properties
()));
assertThat
(
actual
.
getRouteResult
().
getRouteUnits
().
size
(),
is
(
2
));
assertThat
(
actual
.
getRouteResult
().
getActualDataSourceNames
().
contains
(
SHADOW_DATASOURCE
),
is
(
true
));
assertThat
(
actual
.
getRouteResult
().
getActualDataSourceNames
().
contains
(
ACTUAL_DATASOURCE
),
is
(
true
));
}
private
RouteContext
mockSQLRouteContextForShadow
()
{
when
(
sqlStatementContext
.
getSqlStatement
()).
thenReturn
(
insertStatement
);
when
(
sqlStatementContext
.
getDescendingColumnNames
()).
thenReturn
(
Collections
.
singletonList
(
SHADOW_COLUMN
).
iterator
());
when
(
sqlStatementContext
.
getColumnNames
()).
thenReturn
(
Collections
.
singletonList
(
SHADOW_COLUMN
));
InsertValueContext
insertValueContext
=
mock
(
InsertValueContext
.
class
);
when
(
insertValueContext
.
getValue
(
0
)).
thenReturn
(
true
);
when
(
sqlStatementContext
.
getInsertValueContexts
()).
thenReturn
(
Collections
.
singletonList
(
insertValueContext
));
return
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
}
private
RouteContext
mockSQLRouteContext
()
{
when
(
sqlStatementContext
.
getSqlStatement
()).
thenReturn
(
insertStatement
);
return
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
}
private
RouteContext
mockNonDMLSQLRouteContext
()
{
when
(
createTableStatementContext
.
getSqlStatement
()).
thenReturn
(
createTableStatement
);
return
new
RouteContext
(
createTableStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
}
private
RouteUnit
mockRouteUnit
()
{
return
new
RouteUnit
(
new
RouteMapper
(
ACTUAL_DATASOURCE
,
ACTUAL_DATASOURCE
),
Collections
.
singletonList
(
new
RouteMapper
(
"table"
,
"table_0"
)));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录