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