Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 6eacb9f

Browse files
committed
增强callback
1 parent 4782ede commit 6eacb9f

File tree

2 files changed

+86
-19
lines changed

2 files changed

+86
-19
lines changed
Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,81 @@
1-
/**
2-
* http://surenpi.com
1+
/*
2+
* Copyright 2002-2007 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
315
*/
16+
417
package org.suren.autotest.web.framework.data;
518

19+
import java.util.HashMap;
620
import java.util.Map;
721

22+
import org.springframework.beans.BeansException;
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.context.ApplicationContextAware;
825
import org.springframework.stereotype.Component;
926

1027
/**
1128
* @author suren
1229
* @date 2017年1月11日 下午1:32:02
1330
*/
1431
@Component
15-
public class CallbackDynamicData implements DynamicData
32+
public class CallbackDynamicData implements DynamicData, ApplicationContextAware
1633
{
34+
private static final String DYNAMIC_DATA = "dynamic.data";
35+
36+
private ApplicationContext context;
37+
private Map<String, Object> data;
1738

1839
@Override
1940
public String getValue(String orginData)
2041
{
42+
String type = dataPrepare();
43+
44+
Map<String, DynamicData> dynamicDataMap = context.getBeansOfType(DynamicData.class);
45+
for(DynamicData dynamicData : dynamicDataMap.values())
46+
{
47+
if(dynamicData == this)
48+
{
49+
continue;
50+
}
51+
52+
if(dynamicData.getType().equals(type))
53+
{
54+
dynamicData.setData(data);
55+
return dynamicData.getValue(orginData);
56+
}
57+
}
58+
2159
return orginData;
2260
}
61+
62+
/**
63+
* 默认参数准备
64+
*/
65+
private String dataPrepare()
66+
{
67+
if(data == null)
68+
{
69+
data = new HashMap<String, Object>();
70+
}
71+
72+
if(!data.containsKey(DYNAMIC_DATA))
73+
{
74+
data.put(DYNAMIC_DATA, "freemarker");
75+
}
76+
77+
return (String) data.get(DYNAMIC_DATA);
78+
}
2379

2480
@Override
2581
public String getType()
@@ -30,6 +86,14 @@ public String getType()
3086
@Override
3187
public void setData(Map<String, Object> data)
3288
{
89+
this.data = data;
90+
}
91+
92+
@Override
93+
public void setApplicationContext(ApplicationContext context)
94+
throws BeansException
95+
{
96+
this.context = context;
3397
}
3498

3599
}

src/main/java/org/suren/autotest/web/framework/data/XmlDataSource.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,6 @@ public void visit(Element node)
154154
String type = node.attributeValue("type", "simple");
155155
String field = node.attributeValue("field", "value");
156156
String callback = node.attributeValue("callback", "");
157-
158-
LOGGER.debug("Field [{}], value [{}], type [{}], field [{}].", fieldName, value, type, field);
159-
160-
DynamicData dynamicData = getDynamicDataByType(type);
161-
if(dynamicData != null)
162-
{
163-
dynamicData.setData(globalMap);
164-
value = dynamicData.getValue(value);
165-
}
166-
else
167-
{
168-
throw new RuntimeException("Not support dynamic data type : " + type);
169-
}
170157

171158
Method getterMethod = BeanUtils.findMethod(page.getClass(),
172159
"get" + fieldName.substring(0, 1).toUpperCase()
@@ -187,8 +174,23 @@ public void visit(Element node)
187174
AbstractElement absEle = (AbstractElement) eleObj;
188175
absEle.putData("DataType", type);
189176

190-
//解析数据源中单个数据项的扩展参数
191-
parseDataParamList(absEle, node);
177+
LOGGER.debug("Field [{}], value [{}], type [{}], field [{}].", fieldName, value, type, field);
178+
179+
DynamicData dynamicData = getDynamicDataByType(type);
180+
if(dynamicData != null)
181+
{
182+
Map<String, Object> tmpMap = new HashMap<String, Object>(globalMap);
183+
184+
//解析数据源中单个数据项的扩展参数
185+
parseDataParamList(absEle, node, tmpMap);
186+
187+
dynamicData.setData(tmpMap);
188+
value = dynamicData.getValue(value);
189+
}
190+
else
191+
{
192+
throw new RuntimeException("Not support dynamic data type : " + type);
193+
}
192194

193195
if(eleObj instanceof Text)
194196
{
@@ -242,7 +244,7 @@ else if(eleObj instanceof Button)
242244
* @param absEle
243245
* @param node
244246
*/
245-
private void parseDataParamList(AbstractElement absEle, Element node)
247+
private void parseDataParamList(AbstractElement absEle, Element node, Map<String, Object> tmpMap)
246248
{
247249
@SuppressWarnings("unchecked")
248250
List<Element> paramEleList = node.elements("param");
@@ -256,6 +258,7 @@ private void parseDataParamList(AbstractElement absEle, Element node)
256258
String key = element.attributeValue("name");
257259
String value = element.attributeValue("value");
258260
absEle.putData(key, value);
261+
tmpMap.put(key, value);
259262
}
260263
}
261264

0 commit comments

Comments
 (0)