Search in sources :

Example 61 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class ReferencesTest method testDoubleBackRefReferenceMultiValued.

/**
     * Variant of {@link #testDoubleBackReference()} for mult-valued props.
     * 
     * @throws Exception on test error
     */
public void testDoubleBackRefReferenceMultiValued() throws Exception {
    Session session2 = createSession();
    ValueFactory valFac2 = session2.getValueFactory();
    Node bses2 = getTestRootNode(session2).getNode("A").getNode("B");
    getTestRootNode(session2).getNode("C").setProperty("ref to B", new Value[] { valFac2.createValue(bses2), valFac2.createValue(bses2) });
    Session session3 = createSession();
    ValueFactory valFac3 = session3.getValueFactory();
    Node bses3 = getTestRootNode(session3).getNode("A").getNode("B");
    getTestRootNode(session3).getNode("C").setProperty("ref to B", new Value[] { valFac3.createValue(bses3), valFac3.createValue(bses3) });
    saveAndlogout(session2, session3);
    assertRemoveTestNodes();
}
Also used : Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) Session(javax.jcr.Session)

Example 62 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class CopyValueTest method testRandomOperations.

/**
     * Test random operations:
     * create, delete, move, and verify node; save and refresh a session.
     * The test always runs the same sequence of operations.
     */
public void testRandomOperations() throws Exception {
    Random random = new Random(1);
    Node root = superuser.getRootNode();
    if (root.hasNode("testRandom")) {
        root.getNode("testRandom").remove();
        superuser.save();
    }
    Node testRoot = root.addNode("testRandom");
    int len = 1000;
    int[] opCounts = new int[6];
    for (int i = 0; i < len; i++) {
        String node1 = "node" + random.nextInt(len / 20);
        boolean hasNode1 = testRoot.hasNode(node1);
        String node2 = "node" + random.nextInt(len / 20);
        boolean hasNode2 = testRoot.hasNode(node2);
        int op = random.nextInt(6);
        switch(op) {
            case 0:
                {
                    if (hasNode1) {
                        log(node1 + " remove");
                        testRoot.getNode(node1).remove();
                    }
                    opCounts[op]++;
                    Node n = testRoot.addNode(node1);
                    int dataLength = Math.abs(Math.min(10000, (int) (10000 * random.nextGaussian())));
                    byte[] data = new byte[dataLength];
                    log(node1 + " add len:" + dataLength);
                    ValueFactory vf = superuser.getValueFactory();
                    n.setProperty("data", vf.createBinary(new ByteArrayInputStream(data)));
                    n.setProperty("len", dataLength);
                    break;
                }
            case 1:
                opCounts[op]++;
                log("save");
                superuser.save();
                break;
            case 2:
                opCounts[op]++;
                log("refresh");
                superuser.refresh(false);
                break;
            case 3:
                if (hasNode1) {
                    opCounts[op]++;
                    log(node1 + " remove");
                    testRoot.getNode(node1).remove();
                }
                break;
            case 4:
                {
                    if (hasNode1) {
                        opCounts[op]++;
                        Node n = testRoot.getNode(node1);
                        long dataLength = n.getProperty("len").getLong();
                        long l = n.getProperty("data").getLength();
                        log(node1 + " verify len: " + dataLength);
                        assertEquals(dataLength, l);
                    }
                    break;
                }
            case 5:
                if (hasNode1 && !hasNode2) {
                    opCounts[op]++;
                    log(node1 + " copy " + node2);
                    // todo: why is save required?
                    superuser.save();
                    superuser.getWorkspace().copy("/testRandom/" + node1, "/testRandom/" + node2);
                }
                break;
        }
    }
    superuser.save();
    for (int i = 0; i < opCounts.length; i++) {
        log(i + ": " + opCounts[i]);
    }
    testRoot.remove();
    superuser.save();
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory)

Example 63 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class ExportImportTest method doTestExportImportBinary.

