Source Code Cross Referenced for TestSupport.java in  » Scripting » groovy-1.0 » org » codehaus » groovy » classgen » 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 » Scripting » groovy 1.0 » org.codehaus.groovy.classgen 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.codehaus.groovy.classgen;
002:
003:        /*
004:         $Id: TestSupport.java 3104 2005-11-13 16:42:14Z blackdrag $
005:
006:         Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
007:
008:         Redistribution and use of this software and associated documentation
009:         ("Software"), with or without modification, are permitted provided
010:         that the following conditions are met:
011:
012:         1. Redistributions of source code must retain copyright
013:         statements and notices.  Redistributions must also contain a
014:         copy of this document.
015:
016:         2. Redistributions in binary form must reproduce the
017:         above copyright notice, this list of conditions and the
018:         following disclaimer in the documentation and/or other
019:         materials provided with the distribution.
020:
021:         3. The name "groovy" must not be used to endorse or promote
022:         products derived from this Software without prior written
023:         permission of The Codehaus.  For written permission,
024:         please contact info@codehaus.org.
025:
026:         4. Products derived from this Software may not be called "groovy"
027:         nor may "groovy" appear in their names without prior written
028:         permission of The Codehaus. "groovy" is a registered
029:         trademark of The Codehaus.
030:
031:         5. Due credit should be given to The Codehaus -
032:         http://groovy.codehaus.org/
033:
034:         THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
035:         ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
036:         NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
037:         FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
038:         THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
039:         INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
040:         (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
041:         SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
042:         HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
043:         STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
044:         ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
045:         OF THE POSSIBILITY OF SUCH DAMAGE.
046:
047:         */
048:
049:        import groovy.lang.Binding;
050:        import groovy.lang.GroovyClassLoader;
051:        import groovy.lang.GroovyCodeSource;
052:        import groovy.lang.GroovyObject;
053:        import groovy.lang.Script;
054:        import groovy.util.GroovyTestCase;
055:
056:        import java.beans.BeanInfo;
057:        import java.beans.Introspector;
058:        import java.beans.PropertyDescriptor;
059:        import java.io.File;
060:        import java.lang.reflect.Field;
061:        import java.lang.reflect.InvocationTargetException;
062:        import java.lang.reflect.Method;
063:        import java.security.AccessController;
064:        import java.security.PrivilegedAction;
065:
066:        import org.codehaus.groovy.ast.ClassNode;
067:        import org.codehaus.groovy.ast.CompileUnit;
068:        import org.codehaus.groovy.ast.FieldNode;
069:        import org.codehaus.groovy.ast.ModuleNode;
070:        import org.codehaus.groovy.ast.expr.Expression;
071:        import org.codehaus.groovy.ast.expr.FieldExpression;
072:        import org.codehaus.groovy.ast.expr.MethodCallExpression;
073:        import org.codehaus.groovy.ast.stmt.ExpressionStatement;
074:        import org.codehaus.groovy.control.CompilerConfiguration;
075:        import org.codehaus.groovy.runtime.InvokerHelper;
076:        import org.objectweb.asm.Opcodes;
077:
078:        /**
079:         * Base class for test cases
080:         * 
081:         * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
082:         * @version $Revision: 3104 $
083:         */
084:        public class TestSupport extends GroovyTestCase implements  Opcodes {
085:
086:            protected static boolean DUMP_CLASS = false;
087:
088:            // ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
089:            ClassLoader parentLoader = getClass().getClassLoader();
090:            protected GroovyClassLoader loader = (GroovyClassLoader) AccessController
091:                    .doPrivileged(new PrivilegedAction() {
092:                        public Object run() {
093:                            return new GroovyClassLoader(parentLoader);
094:                        }
095:                    });
096:            CompileUnit unit = new CompileUnit(loader,
097:                    new CompilerConfiguration());
098:            ModuleNode module = new ModuleNode(unit);
099:
100:            protected Class loadClass(ClassNode classNode) {
101:                classNode.setModule(module);
102:                Class fooClass = loader.defineClass(classNode, classNode
103:                        .getName()
104:                        + ".groovy", "groovy.testSupport");
105:                return fooClass;
106:            }
107:
108:            protected void assertSetProperty(Object bean, String property,
109:                    Object newValue) throws Exception {
110:                PropertyDescriptor descriptor = getDescriptor(bean, property);
111:                Method method = descriptor.getWriteMethod();
112:                assertTrue("has setter method", method != null);
113:
114:                Object[] args = { newValue };
115:                Object value = invokeMethod(bean, method, args);
116:
117:                assertEquals("should return null", null, value);
118:
119:                assertGetProperty(bean, property, newValue);
120:            }
121:
122:            protected void assertGetProperty(Object bean, String property,
123:                    Object expected) throws Exception {
124:                PropertyDescriptor descriptor = getDescriptor(bean, property);
125:                Method method = descriptor.getReadMethod();
126:                assertTrue("has getter method", method != null);
127:
128:                Object[] args = {};
129:                Object value = invokeMethod(bean, method, args);
130:
131:                /*
132:                System.out.println("Expected: " + expected);
133:                System.out.println("Value: " + value);
134:                
135:                if (expected == null) { System.out.println("Expected is null"); }
136:                if (value == null) { System.out.println("value is null"); }
137:                 */
138:
139:                assertEquals("property value", expected, value);
140:            }
141:
142:            protected Object invokeMethod(Object bean, Method method,
143:                    Object[] args) throws Exception {
144:                try {
145:                    return method.invoke(bean, args);
146:                } catch (InvocationTargetException e) {
147:                    fail("InvocationTargetException: " + e.getTargetException());
148:                    return null;
149:                }
150:            }
151:
152:            protected PropertyDescriptor getDescriptor(Object bean,
153:                    String property) throws Exception {
154:                BeanInfo info = Introspector.getBeanInfo(bean.getClass());
155:                PropertyDescriptor[] descriptors = info
156:                        .getPropertyDescriptors();
157:                for (int i = 0; i < descriptors.length; i++) {
158:                    PropertyDescriptor descriptor = descriptors[i];
159:                    if (descriptor.getName().equals(property)) {
160:                        return descriptor;
161:                    }
162:                }
163:                fail("Could not find property: " + property + " on bean: "
164:                        + bean);
165:                return null;
166:            }
167:
168:            protected void assertField(Class aClass, String name,
169:                    int modifiers, ClassNode type) throws Exception {
170:                Field field = aClass.getDeclaredField(name);
171:
172:                assertTrue("Found field called: " + name, field != null);
173:                assertEquals("Name", name, field.getName());
174:                assertEquals("Type", type.getName(), field.getType().getName());
175:                assertEquals("Modifiers", modifiers, field.getModifiers());
176:            }
177:
178:            protected ExpressionStatement createPrintlnStatement(
179:                    Expression expression) throws NoSuchFieldException {
180:                return new ExpressionStatement(new MethodCallExpression(
181:                        new FieldExpression(FieldNode.newStatic(System.class,
182:                                "out")), "println", expression));
183:            }
184:
185:            /**
186:             * Asserts that the script runs without any exceptions
187:             */
188:            protected void assertScript(String text) throws Exception {
189:                assertScript(text, getTestClassName());
190:            }
191:
192:            protected void assertScript(final String text,
193:                    final String scriptName) throws Exception {
194:                log.info("About to execute script");
195:                log.info(text);
196:                GroovyCodeSource gcs = (GroovyCodeSource) AccessController
197:                        .doPrivileged(new PrivilegedAction() {
198:                            public Object run() {
199:                                return new GroovyCodeSource(text, scriptName,
200:                                        "/groovy/testSupport");
201:                            }
202:                        });
203:                Class groovyClass = loader.parseClass(gcs);
204:                Script script = InvokerHelper.createScript(groovyClass,
205:                        new Binding());
206:                script.run();
207:            }
208:
209:            protected void assertScriptFile(String fileName) throws Exception {
210:                log.info("About to execute script: " + fileName);
211:
212:                Class groovyClass = loader.parseClass(new GroovyCodeSource(
213:                        new File(fileName)));
214:                Script script = InvokerHelper.createScript(groovyClass,
215:                        new Binding());
216:                script.run();
217:            }
218:
219:            protected GroovyObject compile(String fileName) throws Exception {
220:                Class groovyClass = loader.parseClass(new GroovyCodeSource(
221:                        new File(fileName)));
222:
223:                GroovyObject object = (GroovyObject) groovyClass.newInstance();
224:
225:                assertTrue(object != null);
226:
227:                return object;
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.