View Javadoc

1   //$Id: DataSet.java 256 2006-10-23 21:56:10Z 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.data;
39  
40  import java.util.Collection;
41  import java.util.HashMap;
42  import java.util.Iterator;
43  import java.util.Map;
44  
45  /**
46   * Class containing all data objects used for executing a testclass. <br/ > The
47   * structure is a direct mapping of the xml resource structure of <br/ >
48   * <code>class - method - testcase</code>
49   * 
50   * @author jg
51   */
52  abstract public class DataSet implements IDataSet {
53  
54      /**
55       * id to identify the dataset in a cache or repository
56       */
57      private String dataSetId;
58  
59      protected TypedObjectMap objectMap;
60  
61      private Map subDataMap;
62  
63      private IDataSet parentSet;
64  
65      /**
66       * @param setId specifies id for identification of dataset
67       */
68      public DataSet(String setId, IDataSet parent) {
69          this.dataSetId = setId;
70          this.objectMap = new TypedObjectMap();
71          this.subDataMap = new HashMap();
72          this.parentSet = parent;
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see junitx.ddtunit.data.IDataSet#putObject(java.lang.String,
79       *      junitx.ddtunit.data.TypedObject)
80       */
81      public void putObject(String id, TypedObject entry) {
82          this.objectMap.put(id, entry);
83      }
84  
85      /*
86       * (non-Javadoc)
87       * 
88       * @see junitx.ddtunit.data.IDataSet#getObject(java.lang.String,
89       *      java.lang.String)
90       */
91      public TypedObject getObject(String id, String type) {
92          return this.objectMap.get(id, type);
93      }
94  
95      /*
96       * (non-Javadoc)
97       * 
98       * @see junitx.ddtunit.data.IDataSet#getObject(java.lang.String)
99       */
100     public TypedObject getObject(String id) {
101         return this.objectMap.get(id);
102     }
103 
104     public TypedObject findObject(String id) {
105         TypedObject local = getObject(id);
106         if (local == null && parentSet != null) {
107             local = parentSet.findObject(id);
108         }
109         return local;
110     }
111 
112     public TypedObject findObject(String id, String type) {
113         if (type == null || TypedObject.UNKNOWN_TYPE.equals(type)) {
114             return findObject(id);
115         }
116         TypedObject local = getObject(id, type);
117         if (local == null && parentSet != null) {
118             local = parentSet.findObject(id, type);
119         }
120         return local;
121     }
122 
123     /*
124      * (non-Javadoc)
125      * 
126      * @see junitx.ddtunit.data.IDataSet#put(java.lang.String,
127      *      junitx.ddtunit.data.IDataSet)
128      */
129     public void put(String id, IDataSet dataSet) {
130         this.subDataMap.put(id, dataSet);
131     }
132 
133     /*
134      * (non-Javadoc)
135      * 
136      * @see junitx.ddtunit.data.IDataSet#get(java.lang.String)
137      */
138     public IDataSet get(String id) {
139         IDataSet dataSet = null;
140         if (containsKey(id)) {
141             dataSet = (IDataSet) this.subDataMap.get(id);
142         }
143         return dataSet;
144     }
145 
146     /*
147      * (non-Javadoc)
148      * 
149      * @see junitx.ddtunit.data.IDataSet#containsKey(java.lang.String)
150      */
151     public boolean containsKey(String key) {
152         return this.subDataMap.containsKey(key);
153     }
154 
155     /*
156      * (non-Javadoc)
157      * 
158      * @see junitx.ddtunit.data.IDataSet#getSubDataKeyIterator()
159      */
160     public Iterator getSubDataIterator() {
161         return this.subDataMap.entrySet().iterator();
162     }
163 
164     /*
165      * (non-Javadoc)
166      * 
167      * @see junitx.ddtunit.data.IDataSet#getSubDataValues()
168      */
169     public Collection getSubDataValues() {
170         return this.subDataMap.values();
171     }
172 
173     /*
174      * (non-Javadoc)
175      * 
176      * @see junitx.ddtunit.data.IDataSet#size()
177      */
178     public int size() {
179         return this.subDataMap.size();
180     }
181 
182     /**
183      * Return human readable description of object.
184      * 
185      * @return Desciption of object.
186      */
187     public String toString() {
188         StringBuffer info = new StringBuffer("DataSet(").append(this.dataSetId)
189             .append("-type=").append(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 }