001: package org.jtestcase.core.mapping;
002:
003: import java.io.FileNotFoundException;
004: import java.util.HashMap;
005: import java.util.Iterator;
006:
007: import org.jtestcase.JTestCaseException;
008: import org.jtestcase.core.digester.DigesterException;
009: import org.jtestcase.core.digester.JTestCaseDigester;
010: import org.jtestcase.core.model.AssertParamGroupInstance;
011: import org.jtestcase.core.model.AssertParamInstance;
012: import org.jtestcase.core.model.ParamGroupInstance;
013: import org.jtestcase.core.model.ParamInstance;
014: import org.jtestcase.core.type.ComplexTypeConverter;
015: import org.jtestcase.core.type.TypeConversionException;
016: import org.jtestcase.util.MultiKeyHashtable;
017:
018: public class HashMapper {
019:
020: /**
021: * The digester used for parsing the XML
022: */
023: private JTestCaseDigester digester;
024:
025: /**
026: * Type convert facility. This class is used to map "string" representation
027: * of type to contrete instances of the type represented
028: */
029: private ComplexTypeConverter typeConverter;
030:
031: public HashMapper(JTestCaseDigester digester,
032: ComplexTypeConverter typeConverter) {
033: this .digester = digester;
034: this .typeConverter = typeConverter;
035: }
036:
037: /**
038: * <p>
039: * Get all params for a given TestCaseInstance. Hashed key is param's name,
040: * value is value of param. This method is normally used with
041: * getNameOfTestCases().
042: * </p>
043: * <p>
044: * HashMap's key is param's name defined in
045: * /tests/class/method/params/param@name,
046: * </p>
047: * <p>
048: * HashMap's value is param's Object value with type as indicated in
049: * /test/class/method/params/param@type.
050: * </p>
051: *
052: * @return HashMap.
053: * @throws JTestCaseException
054: * if an internal error occurs
055: * @throws IllegalAccessException
056: * @throws InstantiationException
057: */
058: public HashMap getTestCaseParams(String className, String method,
059: String testcase) throws JTestCaseException,
060: InstantiationException, IllegalAccessException {
061: HashMap paramValues = new HashMap();
062: try {
063: Iterator paramInstances = digester.getParamsInstances(
064: className, method, testcase).iterator();
065: while (paramInstances.hasNext()) {
066: ParamInstance paramInstance = (ParamInstance) paramInstances
067: .next();
068: Object value = typeConverter
069: ._convertType(paramInstance);
070: paramValues.put(paramInstance.getName(), value);
071: }
072: Iterator paramGroupInstances = digester
073: .getParamGroupInstances(className, method, testcase)
074: .iterator();
075: while (paramGroupInstances.hasNext()) {
076: ParamGroupInstance paramGroupInstance = (ParamGroupInstance) paramGroupInstances
077: .next();
078: _getParamValuesFromGroupInstance(paramGroupInstance
079: .getName(), paramValues, paramGroupInstance);
080: }
081: } catch (TypeConversionException tce) {
082: throw new JTestCaseException(tce.getMessage());
083: } catch (DigesterException de) {
084: throw new JTestCaseException(
085: "Error retrieving params in xml file : \n"
086: + de.getMessage());
087: } catch (FileNotFoundException de) {
088: throw new JTestCaseException("Cannot read xml file : \n"
089: + de.getMessage());
090: }
091:
092: return paramValues;
093: }
094:
095: /**
096: * Extracts the param values from a ParamGroupInstance
097: *
098: * @param path
099: * the path so far
100: * @param flatParams
101: * the hashtable with the params
102: * @param paramGroupInstance
103: * the ParamGroupInstance object to be analysed
104: * @throws JTestCaseException
105: * if an internal error occurs
106: * @throws IllegalAccessException
107: * @throws InstantiationException
108: */
109: private void _getParamValuesFromGroupInstance(String path,
110: HashMap flatParams, ParamGroupInstance paramGroupInstance)
111: throws JTestCaseException, InstantiationException,
112: IllegalAccessException {
113: Iterator paramIter = paramGroupInstance.getParamInstances()
114: .iterator();
115: try {
116: while (paramIter.hasNext()) {
117: ParamInstance param = (ParamInstance) paramIter.next();
118: Object value = typeConverter._convertType(param);
119: flatParams.put(path + "/" + param.getName(), value);
120: }
121: Iterator paramGroupIter = paramGroupInstance
122: .getParamGroupInstances().iterator();
123: while (paramGroupIter.hasNext()) {
124: ParamGroupInstance paramGroup = (ParamGroupInstance) paramGroupIter
125: .next();
126: _getParamValuesFromGroupInstance(path + "/"
127: + paramGroup.getName(), flatParams, paramGroup);
128: }
129: } catch (TypeConversionException tce) {
130: throw new JTestCaseException(tce.getMessage());
131: }
132: }
133:
134: /**
135: * Get all assert params for a given method and its test case value into
136: * Hashtable. Hashed key is assert param's name, value is String value of
137: * assert param. This method is normally used with getNameOfTestCases().
138: *
139: * @param method -
140: * name of tested method. Defined in data file in
141: * /tests/class/method@test-case.
142: * @param testcase -
143: * name of test case. Defined in data file in
144: * /tests/class/method@test-case. Should be unique for the given
145: * "methodName".
146: * @return Hashtable. Key is assert param's name defined in
147: * /tests/class/method/asserts/assertparm@name, value is assert
148: * param's Object value with type as indecated in
149: * /test/class/method/asserts/assert@type. If not "type" specified
150: * in data file, then "String" is default type.
151: * @throws JTestCaseException
152: * if an internal error occurs
153: * @throws IllegalAccessException
154: * @throws InstantiationException
155: * @throws FileNotFoundException
156: */
157: public MultiKeyHashtable getTestCaseAssertParams(String className,
158: String method, String testcase) throws JTestCaseException,
159: InstantiationException, IllegalAccessException {
160: MultiKeyHashtable assertValues = new MultiKeyHashtable();
161: try {
162: Iterator assertInstances = digester
163: .getAssertsParamInstances(className, method,
164: testcase).iterator();
165: while (assertInstances.hasNext()) {
166: AssertParamInstance assertInstance = (AssertParamInstance) assertInstances
167: .next();
168: String[] key = new String[2];
169: key[0] = assertInstance.getName();
170: key[1] = assertInstance.getAction();
171: Object value = typeConverter
172: ._convertType(assertInstance);
173: assertValues.put(key, value);
174: }
175: Iterator assertParamGroupInstances = digester
176: .getAssertParamGroupInstances(className, method,
177: testcase).iterator();
178: while (assertParamGroupInstances.hasNext()) {
179: AssertParamGroupInstance assertParamGroupInstance = (AssertParamGroupInstance) assertParamGroupInstances
180: .next();
181: _getAssertParamValuesFromGroupInstance(
182: assertParamGroupInstance.getName(),
183: assertValues, assertParamGroupInstance);
184: }
185: } catch (TypeConversionException tce) {
186: throw new JTestCaseException(
187: "Error converting assert to Java type");
188: } catch (DigesterException e) {
189: throw new JTestCaseException("Error retrieving asserts.");
190: } catch (FileNotFoundException de) {
191: throw new JTestCaseException("Cannot read xml file : \n"
192: + de.getMessage());
193: }
194: return assertValues;
195: }
196:
197: /**
198: * Extracts the assert values from a AssertGroupInstance
199: *
200: * @param path
201: * the path so far
202: * @param flatAsserts
203: * the hashtable with the asserts
204: * @param assertGroupInstance
205: * the AssertGroupInstance object to be analysed
206: * @throws JTestCaseException
207: * if an internal error occurs
208: * @throws IllegalAccessException
209: * @throws InstantiationException
210: */
211: protected void _getAssertParamValuesFromGroupInstance(String path,
212: MultiKeyHashtable flatAsserts,
213: AssertParamGroupInstance assertParamGroupInstance)
214: throws JTestCaseException, InstantiationException,
215: IllegalAccessException {
216: Iterator assertIter = assertParamGroupInstance
217: .getAssertParamInstances().iterator();
218: try {
219: while (assertIter.hasNext()) {
220: AssertParamInstance asert = (AssertParamInstance) assertIter
221: .next();
222: Object value = typeConverter._convertType(asert);
223: String[] key = new String[2];
224: key[0] = path + "/" + asert.getName();
225: key[1] = asert.getAction();
226: flatAsserts.put(key, value);
227: }
228: Iterator assertGroupIter = assertParamGroupInstance
229: .getAssertParamGroupInstances().iterator();
230: while (assertGroupIter.hasNext()) {
231: AssertParamGroupInstance assertGroup = (AssertParamGroupInstance) assertGroupIter
232: .next();
233: _getAssertParamValuesFromGroupInstance(path + "/"
234: + assertGroup.getName(), flatAsserts,
235: assertGroup);
236: }
237: } catch (TypeConversionException tce) {
238:
239: throw new JTestCaseException(
240: "Error converting param to Java type");
241:
242: }
243: }
244:
245: }
|