001: /*
002: * @(#)TestContext.java 1.7 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package gunit.framework;
028:
029: import java.util.*;
030: import java.io.*;
031:
032: /**
033: * <code>TestContext</code> provides the context for the test. An
034: * instance is got from the factory method
035: * <code>TestContext.getInstance()</code>
036: */
037: public class TestContext {
038: /**
039: * Value of this property is a <code>Class</code> object that
040: * specifies test container class
041: */
042: public static final String TEST_CONTAINER_CLASS = "gunit.test.container.class";
043:
044: /**
045: * Value of this property is a <code>Class</code> object that
046: * specifies result printer class, which should be a subclass
047: * of <code>junit.textui.ResultPrinter</code>
048: */
049: public static final String RESULT_PRINTER_CLASS = "gunit.result.printer.class";
050:
051: /**
052: * Value of this property is a <code>Class</code> object that
053: * specifies result container class
054: */
055: public static final String TEST_RESULT_VERIFIER_CLASS = "gunit.test.result.verifier.class";
056:
057: /**
058: * Value of this property is a <code>String</code> object that
059: * specifies a file that contains the arguments for the testcases
060: */
061: public static final String TEST_ARGS_FILENAME = "gunit.test.args.filename";
062:
063: /**
064: * Value of this property is a <code>String</code> object that
065: * specifies the directories (seperated using :)
066: * where the reference images are kept
067: */
068: public static final String REF_IMAGE_PATH = "gunit.refimage.path";
069:
070: /**
071: * Value of this property is a <code>TestFilter</code> object.
072: */
073: public static final String TEST_FILTER = "gunit.test.filter.object";
074:
075: private static TestContext instance = null;
076:
077: public synchronized static TestContext getInstance() {
078: if (instance == null) {
079: instance = new TestContext();
080: }
081: return instance;
082: }
083:
084: Map map;
085: Map modifiedMap;
086:
087: private TestContext() {
088: // load the defaults from the stream
089: init();
090: }
091:
092: private void init() {
093: InputStream stream = getClass().getResourceAsStream(
094: "defaults.properties");
095: if (stream == null) {
096: return;
097: }
098:
099: Properties prop = new Properties();
100: try {
101: prop.load(stream);
102: } catch (Exception ex) {
103: }
104:
105: this .map = new HashMap();
106: this .modifiedMap = new HashMap();
107: this .map.putAll(prop);
108: }
109:
110: public String getStringValue(String name) {
111: //System.out.println("get("+name+")="+this.map.get(name));
112: return (String) this .map.get(name);
113: }
114:
115: public Class getClassValue(String name) {
116: String value = getStringValue(name);
117: if (value == null)
118: return null;
119: try {
120: return Class.forName(value);
121: } catch (Exception ex) {
122: return null;
123: }
124: }
125:
126: public int getIntValue(String name) {
127: String value = getStringValue(name);
128: if (value == null)
129: return 0;
130: try {
131: return Integer.parseInt(value);
132: } catch (Exception ex) {
133: return 0;
134: }
135: }
136:
137: public boolean getBooleanValue(String name) {
138: String value = getStringValue(name);
139: if (value == null)
140: return false;
141: try {
142: return Boolean.valueOf(value).booleanValue();
143: } catch (Exception ex) {
144: return false;
145: }
146: }
147:
148: public Object getObjectValue(String name) {
149: Class value = getClassValue(name);
150: if (value == null)
151: return null;
152: try {
153: return value.newInstance();
154: } catch (Exception ex) {
155: return null;
156: }
157: }
158:
159: /**
160: * Returns the object assigned to the <i>name</i> without doing any
161: * processing as the other <code>getXXXValue()</code> methods
162: */
163: public Object getValue(String name) {
164: Object value = this .map.get(name);
165: return value;
166: }
167:
168: public synchronized void setValue(String name, Object value) {
169: if (this .modifiedMap.get(name) == null) {
170: this .modifiedMap.put(name, Boolean.TRUE);
171: this .map.put(name, value);
172: }
173: }
174:
175: private void dumpMap(String mesg, Map m) {
176: Iterator iter = m.keySet().iterator();
177: System.out.println(mesg);
178: while (iter.hasNext()) {
179: Object key = iter.next();
180: System.out.println(key + "=" + m.get(key));
181: }
182: }
183: }
|