0001: /*
0002: * This program is licensed under Common Public License Version 0.5.
0003: *
0004: * For License Information and conditions of use, see "LICENSE" in packaged
0005: *
0006: * Change History (based on CVS versions):
0007: *
0008: * Version: Date: Author: Description:
0009: * 1.1 2002/09/19 yuquing_wang 1. added lib directory for xtype.jar and xutil.jar. This dir is no longer temp.
0010: * 2. changed package to net.wangs.jtestcase.*;
0011: * 3. removed xutil features from jtestcase.
0012: * 4. enabled Map, Collection data type in jtestcase.
0013: * 5. added documentation to installed package
0014: * 1.2 2002/09/22 yuquing_wang 1. change usage of XType in JTestCase due to changes in XType
0015: * 2. change data and code in MyHashtableTest
0016: * 1.3 2002/09/25 yuquing_wang .
0017: * 1.4 2003/11/04 ckoelle Paramgroups allowed
0018: * Date formats handled correctly
0019: * Hastables allowed as params
0020: * 1.5 2003/11/28 ckoelle All getAssert* methods return null if no assert-tags are in the XML (instead of exception)
0021: * 1.6 2003/12/09 ckoelle Handle empty nodes in params and asserts correctly
0022: * 1.7 2004/04/23 ckoelle Use new possibilities of XType for complex data types
0023: * 1.8 2004/05/04 ckoelle Provide possibility to switch off XML schema validation
0024: * 1.9 2004/05/26 ckoelle Allow class tags without method tags
0025: * 1.10 2004/05/26 ckoelle Add method getClassUnderTest()
0026: * 1.11 2004/08/24 ckoelle Correct javadoc comments in assertTestCase (Bugs item #1015065)
0027: * 1.12 2004/08/27 ckoelle getControlParams() added
0028: * 1.13 2004/09/21 ckoelle Datatype time added in _convertType
0029: * 1.14 2004/10/04 ckoelle Allow assertgroups in getAssertValues
0030: * 1.15 2004/10/08 ckoelle Rework the getAssert* methods
0031: * 1.16 2004/12/07 ckoelle Add getTestCaseParameter
0032: * Switch off schema validation by default
0033: * 1.17 2005/01/18 ckoelle Allow multiple assertions on one variable.
0034: * All getAssert* methods replaced by getTestCaseAssert* methods which return MultiKeyHashtables.
0035: */
0036:
0037: package net.wangs.jtestcase;
0038:
0039: import java.text.DateFormat;
0040: import java.util.Enumeration;
0041: import java.util.Hashtable;
0042: import java.util.Iterator;
0043: import java.util.Locale;
0044: import java.util.Vector;
0045:
0046: import net.wangs.xmlutil.MultiKeyHashtable;
0047: import net.wangs.xmlutil.XMLUtil;
0048: import net.wangs.xmlutil.XMLUtilException;
0049: import net.wangs.xmlutil.XMLValidator;
0050: import net.wangs.xtype.core.XType;
0051:
0052: import org.w3c.dom.Element;
0053: import org.w3c.dom.Node;
0054: import org.w3c.dom.NodeList;
0055:
0056: /**
0057: * This utility class is used for Unit test to help reading data (test cases) out from xml file.
0058: *
0059: * @author <a href="mailto:yuqingwang_99@yahoo.com">Yuqing Wang</a>
0060: *
0061: */
0062:
0063: public class JTestCase {
0064:
0065: /**
0066: * Constant to indicate that a param value has to be get.
0067: */
0068: private static final int PARAM_VALUE = 1;
0069: /**
0070: * Constant to indicate that an assert value has to be get.
0071: */
0072: private static final int ASSERT_VALUE = 2;
0073: /**
0074: * Contant to indicate that the values of the parameters have to be read.
0075: */
0076: private static final int VALUES = 1;
0077: /**
0078: * Contant to indicate that the types of the parameters have to be read.
0079: */
0080: private static final int TYPES = 2;
0081: /**
0082: * Contant to indicate that the actions of the parameters have to be read.
0083: */
0084: private static final int ACTIONS = 3;
0085: /**
0086: * The XML representation of the complete XML file
0087: */
0088: private XMLUtil _base;
0089: /**
0090: * The XML representation of the class-tag
0091: */
0092: private XMLUtil _root;
0093: private MultiKeyHashtable _methodsByNameAndTestCase;
0094: private String locale = null;
0095: /**
0096: * The class name which is used to find the class tag in XML
0097: */
0098: private String mClassName = null;
0099:
0100: /**
0101: * Constructed from full file name and class name. This class name is specifed in data file
0102: * under "/tests/class@name". It's recommended that its value equals to the real testing
0103: * class's name.
0104: * @param fileName - name of data file. Will be searched by absolute path first. If not found,
0105: * then will be searched based on classpath.
0106: * @param className - name of test program. Is defined in data file in /tests/class@name
0107: * @throws JTestCaseException Problem with reading the file, validating the XML or with the XML structure
0108: */
0109: public JTestCase(String fileName, String className)
0110: throws JTestCaseException {
0111: // mClassName = className;
0112: if (mClassName != null && className != null
0113: && !mClassName.equals(className)) {
0114: try {
0115: String validate = System
0116: .getProperty("jtestcase.script.validation");
0117: boolean validating = false;
0118: System
0119: .setProperty("xmlutil.script.validation",
0120: "false");
0121:
0122: if (validate != null && validate.compareTo("true") == 0) {
0123: validating = false;
0124: System.setProperty("xmlutil.script.validation",
0125: "true");
0126: }
0127: if (validating) {
0128: if (!XMLValidator.validate(fileName)) {
0129: String msg = "***Error: " + fileName
0130: + " is not a valid XML file!";
0131: throw new JTestCaseException(msg);
0132: }
0133: }
0134:
0135: _base = new XMLUtil(fileName);
0136:
0137: Node classNode = _base.getNodeByAttrValue(
0138: "/tests/class", "name", className);
0139: if (classNode == null) {
0140: String msg = "***Error: classname= " + className
0141: + " not match with data file";
0142: throw new JTestCaseException(msg);
0143: }
0144:
0145: _root = new XMLUtil(classNode);
0146:
0147: if (_root.getNodeCount("/class/method") > 0) {
0148: _methodsByNameAndTestCase = _root
0149: .getNodesHashedByNamedAttrs(
0150: "/class/method", new String[] {
0151: "name", "test-case" });
0152: if (_methodsByNameAndTestCase == null) {
0153: String msg = "***Error: something wrong in data file with classname= "
0154: + className
0155: + ". Make sure data file conforms to XML Schema.";
0156: throw new JTestCaseException(msg);
0157: }
0158: }
0159: } catch (XMLUtilException e) {
0160: throw new JTestCaseException(e.getMessage());
0161: }
0162: mClassName = className;
0163: }
0164: }
0165:
0166: /**
0167: * Retrieves all control parameters. Control parameters are parameters that are defined for
0168: * the whole JTestCase data set. They are valid for all class-tags in the file.
0169: * @return A hashtable with the control parameters. Key is defined in /tests/params/param@name,
0170: * value is param's object value with type as indicated in /test/class/params/param@type.
0171: * If "type" is not specified in data file, then "String" is default type.
0172: * @throws JTestCaseException in case of any errors
0173: */
0174: public Hashtable getControlParams() throws JTestCaseException {
0175: return _getParams(_base, "tests/params/param", "", VALUES);
0176: }
0177:
0178: /**
0179: * Retrieves all global (class-wide) params into Hashtable. HashKey is param's name, HashValue
0180: * is param's value.
0181: *
0182: * These params are common to all tested methods for the specified tested class, and should be
0183: * retrieved in testing class's constructor.
0184: * @return Hashtable. Key is param's name defined in /tests/class/params/param@name, value is
0185: * param's Object value with type as indicated in /test/class/params/param@type. If not "type"
0186: * specified in data file, then "String" is default type.
0187: */
0188: public Hashtable getGlobalParams() throws JTestCaseException {
0189:
0190: // return _root.getNodeValuesHashedByNamedAttr("/class/params/param", "name");
0191: return _getParams(_root, "class/params/param", "", VALUES);
0192: }
0193:
0194: /**
0195: * Helps setting the Locale for DateFormats
0196: * @return the global Parameter named 'locale'
0197: * @throws JTestCaseException
0198: */
0199: public String getGlobalParamLocale() throws JTestCaseException {
0200:
0201: String result = null;
0202: try {
0203: result = _root.getNodeValueByAttrValue(
0204: "class/params/param", "name", "locale");
0205: } catch (Exception e) {
0206: throw new JTestCaseException(e.getMessage());
0207: }
0208: return result;
0209:
0210: }
0211:
0212: /**
0213: * Retrieves types of all global (class-wided) params into Hashtable. HashKey is param's name,
0214: * HashValue is param's type in String which represents java class's name.
0215: * These params are common to all tested methods for the specified tested class, and should be
0216: * retrieved in testing class's constructor.
0217: *
0218: * @return Hashtable. Key is param's name defined in /tests/class/params/param@name, value is
0219: * param's Object value with type as indecated in /test/class/params/param@type. If not "type"
0220: * specified in data file, then "String" is default type.
0221: */
0222: public Hashtable getGlobalParamTypes() throws JTestCaseException {
0223:
0224: try {
0225: return _root.getAttrValuesHashedByNamedAttr(
0226: "/class/params/param", "type", "name");
0227: } catch (XMLUtilException e) {
0228: throw new JTestCaseException(e.getMessage());
0229: }
0230: }
0231:
0232: /**
0233: * Get number of test cases for a given method. This method name is specified in data file
0234: * in /tests/class/method@name. It's recommended that its value equals to the real
0235: * testing method's name.
0236: *
0237: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0238: * @return int. Number of test cases that are defined for this method in data file.
0239: */
0240: public int getCountOfTestCases(String methodName)
0241: throws JTestCaseException {
0242:
0243: try {
0244: Vector nodes = _root.getNodesByAttrValue("/class/method",
0245: "name", methodName);
0246:
0247: if (nodes == null) {
0248: String msg = "***Error: methodname=" + methodName
0249: + " not match with data file";
0250: throw new JTestCaseException(msg);
0251: }
0252:
0253: return nodes.size();
0254: } catch (XMLUtilException e) {
0255: throw new JTestCaseException(e.getMessage());
0256: }
0257: }
0258:
0259: /**
0260: * Get all params for a given method and its test case value into Hashtable. Hashed key
0261: * is param's name, value is value of param.
0262: *
0263: * This method is normally used with getNameOfTestCases().
0264: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0265: * @param testCase - name of test case. Defined in data file in /tests/class/method@test-case.
0266: * Should be unique for the given "methodName".
0267: * @return Hashtable. Key is param's name defined in /tests/class/method/params/param@name,
0268: * value is param's Object value with type as indecated in /test/class/method/params/param@type.
0269: * If not "type" specified in data file, then "String" is default type.
0270: */
0271: public Hashtable getTestCaseParams(String methodName,
0272: String testCase) throws JTestCaseException {
0273:
0274: try {
0275: Node methodNode = (Node) _methodsByNameAndTestCase
0276: .get(new String[] { methodName, testCase });
0277:
0278: if (methodNode == null) {
0279: String msg = "***Error: methodname=" + methodName
0280: + " not match with data file";
0281: throw new JTestCaseException(msg);
0282: }
0283:
0284: XMLUtil nodeUtil = new XMLUtil(methodNode);
0285:
0286: // return nodeUtil.getNodeValuesHashedByNamedAttr("method/params/param", "name");
0287: return _getGroupedParams(nodeUtil, "method/params", "",
0288: VALUES);
0289: } catch (XMLUtilException e) {
0290: throw new JTestCaseException(e.getMessage());
0291: }
0292:
0293: }
0294:
0295: /**
0296: * Get all params for a given method and the index number of test case into Hashtable. Hashed
0297: * key is param's name, value is String value of param.
0298: *
0299: * This method is normally used with getCountOfTestCases().
0300: *
0301: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0302: * @param index - index number of test case for this method, start from 0.
0303: * @return Hashtable. Key is param's name defined in /tests/class/method/params/param@name,
0304: * value is param's Object value with type as indecated in /test/class/method/params/param@type.
0305: * If not "type" specified in data file, then "String" is default type.
0306: */
0307: public Hashtable getTestCaseParams(String methodName, int index)
0308: throws JTestCaseException {
0309:
0310: try {
0311: Vector nodes = _root.getNodesByAttrValue("/class/method",
0312: "name", methodName);
0313:
0314: if (nodes == null) {
0315: String msg = "***Error: methodname=" + methodName
0316: + " not match with data file";
0317: throw new JTestCaseException(msg);
0318: }
0319:
0320: if (nodes.size() <= index) {
0321: throw new JTestCaseException(
0322: "Index out of bound: method=" + methodName
0323: + " index=" + index);
0324: }
0325:
0326: Node methodNode = (Node) nodes.elementAt(index);
0327:
0328: XMLUtil nodeUtil = new XMLUtil(methodNode);
0329:
0330: // return nodeUtil.getNodeValuesHashedByNamedAttr("method/params/param", "name");
0331: return _getGroupedParams(nodeUtil, "method/params", "",
0332: VALUES);
0333: } catch (XMLUtilException e) {
0334: throw new JTestCaseException(e.getMessage());
0335: }
0336:
0337: }
0338:
0339: /**
0340: * Get types of all params for a given method and its test case value into Hashtable. Hashed key
0341: * is param's name, value is String value of param type, which conforms to java class name.
0342: *
0343: * This method is normally used with getNameOfTestCases().
0344: *
0345: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0346: * @param testCase - name of test case. Defined in data file in /tests/class/method@test-case.
0347: * Should be unique for the given "methodName".
0348: * @return Hashtable. Key is param's name defined in /tests/class/method/params/param@name,
0349: * value is param's type value defined in /test/class/method/params/param@type.
0350: */
0351: public Hashtable getTestCaseParamTypes(String methodName,
0352: String testCase) throws JTestCaseException {
0353:
0354: try {
0355: Node methodNode = (Node) _methodsByNameAndTestCase
0356: .get(new String[] { methodName, testCase });
0357:
0358: if (methodNode == null) {
0359: String msg = "***Error: methodname=" + methodName
0360: + " not match with data file";
0361: throw new JTestCaseException(msg);
0362: }
0363:
0364: XMLUtil nodeUtil = new XMLUtil(methodNode);
0365:
0366: return nodeUtil.getAttrValuesHashedByNamedAttr(
0367: "method/params/param", "type", "name");
0368: } catch (XMLUtilException e) {
0369: throw new JTestCaseException(e.getMessage());
0370: }
0371: }
0372:
0373: /**
0374: * Get types of all params for a given method and the index number of test case into Hashtable. Hashed
0375: * key is param's name, value is String value of param type, which conforms to java class name.
0376: *
0377: * This method is normally used with getCountOfTestCases().
0378: *
0379: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0380: * @param index - index number of test case for this method, start from 0.
0381: * @return Hashtable. Key is param's name defined in /tests/class/method/params/param@name,
0382: * value is param's type value defined in /test/class/method/params/param@type.
0383: */
0384: public Hashtable getTestCaseParamTypes(String methodName, int index)
0385: throws JTestCaseException {
0386:
0387: try {
0388: Vector nodes = _root.getNodesByAttrValue("/class/method",
0389: "name", methodName);
0390:
0391: if (nodes == null) {
0392: String msg = "***Error: methodname=" + methodName
0393: + " not match with data file";
0394: throw new JTestCaseException(msg);
0395: }
0396:
0397: if (nodes.size() <= index) {
0398: throw new JTestCaseException(
0399: "Index out of bound: method=" + methodName
0400: + " index=" + index);
0401: }
0402:
0403: Node methodNode = (Node) nodes.elementAt(index);
0404:
0405: XMLUtil nodeUtil = new XMLUtil(methodNode);
0406:
0407: return nodeUtil.getAttrValuesHashedByNamedAttr(
0408: "method/params/param", "type", "name");
0409: } catch (XMLUtilException e) {
0410: throw new JTestCaseException(e.getMessage());
0411: }
0412: }
0413:
0414: /**
0415: * Get the value of the specific paramater given his name for a specific methodName
0416: * and a specific testCase.
0417: *
0418: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0419: * @param testCase - name of test case. Defined in data file in /tests/class/method@test-case.
0420: * Should be unique for the given "methodName".
0421: * @param parameter the name of the parameter to be read.
0422: * @return the value of the specific parameter for the specific method and testCase.
0423: * @since 2.1.3
0424: * @author ckoelle
0425: */
0426: public Object getTestCaseParameter(String methodName,
0427: String testCase, String parameter)
0428: throws JTestCaseException {
0429: try {
0430: Node methodNode = (Node) _methodsByNameAndTestCase
0431: .get(new String[] { methodName, testCase });
0432:
0433: if (methodNode == null) {
0434: String msg = "***Error: methodname=" + methodName
0435: + " not match with data file";
0436: throw new JTestCaseException(msg);
0437: }
0438:
0439: XMLUtil nodeUtil = new XMLUtil(methodNode);
0440: return _getGroupedParams(nodeUtil, "method/params", "",
0441: VALUES).get(parameter);
0442: } catch (XMLUtilException e) {
0443: throw new JTestCaseException(e.getMessage());
0444: } catch (NumberFormatException e) {
0445: throw new JTestCaseException(e.getMessage());
0446: }
0447: }
0448:
0449: /**
0450: * Get all assert actions for a given method and its test case value into Hashtable. Hashed key is
0451: * assert param's name, value is String value of assert action.
0452: *
0453: * This method is normally used with getNameOfTestCases().
0454: *
0455: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0456: * @param testCase - name of test case. Defined in data file in /tests/class/method@test-case.
0457: * Should be unique for the given "methodName".
0458: * @return Hashtable. Key is param's name defined in /tests/class/method/params/param@name,
0459: * value is param's type value defined in /test/class/method/asserts/assert@action.
0460: * @deprecated replaced by getTestCaseAssertActions
0461: */
0462: public Hashtable getAssertActions(String methodName, String testCase)
0463: throws JTestCaseException {
0464:
0465: try {
0466: Node methodNode = (Node) _methodsByNameAndTestCase
0467: .get(new String[] { methodName, testCase });
0468:
0469: if (methodNode == null) {
0470: String msg = "***Error: methodname=" + methodName
0471: + " not match with data file";
0472: throw new JTestCaseException(msg);
0473: }
0474:
0475: XMLUtil nodeUtil = new XMLUtil(methodNode);
0476: Hashtable returnValue = new Hashtable();
0477: MultiKeyHashtable allAsserts = _getGroupedAsserts(nodeUtil,
0478: "/method/asserts", "", ACTIONS);
0479: if (allAsserts == null) {
0480: return returnValue;
0481: }
0482: Iterator keysIter = allAsserts.keySet().iterator();
0483: while (keysIter.hasNext()) {
0484: String[] keys = (String[]) keysIter.next();
0485: String val = (String) allAsserts.get(keys);
0486: returnValue.put(keys[0], val);
0487: }
0488: return returnValue;
0489: } catch (XMLUtilException e) {
0490: throw new JTestCaseException(e.getMessage());
0491: }
0492: }
0493:
0494: /**
0495: * Get all assert actions for a given method and its test case value into Hashtable. Hashed key is
0496: * assert param's name, value is String value of assert action.
0497: *
0498: * This method is normally used with getNameOfTestCases().
0499: *
0500: * @param methodName - name of tested method. Defined in data file in /tests/class/method@name.
0501: * @param testCase - name of test case. Defined in data file in /tests/class/method@test-case.
0502: * Should be unique for the given "methodName".
0503: * @return Hashtable. Key is param's name defined in /tests/class/method/params/param@name,
0504: * value is param's type value defined in /test/class/method/asserts/assert@action.
0505: */
0506: public MultiKeyHashtable getTestCaseAssertActions(
0507: String methodName, String testCase)
0508: throws JTestCaseException {
0509:
0510: try {
0511: Node methodNode = (Node) _methodsByNameAndTestCase
0512: .get(new String[] { methodName, testCase });
0513:
0514: if (methodNode == null) {
0515: String msg = "***Error: methodname=" + methodName
0516: + " not match with data file";
0517: throw new JTestCaseException(msg);
0518: }
0519:
0520: XMLUtil nodeUtil = new XMLUtil(methodNode);
0521: //System.out.println("getTestCaseAssertActions: "+methodName+": "+testCase);
0522: return _getGroupedAsserts(nodeUtil, "/method/asserts", "",
0523: ACTIONS);
0524: } catch (XMLUtilException e) {
0525: throw new JTestCaseException(e.getMessage());
0526: }
0527: }
0528:
0529: /**
0530: * Get all assert actions for a given method and its test case value into Hashtable. Hashed key
0531: * is assert param's name, value is String value of assert action.
0532: *
0533: * This method os normally used with getCountOfTestCases().
0534: *
0535: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0536: * @param index - index number of test case for this method, start from 0.
0537: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0538: * value is assert param's action value defined in /test/class/method/asserts/assert@action.
0539: * @deprecated replaced by getTestCaseAssertActions
0540: */
0541: public Hashtable getAssertActions(String methodName, int index)
0542: throws JTestCaseException {
0543:
0544: try {
0545: Vector nodes = _root.getNodesByAttrValue("/class/method",
0546: "name", methodName);
0547:
0548: if (nodes == null) {
0549: String msg = "***Error: methodname=" + methodName
0550: + " not match with data file";
0551: throw new JTestCaseException(msg);
0552: }
0553:
0554: if (nodes.size() <= index) {
0555: throw new JTestCaseException(
0556: "Index out of bound: method=" + methodName
0557: + " index=" + index);
0558: }
0559:
0560: Node methodNode = (Node) nodes.elementAt(index);
0561:
0562: XMLUtil nodeUtil = new XMLUtil(methodNode);
0563:
0564: Hashtable returnValue = null;
0565: MultiKeyHashtable allAsserts = _getGroupedAsserts(nodeUtil,
0566: "/method/asserts", "", ACTIONS);
0567: if (allAsserts == null) {
0568: return returnValue;
0569: }
0570: Iterator keysIter = allAsserts.keySet().iterator();
0571: while (keysIter.hasNext()) {
0572: String[] keys = (String[]) keysIter.next();
0573: String val = (String) allAsserts.get(keys);
0574: returnValue.put(keys[0], val);
0575: }
0576: return returnValue;
0577: } catch (XMLUtilException e) {
0578: throw new JTestCaseException(e.getMessage());
0579: }
0580:
0581: }
0582:
0583: /**
0584: * Get all assert actions for a given method and its test case value into Hashtable. Hashed key
0585: * is assert param's name, value is String value of assert action.
0586: *
0587: * This method os normally used with getCountOfTestCases().
0588: *
0589: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0590: * @param index - index number of test case for this method, start from 0.
0591: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0592: * value is assert param's action value defined in /test/class/method/asserts/assert@action.
0593: */
0594: public MultiKeyHashtable getTestCaseAssertActions(
0595: String methodName, int index) throws JTestCaseException {
0596:
0597: try {
0598: Vector nodes = _root.getNodesByAttrValue("/class/method",
0599: "name", methodName);
0600:
0601: if (nodes == null) {
0602: String msg = "***Error: methodname=" + methodName
0603: + " not match with data file";
0604: throw new JTestCaseException(msg);
0605: }
0606:
0607: if (nodes.size() <= index) {
0608: throw new JTestCaseException(
0609: "Index out of bound: method=" + methodName
0610: + " index=" + index);
0611: }
0612:
0613: Node methodNode = (Node) nodes.elementAt(index);
0614:
0615: XMLUtil nodeUtil = new XMLUtil(methodNode);
0616:
0617: return _getGroupedAsserts(nodeUtil, "/method/asserts", "",
0618: ACTIONS);
0619: } catch (XMLUtilException e) {
0620: throw new JTestCaseException(e.getMessage());
0621: }
0622:
0623: }
0624:
0625: /**
0626: * Get all assert values for a given method and its test case value into Hashtable. Hashed key is
0627: * assert param's name, value is String value of assert param.
0628: *
0629: * This method is normally used with getNameOfTestCases().
0630: *
0631: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0632: * @param testCase - name of test case. Defined in data file in /tests/class/method@test-case.
0633: * Should be unique for the given "methodName".
0634: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0635: * value is assert param's Object value with type as indecated in /test/class/method/asserts/assert@type.
0636: * If not "type" specified in data file, then "String" is default type.
0637: * @deprecated replaced by getTestCaseAssertValues
0638: */
0639: public Hashtable getAssertValues(String methodName, String testCase)
0640: throws JTestCaseException {
0641:
0642: try {
0643: Node methodNode = (Node) _methodsByNameAndTestCase
0644: .get(new String[] { methodName, testCase });
0645:
0646: if (methodNode == null) {
0647: String msg = "***Error: methodname=" + methodName
0648: + " not match with data file";
0649: throw new JTestCaseException(msg);
0650: }
0651:
0652: XMLUtil nodeUtil = new XMLUtil(methodNode);
0653: Hashtable returnValue = new Hashtable();
0654: MultiKeyHashtable allAsserts = _getGroupedAsserts(nodeUtil,
0655: "/method/asserts", "", VALUES);
0656: if (allAsserts == null) {
0657: return returnValue;
0658: }
0659: Iterator keysIter = allAsserts.keySet().iterator();
0660: while (keysIter.hasNext()) {
0661: String[] keys = (String[]) keysIter.next();
0662: Object val = allAsserts.get(keys);
0663: returnValue.put(keys[0], val);
0664: }
0665: return returnValue;
0666: } catch (XMLUtilException e) {
0667: throw new JTestCaseException(e.getMessage());
0668: }
0669: }
0670:
0671: /**
0672: * Get all assert values for a given method and its test case value into Hashtable. Hashed key is
0673: * assert param's name, value is String value of assert param.
0674: *
0675: * This method is normally used with getNameOfTestCases().
0676: *
0677: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0678: * @param testCase - name of test case. Defined in data file in /tests/class/method@test-case.
0679: * Should be unique for the given "methodName".
0680: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0681: * value is assert param's Object value with type as indecated in /test/class/method/asserts/assert@type.
0682: * If not "type" specified in data file, then "String" is default type.
0683: */
0684: public MultiKeyHashtable getTestCaseAssertValues(String methodName,
0685: String testCase) throws JTestCaseException {
0686:
0687: try {
0688: Node methodNode = (Node) _methodsByNameAndTestCase
0689: .get(new String[] { methodName, testCase });
0690:
0691: if (methodNode == null) {
0692: String msg = "***Error: methodname=" + methodName
0693: + " not match with data file";
0694: throw new JTestCaseException(msg);
0695: }
0696:
0697: XMLUtil nodeUtil = new XMLUtil(methodNode);
0698: return _getGroupedAsserts(nodeUtil, "/method/asserts", "",
0699: VALUES);
0700: } catch (XMLUtilException e) {
0701: throw new JTestCaseException(e.getMessage());
0702: }
0703: }
0704:
0705: /**
0706: * Get all assert values for a given method and its test case value into Hashtable. Hashed key
0707: * is assert param's name, value is String value of assert param.
0708: *
0709: * This method os normally used with getCountOfTestCases().
0710: *
0711: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0712: * @param index - index number of test case for this method, start from 0.
0713: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0714: * value is assert param's Object value with type as indecated in /test/class/method/asserts/assert@type.
0715: * If not "type" specified in data file, then "String" is default type.
0716: * @deprecated replaced by getTestCaseAssertValues
0717: */
0718: public Hashtable getAssertValues(String methodName, int index)
0719: throws JTestCaseException {
0720:
0721: try {
0722: Vector nodes = _root.getNodesByAttrValue("/class/method",
0723: "name", methodName);
0724:
0725: if (nodes == null) {
0726: String msg = "***Error: methodname=" + methodName
0727: + " not match with data file";
0728: throw new JTestCaseException(msg);
0729: }
0730:
0731: if (nodes.size() <= index) {
0732: throw new JTestCaseException(
0733: "Index out of bound: method=" + methodName
0734: + " index=" + index);
0735: }
0736:
0737: Node methodNode = (Node) nodes.elementAt(index);
0738:
0739: XMLUtil nodeUtil = new XMLUtil(methodNode);
0740: Hashtable returnValue = null;
0741: MultiKeyHashtable allAsserts = _getGroupedAsserts(nodeUtil,
0742: "/method/asserts", "", VALUES);
0743: if (allAsserts == null) {
0744: return returnValue;
0745: }
0746: Iterator keysIter = allAsserts.keySet().iterator();
0747: while (keysIter.hasNext()) {
0748: String[] keys = (String[]) keysIter.next();
0749: Object val = allAsserts.get(keys);
0750: returnValue.put(keys[0], val);
0751: }
0752: return returnValue;
0753: } catch (XMLUtilException e) {
0754: throw new JTestCaseException(e.getMessage());
0755: }
0756:
0757: }
0758:
0759: /**
0760: * Get all assert values for a given method and its test case value into Hashtable. Hashed key
0761: * is assert param's name, value is String value of assert param.
0762: *
0763: * This method os normally used with getCountOfTestCases().
0764: *
0765: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0766: * @param index - index number of test case for this method, start from 0.
0767: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0768: * value is assert param's Object value with type as indecated in /test/class/method/asserts/assert@type.
0769: * If not "type" specified in data file, then "String" is default type.
0770: */
0771: public MultiKeyHashtable getTestCaseAssertValues(String methodName,
0772: int index) throws JTestCaseException {
0773:
0774: try {
0775: Vector nodes = _root.getNodesByAttrValue("/class/method",
0776: "name", methodName);
0777:
0778: if (nodes == null) {
0779: String msg = "***Error: methodname=" + methodName
0780: + " not match with data file";
0781: throw new JTestCaseException(msg);
0782: }
0783:
0784: if (nodes.size() <= index) {
0785: throw new JTestCaseException(
0786: "Index out of bound: method=" + methodName
0787: + " index=" + index);
0788: }
0789:
0790: Node methodNode = (Node) nodes.elementAt(index);
0791:
0792: XMLUtil nodeUtil = new XMLUtil(methodNode);
0793: return _getGroupedAsserts(nodeUtil, "/method/asserts", "",
0794: VALUES);
0795: } catch (XMLUtilException e) {
0796: throw new JTestCaseException(e.getMessage());
0797: }
0798:
0799: }
0800:
0801: /**
0802: * Get types of assert values for a given method and its test case value into Hashtable. Hashed key is
0803: * assert param's name, value is String value of type of assert param. This type is name of java class.
0804: * This method does not return the types of assert values in complex data types. This means if you
0805: * have an assert value of type "java.util.Hashtable" this method returns exactly this string and not
0806: * a hashtable with the data types of the items in the hashtable.
0807: *
0808: * This method is normally used with getNameOfTestCases().
0809: *
0810: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0811: * @param testCase - name of test case.Defined in data file in /tests/class/method@test-case.
0812: * Should be unique for the given "methodName".
0813: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0814: * value is assert param's type value as indecated in /test/class/method/asserts/assert@type.
0815: * @deprecated replaced by getTestCaseAssertTypes
0816: */
0817: public Hashtable getAssertTypes(String methodName, String testCase)
0818: throws JTestCaseException {
0819:
0820: try {
0821: Node methodNode = (Node) _methodsByNameAndTestCase
0822: .get(new String[] { methodName, testCase });
0823:
0824: if (methodNode == null) {
0825: String msg = "***Error: methodname=" + methodName
0826: + " not match with data file";
0827: throw new JTestCaseException(msg);
0828: }
0829:
0830: XMLUtil nodeUtil = new XMLUtil(methodNode);
0831: Hashtable returnValue = new Hashtable();
0832: MultiKeyHashtable allAsserts = _getGroupedAsserts(nodeUtil,
0833: "/method/asserts", "", TYPES);
0834: if (allAsserts == null) {
0835: return returnValue;
0836: }
0837: Iterator keysIter = allAsserts.keySet().iterator();
0838: while (keysIter.hasNext()) {
0839: String[] keys = (String[]) keysIter.next();
0840: String val = (String) allAsserts.get(keys);
0841: returnValue.put(keys[0], val);
0842: }
0843: return returnValue;
0844: } catch (XMLUtilException e) {
0845: throw new JTestCaseException(e.getMessage());
0846: }
0847: }
0848:
0849: /**
0850: * Get types of assert values for a given method and its test case value into Hashtable. Hashed key is
0851: * assert param's name, value is String value of type of assert param. This type is name of java class.
0852: * This method does not return the types of assert values in complex data types. This means if you
0853: * have an assert value of type "java.util.Hashtable" this method returns exactly this string and not
0854: * a hashtable with the data types of the items in the hashtable.
0855: *
0856: * This method is normally used with getNameOfTestCases().
0857: *
0858: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0859: * @param testCase - name of test case.Defined in data file in /tests/class/method@test-case.
0860: * Should be unique for the given "methodName".
0861: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0862: * value is assert param's type value as indecated in /test/class/method/asserts/assert@type.
0863: */
0864: public MultiKeyHashtable getTestCaseAssertTypes(String methodName,
0865: String testCase) throws JTestCaseException {
0866:
0867: try {
0868: Node methodNode = (Node) _methodsByNameAndTestCase
0869: .get(new String[] { methodName, testCase });
0870:
0871: if (methodNode == null) {
0872: String msg = "***Error: methodname=" + methodName
0873: + " not match with data file";
0874: throw new JTestCaseException(msg);
0875: }
0876:
0877: XMLUtil nodeUtil = new XMLUtil(methodNode);
0878: return _getGroupedAsserts(nodeUtil, "/method/asserts", "",
0879: TYPES);
0880: } catch (XMLUtilException e) {
0881: throw new JTestCaseException(e.getMessage());
0882: }
0883: }
0884:
0885: /**
0886: * Get types of assert values for a given method and its test case value into Hashtable, hashed key
0887: * is assert param's name, value is String value of type of assert param. This type is name of java class.
0888: * This method does not return the types of assert values in complex data types. This means if you
0889: * have an assert value of type "java.util.Hashtable" this method returns exactly this string and not
0890: * a hashtable with the data types of the items in the hashtable.
0891: *
0892: * This method os normally used with getCountOfTestCases().
0893: *
0894: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0895: * @param index - index number of test case for this method, start from 0.
0896: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0897: * value is assert param's type value as indecated in /test/class/method/asserts/assert@type.
0898: * @deprecated replaced by getTestCaseAssertTypes
0899: */
0900: public Hashtable getAssertTypes(String methodName, int index)
0901: throws JTestCaseException {
0902:
0903: try {
0904: Vector nodes = _root.getNodesByAttrValue("/class/method",
0905: "name", methodName);
0906:
0907: if (nodes == null) {
0908: String msg = "***Error: methodname=" + methodName
0909: + " not match with data file";
0910: throw new JTestCaseException(msg);
0911: }
0912:
0913: if (nodes.size() <= index) {
0914: throw new JTestCaseException(
0915: "Index out of bound: method=" + methodName
0916: + " index=" + index);
0917: }
0918:
0919: Node methodNode = (Node) nodes.elementAt(index);
0920:
0921: XMLUtil nodeUtil = new XMLUtil(methodNode);
0922: Hashtable returnValue = null;
0923: MultiKeyHashtable allAsserts = _getGroupedAsserts(nodeUtil,
0924: "/method/asserts", "", TYPES);
0925: if (allAsserts == null) {
0926: return returnValue;
0927: }
0928: Iterator keysIter = allAsserts.keySet().iterator();
0929: while (keysIter.hasNext()) {
0930: String[] keys = (String[]) keysIter.next();
0931: String val = (String) allAsserts.get(keys);
0932: returnValue.put(keys[0], val);
0933: }
0934: return returnValue;
0935: } catch (XMLUtilException e) {
0936: throw new JTestCaseException(e.getMessage());
0937: }
0938:
0939: }
0940:
0941: /**
0942: * Get types of assert values for a given method and its test case value into Hashtable, hashed key
0943: * is assert param's name, value is String value of type of assert param. This type is name of java class.
0944: * This method does not return the types of assert values in complex data types. This means if you
0945: * have an assert value of type "java.util.Hashtable" this method returns exactly this string and not
0946: * a hashtable with the data types of the items in the hashtable.
0947: *
0948: * This method os normally used with getCountOfTestCases().
0949: *
0950: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0951: * @param index - index number of test case for this method, start from 0.
0952: * @return Hashtable. Key is assert param's name defined in /tests/class/method/asserts/assert@name,
0953: * value is assert param's type value as indecated in /test/class/method/asserts/assert@type.
0954: */
0955: public MultiKeyHashtable getTestCaseAssertTypes(String methodName,
0956: int index) throws JTestCaseException {
0957:
0958: try {
0959: Vector nodes = _root.getNodesByAttrValue("/class/method",
0960: "name", methodName);
0961:
0962: if (nodes == null) {
0963: String msg = "***Error: methodname=" + methodName
0964: + " not match with data file";
0965: throw new JTestCaseException(msg);
0966: }
0967:
0968: if (nodes.size() <= index) {
0969: throw new JTestCaseException(
0970: "Index out of bound: method=" + methodName
0971: + " index=" + index);
0972: }
0973:
0974: Node methodNode = (Node) nodes.elementAt(index);
0975:
0976: XMLUtil nodeUtil = new XMLUtil(methodNode);
0977: return _getGroupedAsserts(nodeUtil, "/method/asserts", "",
0978: TYPES);
0979: } catch (XMLUtilException e) {
0980: throw new JTestCaseException(e.getMessage());
0981: }
0982:
0983: }
0984:
0985: /**
0986: * Get all test cases' name for a given method into Vector.
0987: *
0988: * This method requires that for a given method, each test case should be named uniquely.
0989: *
0990: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
0991: * @return Vector of names of test cases that are defined for this method in data file in
0992: * /tests/class/method@test-case.
0993: */
0994: public Vector getNameOfTestCases(String methodName)
0995: throws JTestCaseException {
0996:
0997: try {
0998: Vector testCases = new Vector();
0999:
1000: Vector nodes = _root.getNodesByAttrValue("/class/method",
1001: "name", methodName);
1002:
1003: if (nodes == null) {
1004: String msg = "***Error: methodname=" + methodName
1005: + " not match with data file";
1006: throw new JTestCaseException(msg);
1007: }
1008:
1009: for (int i = 0; i < nodes.size(); i++) {
1010: Node methodNode = (Node) nodes.elementAt(i);
1011: XMLUtil nodeUtil = new XMLUtil(methodNode);
1012:
1013: String testCase = nodeUtil.getNamedAttrValue("/method",
1014: "test-case", 1);
1015: testCases.add(testCase);
1016: }
1017:
1018: return testCases;
1019:
1020: } catch (XMLUtilException e) {
1021: throw new JTestCaseException(e.getMessage());
1022: }
1023: }
1024:
1025: /**
1026: * Asserts a given varible's value against its expected value by using expected action.
1027: *<pre>
1028: * Following actions are defined. Expected action in data file should fall into one of them:
1029: * ISNULL, ISNOTNULL, EQUALS, NOTEQUALS, GT (greater than), NOTGT (not greater than),
1030: * LT (less than), NOTLT (not less than), and TRUE (expression is true).
1031: *
1032: * The expected asserting results are defined in data file like this:
1033: * ...
1034: * <asserts>
1035: * <assert name="var1" action="EQUALS" type="int">100</assert>
1036: * <assert name="var2" action="NOTNULL"/>
1037: * </asserts>
1038: * ...
1039: *
1040: * Note: "GT" means "real value" greater than "expected value". Simular to others.
1041: *
1042: * @param varName - name of varible. Defined in data file in /test/class/method/asserts/assert@name.
1043: * @param varValue - actually value of this varible.
1044: * @param methodName - name of this method. Defined in data file in /test/class/method@name.
1045: * @param testCase - name of this test case. Defined in data file in /test/class/method@test-case.
1046: * @return boolean. "true" is assertion is true, else "false".
1047: */
1048: public boolean assertTestCase(String varName, Object varValue,
1049: String methodName, String testCase)
1050: throws JTestCaseException {
1051: return assertTESTCASE(varName, varValue,
1052: getTestCaseAssertActions(methodName, testCase),
1053: getTestCaseAssertValues(methodName, testCase));
1054: }
1055:
1056: /**
1057: * Asserts a given varible's value against its expected value by using expected action.
1058: *<pre>
1059: * Following actions are defined. Expected action in data file should fall into one of them:
1060: * ISNULL, ISNOTNULL, EQUALS, NOTEQUALS, GT (greater than), NOTGT (not greater than),
1061: * LT (less than), NOTLT (not less than), and TRUE (expression is true).
1062: *
1063: * The expected asserting results are defined in data file like this:
1064: * ...
1065: * <asserts>
1066: * <assert name="var1" action="EQUALS" type="int">100</assert>
1067: * <assert name="var2" action="NOTNULL"/>
1068: * </asserts>
1069: * ...
1070: *
1071: * Note: "GT" means "real value" greater than "expected value". Simular to others.
1072: *
1073: * @param varName - name of varible. Defined in data file in /test/class/method/asserts/assert@name.
1074: * @param varValue - actually value of this varible.
1075: * @param methodName - name of this method. Defined in data file in /test/class/method@name.
1076: * @param index - index number of this test case for this method.
1077: * @return boolean. "true" is assertion is true, else "false".
1078: */
1079: public boolean assertTestCase(String varName, Object varValue,
1080: String methodName, int index) throws JTestCaseException {
1081: return assertTESTCASE(varName, varValue,
1082: getTestCaseAssertActions(methodName, index),
1083: getTestCaseAssertValues(methodName, index));
1084: }
1085:
1086: /**
1087: * Asserts a given varible's value against its expected value by using expected action.
1088: *<pre>
1089: * Following actions are defined. Expected action in data file should fall into one of them:
1090: * ISNULL, ISNOTNULL, EQUALS, NOTEQUALS, GT (greater than), NOTGT (not greater than),
1091: * LT (less than), NOTLT (not less than), and TRUE (expression is true).
1092: *
1093: * The expected asserting results are defined in data file like this:
1094: * ...
1095: * <asserts>
1096: * <assert name="var1" action="EQUALS" type="int">100</assert>
1097: * <assert name="var2" action="NOTNULL"/>
1098: * </asserts>
1099: * ...
1100: *
1101: * Note: "GT" means "real value" greater than "expected value". Simular to others.
1102: *
1103: * @param varName - name of varible. Defined in data file in /test/class/method/asserts/assert@name.
1104: * @param varValue - actually value of this varible.
1105: * @param assertActions - a MultiKeyHashtable with the assert actions
1106: * @param assertValues - a MultiKeyHashtable with the assert values
1107: * @return "true" is assertion is true, else "false".
1108: * @throws JTestCaseException
1109: */
1110: private boolean assertTESTCASE(String varName, Object varValue,
1111: MultiKeyHashtable assertActions,
1112: MultiKeyHashtable assertValues) throws JTestCaseException {
1113: Iterator keysIter = assertActions.keySet().iterator();
1114: boolean returnValue = true;
1115: //System.out.println("assertTESTCASE: resultValue--> "+varValue.toString());
1116: while (keysIter.hasNext() && returnValue == true) {
1117: String[] keys = (String[]) keysIter.next();
1118: //System.out.println("assertTESTCASE : varName-->"+varName+" : keys-->"+keys[0]);
1119: if (keys[0].equals(varName)) {//if (keys[0].compareTo(varName) == 0) {
1120: String action = keys[1];
1121: //System.out.println("assertTESTCASE 1: action-->"+action);
1122: if (action == null) {
1123: throw new JTestCaseException(
1124: "***Error: variable "
1125: + varName
1126: + " not defined in data file for this test case.");
1127: }
1128:
1129: if (action.equals("ISNULL")
1130: || action.equals("ISNOTNULL")) {
1131: // These situations do not require expected values.
1132: if (action.equals("ISNULL")) {
1133: returnValue = (varValue == null);
1134: } else {
1135: returnValue = (varValue != null);
1136: }
1137: //System.out.println("assertTESTCASE 1: returnValue-->"+returnValue);
1138: } else {
1139: Object expectedValue = assertValues.get(keys);
1140:
1141: if (action.equals("EQUALS")) {
1142:
1143: returnValue = varValue.equals(expectedValue);
1144: } else if (action.equals("NOTEQUALS")) {
1145: System.out
1146: .println("assertTESTCASE 2: varValue-->"
1147: + varValue.toString()
1148: + " : expectedValue-->"
1149: + expectedValue);
1150: returnValue = !varValue.equals(expectedValue);
1151: // GT, NOTGT, LT, NOTLT only apply to numeric objects, so cast it to Double
1152: } else if (action.equals("GT")) {
1153: returnValue = ((new Double(expectedValue
1154: .toString())).doubleValue() < (new Double(
1155: varValue.toString())).doubleValue());
1156: } else if (action.equals("NOTGT")) {
1157: returnValue = ((new Double(expectedValue
1158: .toString())).doubleValue() >= (new Double(
1159: varValue.toString())).doubleValue());
1160: } else if (action.equals("LT")) {
1161: returnValue = ((new Double(expectedValue
1162: .toString())).doubleValue() > (new Double(
1163: varValue.toString())).doubleValue());
1164: } else if (action.equals("NOTLT")) {
1165: returnValue = ((new Double(expectedValue
1166: .toString())).doubleValue() <= (new Double(
1167: varValue.toString())).doubleValue());
1168: } else if (action.equals("ISTRUE")) {
1169: throw new JTestCaseException(
1170: "***Error: not implemented action 'ISTRUE'!");
1171: } else {
1172: throw new JTestCaseException(
1173: "***Error: asserting action is not valid!");
1174: }
1175: }
1176: }
1177: }
1178: //System.out.println("assertTESTCASE 3: returnValue-->"+returnValue);
1179: return returnValue;
1180: }
1181:
1182: /**
1183: * Returns the class name used to find the class tags in XML
1184: * @return the class name
1185: */
1186: public String getClassUnderTest() {
1187: return mClassName;
1188: }
1189:
1190: /**
1191: * Construct a new object in a given type based on a given object value.
1192: *
1193: * For primative type, please use keywords "int", "short", "long", "double", "char", "byte",
1194: * and "boolean", and the value in return Hashtable is the corresponding Object type:
1195: * "Integer", "Short", "Long", "Double", "Character", "Byte", and "Boolean".
1196: *
1197: * For "java.lang.String" the short forms "string" and "String" are also valid.
1198: * For "java.util.Date" the short forms "date" and "Date" are also valid. The format of
1199: * a valid date field depends on the current locale.
1200: *
1201: * For the other data type, please specify the full Java type name, such as "java.lang.xxx".
1202: *
1203: * If a global Prameter named "locale" is set the value of this parameter is mapped to the
1204: * used DateFormat. This enables local dependent dateFormats
1205: */
1206: private Object _convertType(Node obj, String type, int area)
1207: throws JTestCaseException {
1208: Object newVal = null;
1209: Node child = obj.getFirstChild();
1210: NodeList children = obj.getChildNodes();
1211: DateFormat lDateFormat = null;
1212: DateFormat lTimeFormat = null;
1213: try {
1214: locale = getGlobalParamLocale().toUpperCase();
1215:
1216: if (locale.equals("US")) {
1217: lDateFormat = DateFormat.getDateInstance(
1218: DateFormat.DEFAULT, Locale.US);
1219: lTimeFormat = DateFormat.getDateTimeInstance(
1220: DateFormat.DEFAULT, DateFormat.DEFAULT,
1221: Locale.US);
1222: } else if (locale.equals("UK")) {
1223: lDateFormat = DateFormat.getDateInstance(
1224: DateFormat.DEFAULT, Locale.UK);
1225: lTimeFormat = DateFormat.getDateTimeInstance(
1226: DateFormat.DEFAULT, DateFormat.DEFAULT,
1227: Locale.UK);
1228: } else if (locale.equals("CANADA")) {
1229: lDateFormat = DateFormat.getDateInstance(
1230: DateFormat.DEFAULT, Locale.CANADA);
1231: lTimeFormat = DateFormat.getDateTimeInstance(
1232: DateFormat.DEFAULT, DateFormat.DEFAULT,
1233: Locale.CANADA);
1234: } else if (locale.equals("CHINA")) {
1235: lDateFormat = DateFormat.getDateInstance(
1236: DateFormat.DEFAULT, Locale.CHINA);
1237: lTimeFormat = DateFormat.getDateTimeInstance(
1238: DateFormat.DEFAULT, DateFormat.DEFAULT,
1239: Locale.CHINA);
1240: } else if (locale.equals("JAPAN")) {
1241: lDateFormat = DateFormat.getDateInstance(
1242: DateFormat.DEFAULT, Locale.JAPAN);
1243: lTimeFormat = DateFormat.getDateTimeInstance(
1244: DateFormat.DEFAULT, DateFormat.DEFAULT,
1245: Locale.JAPAN);
1246: } else if (locale.equals("FRANCE")) {
1247: lDateFormat = DateFormat.getDateInstance(
1248: DateFormat.DEFAULT, Locale.FRANCE);
1249: lTimeFormat = DateFormat.getDateTimeInstance(
1250: DateFormat.DEFAULT, DateFormat.DEFAULT,
1251: Locale.FRANCE);
1252: } else if (locale.equals("ITALY")) {
1253: lDateFormat = DateFormat.getDateInstance(
1254: DateFormat.DEFAULT, Locale.ITALY);
1255: lTimeFormat = DateFormat.getDateTimeInstance(
1256: DateFormat.DEFAULT, DateFormat.DEFAULT,
1257: Locale.ITALY);
1258: } else if (locale.equals("TAIWAN")) {
1259: lDateFormat = DateFormat.getDateInstance(
1260: DateFormat.DEFAULT, Locale.TAIWAN);
1261: lTimeFormat = DateFormat.getDateTimeInstance(
1262: DateFormat.DEFAULT, DateFormat.DEFAULT,
1263: Locale.TAIWAN);
1264: } else if (locale.equals("GERMAN")) {
1265: lDateFormat = DateFormat.getDateInstance(
1266: DateFormat.DEFAULT, Locale.GERMAN);
1267: lTimeFormat = DateFormat.getDateTimeInstance(
1268: DateFormat.DEFAULT, DateFormat.DEFAULT,
1269: Locale.GERMAN);
1270: } else {
1271: lDateFormat = DateFormat.getDateInstance();
1272: lTimeFormat = DateFormat.getDateTimeInstance();
1273: }
1274: }
1275: // The globalParameter 'locale' is not set or is not assistet
1276: // --> set the default Locale
1277: catch (JTestCaseException jtce) {
1278: lDateFormat = DateFormat.getDateInstance();
1279: lTimeFormat = DateFormat.getDateTimeInstance();
1280: }
1281:
1282: String val = "";
1283: if (child == null) {
1284: val = "";
1285: } else if ((child.getNodeType() == Node.TEXT_NODE)
1286: && (children.getLength() == 1)) {
1287: val = child.getNodeValue();
1288: //System.out.println("_convertType: "+" val: "+val);
1289: } else {
1290: if (obj.getNodeType() == Node.ELEMENT_NODE) {
1291: // use XType
1292: try {
1293: if (area == PARAM_VALUE) {
1294: return XType.getObjectFromXML((Element) obj,
1295: type, "param");
1296: } else if (area == ASSERT_VALUE) {
1297: return XType.getObjectFromXML((Element) obj,
1298: type, "assert");
1299: }
1300: } catch (Exception e) {
1301: throw new JTestCaseException(
1302: "***Error: error constructing params: type="
1303: + type + "\nError message: "
1304: + e.getMessage());
1305: }
1306: }
1307: }
1308: // constructing from string if type is primative, otherwise using reflection
1309: try {
1310: //System.out.println("_convertType: "+type.toString()+" val: "+val);
1311: if (type.equals("int"))
1312: newVal = new Integer(val);
1313: else if (type.equals("short"))
1314: newVal = new Short(val);
1315: else if (type.equals("short"))
1316: newVal = new Short(val);
1317: else if (type.equals("long"))
1318: newVal = new Long(val);
1319: else if (type.equals("char"))
1320: newVal = new Character(val.charAt(0));
1321: else if (type.equals("byte"))
1322: newVal = new Byte(val);
1323: else if (type.equals("double"))
1324: newVal = new Double(val);
1325: else if (type.equals("boolean"))
1326: newVal = new Boolean(val);
1327: else if (type.equals("java.lang.String")
1328: || type.equals("string") || type.equals("String"))
1329: newVal = val;
1330: else if (type.equals("java.util.Date")
1331: || type.equals("date") || type.equals("Date"))
1332: newVal = lDateFormat.parse(val);
1333: else if (type.equals("Time") || type.equals("time"))
1334: newVal = lTimeFormat.parse(val);
1335: else {
1336: Class objClass = Class.forName(type);
1337: newVal = objClass.getConstructor(
1338: new Class[] { (type).getClass() }).newInstance(
1339: new Object[] { val });
1340: }
1341: } catch (Exception e) {
1342: //System.out.println("Our Error Message is: "+e);
1343: throw new JTestCaseException(
1344: "***Error: error constructing params: type=" + type
1345: + " value=" + val + "\nError message: " + e);
1346: }
1347: return newVal;
1348: }
1349:
1350: /**
1351: * Gets param/paramgroup and assert/assertgroup tags from the XML in a Hashtable
1352: * @param util XMLUtil that contains the XML representation of the params
1353: * @param path The path in the XML that contains the params
1354: * @param keypath
1355: * @param attribute Indicates if we want to read the values, type-attributes or action-attributes
1356: * @return the params in a Hashtable
1357: * @throws JTestCaseException in case of any error
1358: */
1359: private Hashtable _getGroupedParams(XMLUtil util, String path,
1360: String keypath, int attribute) throws JTestCaseException {
1361: try {
1362: Hashtable vals = _getParams(util, path + "/param", keypath,
1363: attribute);
1364: Vector groupNodes = util.getNodes(path + "/paramgroup");
1365: if (groupNodes != null) {
1366: for (int i = 0; i < groupNodes.size(); i++) {
1367: Node groupNode = (Node) groupNodes.elementAt(i);
1368: XMLUtil groupNodeUtil = new XMLUtil(groupNode);
1369: String newKeypath = null;
1370: if (keypath.compareTo("") == 0) {
1371: newKeypath = groupNodeUtil.getNamedAttrValue(
1372: "paramgroup", "name", 1);
1373: } else {
1374: newKeypath = keypath
1375: + "/"
1376: + groupNodeUtil.getNamedAttrValue(
1377: "paramgroup", "name", 1);
1378: }
1379: Hashtable groupVals = null;
1380: groupVals = _getGroupedParams(groupNodeUtil,
1381: "/paramgroup", newKeypath, attribute);
1382: if (groupVals != null) {
1383: vals.putAll(groupVals);
1384: }
1385: }
1386: }
1387: //System.out.println("Value of vals in function Hashtable _getGroupedParams"+vals);
1388: return vals;
1389: } catch (XMLUtilException e) {
1390: throw new JTestCaseException(e.getMessage());
1391: }
1392: }
1393:
1394: /**
1395: * Gets param/paramgroup and assert/assertgroup tags from the XML in a Hashtable
1396: * @param util XMLUtil that contains the XML representation of the params
1397: * @param path The path in the XML that contains the params
1398: * @param keypath
1399: * @param attribute Indicates if we want to read the values, type-attributes or action-attributes
1400: * @return the params in a Hashtable
1401: * @throws JTestCaseException in case of any error
1402: */
1403: private MultiKeyHashtable _getGroupedAsserts(XMLUtil util,
1404: String path, String keypath, int attribute)
1405: throws JTestCaseException {
1406: try {
1407: MultiKeyHashtable vals = _getAsserts(util,
1408: path + "/assert", keypath, attribute);
1409:
1410: if (vals == null) {
1411: //System.out.println("_getGroupedAsserts: "+path+" : "+keypath);
1412: }
1413:
1414: Vector groupNodes = util.getNodes(path + "/assertgroup");
1415: if (groupNodes != null) {
1416: for (int i = 0; i < groupNodes.size(); i++) {
1417: Node groupNode = (Node) groupNodes.elementAt(i);
1418: XMLUtil groupNodeUtil = new XMLUtil(groupNode);
1419: String newKeypath = null;
1420: if (keypath.compareTo("") == 0) {
1421: newKeypath = groupNodeUtil.getNamedAttrValue(
1422: "assertgroup", "name", 1);
1423: } else {
1424: newKeypath = keypath
1425: + "/"
1426: + groupNodeUtil.getNamedAttrValue(
1427: "assertgroup", "name", 1);
1428: }
1429: MultiKeyHashtable groupVals = null;
1430: groupVals = _getGroupedAsserts(groupNodeUtil,
1431: "/assertgroup", newKeypath, attribute);
1432: if (groupVals != null) {
1433: if (vals != null) {
1434: vals.putAll(groupVals);
1435: } else {
1436: vals = groupVals;
1437: }
1438:
1439: }
1440: }
1441: }
1442: //System.out.println("Value of vals in function MultiKeyHashtable _getGroupedParams "+vals);
1443: return vals;
1444: } catch (XMLUtilException e) {
1445: throw new JTestCaseException(e.getMessage());
1446: }
1447: }
1448:
1449: /**
1450: * Get all params for a given XMLUtil object and params' path into Hashtable, hashed key
1451: * is param's name, value is value of param. If the type of param is not specified in data
1452: * file (via attribute "type"), then param has "String" type. For primative type, please use
1453: * keywords "int", "short", "long", "double", "char", "byte", and "boolean", and the value
1454: * in return Hashtable is the corresponding Object type: "Integer", "Short", "Long", "Double",
1455: * "Character", "Byte", and "Boolean". For the other data type, please specify the full Java
1456: * type name, such as "java.lang.xxx".
1457: * @param util XMLUtil that contains the XML representation of the params
1458: * @param path The path in the XML that contains the params
1459: * @param keypath
1460: * @param attribute Indicates if we want to read the values, type-attributes or action-attributes
1461: * @return The params in a hashtable
1462: * @throws JTestCaseException In case of any error
1463: *
1464: */
1465: private Hashtable _getParams(XMLUtil util, String path,
1466: String keypath, int attribute) throws JTestCaseException {
1467: Hashtable vals = null;
1468: try {
1469: vals = util.getNodesHashedByNamedAttr(path, "name");
1470: //System.out.println(" The Path is: "+path+" Value of vals in function Hashtable _getParams is: "+vals.toString());
1471: } catch (XMLUtilException e) {
1472: return null;
1473: }
1474: try {
1475: Hashtable types = util.getAttrValuesHashedByNamedAttr(path,
1476: "type", "name");
1477: Hashtable returnVals = new Hashtable();
1478:
1479: if (types != null && vals != null) {
1480: for (Enumeration e = types.keys(); e.hasMoreElements();) {
1481: String key = (String) e.nextElement();
1482: String type = (String) types.get(key);
1483: //System.out.println("Key is: "+key+" Type is: "+type);
1484: // retrieve value by key
1485: Node val = (Node) vals.get(key);
1486:
1487: // convert to appropriate type
1488: Object newVal = _convertType(val, type, PARAM_VALUE);
1489:
1490: // put back to vals
1491: String keyWithPath = null;
1492: if (keypath.compareTo("") != 0) {
1493: keyWithPath = keypath + "/" + key;
1494: } else {
1495: keyWithPath = key;
1496: }
1497: if (attribute == VALUES) {
1498: returnVals.put(keyWithPath, newVal);
1499: } else if (attribute == TYPES) {
1500: returnVals.put(keyWithPath, type);
1501: }
1502: }
1503: }
1504: //System.out.println("Value of returnVals is: "+returnVals.toString()+" "+returnVals.size());
1505: return returnVals;
1506: } catch (XMLUtilException e) {
1507: throw new JTestCaseException(e.getMessage());
1508: }
1509: }
1510:
1511: /**
1512: * Get all asserts for a given XMLUtil object and asserts' path into a MultiKeyHashtable,
1513: * with keys name and action of asserts.
1514: * @param util XMLUtil that contains the XML representation of the asserts
1515: * @param path The path in the XML that contains the asserts
1516: * @param keypath
1517: * @return The asserts in a MultiKeyHashtable
1518: * @throws JTestCaseException In case of any error
1519: *
1520: */
1521: private MultiKeyHashtable _getAsserts(XMLUtil util, String path,
1522: String keypath, int attribute) throws JTestCaseException {
1523: MultiKeyHashtable vals = null;
1524: String[] attrs = { "name", "action" };
1525: try {
1526: vals = util.getNodesHashedByNamedAttrs(path, attrs);
1527: } catch (XMLUtilException e) {
1528: return null;
1529: }
1530: try {
1531: MultiKeyHashtable types = util
1532: .getAttrValuesHashedByNamedAttrs(path, "type",
1533: attrs);
1534: MultiKeyHashtable returnVals = new MultiKeyHashtable();
1535:
1536: if (types != null && vals != null) {
1537: Iterator keysIter = vals.keySet().iterator();
1538: while (keysIter.hasNext()) {
1539: String[] keys = (String[]) keysIter.next();
1540: String type = (String) types.get(keys);
1541:
1542: // retrieve value by key
1543: Node val = (Node) vals.get(keys);
1544:
1545: // convert to appropriate type
1546: Object newVal = _convertType(val, type,
1547: ASSERT_VALUE);
1548:
1549: // put back to vals
1550: String keyWithPath = null;
1551: if (keypath.compareTo("") != 0) {
1552: keyWithPath = keypath + "/" + keys[0];
1553: } else {
1554: keyWithPath = keys[0];
1555: }
1556: String[] newKeys = { keyWithPath, keys[1] };
1557: if (attribute == VALUES) {
1558: returnVals.put(newKeys, newVal);
1559: } else if (attribute == TYPES) {
1560: returnVals.put(newKeys, type);
1561: } else if (attribute == ACTIONS) {
1562: returnVals.put(newKeys, keys[1]);
1563: }
1564: }
1565: }
1566: return returnVals;
1567: } catch (XMLUtilException e) {
1568: throw new JTestCaseException(e.getMessage());
1569: }
1570: }
1571:
1572: /**
1573: * Get test cases exception name for a given method
1574: *
1575: * @param methodName - name of tested method. Defined in data file in /tests/class/method@test-case.
1576: * @return String representing the exception
1577: */
1578: public String getException(String methodName, String testCase)
1579: throws JTestCaseException {
1580: String checkedException = null;
1581: try {
1582: Node methodNode = (Node) _methodsByNameAndTestCase
1583: .get(new String[] { methodName, testCase });
1584:
1585: if (methodNode == null) {
1586: String msg = "***Error: methodname=" + methodName
1587: + " not match with data file";
1588: throw new JTestCaseException(msg);
1589: }
1590:
1591: XMLUtil nodeUtil = new XMLUtil(methodNode);
1592: Vector vnodes = new Vector();
1593: vnodes = nodeUtil.getNodes("/method/exception");
1594: if (vnodes != null) {
1595: for (int i = 0; i < vnodes.size(); i++) {
1596: if (vnodes.elementAt(i) != null) {
1597: checkedException = nodeUtil.getNodeValue(
1598: "/method/exception", 1);
1599: return checkedException;
1600: }
1601: }
1602: }
1603: return checkedException;
1604:
1605: } catch (XMLUtilException e) {
1606: throw new JTestCaseException(e.getMessage());
1607: }
1608: }
1609: }
|