incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1468595 - in /sling/trunk/contrib/extensions/muppet: muppet-sling/ muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ muppet-sling/src/test/java/org/apache/sling/muppet/sling/ muppet-sling/src/test/java/org/apache/sling/muppet/...
Date Tue, 16 Apr 2013 20:16:11 GMT
Author: bdelacretaz
Date: Tue Apr 16 20:16:11 2013
New Revision: 1468595

URL: http://svn.apache.org/r1468595
Log:
SLING-2822 - direct scripting support added, with two example scripts in sling-demo content

Added:
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpRequest.java
  (with props)
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpResponse.java
  (with props)
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttribute.java
  (with props)
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/TestServletOutputStream.java
  (with props)
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttributeTest.java
  (with props)
    sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-fails.jsp
  (with props)
    sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-passes.esp
Modified:
    sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java
    sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java

Modified: sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml?rev=1468595&r1=1468594&r2=1468595&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml (original)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml Tue Apr 16 20:16:11 2013
@@ -99,6 +99,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.engine</artifactId>
+            <version>2.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
             <scope>provided</scope>
@@ -135,11 +141,17 @@
             <version>4.8.1</version>
             <scope>provided</scope>
         </dependency>
-        <dependency>
+         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.testing</artifactId>
             <version>2.0.14</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>1.9.5</version>
