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

Commit d2c7b17

Browse files
committed
增加对动态参数的支持
1 parent 1d60d2f commit d2c7b17

File tree

4 files changed

+113
-17
lines changed

4 files changed

+113
-17
lines changed

src/main/java/org/suren/autotest/web/framework/settings/DriverConstants.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
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.settings;
518

619
/**

src/main/java/org/suren/autotest/web/framework/settings/SettingUtil.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStream;
2525
import java.lang.reflect.InvocationTargetException;
2626
import java.lang.reflect.Method;
27+
import java.util.ArrayList;
2728
import java.util.Collection;
2829
import java.util.HashMap;
2930
import java.util.HashSet;
@@ -57,6 +58,7 @@
5758
import org.suren.autotest.web.framework.data.ClasspathResource;
5859
import org.suren.autotest.web.framework.data.DataResource;
5960
import org.suren.autotest.web.framework.data.DataSource;
61+
import org.suren.autotest.web.framework.data.DynamicDataSource;
6062
import org.suren.autotest.web.framework.data.FileResource;
6163
import org.suren.autotest.web.framework.hook.ShutdownHook;
6264
import org.suren.autotest.web.framework.page.Page;
@@ -77,6 +79,7 @@ public class SettingUtil implements Closeable
7779

7880
private Map<String, Page> pageMap = new HashMap<String, Page>();
7981
private Map<String, DataSourceInfo> dataSourceMap = new HashMap<String, DataSourceInfo>();
82+
private Map<String, DynamicDataSource> dynamicDataSourceMap;
8083
private ApplicationContext context;
8184

8285
/** 系统配置路径 */
@@ -241,9 +244,11 @@ public void initData()
241244
/**
242245
* 从数据源中加载指定的数据组,设置到所有page类中
243246
* @param 数据组序号(从1开始)
247+
* @return 如果没有数据源,则返回空列表
244248
*/
245-
public void initData(int row)
249+
public List<DynamicDataSource> initData(int row)
246250
{
251+
List<DynamicDataSource> dynamicDataSourceList = new ArrayList<DynamicDataSource>();
247252
Iterator<String> pageIterator = pageMap.keySet().iterator();
248253
while(pageIterator.hasNext())
249254
{
@@ -256,8 +261,15 @@ public void initData(int row)
256261
}
257262

258263
Page page = pageMap.get(pageKey);
259-
initPageData(page, row);
264+
265+
DynamicDataSource dynamicDataSource = initPageData(page, row);
266+
if(dynamicDataSource != null)
267+
{
268+
dynamicDataSourceList.add(dynamicDataSource);
269+
}
260270
}
271+
272+
return dynamicDataSourceList;
261273
}
262274

263275
/**
@@ -266,16 +278,17 @@ public void initData(int row)
266278
* @param row 数据组序号(从1开始)
267279
* @return
268280
*/
269-
public boolean initPageData(Page page, int row)
281+
public DynamicDataSource initPageData(Page page, int row)
270282
{
271283
String dataSourceStr = page.getDataSource();
272284
final DataSourceInfo dataSourceInfo = dataSourceMap.get(dataSourceStr);
273285
if(dataSourceInfo == null)
274286
{
275-
return false;
287+
return null;
276288
}
277289

278-
DataSource dataSource = context.getBean(dataSourceInfo.getType(), DataSource.class);
290+
getDynamicDataSources();
291+
DataSource dataSource = (DataSource) dynamicDataSourceMap.get(dataSourceInfo.getType());//context.getBean(dataSourceInfo.getType(), DataSource.class);
279292
DataResource clzResource = new ClasspathResource(
280293
SettingUtil.class, dataSourceInfo.getResource());
281294
try
@@ -296,7 +309,26 @@ public boolean initPageData(Page page, int row)
296309
e.printStackTrace();
297310
}
298311

299-
return dataSource.loadData(clzResource, row, page);
312+
dataSource.loadData(clzResource, row, page);
313+
314+
return dataSource;
315+
}
316+
317+
public Collection<DynamicDataSource> getDynamicDataSources()
318+
{
319+
if(dynamicDataSourceMap == null)
320+
{
321+
dynamicDataSourceMap = context.getBeansOfType(DynamicDataSource.class);
322+
}
323+
324+
if(dynamicDataSourceMap != null)
325+
{
326+
return dynamicDataSourceMap.values();
327+
}
328+
else
329+
{
330+
return null;
331+
}
300332
}
301333

