Search in sources :

Example 31 with Binary

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

the class GarbageCollectorTest method testGC.

public void testGC() throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    deleteMyNodes();
    runGC(session, true);
    root.addNode("node1");
    Node node2 = root.addNode("node2");
    Node n = node2.addNode("nodeWithBlob").addNode("sub");
    ValueFactory vf = session.getValueFactory();
    Binary b = vf.createBinary(new RandomInputStream(20, 1000));
    n.setProperty("test", b);
    session.save();
    n = node2.addNode("nodeWithTemporaryBlob");
    n.setProperty("test", vf.createBinary(new RandomInputStream(11, 1000)));
    session.save();
    n.remove();
    session.save();
    GarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
    gc.getDataStore().clearInUse();
    gc.setPersistenceManagerScan(false);
    gc.setMarkEventListener(this);
    if (gc.getDataStore() instanceof FileDataStore) {
        // make sure the file is old (access time resolution is 2 seconds)
        Thread.sleep(2000);
    }
    LOG.debug("scanning...");
    gc.mark();
    int count = listIdentifiers(gc);
    LOG.debug("stop scanning; currently " + count + " identifiers");
    LOG.debug("deleting...");
    gc.getDataStore().clearInUse();
    assertTrue(gc.sweep() > 0);
    int count2 = listIdentifiers(gc);
    assertEquals(count - 1, count2);
    // verify the node was moved, and that the binary is still there
    n = root.getNode("node1").getNode("nodeWithBlob").getNode("sub");
    b = n.getProperty("test").getValue().getBinary();
    InputStream in = b.getStream();
    InputStream in2 = new RandomInputStream(20, 1000);
    verifyInputStream(in, in2);
    deleteMyNodes();
    gc.close();
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) Binary(javax.jcr.Binary) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) GarbageCollector(org.apache.jackrabbit.core.gc.GarbageCollector)

Example 32 with Binary

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

the class PathTest method testGetBinaryFromValue.

public void testGetBinaryFromValue() throws RepositoryException, IOException {
    Value v = superuser.getValueFactory().createValue("/a/b/c", PropertyType.PATH);
    Binary binary = v.getBinary();
    byte[] bytes = new byte[(int) binary.getSize()];
    binary.read(bytes, 0);
    binary.dispose();
    assertEquals(prop.getString(), new String(bytes, "UTF-8"));
}
Also used : Value(javax.jcr.Value) Binary(javax.jcr.Binary)

Example 33 with Binary

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

the class PathTest method testGetBinary.

public void testGetBinary() throws RepositoryException, IOException {
    Binary binary = prop.getBinary();
    byte[] bytes = new byte[(int) binary.getSize()];
    binary.read(bytes, 0);
    binary.dispose();
    assertEquals(prop.getString(), new String(bytes, "UTF-8"));
}
Also used : Binary(javax.jcr.Binary)

Example 34 with Binary

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

the class ReferenceBinaryTest method testReferenceBinaryExchangeWithSharedRepository.

public void testReferenceBinaryExchangeWithSharedRepository() throws Exception {
    Session firstSession = superuser;
    // create a binary
    Binary b = vf.createBinary(new RandomInputStream(1, STREAM_LENGTH));
    ReferenceBinary referenceBinary = null;
    if (b instanceof ReferenceBinary) {
        referenceBinary = (ReferenceBinary) b;
    }
    assertNotNull(referenceBinary);
    assertNotNull(referenceBinary.getReference());
    // in the current test the message is exchanged via repository which is shared as well
    // put the reference message value in a property on a node
    String newNode = "sample_" + System.nanoTime();
    firstSession.getRootNode().addNode(newNode).setProperty("reference", referenceBinary.getReference());
    // save the first session
    firstSession.save();
    // get a second session over the same repository / ds
    Session secondSession = getHelper().getRepository().login(new SimpleCredentials("admin", "admin".toCharArray()));
    // read the binary referenced by the referencing binary
    String reference = secondSession.getRootNode().getNode(newNode).getProperty("reference").getString();
    ReferenceBinary ref = new SimpleReferenceBinary(reference);
    assertEquals(b, secondSession.getValueFactory().createValue(ref).getBinary());
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) Binary(javax.jcr.Binary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Session(javax.jcr.Session) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream)

Example 35 with Binary

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

the class ValueFactoryTest method testInputStream.

/**
     * Tests whether a passed <code>InputStream</code> is closed
     * by the implementation.
     *
     * @throws RepositoryException
     */
public void testInputStream() throws RepositoryException {
    InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
    valueFactory.createValue(in);
    assertTrue("ValueFactory.createValue(InputStream) is expected to close the passed input stream", in.isClosed());
    in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
    Binary bin = valueFactory.createBinary(in);
    assertTrue("ValueFactory.createBinary(InputStream) is expected to close the passed input stream", in.isClosed());
    bin.dispose();
}
Also used : InputStreamWrapper(org.apache.jackrabbit.test.api.util.InputStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(javax.jcr.Binary)

Aggregations

Binary (javax.jcr.Binary)51 ByteArrayInputStream (java.io.ByteArrayInputStream)28 InputStream (java.io.InputStream)22 Node (javax.jcr.Node)20 Value (javax.jcr.Value)14 Test (org.junit.Test)12 Session (javax.jcr.Session)10 ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)8 BigDecimal (java.math.BigDecimal)7 SimpleReferenceBinary (org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary)7 RandomInputStream (org.apache.jackrabbit.core.data.RandomInputStream)7 IOException (java.io.IOException)5 Property (javax.jcr.Property)5 RepositoryException (javax.jcr.RepositoryException)5 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 NamespaceRegistry (javax.jcr.NamespaceRegistry)3 ValueFactory (javax.jcr.ValueFactory)3 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)3 QValue (org.apache.jackrabbit.spi.QValue)3