You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2018/08/22 15:13:24 UTC

[GitHub] stefanseifert closed pull request #2: SLING-7845 - Add support for MockNode.orderBefore

stefanseifert closed pull request #2: SLING-7845 - Add support for MockNode.orderBefore
URL: https://github.com/apache/sling-org-apache-sling-testing-jcr-mock/pull/2
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java b/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
index d85d4e1..b420774 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
@@ -470,7 +470,9 @@ public NodeIterator merge(final String srcWorkspace, final boolean bestEffort) t
 
     @Override
     public void orderBefore(final String srcChildRelPath, final String destChildRelPath) throws RepositoryException {
-        throw new UnsupportedOperationException();
+        Item srcChild = srcChildRelPath == null ? null : getMockedSession().getItem(getPath() + "/" + srcChildRelPath);
+        Item destChild = destChildRelPath == null ? null : getMockedSession().getItem(getPath() + "/" + destChildRelPath);
+        getMockedSession().orderBefore(srcChild, destChild);
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java b/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
index 78cafec..f650365 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
@@ -21,6 +21,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Pattern;
@@ -45,6 +46,8 @@
 import org.apache.jackrabbit.value.ValueFactoryImpl;
 import org.xml.sax.ContentHandler;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Mock {@link Session} implementation. This instance holds the JCR data in a
  * simple ordered map.
@@ -218,6 +221,41 @@ RangeIterator listChildren(final String parentPath, final ItemFilter filter) thr
         return new RangeIteratorAdapter(children.iterator(), children.size());
     }
 
+    void orderBefore(Item source, Item destination) throws RepositoryException {
+        if (source == null) {
+            // Nothing to do
+            return;
+        }
+
+        // Find all items matching the source
+        List<ItemData> itemsToMove = new LinkedList<>();
+        for (String key : ImmutableSet.copyOf(items.keySet())) {
+            if (key.startsWith(source.getPath())) {
+                itemsToMove.add(items.remove(key));
+            }
+        }
+
+        if (destination == null) {
+            // Move items to end
+            for (ItemData item : itemsToMove) {
+                items.put(item.getPath(), item);
+            }
+            return;
+        }
+
+        // Cycle items and add them back at the end
+        for (String key : ImmutableSet.copyOf(items.keySet())) {
+            if (key.equals(destination.getPath())) {
+                // Move items before destination
+                for (ItemData item : itemsToMove) {
+                    items.put(item.getPath(), item);
+                }
+            }
+            items.put(key, items.remove(key));
+        }
+    }
+
+
     @Override
     public boolean hasPendingChanges() throws RepositoryException {
         checkLive();
diff --git a/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java b/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java
index c89401d..4ff2525 100644
--- a/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.testing.mock.jcr;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -25,6 +26,8 @@
 import static org.junit.Assert.fail;
 
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
 import javax.jcr.ItemNotFoundException;
@@ -36,6 +39,7 @@
 import javax.jcr.Session;
 
 import org.apache.jackrabbit.JcrConstants;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -191,4 +195,39 @@ public void testIsModified() throws RepositoryException {
         assertFalse(foo.isModified());
     }
     
+    @Test
+    public void testOrderBefore() throws RepositoryException {
+        Node foo = this.session.getRootNode().addNode("foo");
+        this.session.save();
+        foo.addNode("one");
+        foo.addNode("two");
+        foo.addNode("three");
+        session.save();
+        assertArrayEquals("Expected nodes order mismatch",
+                new String[] {"one", "two", "three"},
+                getNodeNames(foo.getNodes()));
+        foo.orderBefore("three", "two");
+        session.save();
+        assertArrayEquals("Expected nodes order mismatch",
+                new String[] {"one", "three", "two"},
+                getNodeNames(foo.getNodes()));
+        foo.orderBefore("one", null);
+        session.save();
+        assertArrayEquals("Expected nodes order mismatch",
+                new String[] {"three", "two", "one"},
+                getNodeNames(foo.getNodes()));
+    }
+
+    private String[] getNodeNames(NodeIterator nodeIterator) {
+        List<String> names = new LinkedList<>();
+        nodeIterator.forEachRemaining(node -> {
+            try {
+                names.add(((Node)node).getName());
+            } catch (RepositoryException e) {
+                Assert.fail(e.getMessage());
+            }
+        });
+        return names.toArray(new String[names.size()]);
+    }
+
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services