Package groovy.lang

Examples of groovy.lang.Script


    }

    public void testNewMethod() throws Exception {
        GroovyShell shell = new GroovyShell();
        Binding binding = new Binding();
        Script script = shell.parse (
                "mappings = {\n" +
                "    \"/$controller/$action?/$id?\" { \n" +
                "        constraints {\n" +
                "            id(matches:/\\d+/)\n" +
                "        }\n" +
                "    }\n" +
                "}\n");

        script.setBinding(binding);
        script.run();

        Closure closure = (Closure) binding.getVariable("mappings");
        List mappings = evaluator.evaluateMappings(closure);

        assertEquals(1, mappings.size());
View Full Code Here


        assertEquals("234", mapping.match("/blog/test/234").getId());
    }

    public void testOldMethod() throws Exception {
        GroovyShell shell = new GroovyShell();
        Script script = shell.parse (
                "mappings {\n" +
                "    \"/$controller/$action?/$id?\" { \n" +
                "        constraints {\n" +
                "            id(matches:/\\d+/)\n" +
                "        }\n" +
                "    }\n" +
                "}\n");

        DefaultUrlMappingEvaluator evaluator = new DefaultUrlMappingEvaluator(new MockServletContext("/test"));
        List mappings = evaluator.evaluateMappings(script.getClass());
        assertEquals(1, mappings.size());
        assertNull(((UrlMapping) mappings.get(0)).getActionName());
        assertNull(((UrlMapping) mappings.get(0)).getControllerName());
        assertEquals("(*)",((UrlMapping) mappings.get(0)).getUrlData().getTokens()[0]);
        assertEquals("(*)?",((UrlMapping) mappings.get(0)).getUrlData().getTokens()[1]);
View Full Code Here

    public void testResourceMappingsWithVersionAndNamespace() throws Exception {
        GroovyShell shell = new GroovyShell();
        Binding binding = new Binding();
        // Resource Entry: "/api/foo"(resources:"foo", version:'1.0', namespace:'v1')
        Script script = shell.parse("mappings = {\n" +
                "\"/api/foo\"(resources: 'foo', version: '1.0', namespace: 'v1')\n" +
        "}");

        script.setBinding(binding);
        script.run();

        Closure closure = (Closure)binding.getVariable("mappings");
        List<UrlMapping> mappings = evaluator.evaluateMappings(closure);
        assertTrue(mappings.size() > 0);
        //Check that version and namespace are correct for each mapping
View Full Code Here

        if (stream != null) {
            GroovyClassLoader gcl = new GroovyClassLoader();
            try {
                Class<?> scriptClass = gcl.parseClass(IOUtils.toString(stream, "UTF-8"));
                Script script = (Script)scriptClass.newInstance();
                script.run();
                Binding binding = script.getBinding();
                if (binding.getVariables().containsKey(ConstraintsEvaluator.PROPERTY_NAME)) {
                    return (Closure<?>)binding.getVariable(ConstraintsEvaluator.PROPERTY_NAME);
                }
                LOG.warn("Unable to evaluate constraints from [" + constraintsScript + "], constraints closure not found!");
                return null;
View Full Code Here

                    context.setAttribute(name, value, scope);
                }
            }
        };
        try {
            Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
            Method methods[] = scriptClass.getMethods();
            Map<String, MethodClosure> closures = new HashMap<String, MethodClosure>();
            for (Method m : methods) {
                String name = m.getName();
                closures.put(name, new MethodClosure(scriptObject, name));
            }

            globalClosures.putAll(closures);
            final MetaClass oldMetaClass = scriptObject.getMetaClass();
            scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {
                public Object invokeMethod(Object object, String name, Object args) {
                    if (args == null) {
                        return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
                    } else if (args instanceof Tuple) {
                        return invokeMethod(object, name, ((Tuple) args).toArray());
                    } else if (args instanceof Object[]) {
                        return invokeMethod(object, name, (Object[]) args);
                    } else {
                        return invokeMethod(object, name, new Object[]{args});
                    }
                }

                public Object invokeMethod(Object object, String name, Object args[]) {
                    try {
                        return super.invokeMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, context);
                    }
                }

                public Object invokeStaticMethod(Object object, String name, Object args[]) {
                    try {
                        return super.invokeStaticMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, context);
                    }
                }
            });
            return scriptObject.run();
        } catch (Exception e) {
            throw new ScriptException(e);
        }
    }
View Full Code Here

    @Override
    public void execute(DbStep step)
    {
        LOG.info("[GROOVY STEP]:" + step.getName() + " (" + step.getVersion() + ")");
        Reader scriptReader = step.getStepReader();
        Script s = shell.parse(scriptReader);
        s.invokeMethod(GROOVY_DBMAIN, new Object[] {dbConnection, variables} );
    }
View Full Code Here

    protected String assertionFailureMessage(Exchange exchange) {
        return "groovy: " + text;
    }

    public Object evaluate(Exchange exchange) {
        Script script = ExchangeHelper.newInstance(exchange, scriptType);
        // lets configure the script
        configure(exchange, script);
        return script.run();
    }
View Full Code Here

    protected String assertionFailureMessage(Exchange exchange) {
        return "groovy: " + text;
    }

    public Object evaluate(Exchange exchange) {
        Script script = ExchangeHelper.newInstance(exchange, scriptType);
        // lets configure the script
        configure(exchange, script);
        return script.run();
    }
View Full Code Here

        return scriptCache;
    }

    @SuppressWarnings("unchecked")
    protected Script getScript(GroovyContext groovyContext, String scriptBaseClassName, String scriptSource) {
        Script script = scriptCache.getScript(scriptBaseClassName, scriptSource);
        script.setBinding(groovyContext.getBinding());
        return script;
    }
View Full Code Here

        }
        try {
            final GroovyContext effective = getEffectiveContext(groovyCtx);
            effective.setEvaluatingLocation(true);
            boolean inGlobalContext = groovyCtx.getParent() instanceof SCXMLSystemContext;
            Script script = getScript(effective, groovyCtx.getScriptBaseClass(), scriptSource);
            Object result = script.run();
            if (inGlobalContext && useInitialScriptAsBaseScript) {
                groovyCtx.setScriptBaseClass(script.getClass().getName());
            }
            return result;
        } catch (Exception e) {
            String exMessage = e.getMessage() != null ? e.getMessage() : e.getClass().getCanonicalName();
            throw new SCXMLExpressionException("evalScript('" + scriptSource + "'): " + exMessage, e);
View Full Code Here

TOP

Related Classes of groovy.lang.Script

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.