Search in sources :

Example 46 with Binary

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

the class ReferenceBinaryIT method testReferenceBinaryExchangeWithSharedRepository.

/**
     * Taken from org.apache.jackrabbit.core.value.ReferenceBinaryTest
     * @throws Exception
     */
@Test
public void testReferenceBinaryExchangeWithSharedRepository() throws Exception {
    Session firstSession = createAdminSession();
    // create a binary
    Binary b = firstSession.getValueFactory().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 = repository.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());
    safeLogout(firstSession);
    safeLogout(secondSession);
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) Binary(javax.jcr.Binary) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) 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) Test(org.junit.Test)

Example 47 with Binary

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

the class BinaryPropertyTest method testValueJcr2.

/**
     * Tests that Value.getStream() delivers the same as Value.getBinary.getStream().
     * We check this by reading each byte of the two streams and assuring that
     * they are equal.
     */
public void testValueJcr2() throws IOException, RepositoryException {
    Value val = PropertyUtil.getValue(prop);
    InputStream in = val.getStream();
    Binary bin = val.getBinary();
    try {
        InputStream in2 = bin.getStream();
        try {
            int b = in.read();
            while (b != -1) {
                int b2 = in2.read();
                assertEquals("Value.getStream() and Value.getBinary().getStream() " + "return different values.", b, b2);
                b = in.read();
            }
            assertEquals("Value.getStream() and Value.getBinary().getStream() " + "return different values.", -1, in2.read());
        } finally {
            try {
                in.close();
            } catch (IOException ignore) {
            }
            try {
                in2.close();
            } catch (IOException ignore) {
            }
        }
    } finally {
        bin.dispose();
    }
}
Also used : InputStream(java.io.InputStream) Value(javax.jcr.Value) Binary(javax.jcr.Binary) IOException(java.io.IOException)

Example 48 with Binary

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

the class BinaryPropertyTest method testGetLengthJcr2.

/**
     * Tests the Binary.getSize() method.
     */
public void testGetLengthJcr2() throws RepositoryException {
    Value val = PropertyUtil.getValue(prop);
    Binary binary = val.getBinary();
    long length;
    try {
        length = binary.getSize();
    } finally {
        binary.dispose();
    }
    long bytes = PropertyUtil.countBytes(val);
    if (bytes != -1) {
        assertEquals("Binary.getSize() returns wrong number of bytes.", bytes, length);
    }
}
Also used : Value(javax.jcr.Value) Binary(javax.jcr.Binary)

Example 49 with Binary

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

the class DefaultSyncContextTest method testCreateValueFromInputStream.

/**
     * @see <a href="https://issues.apache.org/jira/browse/OAK-4231">OAK-4231</a>
     */
@Test
public void testCreateValueFromInputStream() throws Exception {
    byte[] bytes = new byte[] { 'a', 'b' };
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    Binary binary = valueFactory.createBinary(is);
    Value v = syncCtx.createValue(is);
    assertNotNull(v);
    assertEquals(PropertyType.BINARY, v.getType());
    assertEquals(binary, v.getBinary());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Value(javax.jcr.Value) Binary(javax.jcr.Binary) AbstractExternalAuthTest(org.apache.jackrabbit.oak.spi.security.authentication.external.AbstractExternalAuthTest) Test(org.junit.Test)

Example 50 with Binary

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

the class DefaultSyncContextTest method testCreateValueFromBytesArray.

@Test
public void testCreateValueFromBytesArray() throws Exception {
    byte[] bytes = new byte[] { 'a', 'b' };
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    Binary binary = valueFactory.createBinary(is);
    Value v = syncCtx.createValue(bytes);
    assertNotNull(v);
    assertEquals(PropertyType.BINARY, v.getType());
    assertEquals(binary, v.getBinary());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Value(javax.jcr.Value) Binary(javax.jcr.Binary) AbstractExternalAuthTest(org.apache.jackrabbit.oak.spi.security.authentication.external.AbstractExternalAuthTest) Test(org.junit.Test)

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