Source Code Cross Referenced for TestClassParser.java in  » Test-Coverage » GroboUtils » net » sourceforge » groboutils » junit » v1 » parser » 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 » Test Coverage » GroboUtils » net.sourceforge.groboutils.junit.v1.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)TestClassParser.java
003:         */
004:
005:        package net.sourceforge.groboutils.junit.v1.parser;
006:
007:        import java.util.Vector;
008:        import java.util.Enumeration;
009:
010:        import java.io.PrintWriter;
011:        import java.io.StringWriter;
012:
013:        import java.lang.reflect.Method;
014:        import java.lang.reflect.Modifier;
015:
016:        import junit.framework.TestSuite;
017:        import junit.framework.TestCase;
018:        import junit.framework.Test;
019:
020:        import org.apache.log4j.Logger;
021:
022:        /**
023:         * Parses Test classes to discover the usable test methods.
024:         * <P>
025:         * Ripped the test method discovery code out of junit.framework.TestSuite to
026:         * allow it to have usable logic.
027:         * <P>
028:         * This is not covered under the GroboUtils license, but rather under the
029:         * JUnit license (IBM Public License).  This heading may not be totally
030:         * in line with the license, so I'll change it when I find out what needs to
031:         * be changed.
032:         *
033:         * @author     Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
034:         * @version    $Date: 2002/11/05 00:49:31 $
035:         * @since      March 28, 2002
036:         */
037:        public class TestClassParser {
038:            private static final Logger LOG = Logger
039:                    .getLogger(TestClassParser.class);
040:
041:            private Class testClass;
042:            Vector testMethods = new Vector();
043:            private Vector warnings = new Vector();
044:
045:            /**
046:             * The primary constructor, which will cause this instance to know how to
047:             * parse only the passed-in class.
048:             *
049:             * @param theClass the class to parse for testing.
050:             * @exception IllegalArgumentException if <tt>theClass</tt> is
051:             *      <tt>null</tt>.
052:             */
053:            public TestClassParser(final Class theClass) {
054:                if (theClass == null) {
055:                    throw new IllegalArgumentException("no null arguments");
056:                }
057:                this .testClass = theClass;
058:
059:                if (testClass(theClass)) {
060:                    discoverTestMethods(theClass);
061:                }
062:            }
063:
064:            //-------------------------------------------------------------------------
065:            // Public methods
066:
067:            /**
068:             * Retrieve all warnings generated during the introspection of the class,
069:             * or test creation.  If a <tt>clearWarnings()</tt> call was ever made, then
070:             * only those warnings that were encountered after the call will be
071:             * returned.
072:             *
073:             * @return an array of all warnings generated while creating the test
074:             *      array.
075:             */
076:            public String[] getWarnings() {
077:                String w[] = new String[this .warnings.size()];
078:                this .warnings.copyInto(w);
079:                return w;
080:            }
081:
082:            /**
083:             * Remove all current warnings.
084:             */
085:            public void clearWarnings() {
086:                this .warnings.removeAllElements();
087:            }
088:
089:            /**
090:             * Retrieve all public test methods discovered through inspection.
091:             *
092:             * @return all test methods.
093:             */
094:            public Method[] getTestMethods() {
095:                Method m[] = new Method[this .testMethods.size()];
096:                this .testMethods.copyInto(m);
097:                return m;
098:            }
099:
100:            /**
101:             * Get the name of the test suite.  By default, this is the class name.
102:             *
103:             * @return the name of the test suite.
104:             */
105:            public String getName() {
106:                return this .testClass.getName();
107:            }
108:
109:            /**
110:             * Get the class under test.  This will never return <tt>null</tt>, and
111:             * will always match the class passed into the constructor.
112:             *
113:             * @return the class under test.
114:             */
115:            public Class getTestClass() {
116:                return this .testClass;
117:            }
118:
119:            //-------------------------------------------------------------------------
120:            // Parse methods
121:
122:            /**
123:             * Discover if the given class is a valid testing class.
124:             *
125:             * @param theClass the class to parse for testing.
126:             * @return <tt>true</tt> if the class is a public test class, otherwise
127:             *      <tt>false</tt>.
128:             */
129:            protected boolean testClass(final Class theClass) {
130:                boolean result = true;
131:                if (!Modifier.isPublic(theClass.getModifiers())) {
132:                    warning("Class " + theClass.getName() + " is not public.");
133:                    result = false;
134:                }
135:                if (!Test.class.isAssignableFrom(theClass)) {
136:                    warning("Class " + theClass.getName()
137:                            + " does not implement " + Test.class.getName());
138:                    result = false;
139:                }
140:                return result;
141:            }
142:
143:            /**
144:             * Discover and record the test methods of the public test class
145:             * <tt>theClass</tt>.
146:             *
147:             * @param theClass the class to parse for testing.
148:             */
149:            protected void discoverTestMethods(final Class theClass) {
150:                Class super Class = theClass;
151:                Vector names = new Vector();
152:                while (Test.class.isAssignableFrom(super Class)) {
153:                    Method[] methods = super Class.getDeclaredMethods();
154:                    for (int i = 0; i < methods.length; i++) {
155:                        addTestMethod(methods[i], names);
156:                    }
157:                    super Class = super Class.getSuperclass();
158:                }
159:            }
160:
161:            /**
162:             * Adds the method <tt>m</tt> to the inner list of known test methods,
163:             * but only if it is a public test method.
164:             *
165:             * @param m the method to add.
166:             * @param names a list of method names that have already been inspected.
167:             */
168:            protected void addTestMethod(Method m, Vector names) {
169:                String name = m.getName();
170:                if (names.contains(name) || this .testMethods.contains(m)) {
171:                    return;
172:                }
173:
174:                if (isPublicTestMethod(m)) {
175:                    names.addElement(name);
176:
177:                    this .testMethods.addElement(m);
178:                } else {
179:                    // almost a test method
180:                    if (isTestMethod(m)) {
181:                        warning("Test method isn't public: " + m.getName());
182:                    }
183:                }
184:            }
185:
186:            /**
187:             * Asserts that the method is public, and that it is also a test method.
188:             *
189:             * @param m the method under scrutiny.
190:             * @return <tt>true</tt> if <tt>m</tt> is a public test method, otherwise
191:             *      <tt>false</tt>.
192:             * @see #isTestMethod( Method )
193:             */
194:            protected boolean isPublicTestMethod(Method m) {
195:                return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
196:            }
197:
198:            /**
199:             * Test if method <tt>m</tt> is a test method, which means it accepts
200:             * no parameters, returns <tt>void</tt>, and the name of the method
201:             * begins with <tt>test</tt>.
202:             *
203:             * @param m the method under scrutiny.
204:             * @return <tt>true</tt> if <tt>m</tt> is a public test method, otherwise
205:             *      <tt>false</tt>.
206:             */
207:            protected boolean isTestMethod(Method m) {
208:                String name = m.getName();
209:                Class[] parameters = m.getParameterTypes();
210:                Class returnType = m.getReturnType();
211:                return parameters.length == 0 && name.startsWith("test")
212:                        && returnType.equals(Void.TYPE);
213:            }
214:
215:            /**
216:             * Adds a warning message to the inner list of warnings.
217:             *
218:             * @param message the message describing the warning.
219:             */
220:            protected void warning(final String message) {
221:                LOG.debug("WARNING: " + message);
222:                this.warnings.addElement(message);
223:            }
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.