Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
550bf240
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,发现更多精彩内容 >>
提交
550bf240
编写于
6月 01, 2016
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor config yaml test case
上级
1250a83e
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
187 addition
and
165 deletion
+187
-165
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/AllYamlTests.java
...om/dangdang/ddframe/rdb/sharding/config/AllYamlTests.java
+5
-5
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/yaml/YamlShardingConfigTest.java
...rame/rdb/sharding/config/yaml/YamlShardingConfigTest.java
+0
-80
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/yaml/YamlShardingDataSourceTest.java
.../rdb/sharding/config/yaml/YamlShardingDataSourceTest.java
+135
-0
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/yaml/YamlintegratedTest.java
.../ddframe/rdb/sharding/config/yaml/YamlintegratedTest.java
+47
-80
未找到文件。
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/AllYaml
ShardingConfig
Tests.java
→
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/AllYamlTests.java
浏览文件 @
550bf240
...
...
@@ -17,15 +17,15 @@
package
com.dangdang.ddframe.rdb.sharding.config
;
import
com.dangdang.ddframe.rdb.sharding.config.yaml.Yaml
ShardingConfig
Test
;
import
com.dangdang.ddframe.rdb.sharding.config.yaml.Yaml
integrated
Test
;
import
com.dangdang.ddframe.rdb.sharding.config.yaml.Yaml
Integrated
Test
;
import
com.dangdang.ddframe.rdb.sharding.config.yaml.Yaml
ShardingDataSource
Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.Suite
;
@RunWith
(
Suite
.
class
)
@Suite
.
SuiteClasses
({
YamlSharding
Config
Test
.
class
,
Yaml
i
ntegratedTest
.
class
YamlSharding
DataSource
Test
.
class
,
Yaml
I
ntegratedTest
.
class
})
public
class
AllYaml
ShardingConfig
Tests
{
public
class
AllYamlTests
{
}
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/yaml/YamlShardingConfigTest.java
已删除
100644 → 0
浏览文件 @
1250a83e
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package
com.dangdang.ddframe.rdb.sharding.config.yaml
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.URISyntaxException
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
javax.sql.DataSource
;
import
com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource
;
import
com.google.common.base.Function
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Sets
;
import
lombok.RequiredArgsConstructor
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.Parameterized
;
@RunWith
(
Parameterized
.
class
)
@RequiredArgsConstructor
public
class
YamlShardingConfigTest
extends
AbstractYamlShardingDataSourceTest
{
private
final
String
filePath
;
private
final
boolean
hasDataSource
;
@Parameterized
.
Parameters
(
name
=
"{index}:{0}-{1}"
)
public
static
Collection
init
()
{
return
Arrays
.
asList
(
new
Object
[][]{
{
"/configWithDataSourceWithoutProps.yaml"
,
true
},
{
"/configWithoutDataSourceWithoutProps.yaml"
,
false
},
{
"/configWithDataSourceWithProps.yaml"
,
true
},
{
"/configWithoutDataSourceWithProps.yaml"
,
false
},
});
}
@Test
public
void
testWithDataSource
()
throws
SQLException
,
URISyntaxException
,
IOException
{
File
yamlFile
=
new
File
(
YamlShardingConfigTest
.
class
.
getResource
(
filePath
).
toURI
());
DataSource
dataSource
;
if
(
hasDataSource
)
{
dataSource
=
new
YamlShardingDataSource
(
yamlFile
);
}
else
{
dataSource
=
new
YamlShardingDataSource
(
Maps
.
asMap
(
Sets
.
newHashSet
(
"db0"
,
"db1"
),
new
Function
<
String
,
DataSource
>()
{
@Override
public
DataSource
apply
(
final
String
key
)
{
return
createDataSource
(
key
);
}
}),
yamlFile
);
}
try
(
Connection
conn
=
dataSource
.
getConnection
();
Statement
stm
=
conn
.
createStatement
())
{
stm
.
executeQuery
(
"SELECT * FROM t_order"
);
stm
.
executeQuery
(
"SELECT * FROM t_order_item"
);
stm
.
executeQuery
(
"SELECT * FROM config"
);
}
}
}
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/yaml/YamlShardingDataSourceTest.java
0 → 100644
浏览文件 @
550bf240
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package
com.dangdang.ddframe.rdb.sharding.config.yaml
;
import
com.dangdang.ddframe.rdb.sharding.api.rule.DynamicDataNode
;
import
com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule
;
import
com.dangdang.ddframe.rdb.sharding.api.rule.TableRule
;
import
com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder
;
import
com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig
;
import
com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource
;
import
com.dangdang.ddframe.rdb.sharding.jdbc.ShardingContext
;
import
com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.dbcp.BasicDataSource
;
import
org.h2.Driver
;
import
org.junit.Test
;
import
org.yaml.snakeyaml.Yaml
;
import
org.yaml.snakeyaml.constructor.Constructor
;
import
javax.sql.DataSource
;
import
java.io.File
;
import
java.io.IOException
;
import
java.lang.reflect.Field
;
import
java.net.URISyntaxException
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.Map
;
import
static
org
.
hamcrest
.
CoreMatchers
.
hasItems
;
import
static
org
.
hamcrest
.
core
.
Is
.
is
;
import
static
org
.
hamcrest
.
core
.
IsCollectionContaining
.
hasItem
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
@Slf4j
public
class
YamlShardingDataSourceTest
{
@Test
public
void
assertAll
()
throws
IOException
,
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
{
ShardingRule
shardingRule
=
getShardingRule
(
"/config/config-all.yaml"
);
assertThat
(
shardingRule
.
getTableRules
().
size
(),
is
(
3
));
assertThat
(
shardingRule
.
getBindingTableRules
().
size
(),
is
(
1
));
assertThat
(
Arrays
.
asList
(
shardingRule
.
getTableRules
().
toArray
()),
hasItems
(
shardingRule
.
getBindingTableRules
().
iterator
().
next
().
getTableRules
().
toArray
()));
assertThat
(
shardingRule
.
getDataSourceRule
().
getDefaultDataSourceName
(),
is
(
"db0"
));
}
@Test
public
void
assertMin
()
throws
IOException
,
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
{
Map
<
String
,
DataSource
>
dataSourceMap
=
new
HashMap
<>(
1
);
dataSourceMap
.
put
(
"ds"
,
createDataSource
());
ShardingRule
shardingRule
=
getShardingRule
(
dataSourceMap
,
"/config/config-min.yaml"
);
assertThat
(
shardingRule
.
getTableRules
().
size
(),
is
(
1
));
}
@Test
public
void
assertDynamic
()
throws
IOException
,
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
{
Map
<
String
,
DataSource
>
dataSourceMap
=
new
HashMap
<>(
1
);
dataSourceMap
.
put
(
"ds"
,
createDataSource
());
ShardingRule
shardingRule
=
getShardingRule
(
dataSourceMap
,
"/config/config-dynamic.yaml"
);
int
i
=
0
;
for
(
TableRule
each
:
shardingRule
.
getTableRules
())
{
i
++;
assertThat
(
each
.
getActualTables
().
size
(),
is
(
2
));
assertThat
(
each
.
getActualTables
(),
hasItem
(
new
DynamicDataNode
(
"db0"
)));
assertThat
(
each
.
getActualTables
(),
hasItem
(
new
DynamicDataNode
(
"db1"
)));
switch
(
i
)
{
case
1
:
assertThat
(
each
.
getLogicTable
(),
is
(
"config"
));
break
;
case
2
:
assertThat
(
each
.
getLogicTable
(),
is
(
"t_order"
));
break
;
case
3
:
assertThat
(
each
.
getLogicTable
(),
is
(
"t_order_item"
));
break
;
default
:
fail
();
}
}
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
assertClassNotFound
()
throws
IOException
,
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
{
getShardingRule
(
"/config/config-classNotFound.yaml"
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
assertBindingError
()
throws
IOException
,
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
{
Map
<
String
,
DataSource
>
dataSourceMap
=
new
HashMap
<>(
1
);
dataSourceMap
.
put
(
"ds"
,
createDataSource
());
ShardingRule
shardingRule
=
getShardingRule
(
dataSourceMap
,
"/config/config-bindingError.yaml"
);
for
(
TableRule
tableRule
:
shardingRule
.
getBindingTableRules
().
iterator
().
next
().
getTableRules
())
{
log
.
info
(
tableRule
.
toString
());
}
}
private
ShardingRule
getShardingRule
(
final
String
fileName
)
throws
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
,
IOException
{
return
getShardingRule
(
new
YamlShardingDataSource
(
new
File
(
getClass
().
getResource
(
fileName
).
toURI
())));
}
private
ShardingRule
getShardingRule
(
final
Map
<
String
,
DataSource
>
dataSourceMap
,
final
String
fileName
)
throws
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
,
IOException
{
return
getShardingRule
(
new
YamlShardingDataSource
(
dataSourceMap
,
new
File
(
getClass
().
getResource
(
fileName
).
toURI
())));
}
private
ShardingRule
getShardingRule
(
final
ShardingDataSource
shardingDataSource
)
throws
NoSuchFieldException
,
IllegalAccessException
,
URISyntaxException
,
IOException
{
Field
field
=
ShardingDataSource
.
class
.
getDeclaredField
(
"shardingContext"
);
field
.
setAccessible
(
true
);
ShardingContext
shardingContext
=
(
ShardingContext
)
field
.
get
(
shardingDataSource
);
return
shardingContext
.
getShardingRule
();
}
private
DataSource
createDataSource
()
{
BasicDataSource
result
=
new
BasicDataSource
();
result
.
setDriverClassName
(
Driver
.
class
.
getName
());
result
.
setUrl
(
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"
);
result
.
setUsername
(
"sa"
);
result
.
setPassword
(
""
);
return
result
;
}
}
sharding-jdbc-config-parent/sharding-jdbc-config-yaml/src/test/java/com/dangdang/ddframe/rdb/sharding/config/yaml/YamlintegratedTest.java
浏览文件 @
550bf240
...
...
@@ -17,97 +17,64 @@
package
com.dangdang.ddframe.rdb.sharding.config.yaml
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.URISyntaxException
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Collection
;
import
javax.sql.DataSource
;
import
com.dangdang.ddframe.rdb.sharding.api.rule.DynamicDataNode
;
import
com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule
;
import
com.dangdang.ddframe.rdb.sharding.api.rule.TableRule
;
import
com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder
;
import
com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.dbcp.BasicDataSource
;
import
com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource
;
import
com.google.common.base.Function
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Sets
;
import
lombok.RequiredArgsConstructor
;
import
org.junit.Test
;
import
org.
yaml.snakeyaml.Yaml
;
import
org.
yaml.snakeyaml.constructor.Constructor
;
import
org.
junit.runner.RunWith
;
import
org.
junit.runners.Parameterized
;
import
static
org
.
hamcrest
.
CoreMatchers
.
hasItems
;
import
static
org
.
hamcrest
.
core
.
Is
.
is
;
import
static
org
.
hamcrest
.
core
.
IsCollectionContaining
.
hasItem
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
@RunWith
(
Parameterized
.
class
)
@RequiredArgsConstructor
public
class
YamlIntegratedTest
extends
AbstractYamlShardingDataSourceTest
{
@Slf4j
public
class
YamlintegratedTest
{
private
final
String
filePath
;
@Test
public
void
testAll
()
{
Yaml
yaml
=
new
Yaml
(
new
Constructor
(
ShardingRuleConfig
.
class
));
ShardingRuleConfig
config
=
(
ShardingRuleConfig
)
yaml
.
load
(
YamlintegratedTest
.
class
.
getResourceAsStream
(
"/config/config-all.yaml"
));
ShardingRule
shardingRule
=
new
ShardingRuleBuilder
(
"config-all.yaml"
,
config
).
build
();
assertThat
(
shardingRule
.
getTableRules
().
size
(),
is
(
3
));
assertThat
(
shardingRule
.
getBindingTableRules
().
size
(),
is
(
1
));
assertThat
(
Arrays
.
asList
(
shardingRule
.
getTableRules
().
toArray
()),
hasItems
(
shardingRule
.
getBindingTableRules
().
iterator
().
next
().
getTableRules
().
toArray
()));
assertThat
(
shardingRule
.
getDataSourceRule
().
getDefaultDataSourceName
(),
is
(
"db0"
));
}
private
final
boolean
hasDataSource
;
@
Test
public
void
testMin
()
{
Yaml
yaml
=
new
Yaml
(
new
Constructor
(
ShardingRuleConfig
.
class
));
Map
<
String
,
DataSource
>
dsMap
=
new
HashMap
<>();
dsMap
.
put
(
"ds"
,
new
BasicDataSource
());
ShardingRuleConfig
config
=
(
ShardingRuleConfig
)
yaml
.
load
(
YamlintegratedTest
.
class
.
getResourceAsStream
(
"/config/config-min.yaml"
));
ShardingRule
shardingRule
=
new
ShardingRuleBuilder
(
"config-min.yaml"
,
dsMap
,
config
).
build
();
assertThat
(
shardingRule
.
getTableRules
().
size
(),
is
(
1
)
);
@
Parameterized
.
Parameters
(
name
=
"{index}:{0}-{1}"
)
public
static
Collection
init
()
{
return
Arrays
.
asList
(
new
Object
[][]{
{
"/configWithDataSourceWithoutProps.yaml"
,
true
},
{
"/configWithoutDataSourceWithoutProps.yaml"
,
false
},
{
"/configWithDataSourceWithProps.yaml"
,
true
},
{
"/configWithoutDataSourceWithProps.yaml"
,
false
},
}
);
}
@Test
public
void
testDynamic
()
{
Yaml
yaml
=
new
Yaml
(
new
Constructor
(
ShardingRuleConfig
.
class
));
Map
<
String
,
DataSource
>
dsMap
=
new
HashMap
<>();
dsMap
.
put
(
"ds"
,
new
BasicDataSource
());
ShardingRuleConfig
config
=
(
ShardingRuleConfig
)
yaml
.
load
(
YamlintegratedTest
.
class
.
getResourceAsStream
(
"/config/config-dynamic.yaml"
));
ShardingRule
shardingRule
=
new
ShardingRuleBuilder
(
"config-dynamic.yaml"
,
dsMap
,
config
).
build
();
int
i
=
0
;
for
(
TableRule
each
:
shardingRule
.
getTableRules
())
{
i
++;
assertThat
(
each
.
getActualTables
().
size
(),
is
(
2
));
assertThat
(
each
.
getActualTables
(),
hasItem
(
new
DynamicDataNode
(
"db0"
)));
assertThat
(
each
.
getActualTables
(),
hasItem
(
new
DynamicDataNode
(
"db1"
)));
switch
(
i
)
{
case
1
:
assertThat
(
each
.
getLogicTable
(),
is
(
"config"
));
break
;
case
2
:
assertThat
(
each
.
getLogicTable
(),
is
(
"t_order"
));
break
;
case
3
:
assertThat
(
each
.
getLogicTable
(),
is
(
"t_order_item"
));
break
;
default
:
fail
();
}
public
void
testWithDataSource
()
throws
SQLException
,
URISyntaxException
,
IOException
{
File
yamlFile
=
new
File
(
YamlIntegratedTest
.
class
.
getResource
(
filePath
).
toURI
());
DataSource
dataSource
;
if
(
hasDataSource
)
{
dataSource
=
new
YamlShardingDataSource
(
yamlFile
);
}
else
{
dataSource
=
new
YamlShardingDataSource
(
Maps
.
asMap
(
Sets
.
newHashSet
(
"db0"
,
"db1"
),
new
Function
<
String
,
DataSource
>()
{
@Override
public
DataSource
apply
(
final
String
key
)
{
return
createDataSource
(
key
);
}
}),
yamlFile
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testClassNotFound
()
{
Yaml
yaml
=
new
Yaml
(
new
Constructor
(
ShardingRuleConfig
.
class
));
ShardingRuleConfig
config
=
(
ShardingRuleConfig
)
yaml
.
load
(
YamlintegratedTest
.
class
.
getResourceAsStream
(
"/config/config-classNotFound.yaml"
));
new
ShardingRuleBuilder
(
config
).
build
();
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testBindingError
()
{
Yaml
yaml
=
new
Yaml
(
new
Constructor
(
ShardingRuleConfig
.
class
));
Map
<
String
,
DataSource
>
dsMap
=
new
HashMap
<>();
dsMap
.
put
(
"ds"
,
new
BasicDataSource
());
ShardingRuleConfig
config
=
(
ShardingRuleConfig
)
yaml
.
load
(
YamlintegratedTest
.
class
.
getResourceAsStream
(
"/config/config-bindingError.yaml"
));
ShardingRule
shardingRule
=
new
ShardingRuleBuilder
(
"config-bindingError.yaml"
,
dsMap
,
config
).
build
();
for
(
TableRule
tableRule
:
shardingRule
.
getBindingTableRules
().
iterator
().
next
().
getTableRules
())
{
log
.
info
(
tableRule
.
toString
());
try
(
Connection
conn
=
dataSource
.
getConnection
();
Statement
stm
=
conn
.
createStatement
())
{
stm
.
executeQuery
(
"SELECT * FROM t_order"
);
stm
.
executeQuery
(
"SELECT * FROM t_order_item"
);
stm
.
executeQuery
(
"SELECT * FROM config"
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录