Search in sources :

Example 1 with RandomInputStream

use of org.apache.jackrabbit.core.data.RandomInputStream in project jackrabbit-oak by apache.

the class RepositoryTest method testReferenceBinary.

@Test
public void testReferenceBinary() throws RepositoryException {
    ValueFactory valueFactory = getAdminSession().getValueFactory();
    Binary binary = valueFactory.createBinary(new RandomInputStream(1, 256 * 1024));
    String reference = binary instanceof ReferenceBinary ? ((ReferenceBinary) binary).getReference() : null;
    assumeTrue(reference != null);
    Session session = createAdminSession();
    try {
        valueFactory = session.getValueFactory();
        assertEquals(binary, valueFactory.createValue(new SimpleReferenceBinary(reference)).getBinary());
    } finally {
        session.logout();
    }
}
Also used : SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ValueFactory(javax.jcr.ValueFactory) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) Session(javax.jcr.Session) Test(org.junit.Test)

Example 2 with RandomInputStream

use of org.apache.jackrabbit.core.data.RandomInputStream in project jackrabbit-oak by apache.

the class AbstractDataStoreTest method doTest.

/**
 * Assert randomly read stream from record.
 */
void doTest(DataStore ds, int offset) throws Exception {
    ArrayList<DataRecord> list = new ArrayList<DataRecord>();
    HashMap<DataRecord, Integer> map = new HashMap<DataRecord, Integer>();
    for (int i = 0; i < 10; i++) {
        int size = 100000 - (i * 100);
        RandomInputStream in = new RandomInputStream(size + offset, size);
        DataRecord rec = ds.addRecord(in);
        list.add(rec);
        map.put(rec, size);
    }
    Random random = new Random(1);
    for (int i = 0; i < list.size(); i++) {
        int pos = random.nextInt(list.size());
        DataRecord rec = list.get(pos);
        int size = map.get(rec);
        rec = ds.getRecord(rec.getIdentifier());
        Assert.assertEquals(size, rec.getLength());
        RandomInputStream expected = new RandomInputStream(size + offset, size);
        InputStream in = rec.getStream();
        // Workaround for race condition that can happen with low cache size relative to the test
        // read immediately
        byte[] buffer = new byte[1];
        in.read(buffer);
        in = new SequenceInputStream(new ByteArrayInputStream(buffer), in);
        if (random.nextBoolean()) {
            in = readInputStreamRandomly(in, random);
        }
        assertEquals(expected, in);
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Random(java.util.Random) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataRecord(org.apache.jackrabbit.core.data.DataRecord) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream)

Example 3 with RandomInputStream

use of org.apache.jackrabbit.core.data.RandomInputStream in project jackrabbit by apache.

the class LazyTextExtractorFieldTest method testEmptyParser.

/**
 * @see <a
 *      href="https://issues.apache.org/jira/browse/JCR-3296">JCR-3296</a>
 *      Indexing ignored file types creates some garbage
 */
public void testEmptyParser() throws Exception {
    InternalValue val = InternalValue.create(new RandomInputStream(1, 1024));
    Metadata metadata = new Metadata();
    metadata.set(Metadata.CONTENT_TYPE, "application/java-archive");
    metadata.set(Metadata.CONTENT_ENCODING, "UTF-8");
    Parser p = getSearchIndex().getParser();
    ParsingTask task = new ParsingTask(p, val, metadata, Integer.MAX_VALUE) {

        public void setExtractedText(String value) {
            assertEquals("", value);
        }
    };
    task.run();
}
Also used : ParsingTask(org.apache.jackrabbit.core.query.lucene.LazyTextExtractorField.ParsingTask) Metadata(org.apache.tika.metadata.Metadata) InternalValue(org.apache.jackrabbit.core.value.InternalValue) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) Parser(org.apache.tika.parser.Parser)

Example 4 with RandomInputStream

use of org.apache.jackrabbit.core.data.RandomInputStream 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 5 with RandomInputStream

use of org.apache.jackrabbit.core.data.RandomInputStream 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)

Aggregations

RandomInputStream (org.apache.jackrabbit.core.data.RandomInputStream)5 Binary (javax.jcr.Binary)3 Session (javax.jcr.Session)3 ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)3 SimpleReferenceBinary (org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary)3 SimpleCredentials (javax.jcr.SimpleCredentials)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 ValueFactory (javax.jcr.ValueFactory)1 DataRecord (org.apache.jackrabbit.core.data.DataRecord)1 ParsingTask (org.apache.jackrabbit.core.query.lucene.LazyTextExtractorField.ParsingTask)1 InternalValue (org.apache.jackrabbit.core.value.InternalValue)1 Metadata (org.apache.tika.metadata.Metadata)1 Parser (org.apache.tika.parser.Parser)1 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)1