Source Code Cross Referenced for RunTag.java in  » Library » Apache-commons-jelly-1.0-src » org » apache » commons » jelly » tags » 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 » Library » Apache commons jelly 1.0 src » org.apache.commons.jelly.tags.junit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002,2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.jelly.tags.junit;
017:
018:        import java.io.PrintWriter;
019:        import java.io.StringWriter;
020:
021:        import junit.framework.AssertionFailedError;
022:        import junit.framework.Test;
023:        import junit.framework.TestListener;
024:        import junit.framework.TestResult;
025:
026:        import org.apache.commons.jelly.JellyTagException;
027:        import org.apache.commons.jelly.MissingAttributeException;
028:        import org.apache.commons.jelly.TagSupport;
029:        import org.apache.commons.jelly.XMLOutput;
030:        import org.apache.commons.logging.Log;
031:        import org.apache.commons.logging.LogFactory;
032:        import org.xml.sax.SAXException;
033:        import org.xml.sax.helpers.AttributesImpl;
034:
035:        /**
036:         * This tag will run the given Test which could be an individual TestCase or a TestSuite.
037:         * The TestResult can be specified to capture the output, otherwise the results are output
038:         * as XML so that they can be formatted in some custom manner.
039:         *
040:         * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
041:         * @version $Revision: 155420 $
042:         */
043:        public class RunTag extends TagSupport {
044:
045:            /** The Log to which logging calls will be made. */
046:            private static final Log log = LogFactory.getLog(RunTag.class);
047:
048:            private Test test;
049:            private TestResult result;
050:            private TestListener listener;
051:
052:            // Tag interface
053:            //-------------------------------------------------------------------------
054:            public void doTag(XMLOutput output) throws JellyTagException {
055:                Test test = getTest();
056:                if (test == null) {
057:                    test = (Test) context
058:                            .getVariable("org.apache.commons.jelly.junit.suite");
059:                }
060:                if (test == null) {
061:                    throw new MissingAttributeException("test");
062:                }
063:                TestResult result = getResult();
064:                if (result == null) {
065:                    result = createResult(output);
066:                }
067:                TestListener listener = getListener();
068:                if (listener == null) {
069:                    listener = createTestListener(output);
070:                }
071:                result.addListener(listener);
072:                test.run(result);
073:            }
074:
075:            // Properties
076:            //-------------------------------------------------------------------------
077:
078:            /**
079:             * Returns the TestResult used to capture the output of the test.
080:             * @return TestResult
081:             */
082:            public TestResult getResult() {
083:                return result;
084:            }
085:
086:            /**
087:             * Returns the Test to be ran.
088:             * @return Test
089:             */
090:            public Test getTest() {
091:                return test;
092:            }
093:
094:            /**
095:             * Sets the JUnit TestResult used to capture the results of the tst
096:             * @param result The TestResult to use
097:             */
098:            public void setResult(TestResult result) {
099:                this .result = result;
100:            }
101:
102:            /**
103:             * Sets the JUnit Test to run which could be an individual test or a TestSuite
104:             * @param test The test to run
105:             */
106:            public void setTest(Test test) {
107:                this .test = test;
108:            }
109:
110:            /**
111:             * Returns the listener.
112:             * @return TestListener
113:             */
114:            public TestListener getListener() {
115:                return listener;
116:            }
117:
118:            /**
119:             * Sets the TestListener.to be used to format the output of running the unit test cases
120:             * @param listener The listener to set
121:             */
122:            public void setListener(TestListener listener) {
123:                this .listener = listener;
124:            }
125:
126:            // Implementation methods
127:            //-------------------------------------------------------------------------
128:
129:            /**
130:             * Factory method to create a new TestResult to capture the output of
131:             * the test cases
132:             */
133:            protected TestResult createResult(XMLOutput output) {
134:                return new TestResult();
135:            }
136:
137:            /**
138:             * Factory method to create a new TestListener to capture the output of
139:             * the test cases
140:             */
141:            protected TestListener createTestListener(final XMLOutput output) {
142:                return new TestListener() {
143:                    public void addError(Test test, Throwable t) {
144:                        try {
145:                            output.startElement("error");
146:
147:                            output.startElement("message");
148:                            output.write(t.getMessage());
149:                            output.endElement("message");
150:
151:                            output.startElement("stack");
152:                            output.write(stackTraceToString(t));
153:                            output.endElement("stack");
154:
155:                            output.endElement("error");
156:                        } catch (SAXException e) {
157:                            handleSAXException(e);
158:                        }
159:                    }
160:
161:                    public void addFailure(Test test, AssertionFailedError t) {
162:                        try {
163:                            output.startElement("failure");
164:
165:                            output.startElement("message");
166:                            output.write(t.getMessage());
167:                            output.endElement("message");
168:
169:                            output.startElement("stack");
170:                            output.write(stackTraceToString(t));
171:                            output.endElement("stack");
172:
173:                            output.endElement("failure");
174:                        } catch (SAXException e) {
175:                            handleSAXException(e);
176:                        }
177:                    }
178:
179:                    public void endTest(Test test) {
180:                        try {
181:                            output.endElement("test");
182:                        } catch (SAXException e) {
183:                            handleSAXException(e);
184:                        }
185:                    }
186:
187:                    public void startTest(Test test) {
188:                        try {
189:                            String name = test.toString();
190:                            AttributesImpl attributes = new AttributesImpl();
191:                            attributes.addAttribute("", "name", "name",
192:                                    "CDATA", name);
193:
194:                            output.startElement("test", attributes);
195:                        } catch (SAXException e) {
196:                            handleSAXException(e);
197:                        }
198:                    }
199:                };
200:            }
201:
202:            /**
203:             * @return the stack trace as a String
204:             */
205:            protected String stackTraceToString(Throwable t) {
206:                StringWriter writer = new StringWriter();
207:                t.printStackTrace(new PrintWriter(writer));
208:                return writer.toString();
209:            }
210:
211:            /**
212:             * Handles SAX Exceptions
213:             */
214:            protected void handleSAXException(SAXException e) {
215:                log.error("Caught: " + e, e);
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.