jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r1444692 - in /jackrabbit/oak/trunk/oak-jcr/src: main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
Date Mon, 11 Feb 2013 09:30:30 GMT
Author: mreutegg
Date: Mon Feb 11 09:30:30 2013
New Revision: 1444692

URL: http://svn.apache.org/r1444692
Log:
OAK-612: Calling addNode on a node that has orderable child nodes violates specification

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java?rev=1444692&r1=1444691&r2=1444692&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
Mon Feb 11 09:30:30 2013
@@ -274,6 +274,10 @@ public class NodeImpl<T extends NodeDele
                     throw new ItemExistsException();
                 }
 
+                if (getPrimaryNodeType().hasOrderableChildNodes()) {
+                    dlg.orderBefore(oakName, null);
+                }
+
                 NodeImpl<?> childNode = new NodeImpl<NodeDelegate>(added);
                 childNode.internalSetPrimaryType(ntName);
                 childNode.autoCreateItems();
@@ -1431,6 +1435,18 @@ public class NodeImpl<T extends NodeDele
                 String jcrPrimaryType = sessionDelegate.getOakPath(Property.JCR_PRIMARY_TYPE);
                 Value value = sessionDelegate.getValueFactory().createValue(nodeTypeName,
PropertyType.NAME);
                 dlg.setProperty(jcrPrimaryType, value);
+
+                if (nt.hasOrderableChildNodes()) {
+                    // freeze child order with a call to orderBefore()
+                    // only makes sense with a least two child nodes
+                    Iterator<NodeDelegate> children = dlg.getChildren();
+                    NodeDelegate child1 = children.hasNext() ? children.next() : null;
+                    NodeDelegate child2 = children.hasNext() ? children.next() : null;
+                    if (child1 != null && child2 != null) {
+                        dlg.orderBefore(child1.getName(), child2.getName());
+                    }
+                }
+
                 return null;
             }
         });

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java?rev=1444692&r1=1444691&r2=1444692&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
Mon Feb 11 09:30:30 2013
@@ -16,17 +16,20 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.jackrabbit.commons.iterator.NodeIterable;
+import org.junit.Test;
+
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
-import org.junit.Ignore;
-import org.junit.Test;
-
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertTrue;
+import static org.junit.Assert.assertTrue;
 
 public class OrderableNodesTest extends AbstractRepositoryTest {
 
@@ -42,42 +45,56 @@ public class OrderableNodesTest extends 
         doTest("test:orderableFolder");
     }
 
-    @Ignore("OAK-612")
     @Test
-    public void testAddNode() throws Exception {
+    public void setPrimaryType() throws Exception {
         new TestContentLoader().loadTestContent(getAdminSession());
-
+        // start with a node without orderable nodes
         Session session = getAdminSession();
-        Node test = session.getRootNode().addNode("test", "test:orderableFolder");
-        assertTrue(test.getPrimaryNodeType().hasOrderableChildNodes());
-
-        Node n1 = test.addNode("a");
-        Node n2 = test.addNode("b");
-        session.save();
+        Node root = session.getRootNode().addNode("test", "nt:folder");
 
-        NodeIterator it = test.getNodes();
-        assertEquals("a", it.nextNode().getName());
-        assertEquals("b", it.nextNode().getName());
+        List<String> names = new ArrayList<String>();
+        for (int i = 0; i < 100; i++) {
+            String name = "node-" + i;
+            root.addNode(name, "nt:folder");
+            names.add(name);
+        }
+
+        root.setPrimaryType("test:orderableFolder");
+
+        // as of now, the child nodes must be stable and orderable
+        List<String> expected = getChildNames(root);
+        while (!expected.isEmpty()) {
+            String name = expected.remove((int) Math.floor(Math.random() * expected.size()));
+            root.getNode(name).remove();
+
+            assertEquals(expected, getChildNames(root));
+        }
+
+        for (String name : names) {
+            root.addNode(name, "nt:folder");
+            expected.add(name);
+            assertEquals(expected, getChildNames(root));
+        }
     }
 
-    @Ignore("OAK-612")
+    /**
+     * OAK-612
+     */
     @Test
-    public void testAddNode2() throws Exception {
+    public void testAddNode() throws Exception {
         new TestContentLoader().loadTestContent(getAdminSession());
 
         Session session = getAdminSession();
         Node test = session.getRootNode().addNode("test", "test:orderableFolder");
-        Node n1 = test.addNode("a");
-        Node n2 = test.addNode("b");
-        session.save();
+        assertTrue(test.getPrimaryNodeType().hasOrderableChildNodes());
 
-        test.getNode("a").remove();
         test.addNode("a");
+        test.addNode("b");
         session.save();
 
         NodeIterator it = test.getNodes();
-        assertEquals("b", it.nextNode().getName());
         assertEquals("a", it.nextNode().getName());
+        assertEquals("b", it.nextNode().getName());
     }
 
     private void doTest(String nodeType) throws RepositoryException {
@@ -90,8 +107,6 @@ public class OrderableNodesTest extends 
 
         NodeIterator iterator;
 
-        root.orderBefore("a", "b");
-        root.orderBefore("c", null);
         iterator = root.getNodes();
         assertEquals("a", iterator.nextNode().getName());
         assertEquals("b", iterator.nextNode().getName());
@@ -114,4 +129,13 @@ public class OrderableNodesTest extends 
 
         session.save();
     }
+
+    private static List<String> getChildNames(Node node)
+            throws RepositoryException {
+        List<String> names = new ArrayList<String>();
+        for (Node child : new NodeIterable(node.getNodes())) {
+            names.add(child.getName());
+        }
+        return names;
+    }
 }



Mime
View raw message