Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
9901c18c
C
cat
项目概览
梦中观雨
/
cat
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cat
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9901c18c
编写于
10月 18, 2013
作者:
A
ainilife
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add unit tests for abtest
上级
9002ec29
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
483 addition
and
233 deletion
+483
-233
cat-home/src/main/java/com/dianping/cat/system/page/abtest/GroupStrategyParser.java
.../dianping/cat/system/page/abtest/GroupStrategyParser.java
+0
-1
cat-home/src/main/java/com/dianping/cat/system/page/abtest/GsonBuilderManager.java
...m/dianping/cat/system/page/abtest/GsonBuilderManager.java
+7
-5
cat-home/src/main/java/com/dianping/cat/system/page/abtest/Handler.java
...ain/java/com/dianping/cat/system/page/abtest/Handler.java
+2
-1
cat-home/src/main/java/com/dianping/cat/system/page/abtest/conditions/ScriptProvider.java
...ing/cat/system/page/abtest/conditions/ScriptProvider.java
+8
-2
cat-home/src/main/java/com/dianping/cat/system/page/abtest/conditions/URLScriptProvider.java
.../cat/system/page/abtest/conditions/URLScriptProvider.java
+87
-55
cat-home/src/main/resources/freemaker/trafficFilter.ftl
cat-home/src/main/resources/freemaker/trafficFilter.ftl
+4
-3
cat-home/src/main/webapp/js/abtestAllTest.js
cat-home/src/main/webapp/js/abtestAllTest.js
+0
-3
cat-home/src/test/java/com/dianping/cat/AllTests.java
cat-home/src/test/java/com/dianping/cat/AllTests.java
+8
-0
cat-home/src/test/java/com/dianping/cat/home/AllTests.java
cat-home/src/test/java/com/dianping/cat/home/AllTests.java
+6
-0
cat-home/src/test/java/com/dianping/cat/home/abtest/conditions/TrafficFilterCodeGenerationTest.java
...me/abtest/conditions/TrafficFilterCodeGenerationTest.java
+91
-0
cat-home/src/test/java/com/dianping/cat/home/abtest/conditions/TrafficFilterTest.java
...ianping/cat/home/abtest/conditions/TrafficFilterTest.java
+97
-88
cat-home/src/test/java/com/dianping/cat/home/abtest/groupstrategy/GroupStrategyParserTest.java
...at/home/abtest/groupstrategy/GroupStrategyParserTest.java
+41
-50
cat-home/src/test/java/com/dianping/cat/home/abtest/groupstrategy/GsonBuilderManagerTest.java
...cat/home/abtest/groupstrategy/GsonBuilderManagerTest.java
+38
-0
cat-home/src/test/resources/com/dianping/cat/home/abtest/conditions/TrafficFilter
...ces/com/dianping/cat/home/abtest/conditions/TrafficFilter
+92
-23
cat-home/src/test/resources/com/dianping/cat/home/abtest/groupstrategy/descriptor.xml
...com/dianping/cat/home/abtest/groupstrategy/descriptor.xml
+2
-2
未找到文件。
cat-home/src/main/java/com/dianping/cat/system/page/abtest/GroupStrategyParser.java
浏览文件 @
9901c18c
...
...
@@ -51,7 +51,6 @@ public class GroupStrategyParser {
int
pos
=
annotation
.
indexOf
(
"@Inject"
);
System
.
out
.
println
(
expr
.
getData
());
if
(
pos
>=
0
)
{
int
begin
=
annotation
.
indexOf
(
'"'
);
int
end
=
annotation
.
lastIndexOf
(
'"'
);
...
...
cat-home/src/main/java/com/dianping/cat/system/page/abtest/GsonBuilderManager.java
浏览文件 @
9901c18c
...
...
@@ -4,17 +4,19 @@ import com.google.gson.FieldNamingStrategy;
import
com.google.gson.GsonBuilder
;
public
class
GsonBuilderManager
{
private
static
GsonBuilder
s_gsonBuilder
=
new
GsonBuilder
()
;
private
GsonBuilder
m_gsonBuilder
;
static
{
s_gsonBuilder
.
setFieldNamingStrategy
(
new
NonPrexFieldNamingStrategy
());
public
GsonBuilderManager
()
{
m_gsonBuilder
=
new
GsonBuilder
();
m_gsonBuilder
.
setFieldNamingStrategy
(
new
NonPrexFieldNamingStrategy
());
}
public
GsonBuilder
getGsonBuilder
()
{
return
s
_gsonBuilder
;
return
m
_gsonBuilder
;
}
public
static
class
NonPrexFieldNamingStrategy
implements
FieldNamingStrategy
{
public
class
NonPrexFieldNamingStrategy
implements
FieldNamingStrategy
{
@Override
public
String
translateName
(
java
.
lang
.
reflect
.
Field
f
)
{
String
name
=
f
.
getName
();
...
...
cat-home/src/main/java/com/dianping/cat/system/page/abtest/Handler.java
浏览文件 @
9901c18c
...
...
@@ -47,6 +47,7 @@ import com.dianping.cat.system.SystemPage;
import
com.dianping.cat.system.page.abtest.ListViewModel.AbtestItem
;
import
com.dianping.cat.system.page.abtest.advisor.ABTestAdvice
;
import
com.dianping.cat.system.page.abtest.advisor.ABTestAdvisor
;
import
com.dianping.cat.system.page.abtest.conditions.ScriptProvider
;
import
com.dianping.cat.system.page.abtest.conditions.URLScriptProvider
;
import
com.dianping.cat.system.page.abtest.service.ABTestService
;
import
com.google.gson.Gson
;
...
...
@@ -170,7 +171,7 @@ public class Handler implements PageHandler<Context>, LogEnabled, Initializable
StringWriter
sw
=
new
StringWriter
(
5000
);
try
{
Template
t
=
m_configuration
.
getTemplate
(
"scriptFragement.ftl"
);
Template
t
=
m_configuration
.
getTemplate
(
ScriptProvider
.
m_fileName
);
t
.
process
(
root
,
sw
);
}
catch
(
Exception
e
)
{
...
...
cat-home/src/main/java/com/dianping/cat/system/page/abtest/conditions/ScriptProvider.java
浏览文件 @
9901c18c
package
com.dianping.cat.system.page.abtest.conditions
;
import
java.util.List
;
import
java.util.Map
;
import
com.dianping.cat.abtest.model.entity.Condition
;
public
interface
ScriptProvider
{
public
List
<
String
>
actions
();
public
static
final
String
m_fileName
=
"trafficFilter.ftl"
;
public
Map
<
Integer
,
Object
>
options
();
public
String
getScript
(
Condition
condition
);
}
cat-home/src/main/java/com/dianping/cat/system/page/abtest/conditions/URLScriptProvider.java
浏览文件 @
9901c18c
package
com.dianping.cat.system.page.abtest.conditions
;
import
java.util.
ArrayList
;
import
java.util.
List
;
import
java.util.
HashMap
;
import
java.util.
Map
;
import
com.dianping.cat.abtest.model.entity.Condition
;
...
...
@@ -9,88 +9,120 @@ public class URLScriptProvider implements ScriptProvider {
private
String
m_actual
=
"actual"
;
@Override
public
List
<
String
>
actions
()
{
List
<
String
>
actions
=
new
ArrayList
<
String
>();
actions
.
add
(
"Is equals to (case insens.)"
);
actions
.
add
(
"Is not equals to (case insens.)"
);
actions
.
add
(
"Is equals to (case sens.)"
);
actions
.
add
(
"Is not equals to (case sens.)"
);
actions
.
add
(
"Is equals to (case insens.)"
);
actions
.
add
(
"Marches Regex (case insens.)"
);
actions
.
add
(
"Marches Regex (case sens.)"
);
actions
.
add
(
"contains"
);
actions
.
add
(
"does not contain"
);
public
static
final
int
EQUALS_CASE_INSENS
=
1
;
return
actions
;
public
static
final
int
NOT_EQUALS_CASE_INSENS
=
2
;
public
static
final
int
EQUALS_CASE_SENS
=
3
;
public
static
final
int
NOT_EQUALS_CASE_SENS
=
4
;
public
static
final
int
MARCH_CASE_INSENS
=
5
;
public
static
final
int
MARCH_CASE_SENS
=
6
;
public
static
final
int
CONTAIN
=
7
;
public
static
final
int
NOT_CONTAIN
=
8
;
private
String
contain
(
String
expectedUrl
)
{
return
String
.
format
(
"%s.indexOf(\"%s\") > -1"
,
m_actual
,
expectedUrl
);
}
private
String
equalsByCaseInsens
(
String
expectedUrl
)
{
return
String
.
format
(
"%s.equalsIgnoreCase(\"%s\")"
,
m_actual
,
expectedUrl
);
}
private
String
notEqualsByCaseInsens
(
String
expectedUrl
)
{
return
String
.
format
(
"!%s.equalsIgnoreCase(\"%s\")"
,
m_actual
,
expectedUrl
);
}
private
String
equalsByCaseSens
(
String
expectedUrl
)
{
return
String
.
format
(
"%s.equals(\"%s\")"
,
m_actual
,
expectedUrl
);
}
private
String
notEqualsByCaseSens
(
String
expectedUrl
)
{
return
String
.
format
(
"!%s.equals(\"%s\")"
,
m_actual
,
expectedUrl
);
@Override
public
String
getScript
(
Condition
condition
)
{
String
expectedUrl
=
condition
.
getText
();
switch
(
condition
.
getComparator
())
{
case
EQUALS_CASE_INSENS:
return
equalsByCaseInsens
(
expectedUrl
);
case
NOT_EQUALS_CASE_INSENS:
return
notEqualsByCaseInsens
(
expectedUrl
);
case
EQUALS_CASE_SENS:
return
equalsByCaseSens
(
expectedUrl
);
case
NOT_EQUALS_CASE_SENS:
return
notEqualsByCaseSens
(
expectedUrl
);
case
MARCH_CASE_INSENS:
return
marcherByCaseInsens
(
expectedUrl
);
case
MARCH_CASE_SENS:
return
marcherByCaseSens
(
expectedUrl
);
case
CONTAIN:
return
contain
(
expectedUrl
);
case
NOT_CONTAIN:
return
notContain
(
expectedUrl
);
}
return
"false"
;
}
private
String
marcherByCaseInsens
(
String
expectedUrl
)
{
int
pos
=
expectedUrl
.
indexOf
(
'*'
);
String
subUrl
=
expectedUrl
.
substring
(
0
,
pos
);
String
subUrl
=
""
;
if
(
pos
>
0
)
{
if
(
expectedUrl
.
charAt
(
pos
-
1
)
==
'/'
)
{
subUrl
=
expectedUrl
.
substring
(
0
,
pos
-
1
);
}
else
{
subUrl
=
expectedUrl
.
substring
(
0
,
pos
);
}
return
String
.
format
(
"%s.toLowerCase().startsWith(\"%s\".toLowerCase())"
,
m_actual
,
subUrl
);
}
else
{
return
"false"
;
}
return
String
.
format
(
"%s.toLowerCase().startsWith(\"%s\".toLowerCase())"
,
m_actual
,
subUrl
);
}
private
String
marcherByCaseSens
(
String
expectedUrl
)
{
int
pos
=
expectedUrl
.
indexOf
(
'*'
);
if
(
pos
>
-
1
){
String
subUrl
=
expectedUrl
.
substring
(
0
,
pos
);
String
subUrl
=
""
;
if
(
pos
>
0
)
{
if
(
expectedUrl
.
charAt
(
pos
-
1
)
==
'/'
)
{
subUrl
=
expectedUrl
.
substring
(
0
,
pos
-
1
);
}
else
{
subUrl
=
expectedUrl
.
substring
(
0
,
pos
);
}
return
String
.
format
(
"%s.startsWith(\"%s\")"
,
m_actual
,
subUrl
);
}
else
{
}
else
{
return
"false"
;
}
}
private
String
contain
(
String
expectedUrl
){
return
String
.
format
(
"%s.indexOf(\"%s\") > -1"
,
m_actual
,
expectedUrl
);
}
private
String
notContain
(
String
expectedUrl
){
private
String
notContain
(
String
expectedUrl
)
{
return
String
.
format
(
"%s.indexOf(\"%s\") == -1"
,
m_actual
,
expectedUrl
);
}
public
String
getFragement
(
Condition
condition
)
{
String
expectedUrl
=
condition
.
getText
();
switch
(
condition
.
getComparator
())
{
case
1
:
return
equalsByCaseInsens
(
expectedUrl
);
case
2
:
return
notEqualsByCaseInsens
(
expectedUrl
);
case
3
:
return
equalsByCaseSens
(
expectedUrl
);
case
4
:
return
notEqualsByCaseSens
(
expectedUrl
);
case
5
:
return
marcherByCaseInsens
(
expectedUrl
);
case
6
:
return
marcherByCaseSens
(
expectedUrl
);
case
7
:
return
contain
(
expectedUrl
);
case
8
:
return
notContain
(
expectedUrl
);
}
private
String
notEqualsByCaseInsens
(
String
expectedUrl
)
{
return
String
.
format
(
"!%s.equalsIgnoreCase(\"%s\")"
,
m_actual
,
expectedUrl
);
}
return
"false"
;
private
String
notEqualsByCaseSens
(
String
expectedUrl
)
{
return
String
.
format
(
"!%s.equals(\"%s\")"
,
m_actual
,
expectedUrl
);
}
@Override
public
Map
<
Integer
,
Object
>
options
()
{
Map
<
Integer
,
Object
>
actions
=
new
HashMap
<
Integer
,
Object
>();
actions
.
put
(
EQUALS_CASE_INSENS
,
"Is equals to (case insens.)"
);
actions
.
put
(
NOT_EQUALS_CASE_INSENS
,
"Is not equals to (case insens.)"
);
actions
.
put
(
EQUALS_CASE_SENS
,
"Is equals to (case sens.)"
);
actions
.
put
(
NOT_EQUALS_CASE_SENS
,
"Is not equals to (case sens.)"
);
actions
.
put
(
MARCH_CASE_INSENS
,
"Marches Regex (case insens.)"
);
actions
.
put
(
MARCH_CASE_SENS
,
"Marches Regex (case sens.)"
);
actions
.
put
(
CONTAIN
,
"contains"
);
actions
.
put
(
NOT_CONTAIN
,
"does not contain"
);
return
actions
;
}
}
cat-home/src/main/resources/freemaker/
scriptFragement
.ftl
→
cat-home/src/main/resources/freemaker/
trafficFilter
.ftl
浏览文件 @
9901c18c
...
...
@@ -20,9 +20,10 @@ public class TrafficFilter {
if
(
<#
list
run
.
conditions
as
condition
>
<#
if
isFirst1
=
0
>
<#
if
condition
.
seq
=
4
>
<#
if
condition
.
seq
=
5
>
<#
if
isFirst2
=
0
>
)
<#
assign
isFirst2
=
2
>
</#
if
>
</#
if
>
<#
if
operator
=
0
>
...
...
@@ -31,7 +32,7 @@ public class TrafficFilter {
<#
if
operator
=
1
>
||
</#
if
>
<#
if
condition
.
seq
=
3
>
<#
if
condition
.
seq
=
4
>
<#
if
isFirst2
=
1
>
(
<#
assign
isFirst2
=
0
>
...
...
@@ -64,7 +65,7 @@ public class TrafficFilter {
public
boolean
accept
(
HttpServletRequest
request
)
{
String
actual
=
request
.
getRequestURL
().
toString
();
if
(${
urlScriptProvider
.
get
Fragemen
t
(
condition
)})
{
if
(${
urlScriptProvider
.
get
Scrip
t
(
condition
)})
{
return
true
;
}
else
{
return
false
;
...
...
cat-home/src/main/webapp/js/abtestAllTest.js
浏览文件 @
9901c18c
...
...
@@ -58,9 +58,6 @@ function getConditions(){
if
(
name
&&
$
(
this
).
val
()){
var
op
=
"
and
"
;
if
(
name
==
"
1
"
){
op
=
"
or
"
;
}
var
condition
=
getCondition
(
"
url
"
,
op
,
name
,
$
(
this
).
val
(),
i
);
conditions
.
push
(
condition
);
}
...
...
cat-home/src/test/java/com/dianping/cat/AllTests.java
浏览文件 @
9901c18c
...
...
@@ -4,6 +4,9 @@ import org.junit.runner.RunWith;
import
org.junit.runners.Suite
;
import
org.junit.runners.Suite.SuiteClasses
;
import
com.dianping.cat.home.abtest.conditions.TrafficFilterTest
;
import
com.dianping.cat.home.abtest.groupstrategy.GroupStrategyParserTest
;
import
com.dianping.cat.home.abtest.groupstrategy.GsonBuilderManagerTest
;
import
com.dianping.cat.report.baseline.BaselineCreatorTest
;
import
com.dianping.cat.report.graph.ValueTranslaterTest
;
import
com.dianping.cat.report.page.cross.CMDBTest
;
...
...
@@ -105,6 +108,11 @@ ParseTest.class,
/* service */
ServiceReportMergerTest
.
class
,
/* abtest */
GsonBuilderManagerTest
.
class
,
GroupStrategyParserTest
.
class
,
TrafficFilterTest
.
class
,
HeavyReportBuilderTest
.
class
})
public
class
AllTests
{
}
cat-home/src/test/java/com/dianping/cat/home/AllTests.java
浏览文件 @
9901c18c
...
...
@@ -4,6 +4,8 @@ import org.junit.runner.RunWith;
import
org.junit.runners.Suite
;
import
org.junit.runners.Suite.SuiteClasses
;
import
com.dianping.cat.home.abtest.conditions.TrafficFilterCodeGenerationTest
;
import
com.dianping.cat.home.abtest.conditions.TrafficFilterTest
;
import
com.dianping.cat.report.graph.ValueTranslaterTest
;
import
com.dianping.cat.report.page.cross.CMDBTest
;
import
com.dianping.cat.report.page.cross.CrossReportMergerTest
;
...
...
@@ -94,6 +96,10 @@ DependencyReportMergerTest.class, GraphConfigManagerTest.class,
/* CMDB */
CMDBTest
.
class
,
/* abtest */
TrafficFilterCodeGenerationTest
.
class
,
TrafficFilterTest
.
class
,
/* jsError */
ParseTest
.
class
})
public
class
AllTests
{
...
...
cat-home/src/test/java/com/dianping/cat/home/abtest/
ScriptFragement
Test.java
→
cat-home/src/test/java/com/dianping/cat/home/abtest/
conditions/TrafficFilterCodeGeneration
Test.java
浏览文件 @
9901c18c
package
com.dianping.cat.home.abtest
;
package
com.dianping.cat.home.abtest
.conditions
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.StringWriter
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.unidal.helper.Files
;
import
com.dianping.cat.Cat
;
import
com.dianping.cat.abtest.model.entity.Condition
;
import
com.dianping.cat.abtest.model.entity.Run
;
import
com.dianping.cat.system.page.abtest.conditions.ScriptProvider
;
import
com.dianping.cat.system.page.abtest.conditions.URLScriptProvider
;
import
freemarker.template.Configuration
;
import
freemarker.template.Template
;
public
class
ScriptFragementTest
{
public
class
TrafficFilterCodeGenerationTest
{
private
Configuration
m_configuration
;
private
URLScriptProvider
m_urlScriptProvider
;
private
Run
m_run
;
@Before
public
void
init
()
{
public
void
init
()
{
m_configuration
=
new
Configuration
();
m_configuration
.
setDefaultEncoding
(
"UTF-8"
);
try
{
m_configuration
.
setClassForTemplateLoading
(
ScriptFragement
Test
.
class
,
"/freemaker"
);
m_configuration
.
setClassForTemplateLoading
(
TrafficFilterCodeGeneration
Test
.
class
,
"/freemaker"
);
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
}
m_urlScriptProvider
=
new
URLScriptProvider
();
m_run
=
new
Run
();
List
<
Condition
>
conditions
=
m_run
.
getConditions
();
conditions
.
add
(
getCondition
(
1
,
"url"
,
1
,
"and"
,
"http://www.dianping.com"
));
conditions
.
add
(
getCondition
(
5
,
"url"
,
2
,
"and"
,
"http://www.dianping.com/*"
));
conditions
.
add
(
getCondition
(
2
,
"url"
,
3
,
"and"
,
"http://www.dianping.com1"
));
conditions
.
add
(
getCondition
(
3
,
"url"
,
4
,
"and"
,
"http://www.dianping.com2"
));
conditions
.
add
(
getCondition
(
4
,
"url"
,
4
,
"or"
,
"http://www.dianping.com3"
));
conditions
.
add
(
getCondition
(
6
,
"percent"
,
5
,
"and"
,
"100"
));
conditions
.
add
(
getCondition
(
"url"
,
1
,
"http://www.DIANPING.com"
,
1
,
"and"
));
conditions
.
add
(
getCondition
(
"url"
,
5
,
"http://www.dianping.com/*"
,
2
,
"and"
));
conditions
.
add
(
getCondition
(
"url"
,
8
,
"http://www.dianping.com1"
,
3
,
"and"
));
conditions
.
add
(
getCondition
(
"url"
,
3
,
"http://www.dianping.com2"
,
4
,
"or"
));
conditions
.
add
(
getCondition
(
"url"
,
4
,
"http://www.dianping.com/2"
,
4
,
"and"
));
conditions
.
add
(
getCondition
(
"url"
,
6
,
"http://www.dianping.com/*"
,
4
,
"and"
));
conditions
.
add
(
getCondition
(
"percent"
,
6
,
"100"
,
5
,
"and"
));
}
public
Condition
getCondition
(
int
comparator
,
String
name
,
int
seq
,
String
operator
,
String
text
)
{
public
Condition
getCondition
(
String
name
,
int
comparator
,
String
text
,
int
seq
,
String
operator
)
{
Condition
condition
=
new
Condition
();
condition
.
setComparator
(
comparator
);
condition
.
setName
(
name
);
condition
.
setSeq
(
seq
);
condition
.
setOperator
(
operator
);
condition
.
setText
(
text
);
return
condition
;
}
@Test
public
void
test
(){
public
void
test
()
throws
IOException
{
Map
<
Object
,
Object
>
root
=
new
HashMap
<
Object
,
Object
>();
root
.
put
(
"run"
,
m_run
);
root
.
put
(
"urlScriptProvider"
,
m_urlScriptProvider
);
StringWriter
sw
=
new
StringWriter
(
5000
);
try
{
Template
t
=
m_configuration
.
getTemplate
(
"scriptFragement.ftl"
);
Template
t
=
m_configuration
.
getTemplate
(
ScriptProvider
.
m_fileName
);
t
.
process
(
root
,
sw
);
}
catch
(
Exception
e
)
{
Cat
.
logError
(
e
);
e
.
printStackTrace
();
}
System
.
out
.
println
(
sw
.
toString
());
}
InputStream
inputstream
=
getClass
().
getResourceAsStream
(
"TrafficFilter"
);
String
expected
=
Files
.
forIO
().
readFrom
(
inputstream
,
"utf-8"
);
Assert
.
assertEquals
(
expected
,
sw
.
toString
());
}
}
cat-home/src/test/java/com/dianping/cat/home/abtest/conditions/T
estTrafficFilter
.java
→
cat-home/src/test/java/com/dianping/cat/home/abtest/conditions/T
rafficFilterTest
.java
浏览文件 @
9901c18c
...
...
@@ -22,30 +22,39 @@ import javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpSession
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.unidal.eunit.benchmark.BenchmarkClassRunner
;
@RunWith
(
BenchmarkClassRunner
.
class
)
public
class
TestTrafficFilter
{
public
class
TrafficFilterTest
{
private
Invocable
m_inv
;
public
void
test
(
String
url
,
boolean
expected
)
throws
ScriptException
,
NoSuchMethodException
,
FileNotFoundException
{
public
void
check
(
String
url
,
boolean
expected
)
throws
ScriptException
,
NoSuchMethodException
,
FileNotFoundException
{
HttpServletRequest
request
=
new
MockHttpServletRequest
(
url
);
boolean
result
=
false
;
try
{
result
=
(
Boolean
)
m_inv
.
invokeFunction
(
"isEligible"
,
request
);
}
catch
(
Exception
e
){
System
.
out
.
println
(
e
);
}
Assert
.
assertEquals
(
expected
,
result
);
}
@Before
public
void
setup
()
throws
FileNotFoundException
,
ScriptException
{
ScriptEngineManager
mgr
=
new
ScriptEngineManager
();
ScriptEngine
engine
=
mgr
.
getEngineByExtension
(
"java"
);
Reader
reader
=
new
FileReader
(
"src/test/java/com/dianping/cat/home/abtest/conditions/TrafficFilter.java"
);
Invocable
inv
=
(
Invocable
)
engine
.
eval
(
reader
);
HttpServletRequest
request
=
new
MockHttpServletRequest
(
url
);
Assert
.
assertEquals
(
expected
,
inv
.
invokeFunction
(
"isEligible"
,
request
));
Reader
reader
=
new
FileReader
(
"src/test/resources/com/dianping/cat/home/abtest/conditions/TrafficFilter"
);
m_inv
=
(
Invocable
)
engine
.
eval
(
reader
);
}
@Test
//@CpuMeta(loops = 1000)
//@MemoryMeta(loops = 1000)
public
void
realTest
()
throws
FileNotFoundException
,
ScriptException
,
NoSuchMethodException
{
test
(
"http://www.dianping.com"
,
true
);
test
(
"http://www.dianping.com1"
,
false
);
test
(
"http://www.dianping.com/1"
,
false
);
test
(
"http://www.dianping.com/2"
,
true
);
public
void
whether_following_urls_can_be_filtered
()
throws
FileNotFoundException
,
ScriptException
,
NoSuchMethodException
{
check
(
"http://www.dianping.com"
,
true
);
check
(
"http://www.dianping.com1"
,
false
);
check
(
"http://www.dianping.com/1"
,
false
);
check
(
"http://www.dianping.com/2"
,
false
);
}
@SuppressWarnings
(
"rawtypes"
)
...
...
@@ -70,14 +79,15 @@ public class TestTrafficFilter {
}
@Override
public
String
get
CharacterEncoding
()
{
public
String
get
AuthType
()
{
return
null
;
}
@Override
public
void
setCharacterEncoding
(
String
env
)
throws
UnsupportedEncodingException
{
public
String
getCharacterEncoding
()
{
return
null
;
}
@Override
...
...
@@ -93,289 +103,288 @@ public class TestTrafficFilter {
}
@Override
public
S
ervletInputStream
getInputStream
()
throws
IOException
{
public
S
tring
getContextPath
()
{
return
null
;
}
@Override
public
String
getParameter
(
String
name
)
{
public
Cookie
[]
getCookies
(
)
{
return
null
;
}
@Override
public
Enumeration
getParameterNames
(
)
{
public
long
getDateHeader
(
String
name
)
{
return
null
;
return
0
;
}
@Override
public
String
[]
getParameterValues
(
String
name
)
{
public
String
getHeader
(
String
name
)
{
return
null
;
}
@Override
public
Map
getParameterMap
()
{
public
Enumeration
getHeaderNames
()
{
return
null
;
}
@Override
public
String
getProtocol
(
)
{
public
Enumeration
getHeaders
(
String
name
)
{
return
null
;
}
@Override
public
S
tring
getScheme
()
{
public
S
ervletInputStream
getInputStream
()
throws
IOException
{
return
null
;
}
@Override
public
String
getServerName
(
)
{
public
int
getIntHeader
(
String
name
)
{
return
null
;
return
0
;
}
@Override
public
int
getServerPort
()
{
public
String
getLocalAddr
()
{
return
0
;
return
null
;
}
@Override
public
BufferedReader
getReader
()
throws
IOException
{
public
Locale
getLocale
()
{
return
null
;
}
@Override
public
String
getRemoteAddr
()
{
public
Enumeration
getLocales
()
{
return
null
;
}
@Override
public
String
get
RemoteHost
()
{
public
String
get
LocalName
()
{
return
null
;
}
@Override
public
void
setAttribute
(
String
name
,
Object
o
)
{
public
int
getLocalPort
(
)
{
return
0
;
}
@Override
public
void
removeAttribute
(
String
name
)
{
public
String
getMethod
(
)
{
return
null
;
}
@Override
public
Locale
getLocale
(
)
{
public
String
getParameter
(
String
name
)
{
return
null
;
}
@Override
public
Enumeration
getLocales
()
{
public
Map
getParameterMap
()
{
return
null
;
}
@Override
public
boolean
isSecure
()
{
public
Enumeration
getParameterNames
()
{
return
false
;
return
null
;
}
@Override
public
RequestDispatcher
getRequestDispatcher
(
String
path
)
{
public
String
[]
getParameterValues
(
String
name
)
{
return
null
;
}
@Override
public
String
get
RealPath
(
String
path
)
{
public
String
get
PathInfo
(
)
{
return
null
;
}
@Override
public
int
getRemotePort
()
{
public
String
getPathTranslated
()
{
return
0
;
return
null
;
}
@Override
public
String
get
LocalName
()
{
public
String
get
Protocol
()
{
return
null
;
}
@Override
public
String
get
LocalAddr
()
{
public
String
get
QueryString
()
{
return
null
;
}
@Override
public
int
getLocalPort
()
{
public
BufferedReader
getReader
()
throws
IOException
{
return
0
;
return
null
;
}
@Override
public
String
get
AuthType
(
)
{
public
String
get
RealPath
(
String
path
)
{
return
null
;
}
@Override
public
Cookie
[]
getCookies
()
{
public
String
getRemoteAddr
()
{
return
null
;
}
@Override
public
long
getDateHeader
(
String
name
)
{
public
String
getRemoteHost
(
)
{
return
0
;
return
null
;
}
@Override
public
String
getHeader
(
String
name
)
{
public
int
getRemotePort
(
)
{
return
null
;
return
0
;
}
@Override
public
Enumeration
getHeaders
(
String
name
)
{
public
String
getRemoteUser
(
)
{
return
null
;
}
@Override
public
Enumeration
getHeaderNames
(
)
{
public
RequestDispatcher
getRequestDispatcher
(
String
path
)
{
return
null
;
}
@Override
public
int
getIntHeader
(
String
name
)
{
public
String
getRequestedSessionId
(
)
{
return
0
;
return
null
;
}
@Override
public
String
get
Method
()
{
public
String
get
RequestURI
()
{
return
null
;
}
@Override
public
String
getPathInfo
()
{
return
null
;
public
String
Buffer
getRequestURL
()
{
StringBuffer
sb
=
new
StringBuffer
(
m_url
);
return
sb
;
}
@Override
public
String
get
PathTranslated
()
{
public
String
get
Scheme
()
{
return
null
;
}
@Override
public
String
get
ContextPath
()
{
public
String
get
ServerName
()
{
return
null
;
}
@Override
public
String
getQueryString
()
{
public
int
getServerPort
()
{
return
null
;
return
0
;
}
@Override
public
String
get
RemoteUser
()
{
public
String
get
ServletPath
()
{
return
null
;
}
@Override
public
boolean
isUserInRole
(
String
role
)
{
public
HttpSession
getSession
(
)
{
return
false
;
return
null
;
}
@Override
public
Principal
getUserPrincipal
(
)
{
public
HttpSession
getSession
(
boolean
create
)
{
return
null
;
}
@Override
public
String
getRequestedSessionId
()
{
public
Principal
getUserPrincipal
()
{
return
null
;
}
@Override
public
String
getRequestURI
()
{
public
boolean
isRequestedSessionIdFromCookie
()
{
return
null
;
return
false
;
}
@Override
public
StringBuffer
getRequestURL
()
{
StringBuffer
sb
=
new
StringBuffer
(
m_url
);
return
sb
;
public
boolean
isRequestedSessionIdFromUrl
()
{
return
false
;
}
@Override
public
String
getServletPath
()
{
public
boolean
isRequestedSessionIdFromURL
()
{
return
null
;
return
false
;
}
@Override
public
HttpSession
getSession
(
boolean
create
)
{
public
boolean
isRequestedSessionIdValid
(
)
{
return
null
;
return
false
;
}
@Override
public
HttpSession
getSession
()
{
public
boolean
isSecure
()
{
return
null
;
return
false
;
}
@Override
public
boolean
is
RequestedSessionIdValid
(
)
{
public
boolean
is
UserInRole
(
String
role
)
{
return
false
;
}
@Override
public
boolean
isRequestedSessionIdFromCookie
(
)
{
public
void
removeAttribute
(
String
name
)
{
return
false
;
}
@Override
public
boolean
isRequestedSessionIdFromURL
(
)
{
public
void
setAttribute
(
String
name
,
Object
o
)
{
return
false
;
}
@Override
public
boolean
isRequestedSessionIdFromUrl
()
{
public
void
setCharacterEncoding
(
String
env
)
throws
UnsupportedEncodingException
{
return
false
;
}
}
...
...
cat-home/src/test/java/com/dianping/cat/home/abtest/groupstrategy/GroupStrategyParserTest.java
浏览文件 @
9901c18c
...
...
@@ -3,66 +3,57 @@ package com.dianping.cat.home.abtest.groupstrategy;
import
japa.parser.ParseException
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
junit.framework.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.unidal.helper.Files
;
import
com.dianping.cat.abtest.model.entity.Condition
;
import
com.dianping.cat.abtest.model.entity.Field
;
import
com.dianping.cat.abtest.model.entity.GroupstrategyDescriptor
;
import
com.dianping.cat.system.page.abtest.GroupStrategyParser
;
import
com.dianping.cat.system.page.abtest.GsonBuilderManager
.NonPrexFieldNamingStrategy
;
import
com.dianping.cat.system.page.abtest.GsonBuilderManager
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
public
class
GroupStrategyParserTest
{
@Test
public
void
test
()
throws
IOException
,
ParseException
{
GroupStrategyParser
parser
=
new
GroupStrategyParser
();
GroupstrategyDescriptor
descriptor
=
parser
.
parse
(
getClass
().
getResourceAsStream
(
"RetinaImgGroupStrategy"
));
GsonBuilder
builder
=
new
GsonBuilder
();
builder
.
setFieldNamingStrategy
(
new
NonPrexFieldNamingStrategy
());
Gson
gson
=
builder
.
create
();
String
json
=
gson
.
toJson
(
descriptor
,
GroupstrategyDescriptor
.
class
);
System
.
out
.
println
(
json
);
String
expectedJson
=
Files
.
forIO
().
readFrom
(
getClass
().
getResourceAsStream
(
"descriptor.json"
),
"utf-8"
);
String
actualJson
=
gson
.
toJson
(
descriptor
);
System
.
out
.
println
(
actualJson
);
Assert
.
assertEquals
(
expectedJson
.
replaceAll
(
"\\s*"
,
""
),
actualJson
.
replaceAll
(
"\\s*"
,
""
));
for
(
Field
field
:
descriptor
.
getFields
()){
field
.
setValue
(
""
);
public
class
GroupStrategyParserTest
{
private
GroupStrategyParser
m_parser
;
private
GroupstrategyDescriptor
m_descriptorObject
;
private
String
m_descriptorXml
;
private
String
m_descriptorJson
;
@Before
public
void
setup
()
{
try
{
m_parser
=
new
GroupStrategyParser
();
m_descriptorObject
=
m_parser
.
parse
(
getClass
().
getResourceAsStream
(
"RetinaImgGroupStrategy"
));
m_descriptorXml
=
Files
.
forIO
().
readFrom
(
getClass
().
getResourceAsStream
(
"descriptor.xml"
),
"utf-8"
);
m_descriptorJson
=
Files
.
forIO
().
readFrom
(
getClass
().
getResourceAsStream
(
"descriptor.json"
),
"utf-8"
);
}
catch
(
Exception
e
)
{
// do nothing
}
System
.
out
.
println
(
descriptor
);
String
expected
=
Files
.
forIO
().
readFrom
(
getClass
().
getResourceAsStream
(
"descriptor.xml"
),
"utf-8"
);
Assert
.
assertEquals
(
expected
.
replaceAll
(
"\\s*"
,
""
),
descriptor
.
toString
().
replaceAll
(
"\\s*"
,
""
));
Condition
condition
=
new
Condition
();
condition
.
setComparator
(
1
);
condition
.
setName
(
"url"
);
condition
.
setOperator
(
"and"
);
condition
.
setText
(
"http://www.dianping.com"
);
Condition
condition1
=
new
Condition
();
condition1
.
setComparator
(
1
);
condition1
.
setName
(
"url"
);
condition1
.
setOperator
(
"and"
);
condition1
.
setText
(
"http://www.dianping.com"
);
List
<
Condition
>
conditions
=
new
ArrayList
<
Condition
>();
conditions
.
add
(
condition
);
conditions
.
add
(
condition1
);
System
.
out
.
println
(
gson
.
toJson
(
conditions
));
}
@Test
public
void
given_groupstrategy_parser_can_correctly_parse
()
{
assertEscapeString
(
m_descriptorXml
,
m_descriptorObject
.
toString
());
}
@Test
public
void
then_gsonbuilder_can_correctly_parse_json_to_object
()
throws
IOException
,
ParseException
{
GsonBuilderManager
manager
=
new
GsonBuilderManager
();
Gson
gson
=
manager
.
getGsonBuilder
().
create
();
String
json
=
gson
.
toJson
(
m_descriptorObject
,
GroupstrategyDescriptor
.
class
);
assertEscapeString
(
m_descriptorJson
,
json
);
}
public
void
assertEscapeString
(
String
expectedString
,
String
actualString
)
{
Assert
.
assertEquals
(
expectedString
.
replaceAll
(
"\\s*"
,
""
),
actualString
.
replaceAll
(
"\\s*"
,
""
));
}
}
cat-home/src/test/java/com/dianping/cat/home/abtest/groupstrategy/GsonBuilderManagerTest.java
0 → 100644
浏览文件 @
9901c18c
package
com.dianping.cat.home.abtest.groupstrategy
;
import
junit.framework.Assert
;
import
org.junit.Test
;
import
com.dianping.cat.system.page.abtest.GsonBuilderManager
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
public
class
GsonBuilderManagerTest
{
@Test
public
void
test_none_prefix_field
(){
GsonBuilderManager
manager
=
new
GsonBuilderManager
();
GsonBuilder
builder
=
manager
.
getGsonBuilder
();
Gson
gson
=
builder
.
create
();
MockObject
object
=
new
MockObject
(
"name"
,
"type"
);
String
json
=
gson
.
toJson
(
object
,
MockObject
.
class
);
Assert
.
assertEquals
(
"{\"name\":\"name\",\"type\":\"type\"}"
,
json
);
}
public
class
MockObject
{
private
String
m_name
;
private
String
m_type
;
public
MockObject
(
String
name
,
String
type
)
{
m_name
=
name
;
m_type
=
type
;
}
@Override
public
String
toString
()
{
return
"MockObject [m_name="
+
m_name
+
", m_type="
+
m_type
+
"]"
;
}
}
}
cat-home/src/test/
java/com/dianping/cat/home/abtest/conditions/TrafficFilter.java
→
cat-home/src/test/
resources/com/dianping/cat/home/abtest/conditions/TrafficFilter
浏览文件 @
9901c18c
package
com.dianping.cat.home.abtest.conditions
;
import
java.util.Random
;
import
javax.servlet.http.HttpServletRequest
;
package
com
.
dianping
.
cat
.
abtest
.
conditions
;
import
java
.
util
.
Random
;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
com
.
dianping
.
cat
.
abtest
.
spi
.
internal
.
conditions
.
ABTestCondition
;
public
class
TrafficFilter
{
private
Condition1
m_condition1
=
new
Condition1
();
private
Condition2
m_condition2
=
new
Condition2
();
...
...
@@ -16,76 +16,144 @@ public class TrafficFilter {
private
Condition5
m_condition5
=
new
Condition5
();
private
Condition6
m_condition6
=
new
Condition6
();
private
Condition7
m_condition7
=
new
Condition7
();
public
boolean
isEligible
(
HttpServletRequest
request
)
{
if
(
m_condition1
.
accept
(
request
)
||
m_condition2
.
accept
(
request
)
&&
m_condition3
.
accept
(
request
)
&&
(
m_condition4
.
accept
(
request
))
&&
m_condition5
.
accept
(
request
))
{
return
true
;
}
if
(
m_condition1
.
accept
(
request
)
&&
m_condition2
.
accept
(
request
)
&&
m_condition3
.
accept
(
request
)
&&
(
m_condition4
.
accept
(
request
)
||
m_condition5
.
accept
(
request
)
&&
m_condition6
.
accept
(
request
)
)
&&
m_condition7
.
accept
(
request
)
)
{
return
true
;
}
return
false
;
}
public
class
Condition1
implements
ABTestCondition
{
@
Override
public
boolean
accept
(
HttpServletRequest
request
)
{
String
actual
=
request
.
getRequestURL
().
toString
();
if
(
actual
.
equalsIgnoreCase
(
"http://www.dianping.com"
))
{
if
(
actual
.
equalsIgnoreCase
(
"http://www.DIANPING.com"
))
{
return
true
;
}
else
{
return
false
;
}
}
}
}
public
class
Condition2
implements
ABTestCondition
{
@
Override
public
boolean
accept
(
HttpServletRequest
request
)
{
String
actual
=
request
.
getRequestURL
().
toString
();
if
(
actual
.
toLowerCase
().
startsWith
(
"http://www.dianping.com/"
.
toLowerCase
()))
{
if
(
actual
.
toLowerCase
().
startsWith
(
"http://www.dianping.com"
.
toLowerCase
()))
{
return
true
;
}
else
{
return
false
;
}
}
}
}
public
class
Condition3
implements
ABTestCondition
{
@
Override
public
boolean
accept
(
HttpServletRequest
request
)
{
String
actual
=
request
.
getRequestURL
().
toString
();
if
(!
actual
.
equalsIgnoreCase
(
"http://www.dianping.com1"
))
{
if
(
actual
.
indexOf
(
"http://www.dianping.com1"
)
==
-
1
)
{
return
true
;
}
else
{
return
false
;
}
}
}
}
public
class
Condition4
implements
ABTestCondition
{
@
Override
public
boolean
accept
(
HttpServletRequest
request
)
{
String
actual
=
request
.
getRequestURL
().
toString
();
if
(!
actual
.
equalsIgnoreCase
(
"http://www.dianping.com/1"
))
{
if
(
actual
.
equals
(
"http://www.dianping.com2"
))
{
return
true
;
}
else
{
return
false
;
}
}
}
}
public
class
Condition5
implements
ABTestCondition
{
@
Override
public
boolean
accept
(
HttpServletRequest
request
)
{
String
actual
=
request
.
getRequestURL
().
toString
();
if
(
!actual.equals("http://www.dianping.com/2")) {
return
true
;
}
else
{
return
false
;
}
}
}
public
class
Condition6
implements
ABTestCondition
{
@
Override
public
boolean
accept
(
HttpServletRequest
request
)
{
String
actual
=
request
.
getRequestURL
().
toString
();
if
(
actual
.
startsWith
(
"http://www.dianping.com"
))
{
return
true
;
}
else
{
return
false
;
}
}
}
public
class
Condition7
implements
ABTestCondition
{
private
int
m_percent
=
-
1
;
private
Random
m_random
=
new
Random
();
@
Override
public
boolean
accept
(
HttpServletRequest
request
)
{
if
(
m_percent
==
-
1
)
{
m_percent
=
100
;
}
if
(
m_percent
==
100
)
{
return
true
;
}
int
random
=
m_random
.
nextInt
(
100
)
+
1
;
if
(
random
<=
m_percent
)
{
return
true
;
}
else
{
...
...
@@ -93,4 +161,5 @@ public class TrafficFilter {
}
}
}
}
\ No newline at end of file
cat-home/src/test/resources/com/dianping/cat/home/abtest/groupstrategy/descriptor.xml
浏览文件 @
9901c18c
<?xml version="1.0" encoding="utf-8"?>
<groupstrategy-descriptor
className=
"RetinaImgGroupStrategy"
fullyQualifiedName=
"com.dianping.tuangou.web.biz.dealgroup.RetinaImgGroupStrategy"
>
<fields>
<field
name=
"percentage"
type=
"int"
input-type=
"input"
modifier-name=
"m_percentage"
></field
>
<field
name=
"deals"
type=
"String"
input-type=
"input"
modifier-name=
"m_deals"
></field
>
<field
name=
"percentage"
type=
"int"
input-type=
"input"
modifier-name=
"m_percentage"
/
>
<field
name=
"deals"
type=
"String"
input-type=
"input"
modifier-name=
"m_deals"
/
>
</fields>
</groupstrategy-descriptor>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录