Source Code Cross Referenced for CalculatorTest.java in  » Testing » jtestcase » sample » tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jtestcase » sample.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.