1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package junitx.ddtunit.util;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.Locale;
44 import java.util.Map;
45 import java.util.Properties;
46 import java.util.Map.Entry;
47
48 import junitx.ddtunit.DDTSetUpException;
49
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55
56
57
58
59 public class DDTConfiguration {
60 public static final String DDTUNIT_CONFIG_PROPERTIES = "/junitx/ddtunit/ddtunitConfig.properties";
61
62 private static DDTConfiguration singleton;
63
64 private static long propsModifyTime;
65
66 private Properties props;
67
68 private String log4jConfigResource;
69
70 private boolean activeRunMonitor;
71
72 private boolean activeXmlValidation;
73
74 private boolean activeParserValidation;
75
76 private boolean activeAsserts;
77
78 private boolean specificationAssert;
79
80 private Locale activeLocale;
81
82 private Map<String, DDTDateFormat> dateMap;
83
84
85
86
87
88 private int hardCacheSize = 25;
89
90 private final static Logger log = LoggerFactory.getLogger(DDTConfiguration.class);
91
92 private static final String PROP_ACTIVE_RUN_MONITOR = "activeRunMonitor";
93
94 private static final String PROP_LOG4J_CONFIG_RESOURCE = "log4jConfigResource";
95
96 private static final String PROP_ACTIVE_XML_VALIDATION = "activeXmlValidation";
97
98 private static final String PROP_ACTIVE_PARSER_VALIDATION = "activeParserValidation";
99
100 private static final String PROP_ACTIVE_LOCALE = "activeLocale";
101
102 private static final String PROP_ACTIVE_ASSERTS = "activeAsserts";
103
104 private static final String PROP_DATE_PREFIX = "date.";
105
106 public static final String PROP_RESOURCE = DDTUNIT_CONFIG_PROPERTIES;
107
108
109
110
111 private DDTConfiguration() {
112 initProps();
113 }
114
115 private void initProps() {
116 this.log4jConfigResource = "/junitx.ddtunit.log4j.properties";
117 this.activeRunMonitor = true;
118 this.activeXmlValidation = true;
119 this.activeParserValidation = false;
120 this.activeAsserts = true;
121 this.specificationAssert = false;
122 this.activeLocale = Locale.getDefault();
123 this.dateMap = new HashMap<String, DDTDateFormat>();
124 }
125
126
127
128
129 public static DDTConfiguration getInstance() {
130 if (singleton == null) {
131 singleton = new DDTConfiguration();
132 }
133 return singleton;
134 }
135
136 public void load() {
137 load(DDTUNIT_CONFIG_PROPERTIES);
138 }
139
140 public void load(String resourceName) {
141 log.debug("load(" + resourceName + ") - START");
142 File propsFile = new File(this.getClass().getResource(resourceName)
143 .getPath());
144 if (propsFile.lastModified() > propsModifyTime) {
145
146 propsModifyTime = propsFile.lastModified();
147 initProps();
148 this.props = new Properties();
149 try {
150 this.props.load(this.getClass().getResourceAsStream(
151 resourceName));
152 } catch (IOException ex) {
153 throw new DDTSetUpException(
154 "Error on initialization of properties", ex);
155 }
156
157 if (this.props.containsKey(PROP_ACTIVE_LOCALE)) {
158 String locale = null;
159 try {
160 locale = (String) this.props.get(PROP_ACTIVE_LOCALE);
161 String[] details = locale.split("_");
162 if (details != null && details.length == 2) {
163 this
164 .setActiveLocale(new Locale(details[0],
165 details[1]));
166 } else {
167 this.setActiveLocale(Locale.getDefault());
168 }
169 Locale.setDefault(this.getActiveLocale());
170 } catch (Exception ex) {
171 log.error("Error on initialization of LOCALE(" + locale
172 + "). Reset to default.");
173 }
174 } else {
175 this.setActiveLocale(Locale.getDefault());
176 }
177 for (Entry entry : this.props.entrySet()) {
178 if (PROP_ACTIVE_RUN_MONITOR.equals(entry.getKey())) {
179 this.setActiveRunMonitor(Boolean
180 .parseBoolean((String) entry.getValue()));
181 } else if (PROP_LOG4J_CONFIG_RESOURCE.equals(entry.getKey())) {
182 this.setLog4jConfigResource((String) entry.getValue());
183 } else if (PROP_ACTIVE_XML_VALIDATION.equals(entry.getKey())) {
184 this.setActiveXmlValidation(Boolean
185 .parseBoolean((String) entry.getValue()));
186 } else if (PROP_ACTIVE_PARSER_VALIDATION.equals(entry.getKey())) {
187 this.setActiveParserValidation(Boolean
188 .parseBoolean((String) entry.getValue()));
189 } else if (PROP_ACTIVE_ASSERTS.equals(entry.getKey())) {
190 this.setActiveAsserts(Boolean.parseBoolean((String) entry
191 .getValue()));
192 } else if (((String) entry.getKey())
193 .startsWith(PROP_DATE_PREFIX)) {
194 String key = ((String) entry.getKey())
195 .substring(PROP_DATE_PREFIX.length());
196 this.dateMap.put(key, new DDTDateFormat((String) entry
197 .getValue(), this.getActiveLocale()));
198 }
199 }
200 }
201 }
202
203 public boolean isActiveRunMonitor() {
204
205 return activeRunMonitor;
206 }
207
208 public String getLog4jConfigResource() {
209 return log4jConfigResource;
210 }
211
212 public boolean isActiveXmlValidation() {
213 return activeXmlValidation;
214 }
215
216 public int getHardCacheSize() {
217 return this.hardCacheSize;
218 }
219
220 public boolean isActiveParserValidation() {
221 return this.activeParserValidation;
222 }
223
224 public void setActiveParserValidation(boolean parserValidationSupport) {
225 this.activeParserValidation = parserValidationSupport;
226 }
227
228 public void setActiveXmlValidation(boolean activeXmlValidation) {
229 this.activeXmlValidation = activeXmlValidation;
230 }
231
232 public boolean isActiveAsserts() {
233 return this.activeAsserts;
234 }
235
236 public void setActiveAsserts(boolean assertSupport) {
237 this.activeAsserts = assertSupport;
238 }
239
240 public boolean isSpecificationAssert() {
241 return this.specificationAssert;
242 }
243
244 public void setSpecificationAssert(boolean specificationAssert) {
245 this.specificationAssert = specificationAssert;
246 }
247
248 public Locale getActiveLocale() {
249 return activeLocale;
250 }
251
252 public void setActiveRunMonitor(boolean activeRunMonitor) {
253 this.activeRunMonitor = activeRunMonitor;
254 }
255
256 public void setLog4jConfigResource(String log4jConfigResource) {
257 this.log4jConfigResource = log4jConfigResource;
258 }
259
260 public void setActiveLocale(Locale activeLocale) {
261 this.activeLocale = activeLocale;
262 }
263
264 public Map<String, DDTDateFormat> getDateMap() {
265 return dateMap;
266 }
267 }