|
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. |
3 | 15 | */ |
| 16 | + |
4 | 17 | package org.suren.autotest.web.framework.data; |
5 | 18 |
|
6 | 19 | import java.text.SimpleDateFormat; |
7 | | -import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
8 | 21 | import java.util.Date; |
9 | | -import java.util.List; |
| 22 | +import java.util.HashSet; |
10 | 23 | import java.util.Map; |
| 24 | +import java.util.Set; |
11 | 25 |
|
12 | 26 | import org.springframework.stereotype.Component; |
13 | 27 | import org.suren.autotest.web.framework.util.CommonNumberUtil; |
|
21 | 35 | * @date 2017年1月4日 下午12:33:01 |
22 | 36 | */ |
23 | 37 | @Component |
24 | | -public class SimpleDynamicData implements DynamicData |
| 38 | +public class SimpleDynamicData implements DynamicData, DynamicDateFormat |
25 | 39 | { |
26 | | - private List<String> formatList = new ArrayList<String>(); |
27 | | - |
| 40 | + private Set<String> formatSet = new HashSet<String>(); |
| 41 | + |
| 42 | + private SimpleDateFormat dateFormat = new SimpleDateFormat(); |
28 | 43 | private String random = "${random-"; |
29 | 44 |
|
30 | 45 | public SimpleDynamicData() |
31 | 46 | { |
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"); |
34 | 51 | } |
35 | 52 |
|
36 | 53 | @Override |
37 | 54 | public String getValue(final String orginData) |
38 | 55 | { |
39 | 56 | String value = orginData; |
40 | 57 |
|
| 58 | + Date nowDate = new Date(); |
41 | 59 | value = value.replace("${now}", String.valueOf(System.currentTimeMillis())); |
42 | 60 |
|
43 | | - for(String format : formatList) |
| 61 | + for(String format : formatSet) |
44 | 62 | { |
45 | 63 | String param = "${now " + format + "}"; |
46 | 64 | if(value.contains(param)) |
47 | 65 | { |
48 | | - value = value.replace(param, new SimpleDateFormat(format).format(new Date())); |
| 66 | + dateFormat.applyPattern(format); |
| 67 | + value = value.replace(param, dateFormat.format(nowDate)); |
49 | 68 | } |
50 | 69 | } |
51 | 70 |
|
@@ -109,4 +128,26 @@ public void setData(Map<String, Object> data) |
109 | 128 | { |
110 | 129 | } |
111 | 130 |
|
| 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 | + |
112 | 153 | } |
0 commit comments