001: //$Id: DDTConfiguration.java 282 2007-07-19 22:46:27Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.util;
038:
039: import java.io.File;
040: import java.io.IOException;
041: import java.util.HashMap;
042: import java.util.Locale;
043: import java.util.Map;
044: import java.util.Properties;
045: import java.util.Map.Entry;
046:
047: import junitx.ddtunit.DDTSetUpException;
048:
049: import org.apache.log4j.Logger;
050:
051: /**
052: * Load configuration file and provide access to specified content in an
053: * instance of this class
054: *
055: * @author jg
056: */
057: public class DDTConfiguration {
058: public static final String DDTUNIT_CONFIG_PROPERTIES = "/junitx/ddtunit/ddtunitConfig.properties";
059:
060: private static DDTConfiguration singleton;
061:
062: private static long propsModifyTime;
063:
064: private Properties props;
065:
066: private String log4jConfigResource;
067:
068: private boolean activeRunMonitor;
069:
070: private boolean activeXmlValidation;
071:
072: private boolean activeParserValidation;
073:
074: private boolean activeAsserts;
075:
076: private boolean specificationAssert;
077:
078: private Locale activeLocale;
079:
080: private Map<String, DDTDateFormat> dateMap;
081:
082: /**
083: * Number of cluster data that could not be deleted by jvm garbage
084: * collection via SoftReference implementation.
085: */
086: private int hardCacheSize = 25;
087:
088: private final static Logger log = Logger
089: .getLogger(DDTConfiguration.class);
090:
091: private static final String PROP_ACTIVE_RUN_MONITOR = "activeRunMonitor";
092:
093: private static final String PROP_LOG4J_CONFIG_RESOURCE = "log4jConfigResource";
094:
095: private static final String PROP_ACTIVE_XML_VALIDATION = "activeXmlValidation";
096:
097: private static final String PROP_ACTIVE_PARSER_VALIDATION = "activeParserValidation";
098:
099: private static final String PROP_ACTIVE_LOCALE = "activeLocale";
100:
101: private static final String PROP_ACTIVE_ASSERTS = "activeAsserts";
102:
103: private static final String PROP_DATE_PREFIX = "date.";
104:
105: public static final String PROP_RESOURCE = DDTUNIT_CONFIG_PROPERTIES;
106:
107: /**
108: * Default constructor
109: */
110: private DDTConfiguration() {
111: initProps();
112: }
113:
114: private void initProps() {
115: this .log4jConfigResource = "/junitx/ddtunit/log4j.properties";
116: this .activeRunMonitor = true;
117: this .activeXmlValidation = true;
118: this .activeParserValidation = false;
119: this .activeAsserts = true;
120: this .specificationAssert = false;
121: this .activeLocale = Locale.getDefault();
122: this .dateMap = new HashMap<String, DDTDateFormat>();
123: }
124:
125: /**
126: * @return only one instance of config information
127: */
128: public static DDTConfiguration getInstance() {
129: if (singleton == null) {
130: singleton = new DDTConfiguration();
131: }
132: return singleton;
133: }
134:
135: public void load() {
136: load(DDTUNIT_CONFIG_PROPERTIES);
137: }
138:
139: public void load(String resourceName) {
140: log.debug("load(" + resourceName + ") - START");
141: File propsFile = new File(this .getClass().getResource(
142: resourceName).getPath());
143: if (propsFile.lastModified() > propsModifyTime) {
144:
145: propsModifyTime = propsFile.lastModified();
146: initProps();
147: this .props = new Properties();
148: try {
149: this .props.load(this .getClass().getResourceAsStream(
150: resourceName));
151: } catch (IOException ex) {
152: throw new DDTSetUpException(
153: "Error on initialization of properties", ex);
154: }
155: // check for LOCALE defails before processing date formats
156: if (this .props.containsKey(PROP_ACTIVE_LOCALE)) {
157: String locale = null;
158: try {
159: locale = (String) this .props
160: .get(PROP_ACTIVE_LOCALE);
161: String[] details = locale.split("_");
162: if (details != null && details.length == 2) {
163: this .setActiveLocale(new Locale(details[0],
164: details[1]));
165: } else {
166: this .setActiveLocale(Locale.getDefault());
167: }
168: Locale.setDefault(this .getActiveLocale());
169: } catch (Exception ex) {
170: log.error("Error on initialization of LOCALE("
171: + locale + "). Reset to default.");
172: }
173: } else {
174: this .setActiveLocale(Locale.getDefault());
175: }
176: for (Entry entry : this .props.entrySet()) {
177: if (PROP_ACTIVE_RUN_MONITOR.equals(entry.getKey())) {
178: this .setActiveRunMonitor(Boolean
179: .parseBoolean((String) entry.getValue()));
180: } else if (PROP_LOG4J_CONFIG_RESOURCE.equals(entry
181: .getKey())) {
182: this .setLog4jConfigResource((String) entry
183: .getValue());
184: } else if (PROP_ACTIVE_XML_VALIDATION.equals(entry
185: .getKey())) {
186: this .setActiveXmlValidation(Boolean
187: .parseBoolean((String) entry.getValue()));
188: } else if (PROP_ACTIVE_PARSER_VALIDATION.equals(entry
189: .getKey())) {
190: this .setActiveParserValidation(Boolean
191: .parseBoolean((String) entry.getValue()));
192: } else if (PROP_ACTIVE_ASSERTS.equals(entry.getKey())) {
193: this .setActiveAsserts(Boolean
194: .parseBoolean((String) entry.getValue()));
195: } else if (((String) entry.getKey())
196: .startsWith(PROP_DATE_PREFIX)) {
197: String key = ((String) entry.getKey())
198: .substring(PROP_DATE_PREFIX.length());
199: this .dateMap.put(key, new DDTDateFormat(
200: (String) entry.getValue(), this
201: .getActiveLocale()));
202: }
203: }
204: }
205: }
206:
207: public boolean isActiveRunMonitor() {
208:
209: return activeRunMonitor;
210: }
211:
212: public String getLog4jConfigResource() {
213: return log4jConfigResource;
214: }
215:
216: public boolean isActiveXmlValidation() {
217: return activeXmlValidation;
218: }
219:
220: public int getHardCacheSize() {
221: return this .hardCacheSize;
222: }
223:
224: public boolean isActiveParserValidation() {
225: return this .activeParserValidation;
226: }
227:
228: public void setActiveParserValidation(
229: boolean parserValidationSupport) {
230: this .activeParserValidation = parserValidationSupport;
231: }
232:
233: public void setActiveXmlValidation(boolean activeXmlValidation) {
234: this .activeXmlValidation = activeXmlValidation;
235: }
236:
237: public boolean isActiveAsserts() {
238: return this .activeAsserts;
239: }
240:
241: public void setActiveAsserts(boolean assertSupport) {
242: this .activeAsserts = assertSupport;
243: }
244:
245: public boolean isSpecificationAssert() {
246: return this .specificationAssert;
247: }
248:
249: public void setSpecificationAssert(boolean specificationAssert) {
250: this .specificationAssert = specificationAssert;
251: }
252:
253: public Locale getActiveLocale() {
254: return activeLocale;
255: }
256:
257: public void setActiveRunMonitor(boolean activeRunMonitor) {
258: this .activeRunMonitor = activeRunMonitor;
259: }
260:
261: public void setLog4jConfigResource(String log4jConfigResource) {
262: this .log4jConfigResource = log4jConfigResource;
263: }
264:
265: public void setActiveLocale(Locale activeLocale) {
266: this .activeLocale = activeLocale;
267: }
268:
269: public Map<String, DDTDateFormat> getDateMap() {
270: return dateMap;
271: }
272: }
|