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

Commit 1d60d2f

Browse files
committed
修正有关简单数据的日期部分
1 parent 82bc524 commit 1d60d2f

File tree

3 files changed

+90
-15
lines changed

3 files changed

+90
-15
lines changed

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

Lines changed: 23 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.data;
518

619
import java.util.Map;
@@ -12,8 +25,16 @@
1225
*/
1326
public interface DynamicData
1427
{
28+
/**
29+
* 返回格式化后的字符串
30+
* @param orginData
31+
* @return
32+
*/
1533
String getValue(String orginData);
1634

35+
/**
36+
* @return 动态数据类型(用于区分标识)
37+
*/
1738
String getType();
1839

1940
void setData(Map<String, Object> data);

src/main/java/org/suren/autotest/web/framework/data/GroovyDynamicData.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.data;
518

619
import groovy.lang.Binding;

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

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
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.text.SimpleDateFormat;
7-
import java.util.ArrayList;
20+
import java.util.Collections;
821
import java.util.Date;
9-
import java.util.List;
22+
import java.util.HashSet;
1023
import java.util.Map;
24+
import java.util.Set;
1125

1226
import org.springframework.stereotype.Component;
1327
import org.suren.autotest.web.framework.util.CommonNumberUtil;
@@ -21,31 +35,36 @@
2135
* @date 2017年1月4日 下午12:33:01
2236
*/
2337
@Component
24-
public class SimpleDynamicData implements DynamicData
38+
public class SimpleDynamicData implements DynamicData, DynamicDateFormat
2539
{
26-
private List<String> formatList = new ArrayList<String>();
27-
40+
private Set<String> formatSet = new HashSet<String>();
41+
42+
private SimpleDateFormat dateFormat = new SimpleDateFormat();
2843
private String random = "${random-";
2944

3045
public SimpleDynamicData()
3146
{
32-
formatList.add("yyyy-MM-dd");
33-
formatList.add("yyyy-MM-dd HH:mm:ss");
47+
formatSet.add("yyyy");
48+
formatSet.add("MM-dd");
49+
formatSet.add("yyyy-MM-dd");
50+
formatSet.add("yyyy-MM-dd HH:mm:ss");
3451
}
3552

3653
@Override
3754
public String getValue(final String orginData)
3855
{
3956
String value = orginData;
4057

58+
Date nowDate = new Date();
4159
value = value.replace("${now}", String.valueOf(System.currentTimeMillis()));
4260

43-
for(String format : formatList)
61+
for(String format : formatSet)
4462
{
4563
String param = "${now " + format + "}";
4664
if(value.contains(param))
4765
{
48-
value = value.replace(param, new SimpleDateFormat(format).format(new Date()));
66+
dateFormat.applyPattern(format);
67+
value = value.replace(param, dateFormat.format(nowDate));
4968
}
5069
}
5170

@@ -109,4 +128,26 @@ public void setData(Map<String, Object> data)
109128
{
110129
}
111130

131+
@Override
132+
public Set<String> allFormats()
133+
{
134+
return Collections.unmodifiableSet(formatSet);
135+
}
136+
137+
@Override
138+
public boolean addFormat(String format)
139+
{
140+
try
141+
{
142+
dateFormat.applyPattern(format);
143+
formatSet.add(format);
144+
return true;
145+
}
146+
catch(IllegalArgumentException | NullPointerException e)
147+
{
148+
}
149+
150+
return false;
151+
}
152+
112153
}

0 commit comments

Comments
 (0)