View Javadoc

1   /********************************************************************************
2    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
3    * Copyright (c) 2004, Joerg and Kai Gellien
4    * All rights reserved.
5    * 
6    * The Software is provided under the terms of the Common Public License 1.0
7    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
8    * Redistribution and use in source and binary forms, with or without 
9    * modification, are permitted provided that the following conditions
10   * are met:
11   * 
12   *     + Redistributions of source code must retain the above copyright 
13   *       notice, this list of conditions and the following disclaimer. 
14   *       
15   *     + Redistributions in binary form must reproduce the above 
16   *       copyright notice, this list of conditions and the following 
17   *       disclaimer in the documentation and/or other materials provided 
18   *       with the distribution. 
19   *       
20   *     + Neither the name of the authors or DDTUnit, nor the 
21   *       names of its contributors may be used to endorse or promote 
22   *       products derived from this software without specific prior 
23   *       written permission. 
24   * 
25   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
26   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
27   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
28   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 
29   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
30   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
31   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
32   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
33   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
34   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
35   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36   ********************************************************************************/
37  package junitx.ddtunit.data;
38  
39  import java.util.Collection;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Map;
43  
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  /**
48   * @author jg
49   */
50  public class TestClusterDataSet extends DataSet {
51  	private Logger log = LoggerFactory.getLogger(TestClusterDataSet.class);
52  
53  	/**
54  	 * @param setId
55  	 *            under which dataset can be retrieved
56  	 */
57  	public TestClusterDataSet(String setId, IDataSet parent) {
58  		super(setId, parent);
59  	}
60  
61  	/**
62  	 * @return number of tests in all groups of this cluster
63  	 */
64  	public int size() {
65  		int count = 0;
66  		for (Iterator iter = getSubDataIterator(); iter.hasNext();) {
67  			count += ((TestGroupDataSet) ((Map.Entry) iter.next()).getValue())
68  					.size();
69  		}
70  		return count;
71  	}
72  
73  	/**
74  	 * @return number of testgroup containing testdata
75  	 */
76  	public int size(String group) {
77  		int count = 0;
78  		if (containsKey(group)) {
79  			count = ((TestGroupDataSet) get(group)).size();
80  		}
81  		return count;
82  	}
83  
84  	/**
85  	 * Check if test exists for specified group
86  	 * 
87  	 * @param groupId
88  	 *            name to check
89  	 * @param testId
90  	 *            name to check
91  	 * @return true if test data is found
92  	 */
93  	public boolean containsTest(String groupId, String testId) {
94  		boolean check = false;
95  		if (containsKey(groupId)
96  				&& ((TestGroupDataSet) get(groupId)).containsKey(testId)) {
97  			check = true;
98  		}
99  		return check;
100 	}
101 
102 	/**
103 	 * Retrieve DataSet of specified group and test if exists. <br/>Otherwise
104 	 * null is returned.
105 	 * 
106 	 * @param groupId
107 	 *            of DataSet to select
108 	 * @param testId
109 	 *            of test under group of DataSet to select
110 	 * 
111 	 * @return DataSet of specified groupId/testId or null if no DataSet was
112 	 *         found.
113 	 */
114 	public TestDataSet getTestDataSet(String groupId, String testId) {
115 		if (groupId == null || testId == null) {
116 			throw new IllegalArgumentException(
117 					"Xml Ids of <group/> and <test/> must be not null.");
118 		}
119 		log.debug("getTestDataSet(" + groupId + ", " + testId + ") - 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 + ") - END");
131 
132 		return dataSet;
133 	}
134 
135 	/**
136 	 * Get iterator of all tests contained in specified test group
137 	 * 
138 	 * @param groupName
139 	 *            of test group to lookup
140 	 * @return Iterator or null if no entry is found
141 	 */
142 	public Iterator getTestEntries(String groupName) {
143 		Iterator iter = null;
144 		if (containsKey(groupName)) {
145 			iter = ((TestGroupDataSet) get(groupName)).getSubDataIterator();
146 		}
147 		return iter;
148 	}
149 
150 	/**
151 	 * Get iterator on collection of all tests contained in specified test group
152 	 * 
153 	 * @param groupId
154 	 *            of test group to lookup
155 	 * @return Iterator of TestDatSets identified by groupId
156 	 */
157 	public Iterator getTestDataSets(String groupId) {
158 		Iterator dataIterator = null;
159 		if (containsKey(groupId)) {
160 			Collection collection = null;
161 			collection = ((TestGroupDataSet) get(groupId)).getSubDataValues();
162 			dataIterator = collection.iterator();
163 		}
164 		return dataIterator;
165 	}
166 
167 	/**
168 	 * Retrieve object of specidied testdata under testgroup
169 	 * 
170 	 * @param groupId
171 	 *            to search
172 	 * @param testId
173 	 *            to search
174 	 * @param objectId
175 	 *            to retrieve
176 	 * @return TypedObject of requested object
177 	 */
178 	public TypedObject getObject(String groupId, String testId, String objectId) {
179 		TypedObject typedObject = this.getTestDataSet(groupId, testId)
180 				.findObject(objectId);
181 		return typedObject;
182 	}
183 
184 	/**
185 	 * Retrieve object of requested testdata under testgroup
186 	 * 
187 	 * @param groupId
188 	 *            to search
189 	 * @param testId
190 	 *            to search
191 	 * @param objectId
192 	 *            to retrieve
193 	 * @param objectType
194 	 *            of object to retrieve
195 	 * @return TypedObject requested
196 	 */
197 	public TypedObject getObject(String groupId, String testId,
198 			String objectId, String objectType) {
199 		TypedObject typedObject = this.getTestDataSet(groupId, testId)
200 				.findObject(objectId, objectType);
201 		return typedObject;
202 	}
203 
204 	/**
205 	 * Retrieve assert object of requested group and test
206 	 * 
207 	 * @param groupId
208 	 *            to search
209 	 * @param testId
210 	 *            to search
211 	 * @param assertId
212 	 *            to retrieve
213 	 * @return AssertObject
214 	 */
215 	public AssertObject getAssert(String groupId, String testId, String assertId) {
216 		AssertObject assertObject = this.getTestDataSet(groupId, testId)
217 				.getAssert(assertId);
218 		return assertObject;
219 	}
220 
221 	/**
222 	 * Retrieve assert object of requested group and test
223 	 * 
224 	 * @param groupId
225 	 *            to search
226 	 * @param testId
227 	 *            to search
228 	 * @param assertId
229 	 *            to search
230 	 * @param assertType
231 	 *            to search
232 	 * @return AssertObject
233 	 */
234 	public AssertObject getAssert(String groupId, String testId,
235 			String assertId, String assertType) {
236 		AssertObject assertObject = this.getTestDataSet(groupId, testId)
237 				.getAssert(assertId, assertType);
238 		return assertObject;
239 	}
240 
241 	public TypedObjectMap getAssertMap(String groupId, String testId) {
242 		TypedObjectMap assertMap = this.getTestDataSet(groupId, testId)
243 				.getAssertMap();
244 		return (TypedObjectMap) assertMap;
245 	}
246 
247 	public List<String> getOrderedTestKeys(String groupName) {
248 		return ((TestGroupDataSet) get(groupName)).getOrderedSubKeys();
249 	}
250 }