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

Commit 82bc524

Browse files
committed
增加动态参数的实现
1 parent 441b823 commit 82bc524

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

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

Lines changed: 16 additions & 3 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.data;
518

619
import org.suren.autotest.web.framework.page.Page;
@@ -10,7 +23,7 @@
1023
* @author suren
1124
* @date Jul 17, 2016 8:45:03 AM
1225
*/
13-
public interface DataSource
26+
public interface DataSource extends DynamicDataSource
1427
{
1528
/**
1629
* 从数据资源接口中加载数据到Page类中

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Iterator;
1111
import java.util.LinkedList;
1212
import java.util.List;
13+
import java.util.Map;
1314
import java.util.Set;
1415

1516
import org.apache.poi.ss.usermodel.Cell;
@@ -19,6 +20,8 @@
1920
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
23+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
24+
import org.springframework.context.annotation.Scope;
2225
import org.springframework.stereotype.Component;
2326
import org.suren.autotest.web.framework.page.Page;
2427

@@ -28,7 +31,8 @@
2831
* @date Jul 17, 2016 8:56:31 AM
2932
*/
3033
@Component("excel_data_source")
31-
public class ExcelDataSource implements DataSource
34+
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35+
public class ExcelDataSource implements DataSource, DynamicDataSource
3236
{
3337
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelDataSource.class);
3438

@@ -118,4 +122,25 @@ public boolean loadData(DataResource resource, int row, Page page)
118122
return false;
119123
}
120124

125+
@Override
126+
public void setGlobalMap(Map<String, Object> globalMap)
127+
{
128+
// TODO Auto-generated method stub
129+
130+
}
131+
132+
@Override
133+
public Map<String, Object> getGlobalMap()
134+
{
135+
// TODO Auto-generated method stub
136+
return null;
137+
}
138+
139+
@Override
140+
public String getName()
141+
{
142+
// TODO Auto-generated method stub
143+
return null;
144+
}
145+
121146
}

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
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

619
import java.io.File;
720
import java.io.IOException;
821
import java.io.InputStream;
22+
import java.io.UnsupportedEncodingException;
923
import java.lang.reflect.InvocationTargetException;
1024
import java.lang.reflect.Method;
1125
import java.net.URL;
26+
import java.net.URLDecoder;
1227
import java.util.HashMap;
1328
import java.util.List;
1429
import java.util.Map;
@@ -26,6 +41,8 @@
2641
import org.slf4j.LoggerFactory;
2742
import org.springframework.beans.BeanUtils;
2843
import org.springframework.beans.factory.annotation.Autowired;
44+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
45+
import org.springframework.context.annotation.Scope;
2946
import org.springframework.stereotype.Component;
3047
import org.suren.autotest.web.framework.core.ui.AbstractElement;
3148
import org.suren.autotest.web.framework.core.ui.Button;
@@ -42,11 +59,15 @@
4259
* @date Jul 17, 2016 8:56:51 AM
4360
*/
4461
@Component("xml_data_source")
45-
public class XmlDataSource implements DataSource
62+
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
63+
public class XmlDataSource implements DataSource, DynamicDataSource
4664
{
4765
private static final Logger LOGGER = LoggerFactory.getLogger(XmlDataSource.class);
4866

67+
public static final String NS_URI = "http://datasource.surenpi.com";
68+
4969
private Page page;
70+
private URL url;
5071
private Map<String, Object> globalMap = new HashMap<String, Object>();
5172

5273
@Autowired
@@ -87,7 +108,7 @@ private void parse(Document doc, int row)
87108
{
88109
String pageClass = page.getClass().getName();
89110
SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
90-
simpleNamespaceContext.addNamespace("ns", "http://datasource.surenpi.com");
111+
simpleNamespaceContext.addNamespace("ns", NS_URI);
91112

92113
XPath xpath = new DefaultXPath("/ns:dataSources");
93114
xpath.setNamespaceContext(simpleNamespaceContext);
@@ -139,6 +160,7 @@ public void visit(Element node)
139160
DynamicData dynamicData = getDynamicDataByType(type);
140161
if(dynamicData != null)
141162
{
163+
dynamicData.setData(globalMap);
142164
value = dynamicData.getValue(value);
143165
}
144166
else
@@ -259,10 +281,12 @@ private DynamicData getDynamicDataByType(String type)
259281
public boolean loadData(DataResource resource, int row, Page page)
260282
{
261283
this.page = page;
262-
URL url = null;
263-
try {
284+
try
285+
{
264286
url = resource.getUrl();
265-
} catch (IOException e) {
287+
}
288+
catch (IOException e)
289+
{
266290
LOGGER.error(e.getMessage(), e);
267291
}
268292

@@ -288,6 +312,7 @@ public boolean loadData(DataResource resource, int row, Page page)
288312
/**
289313
* @return the globalMap
290314
*/
315+
@Override
291316
public Map<String, Object> getGlobalMap()
292317
{
293318
return globalMap;
@@ -296,9 +321,27 @@ public Map<String, Object> getGlobalMap()
296321
/**
297322
* @param globalMap the globalMap to set
298323
*/
324+
@Override
299325
public void setGlobalMap(Map<String, Object> globalMap)
300326
{
301327
this.globalMap = globalMap;
302328
}
303329

330+
@Override
331+
public String getName()
332+
{
333+
try
334+
{
335+
File file = new File(URLDecoder.decode(this.url.getFile(), "utf-8"));
336+
337+
return file.getName();
338+
}
339+
catch (UnsupportedEncodingException e)
340+
{
341+
e.printStackTrace();
342+
}
343+
344+
return this.url.getPath();
345+
}
346+
304347
}

0 commit comments

Comments
 (0)