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();
}
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"));
}
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"));
}
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());
}
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();
}
Aggregations