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

Commit 5e6cbb3

Browse files
committed
增加随机从文本中获取字符串的功能
1 parent ea6119c commit 5e6cbb3

File tree

4 files changed

+1177
-1
lines changed

4 files changed

+1177
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.
15+
*/
16+
17+
package org.suren.autotest.web.framework.data;
18+
19+
import java.io.BufferedReader;
20+
import java.io.IOException;
21+
import java.io.InputStreamReader;
22+
import java.net.URL;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Random;
27+
28+
import org.springframework.stereotype.Component;
29+
30+
/**
31+
* 从文本文件中随机获取一个字符串
32+
* @author suren
33+
* @date 2017年4月4日 下午12:17:36
34+
*/
35+
@Component
36+
public class RandomTxtDynamicData implements DynamicData
37+
{
38+
@Override
39+
public String getValue(String orginData)
40+
{
41+
URL url = getInput(orginData);
42+
if(url == null)
43+
{
44+
throw new RuntimeException(String.format("Can not found random txt [%s] from data dir.", orginData));
45+
}
46+
47+
List<String> list = new ArrayList<String>();
48+
try(BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())))
49+
{
50+
String line = null;
51+
while((line = reader.readLine()) != null)
52+
{
53+
line = line.trim();
54+
if("".equals(line))
55+
{
56+
continue;
57+
}
58+
59+
list.add(line);
60+
}
61+
}
62+
catch (IOException e)
63+
{
64+
e.printStackTrace();
65+
}
66+
67+
if(list.size() == 0)
68+
{
69+
throw new RuntimeException(String.format("Random txt file [%s] content is empty!", orginData));
70+
}
71+
72+
return list.get(new Random().nextInt(list.size()));
73+
}
74+
75+
@Override
76+
public String getType()
77+
{
78+
return "random_text";
79+
}
80+
81+
private URL getInput(String key)
82+
{
83+
URL url = RandomTxtDynamicData.class.getResource("/data/" + key);
84+
85+
return url;
86+
}
87+
88+
@Override
89+
public void setData(Map<String, Object> data)
90+
{
91+
}
92+
93+
}

src/main/resources/autotest.web.framework.datasource.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@
164164
</xs:documentation>
165165
</xs:annotation>
166166
</xs:enumeration>
167+
<xs:enumeration value="random_text">
168+
<xs:annotation>
169+
<xs:documentation>
170+
<![CDATA[从文本文件中随机获取一行文本。]]>
171+
</xs:documentation>
172+
</xs:annotation>
173+
</xs:enumeration>
167174
<xs:enumeration value="page_ref">
168175
<xs:annotation>
169176
<xs:documentation>

src/main/resources/autotest.web.framework.suite.xsd

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
</xs:documentation>
117117
</xs:annotation>
118118
</xs:attribute>
119-
<xs:attribute name="invoker" type="xs:string">
119+
<xs:attribute name="invoker" type="suren:invokerEnum">
120120
<xs:annotation>
121121
<xs:documentation>
122122
<![CDATA[调用外部的静态类,方法名以英文感叹号(!)分割,方法参数类型为SettingUtil,例:org.suren.DemoInvoker!execute。]]>
@@ -172,6 +172,25 @@
172172
</xs:annotation>
173173
</xs:attribute>
174174
</xs:attributeGroup>
175+
176+
<xs:simpleType name="invokerEnum">
177+
<xs:union>
178+
<xs:simpleType>
179+
<xs:restriction base="xs:string">
180+
<xs:enumeration value="JsInvoker"></xs:enumeration>
181+
<xs:enumeration value="EngineInvoker"></xs:enumeration>
182+
<xs:enumeration value="KaptchaInvoker"></xs:enumeration>
183+
<xs:enumeration value="OSInvoker"></xs:enumeration>
184+
<xs:enumeration value="TestNGInvoker"></xs:enumeration>
185+
</xs:restriction>
186+
</xs:simpleType>
187+
<xs:simpleType>
188+
<xs:restriction base="xs:string">
189+
<xs:pattern value="[a-z]Invoker"></xs:pattern>
190+
</xs:restriction>
191+
</xs:simpleType>
192+
</xs:union>
193+
</xs:simpleType>
175194

176195
<xs:simpleType name="actionEnum">
177196
<xs:restriction base="xs:string">

0 commit comments

Comments
 (0)