You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/09/07 11:12:37 UTC

svn commit: r1166075 - /jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CreateNodesTraverseTest.java

Author: thomasm
Date: Wed Sep  7 09:12:36 2011
New Revision: 1166075

URL: http://svn.apache.org/viewvc?rev=1166075&view=rev
Log:
New test case.

Added:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CreateNodesTraverseTest.java

Added: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CreateNodesTraverseTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CreateNodesTraverseTest.java?rev=1166075&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CreateNodesTraverseTest.java (added)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CreateNodesTraverseTest.java Wed Sep  7 09:12:36 2011
@@ -0,0 +1,250 @@
+/*
+ * 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.jackrabbit.spi2microkernel;
+
+import static org.junit.Assert.assertFalse;
+import java.io.ByteArrayInputStream;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Iterator;
+import javax.imageio.spi.ServiceRegistry;
+import javax.jcr.Binary;
+import javax.jcr.ItemExistsException;
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.PropertyType;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.RepositoryFactory;
+import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+import javax.jcr.version.VersionException;
+import org.apache.jackrabbit.mk.MicroKernelFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test creating nodes and traversing.
+ */
+public class CreateNodesTraverseTest {
+
+    // private static final String BASE_URL = "fs:target/repository-test/repository";
+    // private static final String BASE_URL = "mem:fs:target/temp";
+    private static final String BASE_URL = "mem:";
+
+    private static long counter = System.currentTimeMillis();
+
+    private Repository repository;
+    private Session session;
+
+    String ct = "text/plain";
+    int nodeCount, propertyCount;
+
+    private static String getMkUrl(boolean clean) {
+        // XXX: This is a workaround for the missing dispose of the
+        // microkernel from within the repository.
+        if (clean) {
+            counter++;
+        }
+        return BASE_URL + counter + (clean ? ";clean" : "");
+    }
+
+    private Repository getRepository() throws RepositoryException {
+        if (repository == null) {
+            Iterator<RepositoryFactory> factories = ServiceRegistry.lookupProviders(RepositoryFactory.class);
+            while (repository == null && factories.hasNext()) {
+                RepositoryFactory factory = factories.next();
+                repository = factory.getRepository(Collections.singletonMap(
+                        Spi2MicrokernelRepositoryServiceFactory.PARAM_URL, getMkUrl(false)));
+            }
+        }
+
+        return repository;
+    }
+
+    private Session getSession() throws RepositoryException {
+        if (session == null) {
+            session = getRepository().login();
+        }
+        return session;
+    }
+
+    private static void log(String s) {
+        // System.out.println(s);
+    }
+
+    @Before
+    public void setup() throws RepositoryException {
+        MicroKernel mk = MicroKernelFactory.getInstance(getMkUrl(true));
+        try {
+            String head = mk.getHeadRevision();
+            assertFalse(mk.nodeExists("/default", head));
+        }
+        finally {
+            mk.dispose();
+        }
+        getSession();
+    }
+
+    @After
+    public void tearDown() {
+        if (session != null) {
+            session.logout();
+            session = null;
+        }
+    }
+
+    @Test
+    public void test() throws Exception {
+
+        long time;
+        time = System.currentTimeMillis();
+        log("start");
+
+        // Profiler prof = new Profiler();
+        // prof.interval = 1;
+        // prof.startCollecting();
+        createNodes(2, 1, 1, true, 2, "Hallo");
+        // log(prof.getTop(10));
+
+        time = System.currentTimeMillis() - time;
+        log("create: " + time);
+
+        for (int i = 0; i < 3; i++) {
+
+            time = System.currentTimeMillis();
+
+            nodeCount = propertyCount = 0;
+            traverse(session.getRootNode());
+
+            log("  nodes: " + nodeCount + " properties: " + propertyCount);
+
+            // assertEquals(1692, nodeCount);
+
+            nodeCount = propertyCount = 0;
+            dataStoreConsistency(session.getRootNode());
+            // assertEquals(6433, nodeCount);
+
+            time = System.currentTimeMillis() - time;
+            log("traverse: " + time);
+
+        }
+
+    }
+
+    private void createNodes(int maxDocs, int docsPerFolder, int depth, boolean createDocs, int width, String text)
+            throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException,
+            VersionException, ConstraintViolationException, RepositoryException {
+        int doc = 0;
+        Node main = null;
+        byte[] textBytes = text.getBytes();
+        ValueFactory vf = session.getValueFactory();
+        Node root = session.getRootNode().addNode("test", "nt:folder");
+        Calendar cal = Calendar.getInstance();
+        while (doc < maxDocs) {
+            if (doc % (docsPerFolder * docsPerFolder) == 0) {
+                // new top folder
+                main = root.addNode("folder_" + doc, "nt:folder");
+                session.save();
+            }
+            Node parent = main.addNode("folder_" + doc, "nt:folder");
+            session.save();
+            int i = 0;
+            while (i < docsPerFolder) {
+                Node n = parent.addNode("file_" + doc + ".txt", "nt:file");
+                cal.setTimeInMillis(cal.getTimeInMillis() + 1);
+                if (createDocs) {
+                    Node content = n.addNode("jcr:content", "nt:unstructured");
+                    content.setProperty("jcr:lastModified", cal);
+                    content.setProperty("jcr:mimeType", "text/plain");
+                    createLevel(content, text, depth, width);
+                    session.save();
+                } else {
+                    Node content = n.addNode("jcr:content", "nt:resource");
+                    content.setProperty("jcr:lastModified", cal);
+                    content.setProperty("jcr:mimeType", ct);
+                    Binary b = vf.createBinary(new ByteArrayInputStream(textBytes));
+                    content.setProperty("jcr:data", b);
+                }
+                doc++;
+                i++;
+                if (doc == maxDocs) {
+                    break;
+                }
+            }
+        }
+    }
+
+    private void createLevel(Node parent, String content, int depth, int width) throws RepositoryException {
+        for (int i = 0; i < width; i++) {
+            Node n = parent.addNode("node" + depth + i, "nt:unstructured");
+            n.setProperty("data", content);
+            if (depth > 0) {
+                createLevel(n, content, depth - 1, width);
+            }
+        }
+    }
+
+    private void traverse(Node n) throws Exception {
+        PropertyIterator piter = n.getProperties();
+        while (piter.hasNext()) {
+            Property p = piter.nextProperty();
+            propertyCount++;
+            p.getName();
+            p.getValue();
+        }
+        NodeIterator iter = n.getNodes();
+        while (iter.hasNext()) {
+            Node next = iter.nextNode();
+            if (next.getName().equals("jcr:system")) {
+                continue;
+            }
+            nodeCount++;
+            traverse(next);
+        }
+    }
+
+    private void dataStoreConsistency(Node n) throws Exception {
+        PropertyIterator piter = n.getProperties();
+        while (piter.hasNext()) {
+            Property p = piter.nextProperty();
+            // getValue() alone opens a stream but doesn't close it
+            if (p.getType() == PropertyType.BINARY) {
+                if (p.getDefinition().isMultiple()) {
+                    Value[] values = p.getValues();
+                    for (int i = 0; i < values.length; i++) {
+                        values[i].getBinary().getStream().close();
+                    }
+                } else {
+                    p.getBinary().getStream().close();
+                }
+            }
+        }
+        NodeIterator iter = n.getNodes();
+        while (iter.hasNext()) {
+            nodeCount++;
+            dataStoreConsistency(iter.nextNode());
+        }
+    }
+
+}