incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r598667 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/slingservlets/ test/java/org/apache/sling/microsling/integration/microjax/
Date Tue, 27 Nov 2007 15:15:35 GMT
Author: bdelacretaz
Date: Tue Nov 27 07:15:34 2007
New Revision: 598667

URL: http://svn.apache.org/viewvc?rev=598667&view=rev
Log:
SLING-92, MicrojaxAutoPropertiesTest added, tests automatic setting of values for the 'created,
createdBy, lastModified, lastModifiedBy' properties for microjax POST requests, when such
properties are provided with empty values

Added:
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxAutoPropertiesTest.java
  (with props)
Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPropertyValueSetter.java

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java?rev=598667&r1=598666&r2=598667&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
Tue Nov 27 07:15:34 2007
@@ -185,9 +185,9 @@
             // request to create a new node at a specific path - use the supplied path as
is
         }
         
-        Set<Node> changedNodes = new HashSet<Node>();
+        Set<Node> createdNodes = new HashSet<Node>();
         if(currentNode == null) {
-            currentNode = deepCreateNode(s, currentPath, changedNodes);
+            currentNode = deepCreateNode(s, currentPath, createdNodes);
         }
         currentPath = currentNode.getPath();
         
@@ -198,7 +198,7 @@
         }
         
         // walk the request parameters, create and save nodes and properties
-        setPropertiesFromRequest(currentNode, request, savePrefix, changedNodes);
+        setPropertiesFromRequest(currentNode, request, savePrefix, createdNodes);
         
         // sava data and find out where to redirect
         s.save();
@@ -225,10 +225,9 @@
 
     /** Set Node properties from current request 
      *  TODO should handle file uploads as well
-     *  @return the Set of changed Nodes, if any (empty Set if none)
      */
-    private Set<Node> setPropertiesFromRequest(Node n, SlingHttpServletRequest request,

-            String savePrefix, Set<Node> changedNodes)
+    private void setPropertiesFromRequest(Node n, SlingHttpServletRequest request, 
+            String savePrefix, Set<Node> createdNodes)
             throws RepositoryException {
         
         for(Map.Entry<String, RequestParameter[]>  e : request.getRequestParameterMap().entrySet())
{
@@ -237,16 +236,14 @@
                 if(!name.startsWith(savePrefix)) continue;
                 name = name.substring(savePrefix.length());
             }
-            setProperty(n,request,name,e.getValue(),changedNodes);
+            setProperty(n,request,name,e.getValue(),createdNodes);
         }
-        
-        return changedNodes;
     }
     
     /** Set a single Property on node N 
      * @throws RepositoryException */
     private void setProperty(Node n, SlingHttpServletRequest request, String name, 
-            RequestParameter[] values, Set<Node> changedNodes) throws RepositoryException
{
+            RequestParameter[] values, Set<Node> createdNodes) throws RepositoryException
{
         
         // split the relative path identifying the property to be saved
         String proppath = name;
@@ -280,19 +277,17 @@
         final Session s = n.getSession();
         Node parent;
         if(name.startsWith("/")) {
-            parent = deepCreateNode(s, parentpath, changedNodes);
-            changedNodes.add(parent);
+            parent = deepCreateNode(s, parentpath, createdNodes);
             
         } else if (!parentpath.equals("")) {
             parent = (Node) s.getItem(path + "/" + parentpath);
         } else {
             parent = (Node) s.getItem(path);
         }
-        changedNodes.add(parent);
         
         // TODO String typehint = request.getParameter(proppath + "@TypeHint");
         final String typeHint = null;
-        final boolean nodeIsNew = changedNodes.contains(parent); 
+        final boolean nodeIsNew = createdNodes.contains(parent); 
         propertyValueSetter.setProperty(parent, propname, values, typeHint, nodeIsNew);
 }
 

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPropertyValueSetter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPropertyValueSetter.java?rev=598667&r1=598666&r2=598667&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPropertyValueSetter.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPropertyValueSetter.java
Tue Nov 27 07:15:34 2007
@@ -48,13 +48,16 @@
     void setProperty(Node parent, String name, RequestParameter[] values, String typehint,
boolean nodeWasJustCreated)
             throws RepositoryException {
 
+        // set the same timestamp for all values, to ease testing
+        final Calendar now = Calendar.getInstance();
+        
         if (valueProvided(values)) {
             // if user provided a value, don't mess with it
             setPropertyAsIs(parent, name, values, typehint);
 
         } else if (CREATED_FIELD.equals(name)) {
             if (nodeWasJustCreated) {
-                setCurrentDate(parent, name);
+                setCurrentDate(parent, name, now);
             }
 
         } else if (CREATED_BY_FIELD.equals(name)) {
@@ -63,7 +66,7 @@
             }
 
         } else if (LAST_MODIFIED_FIELD.equals(name)) {
-            setCurrentDate(parent, name);
+            setCurrentDate(parent, name, now);
 
         } else if (LAST_MODIFIED_BY_FIELD.equals(name)) {
             setCurrentUser(parent, name);
@@ -75,9 +78,9 @@
     }
 
     /** set property to the current Date */
