001: /********************************************************************************
002: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
003: * Copyright (c) 2004, Joerg and Kai Gellien
004: * All rights reserved.
005: *
006: * The Software is provided under the terms of the Common Public License 1.0
007: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of the authors or DDTUnit, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package junitx.ddtunit.data;
037:
038: import java.util.Collection;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042:
043: import org.apache.log4j.Logger;
044:
045: /**
046: * @author jg
047: */
048: public class TestClusterDataSet extends DataSet {
049: private Logger log = Logger.getLogger(TestClusterDataSet.class);
050:
051: /**
052: * @param setId
053: * under which dataset can be retrieved
054: */
055: public TestClusterDataSet(String setId, IDataSet parent) {
056: super (setId, parent);
057: }
058:
059: /**
060: * @return number of tests in all groups of this cluster
061: */
062: public int size() {
063: int count = 0;
064: for (Iterator iter = getSubDataIterator(); iter.hasNext();) {
065: count += ((TestGroupDataSet) ((Map.Entry) iter.next())
066: .getValue()).size();
067: }
068: return count;
069: }
070:
071: /**
072: * @return number of testgroup containing testdata
073: */
074: public int size(String group) {
075: int count = 0;
076: if (containsKey(group)) {
077: count = ((TestGroupDataSet) get(group)).size();
078: }
079: return count;
080: }
081:
082: /**
083: * Check if test exists for specified group
084: *
085: * @param groupId
086: * name to check
087: * @param testId
088: * name to check
089: * @return true if test data is found
090: */
091: public boolean containsTest(String groupId, String testId) {
092: boolean check = false;
093: if (containsKey(groupId)
094: && ((TestGroupDataSet) get(groupId))
095: .containsKey(testId)) {
096: check = true;
097: }
098: return check;
099: }
100:
101: /**
102: * Retrieve DataSet of specified group and test if exists. <br/>Otherwise
103: * null is returned.
104: *
105: * @param groupId
106: * of DataSet to select
107: * @param testId
108: * of test under group of DataSet to select
109: *
110: * @return DataSet of specified groupId/testId or null if no DataSet was
111: * found.
112: */
113: public TestDataSet getTestDataSet(String groupId, String testId) {
114: if (groupId == null || testId == null) {
115: throw new IllegalArgumentException(
116: "Xml Ids of <group/> and <test/> must be not null.");
117: }
118: log.debug("getTestDataSet(" + groupId + ", " + testId
119: + ") - START");
120: TestDataSet dataSet = null;
121: if ((groupId != null) && containsKey(groupId)) {
122: TestGroupDataSet groupDataSet = (TestGroupDataSet) this
123: .get(groupId);
124:
125: if (testId != null && groupDataSet.containsKey(testId)) {
126: dataSet = (TestDataSet) groupDataSet.get(testId);
127: }
128: }
129:
130: log.debug("getTestDataSet(" + groupId + ", " + testId
131: + ") - END");
132:
133: return dataSet;
134: }
135:
136: /**
137: * Get iterator of all tests contained in specified test group
138: *
139: * @param groupName
140: * of test group to lookup
141: * @return Iterator or null if no entry is found
142: */
143: public Iterator getTestEntries(String groupName) {
144: Iterator iter = null;
145: if (containsKey(groupName)) {
146: iter = ((TestGroupDataSet) get(groupName))
147: .getSubDataIterator();
148: }
149: return iter;
150: }
151:
152: /**
153: * Get iterator on collection of all tests contained in specified test group
154: *
155: * @param groupId
156: * of test group to lookup
157: * @return Iterator of TestDatSets identified by groupId
158: */
159: public Iterator getTestDataSets(String groupId) {
160: Iterator dataIterator = null;
161: if (containsKey(groupId)) {
162: Collection collection = null;
163: collection = ((TestGroupDataSet) get(groupId))
164: .getSubDataValues();
165: dataIterator = collection.iterator();
166: }
167: return dataIterator;
168: }
169:
170: /**
171: * Retrieve object of specidied testdata under testgroup
172: *
173: * @param groupId
174: * to search
175: * @param testId
176: * to search
177: * @param objectId
178: * to retrieve
179: * @return TypedObject of requested object
180: */
181: public TypedObject getObject(String groupId, String testId,
182: String objectId) {
183: TypedObject typedObject = this .getTestDataSet(groupId, testId)
184: .findObject(objectId);
185: return typedObject;
186: }
187:
188: /**
189: * Retrieve object of requested testdata under testgroup
190: *
191: * @param groupId
192: * to search
193: * @param testId
194: * to search
195: * @param objectId
196: * to retrieve
197: * @param objectType
198: * of object to retrieve
199: * @return TypedObject requested
200: */
201: public TypedObject getObject(String groupId, String testId,
202: String objectId, String objectType) {
203: TypedObject typedObject = this .getTestDataSet(groupId, testId)
204: .findObject(objectId, objectType);
205: return typedObject;
206: }
207:
208: /**
209: * Retrieve assert object of requested group and test
210: *
211: * @param groupId
212: * to search
213: * @param testId
214: * to search
215: * @param assertId
216: * to retrieve
217: * @return AssertObject
218: */
219: public AssertObject getAssert(String groupId, String testId,
220: String assertId) {
221: AssertObject assertObject = this
222: .getTestDataSet(groupId, testId).getAssert(assertId);
223: return assertObject;
224: }
225:
226: /**
227: * Retrieve assert object of requested group and test
228: *
229: * @param groupId
230: * to search
231: * @param testId
232: * to search
233: * @param assertId
234: * to search
235: * @param assertType
236: * to search
237: * @return AssertObject
238: */
239: public AssertObject getAssert(String groupId, String testId,
240: String assertId, String assertType) {
241: AssertObject assertObject = this
242: .getTestDataSet(groupId, testId).getAssert(assertId,
243: assertType);
244: return assertObject;
245: }
246:
247: public TypedObjectMap getAssertMap(String groupId, String testId) {
248: TypedObjectMap assertMap = this .getTestDataSet(groupId, testId)
249: .getAssertMap();
250: return (TypedObjectMap) assertMap;
251: }
252:
253: public List<String> getOrderedTestKeys(String groupName) {
254: return ((TestGroupDataSet) get(groupName)).getOrderedSubKeys();
255: }
256: }
|