001: /*
002: * This program is licensed under Common Public License Version 0.5.
003: *
004: * For License Information and conditions of use, see "LICENSE" in packaged
005: *
006: */
007: package sample.tests;
008:
009: import java.io.FileNotFoundException;
010: import java.util.HashMap;
011: import java.util.Vector;
012:
013: import junit.framework.Test;
014: import junit.framework.TestCase;
015: import junit.framework.TestSuite;
016:
017: import org.jtestcase.JTestCase;
018: import org.jtestcase.JTestCaseException;
019: import org.jtestcase.TestCaseInstance;
020:
021: /**
022: * Example for the use of JTestCase
023: *
024: * @author <a href="mailto:yuqingwang_99@yahoo.com">Yuqing Wang</a>
025: * @author <a href="mailto:faustothegrey@sourceforge.net">Fausto Lelli</a>
026: *
027: */
028: public class CalculatorTest extends TestCase {
029:
030: /**
031: * JTestCase instance to be used in this example
032: */
033: private JTestCase _jtestcase = null;
034:
035: /**
036: * Main method if you want to run the example from command line
037: *
038: * @param args
039: * command line parameters
040: */
041: public static void main(String[] args) {
042: junit.textui.TestRunner.run(suite());
043: }
044:
045: /**
046: * Read the XML file with the test data and build the JTestCase instance
047: *
048: * @param name
049: */
050: public CalculatorTest(String name) {
051: super (name);
052:
053: String dataFile = "sample/tests/test-data.xml";
054: try {
055: _jtestcase = new JTestCase(dataFile, "Calculator");
056: } catch (FileNotFoundException e) {
057: e.printStackTrace();
058: } catch (JTestCaseException e) {
059: e.printStackTrace();
060: }
061:
062: }
063:
064: /**
065: * Suite method that collects all test cases
066: *
067: * @return The test suite
068: */
069: public static Test suite() {
070: TestSuite retval = new TestSuite();
071: retval.addTest(new CalculatorTest("testCalculate"));
072: return (retval);
073: }
074:
075: /**
076: * Tests for the calculate method of Calculater.java This example shows how: -
077: * to extract the number of test cases from JTestCase and to build a loop
078: * over them - to extract the test data for a special test case - to use the
079: * test data in the test - to assert the test results with JTestCase methods
080: */
081: public void testCalculate() {
082:
083: if (_jtestcase == null)
084: fail("couldn't read xml definition file");
085:
086: final String METHOD = "calculate";
087:
088: Vector testCases = null;
089: try {
090: testCases = _jtestcase
091: .getTestCasesInstancesInMethod(METHOD);
092: } catch (JTestCaseException e) {
093: e.printStackTrace();
094: fail("error parsing xml file for calculate method "
095: + METHOD);
096: }
097:
098: // For each test case
099:
100: for (int i = 0; i < testCases.size(); i++) {
101:
102: // Retrieve name of test case
103:
104: TestCaseInstance testCase = (TestCaseInstance) testCases
105: .elementAt(i);
106:
107: try {
108:
109: // Get hashed params for this test case
110:
111: HashMap params = testCase.getTestCaseParams();
112:
113: Integer var1Int = (Integer) params.get("var1");
114: Integer var2Int = (Integer) params.get("var2");
115: String opt = (String) params.get("opt");
116:
117: int var1 = var1Int.intValue();
118: int var2 = var2Int.intValue();
119:
120: // All testing related to Calculate here
121: Calculator calculator = new Calculator();
122: int result = calculator.calculate(var1, var2, opt);
123:
124: // Asserting result
125: boolean succeed = testCase.assertTestVariable("result",
126: (new Integer(result)));
127:
128: assertTrue(testCase.getFailureReason(), succeed);
129:
130: } catch (JTestCaseException e) {
131:
132: // An error as occurred while processing JTestCase input
133: System.err.print("Error executing test case "
134: + testCase.getTestCaseName());
135: e.printStackTrace();
136:
137: // Here you have a choice, 1) skip to the next test case ...
138: continue;
139:
140: // Or you can fail all the test cases for this method
141: // *uncomment the following*
142: /*
143: * fail("Error in test case , failing all test cases for method " + METHOD);
144: */
145: }
146: }
147: }
148: }
|