-    private void setCurrentDate(Node parent, String name) throws RepositoryException {
+    private void setCurrentDate(Node parent, String name, Calendar now) throws RepositoryException
{
         removePropertyIfExists(parent, name);
-        parent.setProperty(name, Calendar.getInstance());
+        parent.setProperty(name, now);
     }
 
     /** set property to the current User id */
@@ -127,7 +130,8 @@
         boolean result = false;
 
         for (RequestParameter p : values) {
-            if(p!=null && p.getString()!=null) {
+            final String val = p.getString();
+            if(val!=null && val.length() > 0) {
                 result = true;
                 break;
             }

Added: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxAutoPropertiesTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxAutoPropertiesTest.java?rev=598667&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxAutoPropertiesTest.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxAutoPropertiesTest.java
Tue Nov 27 07:15:34 2007
@@ -0,0 +1,102 @@
+/*
+ * 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.microsling.integration.microjax;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.microsling.integration.MicroslingHttpTestBase;
+
+/** {#link MicrojaxPropertyValueSetter} sets the value of some properties
+ *  automatically if they are empty. This is tested here with various cases.
+ */  
+
+public class MicrojaxAutoPropertiesTest extends MicroslingHttpTestBase {
+
+    public static final String TEST_BASE_PATH = "/microjax-tests";
+    private String postUrl;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        postUrl = HTTP_BASE_URL + TEST_BASE_PATH + "/" + System.currentTimeMillis();
+    }
+    
+   public void testPostPathIsUnique() throws IOException {
+        assertHttpStatus(postUrl, HttpServletResponse.SC_NOT_FOUND,
+                "Path must not exist before test: " + postUrl);
+    }
+
+    public void testCreatedAndModified() throws IOException {
+        final Map <String, String> props = new HashMap <String, String> ();
+        props.put("a","123");
+        
+        props.put("created","");
+        props.put("createdBy","");
+        props.put("lastModified","");
+        props.put("lastModifiedBy","");
+        
+        final String createdNodeUrl = testClient.createNode(postUrl + "/*", props);
+        String content = getContent(createdNodeUrl + ".json", CONTENT_TYPE_JSON);
+        
+        assertJavascript("123", content, "out.println(data.a)");
+        assertJavascript("admin", content, "out.println(data.createdBy)");
+        assertJavascript("admin", content, "out.println(data.lastModifiedBy)");
+        assertJavascript("true", content, "out.println(data.created.length > 0)");
+        assertJavascript("true", content, "out.println(data.lastModified.length > 0)");
+        assertJavascript("true", content, "out.println(data.lastModified == data.created)");
+
+        // update node and check that "last modified" has changed
+        try {
+            Thread.sleep(1000L);
+        } catch(InterruptedException ignored) {
+            // ignore
+        }
+        
+        testClient.createNode(createdNodeUrl, props);
+        content = getContent(createdNodeUrl + ".json", CONTENT_TYPE_JSON);
+        
+        assertJavascript("123", content, "out.println(data.a)");
+        assertJavascript("admin", content, "out.println(data.createdBy)");
+        assertJavascript("admin", content, "out.println(data.lastModifiedBy)");
+        assertJavascript("true", content, "out.println(data.created.length > 0)");
+        assertJavascript("true", content, "out.println(data.lastModified.length > 0)");
+        assertJavascript("true", content, "out.println(data.lastModified > data.created)");
+    }
+    
+    public void testWithSpecificValues() throws IOException {
+        final Map <String, String> props = new HashMap <String, String> ();
+        props.put("a","123");
+        
+        props.put("created","a");
+        props.put("createdBy","b");
+        props.put("lastModified","c");
+        props.put("lastModifiedBy","d");
+        
+        final String createdNodeUrl = testClient.createNode(postUrl + "/*", props);
+        final String content = getContent(createdNodeUrl + ".json", CONTENT_TYPE_JSON);
+        
+        assertJavascript("123", content, "out.println(data.a)");
+        assertJavascript("a", content, "out.println(data.created)");
+        assertJavascript("b", content, "out.println(data.createdBy)");
+        assertJavascript("c", content, "out.println(data.lastModified)");
+        assertJavascript("d", content, "out.println(data.lastModifiedBy)");
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxAutoPropertiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/microjax/MicrojaxAutoPropertiesTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message