Author: bdelacretaz
Date: Wed Jun 25 01:42:26 2008
New Revision: 671465

URL: http://svn.apache.org/viewvc?rev=671465&view=rev
Log:
SLING-556 - FreeMarker wrapper was not correctly dereferencing multi value 
properties - contributed by Bryce Ewing, thanks!

Added:
    
incubator/sling/trunk/scripting/freemarker/src/test/java/org/apache/sling/scripting/freemarker/wrapper/NodeReferenceTest.java
   (with props)
Modified:
    
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeListModel.java
    
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeModel.java

Modified: 
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeListModel.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeListModel.java?rev=671465&r1=671464&r2=671465&view=diff
==============================================================================
--- 
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeListModel.java
 (original)
+++ 
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeListModel.java
 Wed Jun 25 01:42:26 2008
@@ -33,6 +33,10 @@
 
     private List<Node> nodes;
 
+    public NodeListModel(List<Node> nodes) {
+        this.nodes = nodes;
+    }
+
     public NodeListModel(NodeIterator nodes) {
         this.nodes = new ArrayList<Node>();
         while (nodes.hasNext()) {

Modified: 
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeModel.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeModel.java?rev=671465&r1=671464&r2=671465&view=diff
==============================================================================
--- 
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeModel.java
 (original)
+++ 
incubator/sling/trunk/scripting/freemarker/src/main/java/org/apache/sling/scripting/freemarker/wrapper/NodeModel.java
 Wed Jun 25 01:42:26 2008
@@ -18,10 +18,9 @@
 
 import freemarker.template.*;
 
-import javax.jcr.Node;
-import javax.jcr.RepositoryException;
-import javax.jcr.ItemNotFoundException;
-import javax.jcr.PropertyType;
+import javax.jcr.*;
+import java.util.List;
+import java.util.ArrayList;
 
 /**
  * A wrapper for JCR nodes to support freemarker scripting.
@@ -131,7 +130,16 @@
                     return new NodeModel(node.getNode(key));
                 }
                 else if (node.hasProperty(key) && 
node.getProperty(key).getType() == PropertyType.REFERENCE) {
-                    return new NodeModel(node.getProperty(key).getNode());
+                    if (node.getProperty(key).getDefinition().isMultiple()) {
+                        List<Node> nodes = new ArrayList<Node>();
+                        for (Value value : node.getProperty(key).getValues()) {
+                            
nodes.add(node.getSession().getNodeByUUID(value.getString()));
+                        }
+                        return new NodeListModel(nodes);
+                    }
+                    else {
+                        return new NodeModel(node.getProperty(key).getNode());
+                    }
                 }
                 return null;
             } catch (RepositoryException e) {

Added: 
incubator/sling/trunk/scripting/freemarker/src/test/java/org/apache/sling/scripting/freemarker/wrapper/NodeReferenceTest.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/freemarker/src/test/java/org/apache/sling/scripting/freemarker/wrapper/NodeReferenceTest.java?rev=671465&view=auto
==============================================================================
--- 
incubator/sling/trunk/scripting/freemarker/src/test/java/org/apache/sling/scripting/freemarker/wrapper/NodeReferenceTest.java
 (added)
+++ 
incubator/sling/trunk/scripting/freemarker/src/test/java/org/apache/sling/scripting/freemarker/wrapper/NodeReferenceTest.java
 Wed Jun 25 01:42:26 2008
@@ -0,0 +1,67 @@
+/*
+ * 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.scripting.freemarker.wrapper;
+
+import org.apache.sling.scripting.freemarker.FreemarkerTestBase;
+
+import javax.jcr.Node;
+import javax.jcr.PropertyType;
+
+/**
+ * Test references to nodes both as singular properties and list properties.
+ */
+public class NodeReferenceTest extends FreemarkerTestBase {
+
+    private Node node1;
+    private Node node2;
+    private Node node3;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        node1 = rootNode.addNode("nodefrom", "nt:unstructured");
+
+        node2 = rootNode.addNode("reference1");
+        node2.addMixin("mix:referenceable");
+
+        node3 = rootNode.addNode("reference2");
+        node3.addMixin("mix:referenceable");
+
+        node1.setProperty("singlereference", node2.getUUID(), 
PropertyType.REFERENCE);
+        node1.setProperty("multireference", new String[]{ node2.getUUID(), 
node3.getUUID()}, PropertyType.REFERENCE);
+    }
+
+    public void testReferenceAsProperty() throws Exception {
+        assertEquals(node2.getUUID(), freemarker.evalToString("[EMAIL 
PROTECTED]"));
+    }
+
+    public void testReferenceAsNode() throws Exception {
+        assertEquals(node2.getPath(), 
freemarker.evalToString("${node.nodefrom.singlereference}"));
+    }
+
+    public void testMultiReferenceAsProperty() throws Exception {
+        String expect = node2.getUUID() + node3.getUUID();
+        assertEquals(expect, freemarker.evalToString("<#list [EMAIL PROTECTED] 
as ref>${ref}</#list>"));
+    }
+
+    public void testMultiReferenceAsNode() throws Exception {
+        String expect = node2.getPath() + node3.getPath();
+        assertEquals(expect, freemarker.evalToString("<#list 
node.nodefrom.multireference as ref>${ref}</#list>"));
+    }
+
+}

Propchange: 
incubator/sling/trunk/scripting/freemarker/src/test/java/org/apache/sling/scripting/freemarker/wrapper/NodeReferenceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/sling/trunk/scripting/freemarker/src/test/java/org/apache/sling/scripting/freemarker/wrapper/NodeReferenceTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL


Reply via email to