For one I did not use the proper path on addNode :-p
But the versioning part was a little more tricky to get right. Still
find the checkin/checkout/save semantics quite confusing. But this
seems to work as expected:
private Node write(Session session, String path, String content)
throws RepositoryException {
Node root = session.getRootNode();
Node node;
boolean isNew;
if (root.hasNode(path)) {
node = root.getNode(path);
node.checkout();
isNew = false;
} else {
node = root.addNode(path, "nt:resource");
node.setProperty("jcr:mimeType", "text/plain");
node.setProperty("jcr:encoding", "utf-8");
node.addMixin("mix:versionable");
isNew = true;
}
node.setProperty("jcr:data", content);
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(System.currentTimeMillis());
node.setProperty("jcr:lastModified", lastModified);
session.save();
if (!isNew) {
node.checkin();
}
return node;
}
..
public void testVersioning() throws Exception {
Node node;
node = write(session, "test", "test1");
assertEquals(1, versions(node));
assertEquals("test1", read(session, "test"));
node = write(session, "test", "test2");
assertEquals(2, versions(node));
assertEquals("test2", read(session, "test"));
node = write(session, "test", "test3");
assertEquals(3, versions(node));
assertEquals("test3", read(session, "test"));
node = write(session, "test", "test4");
assertEquals(4, versions(node));
assertEquals("test4", read(session, "test"));
}
Would have hoped for such an example in the docs.
cheers
--
Torsten