private void doTestExportImportBinary(int len) throws RepositoryException {
    Session session = getHelper().getReadWriteSession();
    try {
        Node root = session.getRootNode();
        clean(root);
        Node test = root.addNode("testBinary");
        session.save();
        byte[] data = new byte[len];
        Random random = new Random(1);
        random.nextBytes(data);
        ValueFactory vf = session.getValueFactory();
        test.setProperty("data", vf.createBinary(new ByteArrayInputStream(data)));
        session.save();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        session.exportSystemView("/testBinary", out, false, false);
        byte[] output = out.toByteArray();
        Node test2 = root.addNode("testBinary2");
        Node test3 = root.addNode("testBinary3");
        session.save();
        session.importXML("/testBinary2", new ByteArrayInputStream(output), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
        session.save();
        session.getWorkspace().importXML("/testBinary3", new ByteArrayInputStream(output), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
        test2 = root.getNode("testBinary2");
        test2 = test2.getNode("testBinary");
        test3 = root.getNode("testBinary3");
        test3 = test3.getNode("testBinary");
        byte[] data2 = readFromStream(test2.getProperty("data").getBinary().getStream());
        byte[] data3 = readFromStream(test3.getProperty("data").getBinary().getStream());
        assertEquals(data.length, data2.length);
        assertEquals(data.length, data3.length);
        for (int i = 0; i < len; i++) {
            assertEquals(data[i], data2[i]);
            assertEquals(data[i], data3[i]);
        }
        clean(root);
    } catch (Exception e) {
        e.printStackTrace();
        assertFalse(e.getMessage(), true);
    } finally {
        session.logout();
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Session(javax.jcr.Session)

Example 64 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class GCEventListenerTest method doTestEventListener.

private void doTestEventListener(boolean allowPmScan) throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    if (root.hasNode(TEST_NODE_NAME)) {
        root.getNode(TEST_NODE_NAME).remove();
        session.save();
    }
    Node test = root.addNode(TEST_NODE_NAME);
    Random random = new Random();
    byte[] data = new byte[10000];
    for (int i = 0; i < 10; i++) {
        Node n = test.addNode("x" + i);
        random.nextBytes(data);
        ValueFactory vf = session.getValueFactory();
        n.setProperty("data", vf.createBinary(new ByteArrayInputStream(data)));
        session.save();
        if (i % 2 == 0) {
            n.remove();
            session.save();
        }
    }
    session.save();
    SessionImpl si = (SessionImpl) session;
    DataStoreGarbageCollector gc = si.createDataStoreGarbageCollector();
    DataStore ds = ((GarbageCollector) gc).getDataStore();
    if (ds != null) {
        ds.clearInUse();
        boolean pmScan = gc.isPersistenceManagerScan();
        gc.setPersistenceManagerScan(allowPmScan);
        gotNullNode = false;
        gotNode = false;
        gc.setMarkEventListener(this);
        gc.mark();
        if (pmScan && allowPmScan) {
            assertTrue("PM scan without null Node", gotNullNode);
            assertFalse("PM scan, but got a real node", gotNode);
        } else {
            assertFalse("Not a PM scan - but got a null Node", gotNullNode);
            assertTrue("Not a PM scan - without a real node", gotNode);
        }
        int deleted = gc.sweep();
        LOG.debug("Deleted " + deleted);
        assertTrue("Should delete at least one item", deleted >= 0);
        gc.close();
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) GarbageCollector(org.apache.jackrabbit.core.gc.GarbageCollector)

Example 65 with ValueFactory

use of javax.jcr.ValueFactory in project jackrabbit by apache.

the class GCSubtreeMoveTest method setupNodes.

private void setupNodes() {
    try {
        Node rootNode = sessionMover.getRootNode();
        rootNode.addNode("node0");
        rootNode.addNode("node1");
        Node node2 = rootNode.addNode("node2");
        Node node3 = node2.addNode("node3");
        Node nodeWithBinary = node3.addNode("node-with-binary");
        ValueFactory vf = sessionGarbageCollector.getValueFactory();
        nodeWithBinary.setProperty("prop", vf.createBinary(new RandomInputStream(10, 1000)));
        sessionMover.save();
        sleepForFile();
    } catch (RepositoryException e) {
        failWithException(e);
    }
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory)

Aggregations

ValueFactory (javax.jcr.ValueFactory)105 Value (javax.jcr.Value)51 Node (javax.jcr.Node)50 Session (javax.jcr.Session)40 Test (org.junit.Test)17 RepositoryException (javax.jcr.RepositoryException)16 InputStream (java.io.InputStream)13 AccessControlManager (javax.jcr.security.AccessControlManager)13 HashMap (java.util.HashMap)12 Privilege (javax.jcr.security.Privilege)12 Property (javax.jcr.Property)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Query (javax.jcr.query.Query)8 Calendar (java.util.Calendar)7 QueryManager (javax.jcr.query.QueryManager)7 RowIterator (javax.jcr.query.RowIterator)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 NodeIterator (javax.jcr.NodeIterator)6 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)6 IOException (java.io.IOException)5