Source Code Cross Referenced for GroovyInvoker.java in  » Testing » webtest » com » canoo » webtest » extension » groovy » 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 » webtest » com.canoo.webtest.extension.groovy 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright © 2005-2007 Canoo Engineering AG, Switzerland.
002:        package com.canoo.webtest.extension.groovy;
003:
004:        import groovy.lang.Binding;
005:        import groovy.lang.GroovyShell;
006:
007:        import org.apache.commons.lang.StringUtils;
008:        import org.apache.log4j.Level;
009:        import org.apache.log4j.Logger;
010:        import org.codehaus.groovy.control.CompilationFailedException;
011:
012:        import com.canoo.webtest.engine.StepExecutionException;
013:        import com.canoo.webtest.engine.StepFailedException;
014:        import com.canoo.webtest.steps.Step;
015:
016:        /**
017:         * @author Unknown
018:         * @author Marc Guillemot
019:         */
020:        class GroovyInvoker {
021:            private static final Logger LOG = Logger
022:                    .getLogger(GroovyInvoker.class);
023:            /**
024:             * Key used to save the binding as webtest properties and allow reuse across different Groovy steps of a webtest
025:             */
026:            private static final String KEY_GROOVY_BINDING = GroovyInvoker.class
027:                    .getName()
028:                    + "#binding";
029:
030:            public void doExecute(final Step step, final String script) {
031:                final Binding variables = getBinding(step);
032:
033:                // set standard variable (need to be set even if binding is reused as these 2 variables are bound to current step)
034:                variables.setVariable("step", step);
035:                final DummyPrinter out = new DummyPrinter(step, Level.INFO);
036:                variables.setVariable("out", out);
037:                final GroovyShell shell = new GroovyShell(getClass()
038:                        .getClassLoader(), variables);
039:                try {
040:                    LOG.debug("Evaluating script: "
041:                            + StringUtils.abbreviate(script, 20));
042:                    shell.evaluate(script);
043:                } catch (final CompilationFailedException e) {
044:                    LOG.error("CompilationFailedException", e);
045:                    throw new StepExecutionException(
046:                            "Cannot compile groovy code: " + script, step, e);
047:                } catch (final AssertionError e) {
048:                    LOG.info("AssertionError", e);
049:                    throw new StepFailedException(
050:                            "Assertion failed within groovy code: " + script,
051:                            step);
052:                } catch (final RuntimeException e) {
053:                    LOG.error("RuntimeException", e);
054:                    throw new StepExecutionException("Error invoking groovy: "
055:                            + e.getMessage(), step, e);
056:                } finally {
057:                    out.flush();
058:                }
059:            }
060:
061:            /**
062:             * Gets the binding to use for step execution. Retrieve the binding for this webtest if one exists
063:             * or create a new one if this is the first groovy step of this webtest
064:             * @param step the current step
065:             * @return the binding to use for script execution
066:             */
067:            Binding getBinding(final Step step) {
068:                Binding binding = (Binding) step.getWebtestProperties().get(
069:                        KEY_GROOVY_BINDING);
070:                if (binding == null) {
071:                    LOG
072:                            .info("No existing binding for this webtest, creating a new one");
073:                    binding = new Binding();
074:                    step.getWebtestProperties()
075:                            .put(KEY_GROOVY_BINDING, binding);
076:                } else {
077:                    LOG.info("Reusing existing binding of this webtest.");
078:                }
079:
080:                return binding;
081:            }
082:
083:        }
084:
085:        /**
086:         * A small bridge between groovy output and Step's logger. This doesn't need to
087:         * be a {@link java.io.PrintWriter}: an object with methods <code>print</code>
088:         * and <code>println</code> is enough.
089:         */
090:        class DummyPrinter {
091:            private final Logger fLogger;
092:            private final Level fLevel;
093:            private final StringBuffer fBuffer = new StringBuffer();
094:
095:            DummyPrinter(final Step step, final Level level) {
096:                fLogger = Logger.getLogger(step.getClass());
097:                fLevel = level;
098:            }
099:
100:            void println(final Object object) {
101:                print(object);
102:                final String message = fBuffer.toString();
103:                fBuffer.setLength(0);
104:                fLogger.log(fLevel, message);
105:            }
106:
107:            void print(final Object object) {
108:                fBuffer.append(String.valueOf(object));
109:            }
110:
111:            /**
112:             * prints the remaining message (if any)
113:             */
114:            void flush() {
115:                if (fBuffer.length() > 0)
116:                    println(""); // forces print of the current buffer
117:            }
118:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.