001: //$Id: DataSet.java 256 2006-10-23 21:56:10Z 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.data;
038:
039: import java.util.Collection;
040: import java.util.HashMap;
041: import java.util.Iterator;
042: import java.util.Map;
043:
044: /**
045: * Class containing all data objects used for executing a testclass. <br/ > The
046: * structure is a direct mapping of the xml resource structure of <br/ >
047: * <code>class - method - testcase</code>
048: *
049: * @author jg
050: */
051: abstract public class DataSet implements IDataSet {
052:
053: /**
054: * id to identify the dataset in a cache or repository
055: */
056: private String dataSetId;
057:
058: protected TypedObjectMap objectMap;
059:
060: private Map subDataMap;
061:
062: private IDataSet parentSet;
063:
064: /**
065: * @param setId specifies id for identification of dataset
066: */
067: public DataSet(String setId, IDataSet parent) {
068: this .dataSetId = setId;
069: this .objectMap = new TypedObjectMap();
070: this .subDataMap = new HashMap();
071: this .parentSet = parent;
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see junitx.ddtunit.data.IDataSet#putObject(java.lang.String,
078: * junitx.ddtunit.data.TypedObject)
079: */
080: public void putObject(String id, TypedObject entry) {
081: this .objectMap.put(id, entry);
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see junitx.ddtunit.data.IDataSet#getObject(java.lang.String,
088: * java.lang.String)
089: */
090: public TypedObject getObject(String id, String type) {
091: return this .objectMap.get(id, type);
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see junitx.ddtunit.data.IDataSet#getObject(java.lang.String)
098: */
099: public TypedObject getObject(String id) {
100: return this .objectMap.get(id);
101: }
102:
103: public TypedObject findObject(String id) {
104: TypedObject local = getObject(id);
105: if (local == null && parentSet != null) {
106: local = parentSet.findObject(id);
107: }
108: return local;
109: }
110:
111: public TypedObject findObject(String id, String type) {
112: if (type == null || TypedObject.UNKNOWN_TYPE.equals(type)) {
113: return findObject(id);
114: }
115: TypedObject local = getObject(id, type);
116: if (local == null && parentSet != null) {
117: local = parentSet.findObject(id, type);
118: }
119: return local;
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see junitx.ddtunit.data.IDataSet#put(java.lang.String,
126: * junitx.ddtunit.data.IDataSet)
127: */
128: public void put(String id, IDataSet dataSet) {
129: this .subDataMap.put(id, dataSet);
130: }
131:
132: /*
133: * (non-Javadoc)
134: *
135: * @see junitx.ddtunit.data.IDataSet#get(java.lang.String)
136: */
137: public IDataSet get(String id) {
138: IDataSet dataSet = null;
139: if (containsKey(id)) {
140: dataSet = (IDataSet) this .subDataMap.get(id);
141: }
142: return dataSet;
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see junitx.ddtunit.data.IDataSet#containsKey(java.lang.String)
149: */
150: public boolean containsKey(String key) {
151: return this .subDataMap.containsKey(key);
152: }
153:
154: /*
155: * (non-Javadoc)
156: *
157: * @see junitx.ddtunit.data.IDataSet#getSubDataKeyIterator()
158: */
159: public Iterator getSubDataIterator() {
160: return this .subDataMap.entrySet().iterator();
161: }
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see junitx.ddtunit.data.IDataSet#getSubDataValues()
167: */
168: public Collection getSubDataValues() {
169: return this .subDataMap.values();
170: }
171:
172: /*
173: * (non-Javadoc)
174: *
175: * @see junitx.ddtunit.data.IDataSet#size()
176: */
177: public int size() {
178: return this .subDataMap.size();
179: }
180:
181: /**
182: * Return human readable description of object.
183: *
184: * @return Desciption of object.
185: */
186: public String toString() {
187: StringBuffer info = new StringBuffer("DataSet(").append(
188: this .dataSetId).append("-type=").append(
189: this .getClass().getName());
190:
191: return info.toString();
192: }
193:
194: /*
195: * (non-Javadoc)
196: *
197: * @see junitx.ddtunit.data.IDataSet#getId()
198: */
199: public String getId() {
200: return dataSetId;
201: }
202:
203: }
|