302334
/**
@@ -356,7 +388,7 @@ private void parse(Document doc) throws IOException, DocumentException, SAXExcep
356388
simpleNamespaceContext.addNamespace("ns", "http://surenpi.com");
357389

358390
SeleniumEngine seleniumEngine = getEngine();
359-
if(seleniumEngine.getDriverStr() == null || "".equals(seleniumEngine.getDriverStr()))
391+
if(StringUtils.isBlank(seleniumEngine.getDriverStr()))
360392
{
361393
XPath xpath = new DefaultXPath("/ns:autotest/ns:engine");
362394
xpath.setNamespaceContext(simpleNamespaceContext);
@@ -650,6 +682,11 @@ private String convertBeanName(final String pageClsStr)
650682
result = pageClsStr.substring(index + 1);
651683
}
652684

685+
if(result.length() > 1 && result.charAt(1) >= 'A' && result.charAt(1) <= 'Z')
686+
{
687+
return result;
688+
}
689+
653690
return StringUtils.uncapitalize(result);
654691
}
655692

src/main/java/org/suren/autotest/web/framework/settings/SpringUtils.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
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.settings;
518

619
import org.springframework.beans.BeansException;

src/main/java/org/suren/autotest/web/framework/settings/SuiteParser.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
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.settings;
518

619
import java.io.InputStream;
720
import java.util.ArrayList;
21+
import java.util.Collections;
822
import java.util.List;
923

1024
import org.dom4j.Document;
@@ -26,6 +40,11 @@
2640
*/
2741
public class SuiteParser
2842
{
43+
/** 命名空间地址 */
44+
public static final String NS_URI = "http://suite.surenpi.com";
45+
46+
/** 支持的文件后缀 */
47+
private final List<String> supportList = new ArrayList<String>(1);
2948
private SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
3049

3150
/**
@@ -41,14 +60,14 @@ public Suite parse(InputStream suiteInputStream) throws DocumentException
4160

4261
Document document = reader.read(suiteInputStream);
4362

44-
simpleNamespaceContext.addNamespace("ns", "http://suite.surenpi.com");
63+
simpleNamespaceContext.addNamespace("ns", NS_URI);
4564

4665
XPath xpath = new DefaultXPath("/ns:suite");
4766
xpath.setNamespaceContext(simpleNamespaceContext);
4867
Element suiteEle = (Element) xpath.selectSingleNode(document);
4968
if (suiteEle == null)
5069
{
51-
throw new RuntimeException("can not found suite config.");
70+
throw new RuntimeException("Can not found suite config.");
5271
}
5372

5473
Suite suite = new Suite();
@@ -70,6 +89,20 @@ public Suite parse(InputStream suiteInputStream) throws DocumentException
7089

7190
return suite;
7291
}
92+
93+
/**
94+
* 返回结果不允许修改
95+
* @return 支持的文件后缀
96+
*/
97+
public List<String> getSupport()
98+
{
99+
if(supportList.size() == 0)
100+
{
101+
supportList.add(".xml");
102+
}
103+
104+
return Collections.unmodifiableList(supportList);
105+
}
73106

74107
/**
75108
* 解析page配置
@@ -93,7 +126,7 @@ private void pagesParse(Document document, Suite suite)
93126
List<Element> pageNodes = xpath.selectNodes(document);
94127
if(pageNodes == null || pageNodes.size() == 0)
95128
{
96-
throw new RuntimeException("can not found page config.");
129+
throw new RuntimeException("Can not found page config.");
97130
}
98131

99132
for(Element pageEle : pageNodes)
@@ -106,7 +139,7 @@ private void pagesParse(Document document, Suite suite)
106139
Element actionsEle = (Element) xpath.selectSingleNode(pageEle);
107140
if(actionsEle == null)
108141
{
109-
throw new RuntimeException("can not found actions config.");
142+
throw new RuntimeException("Can not found actions config.");
110143
}
111144

112145
String disable = actionsEle.attributeValue("disable", "false");

0 commit comments

Comments
 (0)