Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
DolphinScheduler
提交
039f02a6
DolphinScheduler
项目概览
apache
/
DolphinScheduler
上一次同步 接近 2 年
通知
707
Star
9572
Fork
3514
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
DolphinScheduler
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
039f02a6
编写于
12月 17, 2019
作者:
K
khadgarmage
提交者:
GitHub
12月 17, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add common utils CollectionUtils.java DateUtils.java unit test (#1496)
* dateutil test * pom.xml
上级
da1afb7a
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
187 addition
and
43 deletion
+187
-43
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/CollectionUtils.java
...apache/dolphinscheduler/common/utils/CollectionUtils.java
+21
-21
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java
...a/org/apache/dolphinscheduler/common/utils/DateUtils.java
+3
-3
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/dependent/DependentDateUtils.java
...nscheduler/common/utils/dependent/DependentDateUtils.java
+1
-1
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/CollectionUtilsTest.java
...he/dolphinscheduler/common/utils/CollectionUtilsTest.java
+58
-8
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java
...g/apache/dolphinscheduler/common/utils/DateUtilsTest.java
+99
-3
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java
...rg/apache/dolphinscheduler/dao/mapper/UserMapperTest.java
+2
-2
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java
...ache/dolphinscheduler/server/worker/task/sql/SqlTask.java
+1
-3
pom.xml
pom.xml
+2
-2
未找到文件。
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/CollectionUtils.java
浏览文件 @
039f02a6
...
...
@@ -86,21 +86,20 @@ public class CollectionUtils {
* @return string to map
*/
public
static
Map
<
String
,
String
>
stringToMap
(
String
str
,
String
separator
,
String
keyPrefix
)
{
if
(
null
==
str
||
""
.
equals
(
str
))
{
return
null
;
Map
<
String
,
String
>
emptyMap
=
new
HashMap
<>(
0
);
if
(
StringUtils
.
isEmpty
(
str
))
{
return
emptyMap
;
}
if
(
null
==
separator
||
""
.
equals
(
separator
))
{
return
null
;
if
(
StringUtils
.
isEmpty
(
separator
))
{
return
emptyMap
;
}
String
[]
strings
=
str
.
split
(
separator
);
int
mapLength
=
strings
.
length
;
if
((
strings
.
length
%
2
)
!=
0
)
{
mapLength
=
mapLength
+
1
;
}
Map
<
String
,
String
>
map
=
new
HashMap
<>(
mapLength
);
Map
<
String
,
String
>
map
=
new
HashMap
<>(
strings
.
length
);
for
(
int
i
=
0
;
i
<
strings
.
length
;
i
++)
{
String
[]
strArray
=
strings
[
i
].
split
(
"="
);
if
(
strArray
.
length
!=
2
)
{
return
emptyMap
;
}
//strArray[0] KEY strArray[1] VALUE
if
(
StringUtils
.
isEmpty
(
keyPrefix
))
{
map
.
put
(
strArray
[
0
],
strArray
[
1
]);
...
...
@@ -146,7 +145,7 @@ public class CollectionUtils {
* @param obj the object
* @return the maximum frequency of the object
*/
p
ublic
final
int
max
(
final
Object
obj
)
{
p
rivate
int
max
(
final
Object
obj
)
{
return
Math
.
max
(
freqA
(
obj
),
freqB
(
obj
));
}
...
...
@@ -156,7 +155,7 @@ public class CollectionUtils {
* @param obj the object
* @return the minimum frequency of the object
*/
p
ublic
final
int
min
(
final
Object
obj
)
{
p
rivate
int
min
(
final
Object
obj
)
{
return
Math
.
min
(
freqA
(
obj
),
freqB
(
obj
));
}
...
...
@@ -180,10 +179,10 @@ public class CollectionUtils {
return
getFreq
(
obj
,
cardinalityB
);
}
private
final
int
getFreq
(
final
Object
obj
,
final
Map
<?,
Integer
>
freqMap
)
{
private
int
getFreq
(
final
Object
obj
,
final
Map
<?,
Integer
>
freqMap
)
{
final
Integer
count
=
freqMap
.
get
(
obj
);
if
(
count
!=
null
)
{
return
count
.
intValue
()
;
return
count
;
}
return
0
;
}
...
...
@@ -203,7 +202,7 @@ public class CollectionUtils {
return
true
;
}
if
(
(
a
==
null
&&
b
!=
null
)
||
a
!=
null
&&
b
==
null
)
{
if
(
a
==
null
||
b
==
null
)
{
return
false
;
}
...
...
@@ -253,12 +252,7 @@ public class CollectionUtils {
public
static
<
O
>
Map
<
O
,
Integer
>
getCardinalityMap
(
final
Iterable
<?
extends
O
>
coll
)
{
final
Map
<
O
,
Integer
>
count
=
new
HashMap
<
O
,
Integer
>();
for
(
final
O
obj
:
coll
)
{
final
Integer
c
=
count
.
get
(
obj
);
if
(
c
==
null
)
{
count
.
put
(
obj
,
Integer
.
valueOf
(
1
));
}
else
{
count
.
put
(
obj
,
Integer
.
valueOf
(
c
.
intValue
()
+
1
));
}
count
.
put
(
obj
,
count
.
getOrDefault
(
obj
,
0
)
+
1
);
}
return
count
;
}
...
...
@@ -273,6 +267,12 @@ public class CollectionUtils {
*/
public
static
<
T
extends
Object
>
List
<
Map
<
String
,
Object
>>
getListByExclusion
(
List
<
T
>
originList
,
Set
<
String
>
exclusionSet
)
{
List
<
Map
<
String
,
Object
>>
instanceList
=
new
ArrayList
<>();
if
(
exclusionSet
==
null
)
{
exclusionSet
=
new
HashSet
<>();
}
if
(
originList
==
null
)
{
return
instanceList
;
}
Map
<
String
,
Object
>
instanceMap
;
for
(
T
instance
:
originList
)
{
Map
<
String
,
Object
>
dataMap
=
new
BeanMap
(
instance
);
...
...
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java
浏览文件 @
039f02a6
...
...
@@ -291,14 +291,14 @@ public class DateUtils {
* get some hour of day
*
* @param date date
* @param
hours
hours
* @param
offsetHour
hours
* @return some hour of day
* */
public
static
Date
getSomeHourOfDay
(
Date
date
,
int
hours
)
{
public
static
Date
getSomeHourOfDay
(
Date
date
,
int
offsetHour
)
{
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
date
);
cal
.
set
(
Calendar
.
HOUR_OF_DAY
,
cal
.
get
(
Calendar
.
HOUR_OF_DAY
)
-
hours
);
cal
.
set
(
Calendar
.
HOUR_OF_DAY
,
cal
.
get
(
Calendar
.
HOUR_OF_DAY
)
+
offsetHour
);
cal
.
set
(
Calendar
.
MINUTE
,
0
);
cal
.
set
(
Calendar
.
SECOND
,
0
);
cal
.
set
(
Calendar
.
MILLISECOND
,
0
);
...
...
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/dependent/DependentDateUtils.java
浏览文件 @
039f02a6
...
...
@@ -34,7 +34,7 @@ public class DependentDateUtils {
public
static
List
<
DateInterval
>
getLastHoursInterval
(
Date
businessDate
,
int
hourNumber
){
List
<
DateInterval
>
dateIntervals
=
new
ArrayList
<>();
for
(
int
index
=
hourNumber
;
index
>
0
;
index
--){
Date
lastHour
=
DateUtils
.
getSomeHourOfDay
(
businessDate
,
index
);
Date
lastHour
=
DateUtils
.
getSomeHourOfDay
(
businessDate
,
-
index
);
Date
beginTime
=
DateUtils
.
getStartOfHour
(
lastHour
);
Date
endTime
=
DateUtils
.
getEndOfHour
(
lastHour
);
dateIntervals
.
add
(
new
DateInterval
(
beginTime
,
endTime
));
...
...
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/CollectionUtilsTest.java
浏览文件 @
039f02a6
...
...
@@ -16,6 +16,7 @@
*/
package
org.apache.dolphinscheduler.common.utils
;
import
org.apache.dolphinscheduler.common.Constants
;
import
org.junit.Assert
;
import
org.junit.Test
;
...
...
@@ -26,19 +27,26 @@ public class CollectionUtilsTest {
@Test
public
void
equalLists
()
{
Assert
.
assertTrue
(
CollectionUtils
.
equalLists
(
null
,
null
));
Assert
.
assertTrue
(
CollectionUtils
.
equalLists
(
new
ArrayList
<
Integer
>(),
new
ArrayList
<
Integer
>()));
List
<
Integer
>
a
=
new
ArrayList
<
Integer
>();
a
.
add
(
1
);
a
.
add
(
2
);
a
.
add
(
3
);
List
<
Integer
>
b
=
new
ArrayList
<
Integer
>();
b
.
add
(
3
);
b
.
add
(
1
);
b
.
add
(
2
);
Assert
.
assertTrue
(
CollectionUtils
.
equalLists
(
a
,
b
));
a
.
add
(
1
);
Assert
.
assertFalse
(
CollectionUtils
.
equalLists
(
a
,
b
));
b
.
add
(
2
);
Assert
.
assertFalse
(
CollectionUtils
.
equalLists
(
a
,
b
));
a
.
add
(
2
);
b
.
add
(
1
);
Assert
.
assertTrue
(
CollectionUtils
.
equalLists
(
a
,
b
)
);
Assert
.
assertTrue
(
CollectionUtils
.
equalLists
(
null
,
null
)
);
List
<
Integer
>
c
=
new
ArrayList
<
Integer
>(
);
Assert
.
assertFalse
(
CollectionUtils
.
equalLists
(
c
,
null
));
Assert
.
assertFalse
(
CollectionUtils
.
equalLists
(
c
,
a
));
a
.
add
(
4
);
b
.
add
(
2
);
Assert
.
assertFalse
(
CollectionUtils
.
equalLists
(
a
,
b
)
);
Assert
.
assertFalse
(
CollectionUtils
.
equalLists
(
null
,
new
ArrayList
<
Integer
>()
));
Assert
.
assertFalse
(
CollectionUtils
.
equalLists
(
new
ArrayList
<
Integer
>(),
null
));
}
@Test
...
...
@@ -56,7 +64,49 @@ public class CollectionUtilsTest {
@Test
public
void
stringToMap
()
{
Map
<
String
,
String
>
a
=
CollectionUtils
.
stringToMap
(
"a=b;c=d
"
,
";"
,
"
"
);
Map
<
String
,
String
>
a
=
CollectionUtils
.
stringToMap
(
"a=b;c=d
;"
,
";
"
);
Assert
.
assertNotNull
(
a
);
Assert
.
assertTrue
(
a
.
size
()
==
2
);
a
=
CollectionUtils
.
stringToMap
(
null
,
";"
);
Assert
.
assertTrue
(
a
.
isEmpty
());
a
=
CollectionUtils
.
stringToMap
(
""
,
";"
);
Assert
.
assertTrue
(
a
.
isEmpty
());
a
=
CollectionUtils
.
stringToMap
(
"a=b;c=d"
,
""
);
Assert
.
assertTrue
(
a
.
isEmpty
());
a
=
CollectionUtils
.
stringToMap
(
"a=b;c=d"
,
null
);
Assert
.
assertTrue
(
a
.
isEmpty
());
a
=
CollectionUtils
.
stringToMap
(
"a=b;c=d;e=f"
,
";"
);
Assert
.
assertEquals
(
a
.
size
(),
3
);
a
=
CollectionUtils
.
stringToMap
(
"a;b=f"
,
";"
);
Assert
.
assertTrue
(
a
.
isEmpty
());
a
=
CollectionUtils
.
stringToMap
(
"a=b;c=d;e=f;"
,
";"
,
"test"
);
Assert
.
assertEquals
(
a
.
size
(),
3
);
Assert
.
assertNotNull
(
a
.
get
(
"testa"
));
}
@Test
public
void
getListByExclusion
()
{
Assert
.
assertNotNull
(
CollectionUtils
.
getListByExclusion
(
null
,
null
));
List
<
Integer
>
originList
=
new
ArrayList
<>();
originList
.
add
(
1
);
originList
.
add
(
2
);
List
<
Map
<
String
,
Object
>>
ret
=
CollectionUtils
.
getListByExclusion
(
originList
,
null
);
Assert
.
assertEquals
(
ret
.
size
(),
2
);
ret
=
CollectionUtils
.
getListByExclusion
(
originList
,
new
HashSet
<>());
Assert
.
assertEquals
(
ret
.
size
(),
2
);
Assert
.
assertFalse
(
ret
.
get
(
0
).
isEmpty
());
Set
<
String
>
exclusion
=
new
HashSet
<>();
exclusion
.
add
(
Constants
.
CLASS
);
ret
=
CollectionUtils
.
getListByExclusion
(
originList
,
exclusion
);
Assert
.
assertEquals
(
ret
.
size
(),
2
);
Assert
.
assertTrue
(
ret
.
get
(
0
).
isEmpty
());
}
@Test
public
void
isNotEmpty
()
{
List
<
Integer
>
list
=
new
ArrayList
<>();
Assert
.
assertFalse
(
CollectionUtils
.
isNotEmpty
(
list
));
Assert
.
assertFalse
(
CollectionUtils
.
isNotEmpty
(
null
));
}
}
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java
浏览文件 @
039f02a6
...
...
@@ -18,13 +18,11 @@ package org.apache.dolphinscheduler.common.utils;
import
org.junit.Assert
;
import
org.junit.Test
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
DateUtilsTest
{
@Test
public
void
format2Readable
()
throws
ParseException
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
...
...
@@ -54,4 +52,102 @@ public class DateUtilsTest {
Assert
.
assertEquals
(
sunday
,
sunday1
);
}
}
\ No newline at end of file
@Test
public
void
diffHours
(){
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-28 00:00:00"
);
Date
d2
=
DateUtils
.
stringToDate
(
"2019-01-28 20:00:00"
);
Assert
.
assertEquals
(
DateUtils
.
diffHours
(
d1
,
d2
),
20
);
Date
d3
=
DateUtils
.
stringToDate
(
"2019-01-28 20:00:00"
);
Assert
.
assertEquals
(
DateUtils
.
diffHours
(
d3
,
d2
),
0
);
Assert
.
assertEquals
(
DateUtils
.
diffHours
(
d2
,
d1
),
20
);
Date
d4
=
null
;
Assert
.
assertEquals
(
DateUtils
.
diffHours
(
d2
,
d4
),
0
);
}
@Test
public
void
dateToString
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-28"
);
Assert
.
assertNull
(
d1
);
d1
=
DateUtils
.
stringToDate
(
"2019-01-28 00:00:00"
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
d1
),
"2019-01-28 00:00:00"
);
}
@Test
public
void
getSomeDay
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 00:00:00"
);
Date
curr
=
DateUtils
.
getSomeDay
(
d1
,
1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-02-01 00:00:00"
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
DateUtils
.
getSomeDay
(
d1
,
-
31
)),
"2018-12-31 00:00:00"
);
}
@Test
public
void
getFirstDayOfMonth
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 00:00:00"
);
Date
curr
=
DateUtils
.
getFirstDayOfMonth
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-01 00:00:00"
);
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 01:59:00"
);
curr
=
DateUtils
.
getFirstDayOfMonth
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-01 01:59:00"
);
}
@Test
public
void
getSomeHourOfDay
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 11:59:59"
);
Date
curr
=
DateUtils
.
getSomeHourOfDay
(
d1
,
-
1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 10:00:00"
);
curr
=
DateUtils
.
getSomeHourOfDay
(
d1
,
0
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 11:00:00"
);
curr
=
DateUtils
.
getSomeHourOfDay
(
d1
,
2
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 13:00:00"
);
curr
=
DateUtils
.
getSomeHourOfDay
(
d1
,
24
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-02-01 11:00:00"
);
}
@Test
public
void
getLastDayOfMonth
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 11:59:59"
);
Date
curr
=
DateUtils
.
getLastDayOfMonth
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 11:59:59"
);
d1
=
DateUtils
.
stringToDate
(
"2019-01-02 11:59:59"
);
curr
=
DateUtils
.
getLastDayOfMonth
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 11:59:59"
);
d1
=
DateUtils
.
stringToDate
(
"2019-02-02 11:59:59"
);
curr
=
DateUtils
.
getLastDayOfMonth
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-02-28 11:59:59"
);
d1
=
DateUtils
.
stringToDate
(
"2020-02-02 11:59:59"
);
curr
=
DateUtils
.
getLastDayOfMonth
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2020-02-29 11:59:59"
);
}
@Test
public
void
getStartOfDay
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 11:59:59"
);
Date
curr
=
DateUtils
.
getStartOfDay
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 00:00:00"
);
}
@Test
public
void
getEndOfDay
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 11:00:59"
);
Date
curr
=
DateUtils
.
getEndOfDay
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 23:59:59"
);
}
@Test
public
void
getStartOfHour
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 11:00:59"
);
Date
curr
=
DateUtils
.
getStartOfHour
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 11:00:00"
);
}
@Test
public
void
getEndOfHour
()
{
Date
d1
=
DateUtils
.
stringToDate
(
"2019-01-31 11:00:59"
);
Date
curr
=
DateUtils
.
getEndOfHour
(
d1
);
Assert
.
assertEquals
(
DateUtils
.
dateToString
(
curr
),
"2019-01-31 11:59:59"
);
}
}
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java
浏览文件 @
039f02a6
...
...
@@ -154,7 +154,7 @@ public class UserMapperTest {
accessToken
.
setToken
(
"secrettoken"
);
accessToken
.
setCreateTime
(
new
Date
());
accessToken
.
setUpdateTime
(
new
Date
());
accessToken
.
setExpireTime
(
DateUtils
.
getSomeHourOfDay
(
new
Date
(),
-
1
));
accessToken
.
setExpireTime
(
DateUtils
.
getSomeHourOfDay
(
new
Date
(),
1
));
accessTokenMapper
.
insert
(
accessToken
);
return
accessToken
;
}
...
...
@@ -356,4 +356,4 @@ public class UserMapperTest {
accessTokenMapper
.
deleteById
(
accessToken
.
getId
());
}
}
\ No newline at end of file
}
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/sql/SqlTask.java
浏览文件 @
039f02a6
...
...
@@ -261,9 +261,7 @@ public class SqlTask extends AbstractTask {
Map
<
String
,
String
>
connParamMap
=
CollectionUtils
.
stringToMap
(
sqlParameters
.
getConnParams
(),
SEMICOLON
,
HIVE_CONF
);
if
(
connParamMap
!=
null
){
paramProp
.
putAll
(
connParamMap
);
}
paramProp
.
putAll
(
connParamMap
);
connection
=
DriverManager
.
getConnection
(
baseDataSource
.
getJdbcUrl
(),
paramProp
);
...
...
pom.xml
浏览文件 @
039f02a6
...
...
@@ -612,10 +612,10 @@
<version>
${maven-surefire-plugin.version}
</version>
<configuration>
<includes>
<include>
**/common/utils/*.java
</include>
<include>
**/common/graph/*.java
</include>
<include>
**/api/utils/CheckUtilsTest.java
</include>
<include>
**/api/utils/FileUtilsTest.java
</include>
<include>
**/common/graph/*.java
</include>
<include>
**/*CollectionUtilsTest.java
</include>
<!--run test classes-->
</includes>
<!-- <skip>true</skip> -->
</configuration>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录