View Javadoc

1   //$Id: DDTConfiguration.java 358 2009-08-05 13:58:44Z jg_hamburg $
2   /********************************************************************************
3    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4    * Copyright (c) 2004, Joerg and Kai Gellien
5    * All rights reserved.
6    *
7    * The Software is provided under the terms of the Common Public License 1.0
8    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   *     + Redistributions of source code must retain the above copyright
14   *       notice, this list of conditions and the following disclaimer.
15   *
16   *     + Redistributions in binary form must reproduce the above
17   *       copyright notice, this list of conditions and the following
18   *       disclaimer in the documentation and/or other materials provided
19   *       with the distribution.
20   *
21   *     + Neither the name of the authors or DDTUnit, nor the
22   *       names of its contributors may be used to endorse or promote
23   *       products derived from this software without specific prior
24   *       written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Load configuration file and provide access to specified content in an
55   * instance of this class
56   * 
57   * @author jg
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  	 * Number of cluster data that could not be deleted by jvm garbage
86  	 * collection via SoftReference implementation.
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 	 * Default constructor
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 	 * @return only one instance of config information
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 			// check for LOCALE defails before processing date formats
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 }