Data Types
JTestCase can handle all simple data types of Java. JTestCase works always with the
object data types and not with the primitive ones, i.e. java.lang.Integer instead of int.
Additionally JTestCase allows the definition of some more complex data types from
the java.util package. For most of the data types several aliases are allowed in the
type attribute. The allowed data types are (possible aliases in paranthesis):
- Integer (java.lang.Integer, integer, int, Int)
- Short (java.lang.Short, short)
- Long (java.lang.Long, long)
- Char (java.lang.Character, Character, CHAR)
- Byte (java.lang.Byte, byte)
- Float (java.lang.Float, float)
- Double (java.lang.Double, double)
- Boolean (java.lang.Boolean, boolean)
- String (java.lang.String, string)
- Date (java.text.Date, java.util.Date, date)
- java.util.Hashtable
- java.util.HashMap
- java.util.Vector
You can also get data types through refection. See subsection data types through reflection.
And use special data types. Special data type are particular java types like
-
null
value (OBJECT_NULL in the xml)
-
""
(empty strings) value (STRING_EMPTY in the xml)
-
" "
(one space string) value (STRING_SPACE in the xml)
see subsection
special data types .
Data Types through Reflection
It is possible to define your costum data type in the type attribute of a
parameter or assert tag, using the java qualified name of your type.
JTestCase will try to instantiate your class through reflection.
<method name="testGetCustomTypeInstance" test-case="instance">
<params>
<param name="customType" type="org.jtestcase.CustomType"></param>
</params>
</method>
/**
* Test the getTestCaseParameter method.
* @throws Exception
*/
public void testGetCustomTypeInstance() throws Exception {
Vector testCases = null;
HashMap params = null;
String dataFile = "test/org/jtestcase/data/GetParamTest1.xml";
_jtestcase = new JTestCase(dataFile, "GetParamTest");
testCases = _jtestcase.getTestCasesInstancesInMethod("testGetCustomTypeInstance");
// for each test case
for (int i=0; i<testCases.size(); i++) {
TestCaseInstance testCase = ((TestCaseInstance) testCases.elementAt(i));
params = testCase.getTestCaseParams();
CustomType customType = (CustomType) params.get("customType");
System.out.println(customType.toString());
}
}
|
Special Data Types
/**
*
*
*/
public void testNullType() {
Vector testCases = null;
HashMap params = null;
try {
String dataFile = "test/org/jtestcase/data/GetParamTest1.xml";
_jtestcase = new JTestCase(dataFile, "GetParamTest");
} catch (Exception e) {
e.printStackTrace();
fail("cannot read test data file");
}
try {
testCases = _jtestcase
.getTestCasesInstancesInMethod("testNullType");
} catch (Exception e) {
e.printStackTrace();
fail("cannot get names of test cases");
}
// for each test case
for (int i = 0; i < testCases.size(); i++) {
TestCaseInstance testCase = ((TestCaseInstance) testCases
.elementAt(i));
try {
params = testCase.getTestCaseParams();
} catch (Exception e) {
e.printStackTrace();
fail("cannot get params");
}
try {
Object def = params.get("objectNull");
assertTrue("object not null", def == null);
} catch (Exception e) {
e.printStackTrace();
fail("cannot parse data types");
}
}
}
|
- STRING_EMPTY
- STRING_SPACE
/**
* Test the correct reading of string special types.
*/
public void testStringTypes() {
Vector testCases = null;
HashMap params = null;
try {
String dataFile = "test/org/jtestcase/data/GetParamTest1.xml";
_jtestcase = new JTestCase(dataFile, "GetParamTest");
} catch (Exception e) {
e.printStackTrace();
fail("cannot read test data file");
}
try {
testCases = _jtestcase
.getTestCasesInstancesInMethod("testStringTypes");
} catch (Exception e) {
e.printStackTrace();
fail("cannot get names of test cases");
}
// for each test case
for (int i = 0; i < testCases.size(); i++) {
TestCaseInstance testCase = ((TestCaseInstance) testCases
.elementAt(i));
try {
params = testCase.getTestCaseParams();
} catch (Exception e) {
e.printStackTrace();
fail("cannot get params");
}
try {
String empty = (String) params.get("stringEmpty");
assertTrue("string not empty ", "".equals(empty));
String string_space = (String) params.get("stringSpace");
assertTrue("string space not correct: ", " "
.equals(string_space));
} catch (Exception e) {
e.printStackTrace();
fail("cannot parse data types");
}
}
}
|
Date and Time Types
Date and time types are worth an additional remark. At first, in both cases the constructed
Java object is of type java.util.Date. The difference is that in the case of Date you specify
only the date with not time and in the Time case you have to specify date and time.
The definition of dates and times are locale dependent. To handle this in JTestCase it is possible
to define the locale used in the the XML with a global parameter
named "locale". Here you can define the local with a complete locale string defining language and
country, e.g. "en_US" for english in the US variant. If you do not define this global parameter the locale
of your system is used. But be aware that this might cause problems when changing computers or developing in
teams where you can't rely on a homogenous locale on all computers used.