Source Code Cross Referenced for EMBTestRunner.java in  » J2EE » JOnAS-4.8.6 » com » ibm » emb » junit » 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 » J2EE » JOnAS 4.8.6 » com.ibm.emb.junit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.ibm.emb.junit;
002:
003:        import java.io.BufferedWriter;
004:        import java.util.Enumeration;
005:
006:        import junit.framework.AssertionFailedError;
007:        import junit.framework.Test;
008:        import junit.framework.TestFailure;
009:        import junit.framework.TestResult;
010:        import junit.framework.TestSuite;
011:        import junit.textui.ResultPrinter;
012:        import junit.textui.TestRunner;
013:
014:        /**
015:         * An extension of junit.textui.TestRunner which overrides several functions to
016:         * provide logging of failed tests to configurable files.
017:         */
018:        public class EMBTestRunner extends TestRunner {
019:
020:            protected EMBTestConfiguration embTestConfig = null;
021:
022:            ResultPrinter rprinter = new ResultPrinter(System.out);
023:
024:            int fColumn = 0;
025:
026:            /**
027:             * overwritten method
028:             */
029:            public synchronized void addError(Test test, Throwable t) {
030:                super .addError(test, t);
031:            }
032:
033:            /**
034:             * overwritten method
035:             */
036:            public synchronized void addFailure(Test test,
037:                    AssertionFailedError t) {
038:                super .addFailure(test, t);
039:            }
040:
041:            /**
042:             * Constructs a TestRunner.
043:             */
044:            public EMBTestRunner() {
045:                embTestConfig = EMBTestConfiguration.instance();
046:            }
047:
048:            /**
049:             * Creates the TestResult to be used for the test run.
050:             */
051:            protected TestResult createTestResult() {
052:                return new TestResult();
053:            }
054:
055:            /**
056:             * overwritten doRun method which also logs failed tests to a file
057:             */
058:            public TestResult doRun(Test suite, boolean wait) {
059:                TestResult result = createTestResult();
060:                result.addListener(this );
061:                long startTime = System.currentTimeMillis();
062:                suite.run(result);
063:                long endTime = System.currentTimeMillis();
064:                long runTime = endTime - startTime;
065:                System.out.println("Time: " + elapsedTimeAsString(runTime));
066:                try {
067:                    fileWriter().newLine();
068:                    fileWriter().write("Time: " + elapsedTimeAsString(runTime));
069:                    fileWriter().newLine();
070:                    print(result);
071:                    System.out.println();
072:                    fileWriter().newLine();
073:                } catch (java.io.IOException e) {
074:                    System.out.println("Error writing result in "
075:                            + this .getClass().getName());
076:                }
077:                pause(wait);
078:                return result;
079:            }
080:
081:            public synchronized void startTest(Test test) {
082:                super .startTest(test);
083:                try {
084:                    if (fColumn++ >= 80) {
085:                        fileWriter().newLine();
086:                        fColumn = 0;
087:                    }
088:                } catch (java.io.IOException e) {
089:                    System.out.println("Error writing result in "
090:                            + this .getClass().getName());
091:                }
092:            }
093:
094:            /**
095:             * overwritten method which logs summary to a file and prints summary to the
096:             * standard output
097:             */
098:            public synchronized void print(TestResult result) {
099:                try {
100:                    fileHeader(result);
101:                    fileErrors(result);
102:                    fileFailures(result);
103:                    fileWriter().flush();
104:                } catch (java.io.IOException e) {
105:                    System.out.println("Error writng result in "
106:                            + this .getClass().getName());
107:                }
108:            }
109:
110:            /**
111:             * overwritten method which logs errors to a file and prints the errors to
112:             * the standard output
113:             */
114:            public void fileErrors(TestResult result)
115:                    throws java.io.IOException {
116:                if (result.errorCount() != 0) {
117:                    if (result.errorCount() == 1) {
118:                        fileWriter().write(
119:                                "There was " + result.errorCount() + " error:");
120:                        fileWriter().newLine();
121:                    } else {
122:                        fileWriter().write(
123:                                "There were " + result.errorCount()
124:                                        + " errors:");
125:                        fileWriter().newLine();
126:                    }
127:                    int i = 1;
128:                    for (Enumeration e = result.errors(); e.hasMoreElements(); i++) {
129:                        TestFailure failure = (TestFailure) e.nextElement();
130:                        fileWriter().write(i + ") " + failure.failedTest());
131:                        fileWriter().newLine();
132:                        fileWriter().write(
133:                                getFilteredTrace(failure.thrownException()));
134:                        fileWriter().newLine();
135:                    }
136:                }
137:            }
138:
139:            public void endTest(Test test) {
140:            }
141:
142:            /**
143:             * overwritten method which logs failures to a file and prints failures to
144:             * the standard output
145:             */
146:            public void fileFailures(TestResult result)
147:                    throws java.io.IOException {
148:                if (result.failureCount() != 0) {
149:                    if (result.failureCount() == 1) {
150:                        fileWriter().write(
151:                                "There was " + result.failureCount()
152:                                        + " failure:");
153:                        fileWriter().newLine();
154:                    } else {
155:                        fileWriter().write(
156:                                "There were " + result.failureCount()
157:                                        + " failures:");
158:                        fileWriter().newLine();
159:                    }
160:                    int i = 1;
161:                    for (Enumeration e = result.failures(); e.hasMoreElements(); i++) {
162:                        TestFailure failure = (TestFailure) e.nextElement();
163:                        fileWriter().write(i + ") " + failure.failedTest());
164:                        fileWriter().newLine();
165:                        Throwable t = failure.thrownException();
166:                        fileWriter().write(
167:                                getFilteredTrace(failure.thrownException()));
168:                        fileWriter().newLine();
169:                    }
170:                }
171:            }
172:
173:            /**
174:             * overwritten method which prints the header to a file and prints the
175:             * header of the report
176:             */
177:            public void fileHeader(TestResult result)
178:                    throws java.io.IOException {
179:                if (result.wasSuccessful()) {
180:                    fileWriter().newLine();
181:                    fileWriter().write("OK");
182:                    fileWriter().write(" (" + result.runCount() + " tests)");
183:                    fileWriter().newLine();
184:
185:                    // added to print summary to console
186:                    System.out.println();
187:                    System.out.println("OK (" + result.runCount() + " tests)");
188:                    System.out.println();
189:
190:                } else {
191:                    fileWriter().newLine();
192:                    fileWriter().write("FAILURES!!!");
193:                    fileWriter().newLine();
194:                    fileWriter().write(
195:                            "Tests run: " + result.runCount() + ",  Failures: "
196:                                    + result.failureCount() + ",  Errors: "
197:                                    + result.errorCount());
198:                    fileWriter().newLine();
199:
200:                    // print to console
201:                    System.out.println("FAILURES!!!");
202:                    System.out.println("Tests run: " + result.runCount()
203:                            + ", Failures: " + result.failureCount()
204:                            + ", Errors: " + result.errorCount());
205:                    System.out.println();
206:                }
207:            }
208:
209:            /**
210:             * overwritten run method which also logs errors to a file Runs a single
211:             * test and collects its results. This method can be used to start a test
212:             * run from your program.
213:             * 
214:             * <pre>
215:             * 
216:             * public static void main(String[] args) {
217:             *     com.ibm.emb.meb.junit.EMBTestRunner.run(suite());
218:             * }
219:             * </pre>
220:             */
221:            static public TestResult run(Test suite) {
222:                EMBTestRunner aTestRunner = new EMBTestRunner();
223:                return aTestRunner.doRun(suite, false);
224:            }
225:
226:            /**
227:             * Runs a suite extracted from a TestCase subclass.
228:             */
229:            static public void run(Class testClass) {
230:                run(new TestSuite(testClass));
231:            }
232:
233:            /**
234:             * overwritten runAndWait method Runs a single test and waits until the user
235:             * types RETURN.
236:             */
237:            static public void runAndWait(Test suite) {
238:                EMBTestRunner aTestRunner = new EMBTestRunner();
239:                aTestRunner.doRun(suite, true);
240:            }
241:
242:            /**
243:             * fileWriter used to log the results obtained from the EMBTestConfiguration
244:             * singleton
245:             */
246:            protected BufferedWriter fileWriter() {
247:                return embTestConfig.getJunitWriter();
248:            }
249:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.