+            <scope>test</scope>
+        </dependency>
      </dependencies>
 </project>

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpRequest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpRequest.java?rev=1468595&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpRequest.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpRequest.java
Tue Apr 16 20:16:11 2013
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.muppet.sling.impl;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+/** Fake request used to acquire content from Sling.
+ *  TODO we should really provide those in a Sling utility
+ *  package to avoid reinventing them every time. */
+public class HttpRequest implements HttpServletRequest {
+
+    public static final String FAKE_URL_BASE = "http://example.com";
+    private final String path;
+    private final Map<String, Object> attributes;
+    private final Map<String, String> parameters;
+    
+    HttpRequest(String path) {
+        this.path = path;
+        attributes = new HashMap<String, Object>();
+        parameters = new HashMap<String, String>();
+    }
+    
+    public String getAuthType() {
+        return null;
+    }
+
+    public String getContextPath() {
+        return "";
+    }
+
+    public Cookie[] getCookies() {
+        return null;
+    }
+
+    public long getDateHeader(String name) {
+        return 0;
+    }
+
+    public String getHeader(String name) {
+        return null;
+    }
+
+    public Enumeration<?> getHeaderNames() {
+        return null;
+    }
+
+    public Enumeration<?> getHeaders(String name) {
+        return null;
+    }
+
+    public int getIntHeader(String name) {
+        return 0;
+    }
+
+    public String getMethod() {
+        return "GET";
+    }
+
+    public String getPathInfo() {
+        return null;
+    }
+
+    public String getPathTranslated() {
+        return null;
+    }
+
+    public String getQueryString() {
+        return null;
+    }
+
+    public String getRemoteUser() {
+        return null;
+    }
+
+    public String getRequestedSessionId() {
+        return null;
+    }
+
+    public String getRequestURI() {
+        return path;
+    }
+
+    public StringBuffer getRequestURL() {
+        return new StringBuffer(FAKE_URL_BASE + path);
+    }
+
+    public String getServletPath() {
+        return path; 
+    }
+
+    public HttpSession getSession() {
+        return null;
+    }
+
+    public HttpSession getSession(boolean create) {
+        return null;
+    }
+
+    public Principal getUserPrincipal() {
+        return null;
+    }
+
+    public boolean isRequestedSessionIdFromCookie() {
+        return false;
+    }
+
+    public boolean isRequestedSessionIdFromUrl() {
+        return false;
+    }
+
+    public boolean isRequestedSessionIdFromURL() {
+        return false;
+    }
+
+    public boolean isRequestedSessionIdValid() {
+        return false;
+    }
+
+    public boolean isUserInRole(String role) {
+        return false;
+    }
+
+    public Object getAttribute(String name) {
+        return attributes.get(name);
+    }
+
+    public Enumeration<?> getAttributeNames() {
+        return null;
+    }
+
+    public String getCharacterEncoding() {
+        return null;
+    }
+
+    public int getContentLength() {
+        return 0;
+    }
+
+    public String getContentType() {
+        return null;
+    }
+
+    public ServletInputStream getInputStream() throws IOException {
+        return null;
+    }
+
+    public String getLocalAddr() {
+        return null;
+    }
+
+    public Locale getLocale() {
+        return null;
+    }
+
+    public Enumeration<?> getLocales() {
+        return null;
+    }
+
+    public String getLocalName() {
+        return null;
+    }
+
+    public int getLocalPort() {
+        return 0;
+    }
+
+    public String getParameter(String name) {
+        return null;
+    }
+
+    public Map<?,?> getParameterMap() {
+        return parameters;
+    }
+
+    public Enumeration<?> getParameterNames() {
+        return null;
+    }
+
+    public String[] getParameterValues(String name) {
+        return null;
+    }
+
+    public String getProtocol() {
+        return null;
+    }
+
+    public BufferedReader getReader() throws IOException {
+        return null;
+    }
+
+    public String getRealPath(String path) {
+        return null;
+    }
+
+    public String getRemoteAddr() {
+        return null;
+    }
+
+    public String getRemoteHost() {
+        return null;
+    }
+
+    public int getRemotePort() {
+        return 0;
+    }
+
+    public RequestDispatcher getRequestDispatcher(String path) {
+        return null;
+    }
+
+    public String getScheme() {
+        return "http";
+    }
+
+    public String getServerName() {
+        return null;
+    }
+
+    public int getServerPort() {
+        return 0;
+    }
+
+    public boolean isSecure() {
+        return false;
+    }
+
+    public void removeAttribute(String name) {
+        attributes.remove(name);
+    }
+
+    public void setAttribute(String name, Object o) {
+        attributes.put(name, o);
+    }
+
+    public void setCharacterEncoding(String env)
+            throws UnsupportedEncodingException {
+    }
+}

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpRequest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpResponse.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpResponse.java?rev=1468595&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpResponse.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpResponse.java
Tue Apr 16 20:16:11 2013
@@ -0,0 +1,170 @@
+package org.apache.sling.muppet.sling.impl;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.Locale;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+/** Fake response used to acquire content from Sling
+ *  TODO we should really provide those in a Sling utility
+ *  package to avoid reinventing them every time. */
+public class HttpResponse implements HttpServletResponse {
+
+    private int status = 200;
+    private String message;
+    private String encoding = "UTF-8";
+    private String contentType;
+    private final TestServletOutputStream outputStream;
+    private final PrintWriter writer;
+    
+    HttpResponse() throws UnsupportedEncodingException {
+        outputStream = new TestServletOutputStream();
+        writer = new PrintWriter(new OutputStreamWriter(outputStream, encoding));
+    }
+    
+    public void addCookie(Cookie cookie) {
+    }
+
+    public void addDateHeader(String name, long date) {
+    }
+
+    public void addHeader(String name, String value) {
+    }
+
+    public void addIntHeader(String name, int value) {
+    }
+
+    public boolean containsHeader(String name) {
+        return false;
+    }
+
+    public String encodeRedirectUrl(String url) {
+        return null;
+    }
+
+    public String encodeRedirectURL(String url) {
+        return null;
+    }
+
+    public String encodeUrl(String url) {
+        return null;
+    }
+
+    public String encodeURL(String url) {
+        return null;
+    }
+
+    public void sendError(int sc, String msg) throws IOException {
+        status = sc;
+        message = msg;
+    }
+
+    public void sendError(int sc) throws IOException {
+        status = sc;
+    }
+
+    public void sendRedirect(String location) throws IOException {
+    }
+
+    public void setDateHeader(String name, long date) {
+    }
+
+    public void setHeader(String name, String value) {
+    }
+
+    public void setIntHeader(String name, int value) {
+    }
+
+    public void setStatus(int sc, String sm) {
+        status = sc;
+        message = sm;
+    }
+
+    public void setStatus(int sc) {
+        status = sc;
+    }
+
+    public void flushBuffer() throws IOException {
+    }
+
+    public int getBufferSize() {
+        return 0;
+    }
+
+    public String getCharacterEncoding() {
+        return encoding;
+    }
+
+    public String getContentType() {
+        return contentType;
+    }
+
+    public Locale getLocale() {
+        return null;
+    }
+
+    public ServletOutputStream getOutputStream() throws IOException {
+        return outputStream;
+    }
+
+    public PrintWriter getWriter() throws IOException {
+        return writer;
+    }
+
+    public boolean isCommitted() {
+        return false;
+    }
+
+    public void reset() {
+    }
+
+    public void resetBuffer() {
+    }
+
+    public void setBufferSize(int size) {
+    }
+
+    public void setCharacterEncoding(String charset) {
+        encoding = charset;
+    }
+
+    public void setContentLength(int len) {
+    }
+
+    public void setContentType(String type) {
+        contentType = type;
+    }
+
+    public void setLocale(Locale loc) {
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public String getEncoding() {
+        return encoding;
+    }
+
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+    
+    public String getContent() {
+        writer.flush();
+        return outputStream.toString();
+    }
+}

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpResponse.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/HttpResponse.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java?rev=1468595&r1=1468594&r2=1468595&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java
(original)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java
Tue Apr 16 20:16:11 2013
@@ -17,6 +17,7 @@
  */
 package org.apache.sling.muppet.sling.impl;
 
+import org.apache.sling.engine.SlingRequestProcessor;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -26,6 +27,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.scripting.SlingScript;
 import org.apache.sling.muppet.api.MuppetFacade;
 import org.apache.sling.muppet.api.Rule;
 import org.apache.sling.muppet.api.RuleBuilder;
@@ -39,6 +41,9 @@ public class RulesResourceParserImpl imp
     @Reference
     private MuppetFacade muppet;
     
+    @Reference
+    private SlingRequestProcessor requestProcessor;
+    
     @Override
     public List<Rule> parseResource(Resource r) {
         final List<Rule> result = new ArrayList<Rule>();
@@ -47,6 +52,29 @@ public class RulesResourceParserImpl imp
     }
     
     private void recursivelyParseResource(List<Rule> list, Resource r) {
+
+        // Add Rule for r if available
+        final Rule rule = resourceToRule(r);
+        if(rule != null) {
+            list.add(rule);
+        }
+        
+        // And recurse into r's children
+        final Iterator<Resource> it = r.getResourceResolver().listChildren(r);
+        while(it.hasNext()) {
+            recursivelyParseResource(list, it.next());
+        }
+    }
+    
+    /** Convert r to a Rule if possible */
+    Rule resourceToRule(Resource r) {
+        // If r adapts to a Sling script, use it to evaluate our Rule
+        final SlingScript script = r.adaptTo(SlingScript.class);
+        if(script != null) {
+            return new Rule(new ScriptSystemAttribute(requestProcessor, script), ScriptSystemAttribute.SUCCESS_STRING);
+        }
+
+        // else convert using available RuleBuilders if suitable
         final ValueMap props = r.adaptTo(ValueMap.class);
         if(props.containsKey(NAMESPACE) && props.containsKey(RULE_NAME)) {
             for(RuleBuilder b : muppet.getRuleBuilders()) {
@@ -57,15 +85,11 @@ public class RulesResourceParserImpl imp
                     props.get(EXPRESSION, String.class)
                 );
                 if(rule != null) {
-                    list.add(rule);
+                    return rule;
                 }
             }
         }
         
-        final Iterator<Resource> it = r.getResourceResolver().listChildren(r);
-        while(it.hasNext()) {
-            recursivelyParseResource(list, it.next());
-        }
-        
+        return null;
     }
 }

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttribute.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttribute.java?rev=1468595&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttribute.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttribute.java
Tue Apr 16 20:16:11 2013
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.muppet.sling.impl;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.engine.SlingRequestProcessor;
+import org.apache.sling.muppet.api.Rule;
+import org.apache.sling.muppet.api.SystemAttribute;
+
+/** Creates {@link Rule} that executes a Sling script and
+ *  returns its output, filtered to omit comments and blank lines 
+ */   
+class ScriptSystemAttribute implements SystemAttribute {
+    
+    /** A script must return only this line to be successful, any
+     *  other lines besides hash-prefixed comments and empty lines
+     *  are considered errors 
+     */
+    public static final String SUCCESS_STRING = "TEST_PASSED";
+    
+    private final SlingScript script;
+    private final SlingRequestProcessor requestProcessor;
+    
+    ScriptSystemAttribute(SlingRequestProcessor processor, SlingScript script) {
+        this.script = script;
+        this.requestProcessor = processor;
+    }
+
+    @Override
+    public String toString() {
+        return script.getScriptResource().getPath();
+    }
+    
+    @Override
+    public Object getValue() {
+        try {
+            final HttpRequest req = new HttpRequest(script.getScriptResource().getPath());
+            final HttpResponse resp = new HttpResponse();
+            requestProcessor.processRequest(req, resp, script.getScriptResource().getResourceResolver());
+            if(resp.getStatus() != HttpServletResponse.SC_OK) {
+                return "Unexpected requests status: " + resp.getStatus();
+            }
+            return filterContent(resp.getContent());
+        } catch(Exception e) {
+            return e.toString();
+        }
+    }
+    
+    static String filterContent(String content) throws IOException {
+        final BufferedReader br = new BufferedReader(new StringReader(content));
+        String line = null;
+        final StringBuilder result = new StringBuilder();
+        while( (line = br.readLine()) != null) {
+            line = line.trim();
+            if(line.length() == 0) {
+                // ignore
+            } else if(line.startsWith("#")) {
+                // ignore
+            } else {
+                if(result.length() > 0) {
+                    result.append('\n');
+                }
+                result.append(line);
+            }
+        }
+        return result.toString();
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttribute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttribute.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/TestServletOutputStream.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/TestServletOutputStream.java?rev=1468595&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/TestServletOutputStream.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/TestServletOutputStream.java
Tue Apr 16 20:16:11 2013
@@ -0,0 +1,22 @@
+package org.apache.sling.muppet.sling.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletOutputStream;
+
+/** Output stream for our fake HTTP response class */
+public class TestServletOutputStream extends ServletOutputStream {
+
+    private final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+    
+    @Override
+    public String toString() {
+        return bos.toString();
+    }
+    
+    @Override
+    public void write(int b) throws IOException {
+        bos.write(b);
+    }
+}

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/TestServletOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/TestServletOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java?rev=1468595&r1=1468594&r2=1468595&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java
(original)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java
Tue Apr 16 20:16:11 2013
@@ -6,6 +6,10 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.scripting.SlingScript;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
 import static org.apache.sling.muppet.sling.api.RulesResourceParser.NAMESPACE;
 import static org.apache.sling.muppet.sling.api.RulesResourceParser.RULE_NAME;
 import static org.apache.sling.muppet.sling.api.RulesResourceParser.QUALIFIER;
@@ -15,6 +19,7 @@ class MockResource implements Resource {
     private final ResourceResolver resolver;
     private final ValueMap valueMap;
     private final String path;
+    private String scriptCode;
     
     @SuppressWarnings("serial")
     static class PropertiesMap extends HashMap<String, Object> implements ValueMap
{
@@ -48,6 +53,13 @@ class MockResource implements Resource {
         valueMap.put(QUALIFIER, qualifier);
         valueMap.put(EXPRESSION, expression);
         resolver.addResource(this);
+        
+        MockitoAnnotations.initMocks(this);
+    }
+    
+    MockResource(MockResolver resolver, String path, String scriptCode) {
+        this(resolver, path, null, null, null, null);
+        this.scriptCode = scriptCode;
     }
     
     @SuppressWarnings("unchecked")
@@ -55,6 +67,10 @@ class MockResource implements Resource {
     public <AdapterType> AdapterType adaptTo(Class<AdapterType> target) {
         if(target == ValueMap.class) {
             return (AdapterType)valueMap;
+        } else if(target == SlingScript.class && scriptCode != null) {
+            final SlingScript mockScript = Mockito.mock(SlingScript.class);
+            Mockito.doReturn(this).when(mockScript).getScriptResource();
+            return (AdapterType)mockScript;
         }
         return null;
     }

Modified: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java?rev=1468595&r1=1468594&r2=1468595&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java
(original)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java
Tue Apr 16 20:16:11 2013
@@ -91,6 +91,22 @@ public class RulesResourceParserTest {
     }
     
     @Test
+    public void testScriptResource() {
+        final Resource root = new MockResource(resolver, "/foo", "test", "constant", "5",
"> 3");
+        new MockResource(resolver, "/foo/script1", "some script");
+        final List<Rule> rules = parser.parseResource(root); 
+        assertEquals(2, rules.size());
+        final String [] expect = {
+                "Rule: test:constant:5 > 3",
+                "Rule: /foo/script1 TEST_PASSED"
+            };
+            final String allText = rules.toString();
+            for(String resText : expect) {
+                assertTrue("Expecting rules list (" + allText + ") to contain " + resText,
allText.indexOf(resText) >= 0);
+            }
+    }
+    
+    @Test
     public void testResourceTree() {
         final Resource root = new MockResource(resolver, "/foo", "test", "constant", "5",
"> 3");
         new MockResource(resolver, "/foo/1", "test", "constant", "12", "A");

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttributeTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttributeTest.java?rev=1468595&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttributeTest.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttributeTest.java
Tue Apr 16 20:16:11 2013
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.muppet.sling.impl;
+
+import static org.apache.sling.muppet.api.EvaluationResult.Status.ERROR;
+import static org.apache.sling.muppet.api.EvaluationResult.Status.OK;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.engine.SlingRequestProcessor;
+import org.apache.sling.muppet.api.EvaluationResult;
+import org.apache.sling.muppet.api.Rule;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class ScriptSystemAttributeTest {
+    
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+    }
+    
+    private void assertResult(String info, final String scriptOutput, EvaluationResult.Status
status) {
+        final Answer<?> answer = new Answer<Object>() {
+            @Override
+            public Object answer(InvocationOnMock invocation) throws Throwable {
+                final HttpServletResponse response = (HttpServletResponse)invocation.getArguments()[1];
+                response.getWriter().write(scriptOutput);
+                response.getWriter().flush();
+                return null;
+            }
+        };
+        
+        final SlingRequestProcessor processor = Mockito.mock(SlingRequestProcessor.class);
+        
+        try {
+            Mockito.doAnswer(answer).when(processor).processRequest(
+                    Matchers.any(HttpServletRequest.class), 
+                    Matchers.any(HttpServletResponse.class), 
+                    Matchers.any(ResourceResolver.class));
+        } catch(Exception e) {
+            fail("Exception in processRequest: " + e);
+        }
+        
+        final Resource resource = Mockito.mock(Resource.class); 
+        final SlingScript script = Mockito.mock(SlingScript.class);
+        Mockito.when(script.getScriptResource()).thenReturn(resource);
+        final ScriptSystemAttribute a = new ScriptSystemAttribute(processor, script);
+
+        final Rule r = new Rule(a, ScriptSystemAttribute.SUCCESS_STRING);
+        assertEquals("Expecting " + status + " for " + info, status,r.evaluate());
+    }
+    
+    @Test
+    public void testEmptyScripts() {
+        assertResult("Empty script -> error", "", ERROR);
+    }
+    
+    @Test
+    public void testOkScript() {
+        assertResult("TEST_PASSED script -> ok", "TEST_PASSED", OK);
+    }
+    
+    @Test
+    public void testComments() {
+        assertResult("TEST_PASSED script and blank line -> ok", "\n\nTEST_PASSED\n\n",
OK);
+        assertResult("TEST_PASSED script and comments -> ok", "\n\n#comment\n\t  # comment
2\nTEST_PASSED\n\n", OK);
+    }
+    
+    @Test
+    public void testDoublePassed() {
+        assertResult("Double TEST_PASSED script -> error", "TEST_PASSED\nTEST_PASSED",
ERROR);
+    }
+}

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttributeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/impl/ScriptSystemAttributeTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-fails.jsp
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-fails.jsp?rev=1468595&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-fails.jsp
(added)
+++ sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-fails.jsp
Tue Apr 16 20:16:11 2013
@@ -0,0 +1,4 @@
+# example script that fails as a Muppet rule
+Anything that the script outputs
+that is not TEST_PASSED, and empty line or a comment
+causes it to fail.

Propchange: sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-fails.jsp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-passes.esp
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-passes.esp?rev=1468595&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-passes.esp
(added)
+++ sling/trunk/contrib/extensions/muppet/sling-demo/src/main/resources/SLING-CONTENT/apps/muppet/demo/script-that-passes.esp
Tue Apr 16 20:16:11 2013
@@ -0,0 +1,2 @@
+# example script that passes as a Muppet rule
+TEST_PASSED



Mime
View raw message