Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
suliangchun
dbeaver
提交
4b825a80
D
dbeaver
项目概览
suliangchun
/
dbeaver
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dbeaver
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4b825a80
编写于
4月 05, 2019
作者:
J
John Bergqvist
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unit tests for remainder of CommonUtils
These tests were written using Diffblue Cover.
上级
65735a99
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
302 addition
and
0 deletion
+302
-0
bundles/org.jkiss.utils/tests/org/jkiss/utils/CommonUtilsTest.java
...rg.jkiss.utils/tests/org/jkiss/utils/CommonUtilsTest.java
+302
-0
未找到文件。
bundles/org.jkiss.utils/tests/org/jkiss/utils/CommonUtilsTest.java
浏览文件 @
4b825a80
package
org.jkiss.utils
;
package
org.jkiss.utils
;
import
java.lang.reflect.InvocationTargetException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.List
;
import
org.jkiss.utils.CommonUtils
;
import
org.jkiss.utils.CommonUtils
;
import
org.junit.rules.ExpectedException
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
static
org
.
mockito
.
AdditionalMatchers
.
or
;
import
static
org
.
mockito
.
AdditionalMatchers
.
or
;
...
@@ -17,6 +22,17 @@ import org.powermock.modules.junit4.PowerMockRunner;
...
@@ -17,6 +22,17 @@ import org.powermock.modules.junit4.PowerMockRunner;
@RunWith
(
PowerMockRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
public
class
CommonUtilsTest
{
public
class
CommonUtilsTest
{
@Rule
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
@Test
public
void
testIsJavaIdentifier
()
{
Assert
.
assertEquals
(
false
,
CommonUtils
.
isJavaIdentifier
(
""
));
Assert
.
assertEquals
(
false
,
CommonUtils
.
isJavaIdentifier
(
"|"
));
Assert
.
assertEquals
(
false
,
CommonUtils
.
isJavaIdentifier
(
"a-b"
));
Assert
.
assertEquals
(
true
,
CommonUtils
.
isJavaIdentifier
(
"aa"
));
}
@Test
@Test
public
void
testEscapeJavaString
()
throws
Exception
{
public
void
testEscapeJavaString
()
throws
Exception
{
Assert
.
assertEquals
(
""
,
CommonUtils
.
escapeJavaString
(
""
));
Assert
.
assertEquals
(
""
,
CommonUtils
.
escapeJavaString
(
""
));
...
@@ -79,6 +95,89 @@ public class CommonUtilsTest {
...
@@ -79,6 +95,89 @@ public class CommonUtilsTest {
Assert
.
assertEquals
(
"Ab|Cd"
,
CommonUtils
.
toCamelCase
(
"ab|cd"
));
Assert
.
assertEquals
(
"Ab|Cd"
,
CommonUtils
.
toCamelCase
(
"ab|cd"
));
}
}
@Test
public
void
testNotNull
()
{
final
Object
value
=
"value"
;
final
Object
defaultValue
=
"defaultValue"
;
Assert
.
assertEquals
(
value
,
CommonUtils
.
notNull
(
value
,
defaultValue
));
Assert
.
assertEquals
(
defaultValue
,
CommonUtils
.
notNull
(
null
,
defaultValue
));
}
@Test
public
void
testIsEmptyCharSequence
()
{
final
CharSequence
nullValue
=
null
;
final
CharSequence
emptyValue
=
""
;
final
CharSequence
value
=
"abc"
;
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
nullValue
));
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
emptyValue
));
Assert
.
assertFalse
(
CommonUtils
.
isEmpty
(
value
));
}
@Test
public
void
testIsEmptyString
()
{
final
String
nullValue
=
null
;
final
String
emptyValue
=
""
;
final
String
value
=
"abc"
;
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
nullValue
));
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
emptyValue
));
Assert
.
assertFalse
(
CommonUtils
.
isEmpty
(
value
));
}
@Test
public
void
testIsNotEmpty
()
{
final
String
nullValue
=
null
;
final
String
emptyValue
=
""
;
final
String
value
=
"abc"
;
Assert
.
assertFalse
(
CommonUtils
.
isNotEmpty
(
nullValue
));
Assert
.
assertFalse
(
CommonUtils
.
isNotEmpty
(
emptyValue
));
Assert
.
assertTrue
(
CommonUtils
.
isNotEmpty
(
value
));
}
@Test
public
void
testIsEmptyCollection
()
{
final
ArrayList
<
Character
>
nullValue
=
null
;
final
ArrayList
<
Character
>
emptyValue
=
new
ArrayList
<>();
final
ArrayList
<
Character
>
value
=
new
ArrayList
<>();
value
.
add
(
'a'
);
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
nullValue
));
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
emptyValue
));
Assert
.
assertFalse
(
CommonUtils
.
isEmpty
(
value
));
}
@Test
public
void
testIsEmptyMap
()
{
final
HashMap
<
Integer
,
Character
>
nullValue
=
null
;
final
HashMap
<
Integer
,
Character
>
emptyValue
=
new
HashMap
<>();
final
HashMap
<
Integer
,
Character
>
value
=
new
HashMap
<>();
value
.
put
(
0
,
'a'
);
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
nullValue
));
Assert
.
assertTrue
(
CommonUtils
.
isEmpty
(
emptyValue
));
Assert
.
assertFalse
(
CommonUtils
.
isEmpty
(
value
));
}
@Test
public
void
testSafeCollection
()
{
final
ArrayList
<
Character
>
theList
=
new
ArrayList
<>();
Assert
.
assertEquals
(
theList
,
CommonUtils
.
safeCollection
(
null
));
Assert
.
assertEquals
(
theList
,
CommonUtils
.
safeCollection
(
theList
));
}
@Test
public
void
testSafeList
()
{
final
ArrayList
<
Character
>
theList
=
new
ArrayList
<>();
Assert
.
assertEquals
(
theList
,
CommonUtils
.
safeList
(
null
));
Assert
.
assertEquals
(
theList
,
CommonUtils
.
safeList
(
theList
));
}
@Test
public
void
testCopyList
()
{
final
ArrayList
<
Integer
>
theList
=
new
ArrayList
<>();
Assert
.
assertEquals
(
theList
,
CommonUtils
.
copyList
(
null
));
theList
.
add
(
0
);
Assert
.
assertEquals
(
theList
,
CommonUtils
.
copyList
(
theList
));
}
@Test
@Test
public
void
testNotEmpty
()
{
public
void
testNotEmpty
()
{
Assert
.
assertEquals
(
""
,
CommonUtils
.
notEmpty
(
null
));
Assert
.
assertEquals
(
""
,
CommonUtils
.
notEmpty
(
null
));
...
@@ -92,6 +191,35 @@ public class CommonUtilsTest {
...
@@ -92,6 +191,35 @@ public class CommonUtilsTest {
Assert
.
assertEquals
(
"abc"
,
CommonUtils
.
nullIfEmpty
(
"abc"
));
Assert
.
assertEquals
(
"abc"
,
CommonUtils
.
nullIfEmpty
(
"abc"
));
}
}
@Test
public
void
testIsTrue
()
{
Assert
.
assertTrue
(
CommonUtils
.
isTrue
(
true
));
Assert
.
assertFalse
(
CommonUtils
.
isTrue
(
false
));
Assert
.
assertFalse
(
CommonUtils
.
isTrue
(
null
));
}
@Test
public
void
testGetBooleanString
()
{
Assert
.
assertTrue
(
CommonUtils
.
getBoolean
(
"true"
));
Assert
.
assertFalse
(
CommonUtils
.
getBoolean
(
"false"
));
Assert
.
assertFalse
(
CommonUtils
.
getBoolean
(
"null"
));
}
@Test
public
void
testGetBooleanStringDefault
()
{
Assert
.
assertTrue
(
CommonUtils
.
getBoolean
(
""
,
true
));
Assert
.
assertFalse
(
CommonUtils
.
getBoolean
(
"false"
,
true
));
}
@Test
public
void
testGetBooleanObjectDefault
()
{
final
Object
nullValue
=
null
;
final
Object
value
=
0
;
Assert
.
assertTrue
(
CommonUtils
.
getBoolean
(
nullValue
,
true
));
Assert
.
assertTrue
(
CommonUtils
.
getBoolean
(
true
,
false
));
Assert
.
assertFalse
(
CommonUtils
.
getBoolean
(
value
,
true
));
}
@PrepareForTest
({
CommonUtils
.
class
,
System
.
class
})
@PrepareForTest
({
CommonUtils
.
class
,
System
.
class
})
@Test
@Test
public
void
testGetLineSeparator
()
{
public
void
testGetLineSeparator
()
{
...
@@ -103,6 +231,22 @@ public class CommonUtilsTest {
...
@@ -103,6 +231,22 @@ public class CommonUtilsTest {
Assert
.
assertEquals
(
"\n"
,
CommonUtils
.
getLineSeparator
());
Assert
.
assertEquals
(
"\n"
,
CommonUtils
.
getLineSeparator
());
}
}
@Test
public
void
testGetRootCause
()
{
Assert
.
assertEquals
(
"def"
,
CommonUtils
.
getRootCause
(
new
Throwable
(
"abc"
,
new
Throwable
(
"def"
))).
getMessage
());
Assert
.
assertEquals
(
"abc"
,
CommonUtils
.
getRootCause
(
new
Throwable
(
"abc"
)).
getMessage
());
Assert
.
assertNull
(
CommonUtils
.
getRootCause
(
new
InvocationTargetException
(
null
)).
getMessage
());
}
@Test
public
void
testEqualOrEmptyStrings
()
{
Assert
.
assertTrue
(
CommonUtils
.
equalOrEmptyStrings
(
"abc"
,
"abc"
));
Assert
.
assertFalse
(
CommonUtils
.
equalOrEmptyStrings
(
"abc"
,
null
));
Assert
.
assertFalse
(
CommonUtils
.
equalOrEmptyStrings
(
null
,
"def"
));
Assert
.
assertFalse
(
CommonUtils
.
equalOrEmptyStrings
(
"abc"
,
"def"
));
Assert
.
assertTrue
(
CommonUtils
.
equalOrEmptyStrings
(
""
,
""
));
}
@Test
@Test
public
void
testToStringObject
()
{
public
void
testToStringObject
()
{
Assert
.
assertEquals
(
""
,
CommonUtils
.
toString
(
null
));
Assert
.
assertEquals
(
""
,
CommonUtils
.
toString
(
null
));
...
@@ -117,6 +261,71 @@ public class CommonUtilsTest {
...
@@ -117,6 +261,71 @@ public class CommonUtilsTest {
Assert
.
assertEquals
(
"1"
,
CommonUtils
.
toString
(
new
Integer
(
1
),
""
));
Assert
.
assertEquals
(
"1"
,
CommonUtils
.
toString
(
new
Integer
(
1
),
""
));
}
}
@Test
public
void
testToBoolean
()
{
Assert
.
assertFalse
(
CommonUtils
.
toBoolean
(
null
));
Assert
.
assertFalse
(
CommonUtils
.
toBoolean
(
"false"
));
Assert
.
assertTrue
(
CommonUtils
.
toBoolean
(
"true"
));
}
@Test
public
void
testToIntDef
()
{
Assert
.
assertEquals
(
1
,
CommonUtils
.
toInt
(
null
,
1
));
Assert
.
assertEquals
(
2
,
CommonUtils
.
toInt
(
2
,
1
));
Assert
.
assertEquals
(
2
,
CommonUtils
.
toInt
(
"2"
,
1
));
Assert
.
assertEquals
(
1
,
CommonUtils
.
toInt
(
"a"
,
1
));
}
@Test
public
void
testToInt
()
{
Assert
.
assertEquals
(
1
,
CommonUtils
.
toInt
(
1
));
}
@Test
public
void
testIsInt
()
{
Assert
.
assertFalse
(
CommonUtils
.
isInt
(
null
));
Assert
.
assertTrue
(
CommonUtils
.
isInt
(
1
));
Assert
.
assertTrue
(
CommonUtils
.
isInt
(
"2"
));
Assert
.
assertFalse
(
CommonUtils
.
isInt
(
"a"
));
}
@Test
public
void
testToLong
()
{
Assert
.
assertEquals
(
1
,
CommonUtils
.
toLong
(
1L
));
}
@Test
public
void
testToLongDef
()
{
Assert
.
assertEquals
(
1
,
CommonUtils
.
toLong
(
null
,
1L
));
Assert
.
assertEquals
(
2
,
CommonUtils
.
toLong
(
2L
,
1L
));
Assert
.
assertEquals
(
2
,
CommonUtils
.
toLong
(
"2"
,
1L
));
Assert
.
assertEquals
(
1
,
CommonUtils
.
toLong
(
"a"
,
1L
));
}
@Test
public
void
testIsLong
()
{
Assert
.
assertFalse
(
CommonUtils
.
isLong
(
null
));
Assert
.
assertTrue
(
CommonUtils
.
isLong
(
1L
));
Assert
.
assertTrue
(
CommonUtils
.
isLong
(
"2"
));
Assert
.
assertFalse
(
CommonUtils
.
isLong
(
"a"
));
}
@Test
public
void
testToDouble
()
{
Assert
.
assertEquals
(
0.0
,
CommonUtils
.
toDouble
(
null
),
0
);
Assert
.
assertEquals
(
0.1
,
CommonUtils
.
toDouble
(
0.1
),
0
);
Assert
.
assertEquals
(
0.1
,
CommonUtils
.
toDouble
(
"0.1"
),
0
);
Assert
.
assertEquals
(
Double
.
NaN
,
CommonUtils
.
toDouble
(
"a"
),
0
);
}
@Test
public
void
testToDoubleDef
()
{
Assert
.
assertEquals
(
0.1
,
CommonUtils
.
toDouble
(
null
,
0.1
),
0
);
Assert
.
assertEquals
(
0.2
,
CommonUtils
.
toDouble
(
0.2
,
0.1
),
0
);
Assert
.
assertEquals
(
0.2
,
CommonUtils
.
toDouble
(
"0.2"
,
0.1
),
0
);
Assert
.
assertEquals
(
0.1
,
CommonUtils
.
toDouble
(
"a"
,
0.1
),
0
);
}
@Test
@Test
public
void
testToHexString
()
{
public
void
testToHexString
()
{
Assert
.
assertEquals
(
""
,
CommonUtils
.
toHexString
(
null
));
Assert
.
assertEquals
(
""
,
CommonUtils
.
toHexString
(
null
));
...
@@ -188,6 +397,69 @@ public class CommonUtilsTest {
...
@@ -188,6 +397,69 @@ public class CommonUtilsTest {
Assert
.
assertEquals
(
"abc:def"
,
CommonUtils
.
joinStrings
(
":"
,
col
));
Assert
.
assertEquals
(
"abc:def"
,
CommonUtils
.
joinStrings
(
":"
,
col
));
}
}
@Test
public
void
testIsEmptyTrimmed
()
{
Assert
.
assertTrue
(
CommonUtils
.
isEmptyTrimmed
(
null
));
Assert
.
assertTrue
(
CommonUtils
.
isEmptyTrimmed
(
""
));
Assert
.
assertTrue
(
CommonUtils
.
isEmptyTrimmed
(
" "
));
Assert
.
assertFalse
(
CommonUtils
.
isEmptyTrimmed
(
":"
));
}
@Test
public
void
testIsBitSet
()
{
Assert
.
assertTrue
(
CommonUtils
.
isBitSet
(
1
,
1
));
Assert
.
assertFalse
(
CommonUtils
.
isBitSet
(
1
,
2
));
}
enum
enumClass
{
A_B
,
}
enum
enumClassEmpty
{
}
@Test
public
void
testValueOf
()
{
Assert
.
assertNull
(
CommonUtils
.
valueOf
(
enumClass
.
class
,
null
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
valueOf
(
enumClass
.
class
,
null
,
enumClass
.
A_B
,
false
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
valueOf
(
enumClass
.
class
,
" "
,
enumClass
.
A_B
,
false
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
valueOf
(
enumClass
.
class
,
"A B"
,
enumClass
.
A_B
,
false
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
valueOf
(
enumClass
.
class
,
"A B"
,
enumClass
.
A_B
,
true
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
valueOf
(
enumClass
.
class
,
""
,
enumClass
.
A_B
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
valueOf
(
enumClass
.
class
,
"A_B"
,
enumClass
.
A_B
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
valueOf
(
enumClass
.
class
,
"A B"
,
enumClass
.
A_B
));
}
@Test
public
void
testGetItem
()
{
final
ArrayList
<
String
>
collectionList
=
new
ArrayList
<>();
collectionList
.
add
(
"a"
);
Assert
.
assertEquals
(
"a"
,
CommonUtils
.
getItem
(
collectionList
,
0
));
final
HashSet
<
String
>
collectionSet
=
new
HashSet
<>();
collectionSet
.
add
(
"a"
);
collectionSet
.
add
(
"b"
);
Assert
.
assertEquals
(
"b"
,
CommonUtils
.
getItem
(
collectionSet
,
1
));
}
@Test
public
void
testFromOrdinal
()
{
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
fromOrdinal
(
enumClass
.
class
,
0
));
Assert
.
assertEquals
(
enumClass
.
A_B
,
CommonUtils
.
fromOrdinal
(
enumClass
.
class
,
3
));
thrown
.
expect
(
IllegalArgumentException
.
class
);
CommonUtils
.
fromOrdinal
(
enumClassEmpty
.
class
,
3
);
}
@Test
public
void
testFilterCollection
()
{
final
ArrayList
<
Object
>
collection
=
new
ArrayList
<>();
collection
.
add
(
"a"
);
collection
.
add
(
1
);
Assert
.
assertEquals
(
new
String
[]
{
"a"
},
CommonUtils
.
filterCollection
(
collection
,
String
.
class
).
toArray
());
}
@Test
@Test
public
void
testEscapeDisplayString
()
{
public
void
testEscapeDisplayString
()
{
Assert
.
assertEquals
(
"\\n\\r\\t:"
,
CommonUtils
.
escapeDisplayString
(
"\n\r\t:"
));
Assert
.
assertEquals
(
"\\n\\r\\t:"
,
CommonUtils
.
escapeDisplayString
(
"\n\r\t:"
));
...
@@ -198,11 +470,41 @@ public class CommonUtilsTest {
...
@@ -198,11 +470,41 @@ public class CommonUtilsTest {
Assert
.
assertEquals
(
"\t\r\n"
,
CommonUtils
.
unescapeDisplayString
(
"\\t\\r\\n"
));
Assert
.
assertEquals
(
"\t\r\n"
,
CommonUtils
.
unescapeDisplayString
(
"\\t\\r\\n"
));
}
}
@Test
public
void
testHashCode
()
{
Assert
.
assertEquals
(
0
,
CommonUtils
.
hashCode
(
null
));
Assert
.
assertEquals
(
96354
,
CommonUtils
.
hashCode
(
"abc"
));
}
@Test
public
void
testGetOption
()
{
final
HashMap
<
String
,
Boolean
>
options
=
new
HashMap
<>();
options
.
put
(
"A"
,
false
);
options
.
put
(
"B"
,
false
);
options
.
put
(
"C"
,
true
);
Assert
.
assertEquals
(
"default"
,
CommonUtils
.
getOption
(
options
,
"D"
,
"default"
));
Assert
.
assertEquals
(
true
,
CommonUtils
.
getOption
(
options
,
"C"
,
"default"
));
Assert
.
assertFalse
(
CommonUtils
.
getOption
(
null
,
"A"
));
Assert
.
assertFalse
(
CommonUtils
.
getOption
(
options
,
"D"
,
false
));
Assert
.
assertTrue
(
CommonUtils
.
getOption
(
options
,
"C"
,
false
));
}
@Test
@Test
public
void
testFixedLengthString
()
{
public
void
testFixedLengthString
()
{
Assert
.
assertEquals
(
"abc"
,
CommonUtils
.
fixedLengthString
(
"abc"
,
3
));
Assert
.
assertEquals
(
"abc"
,
CommonUtils
.
fixedLengthString
(
"abc"
,
3
));
}
}
@Test
public
void
testStartsWithIgnoreCase
()
{
Assert
.
assertFalse
(
CommonUtils
.
startsWithIgnoreCase
(
""
,
"a"
));
Assert
.
assertFalse
(
CommonUtils
.
startsWithIgnoreCase
(
"abc"
,
""
));
Assert
.
assertTrue
(
CommonUtils
.
startsWithIgnoreCase
(
"abc"
,
"a"
));
Assert
.
assertTrue
(
CommonUtils
.
startsWithIgnoreCase
(
"Abc"
,
"aB"
));
}
@Test
@Test
public
void
testNiceFormatFloat
()
{
public
void
testNiceFormatFloat
()
{
Assert
.
assertEquals
(
"1"
,
CommonUtils
.
niceFormatFloat
(
1
));
Assert
.
assertEquals
(
"1"
,
CommonUtils
.
niceFormatFloat
(
1
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录