use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class TestTransactionEvents method shouldBeAbleToTouchDataOutsideTxDataInAfterCommit.
@Test
public void shouldBeAbleToTouchDataOutsideTxDataInAfterCommit() throws Exception {
// GIVEN
final Node node = createNode("one", "Two", "three", "Four");
dbRule.getGraphDatabaseAPI().registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {
@Override
public void afterCommit(TransactionData data, Object nothing) {
try (Transaction tx = dbRule.beginTx()) {
for (String key : node.getPropertyKeys()) {
// Just to see if one can reach them
node.getProperty(key);
}
tx.success();
}
}
});
try (Transaction tx = dbRule.beginTx()) {
// WHEN/THEN
dbRule.createNode();
node.setProperty("five", "Six");
tx.success();
}
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class TestTransactionEvents method modifiedPropertyCanByFurtherModifiedInBeforeCommit.
@Test
public void modifiedPropertyCanByFurtherModifiedInBeforeCommit() throws Exception {
// Given
// -- create node and set property on it in one transaction
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
final String key = "key";
final Object value1 = "the old value";
final Object value2 = "the new value";
final Node node;
try (Transaction tx = db.beginTx()) {
node = db.createNode();
node.setProperty(key, "initial value");
tx.success();
}
// -- register a tx handler which will override a property
TransactionEventHandler<Void> handler = new TransactionEventHandler.Adapter<Void>() {
@Override
public Void beforeCommit(TransactionData data) throws Exception {
Node modifiedNode = data.assignedNodeProperties().iterator().next().entity();
assertEquals(node, modifiedNode);
modifiedNode.setProperty(key, value2);
return null;
}
};
db.registerTransactionEventHandler(handler);
try (Transaction tx = db.beginTx()) {
// When
node.setProperty(key, value1);
tx.success();
}
// Then
assertThat(node, inTx(db, hasProperty(key).withValue(value2)));
db.unregisterTransactionEventHandler(handler);
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class TestTransactionEvents method shouldNotFireEventForReadOnlyTransaction.
@Test
public void shouldNotFireEventForReadOnlyTransaction() throws Exception {
// GIVEN
Node root = createTree(3, 3);
dbRule.getGraphDatabaseAPI().registerTransactionEventHandler(new ExceptionThrowingEventHandler(new RuntimeException("Just failing")));
// WHEN
try (Transaction tx = dbRule.beginTx()) {
count(dbRule.getGraphDatabaseAPI().traversalDescription().traverse(root));
tx.success();
}
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class TestTransactionEvents method shouldAllowToStringOnCreatedRelationshipInAfterCommit.
@Test
public void shouldAllowToStringOnCreatedRelationshipInAfterCommit() throws Exception {
// GIVEN
Relationship relationship;
Node startNode, endNode;
RelationshipType type = MyRelTypes.TEST;
try (Transaction tx = dbRule.beginTx()) {
startNode = dbRule.createNode();
endNode = dbRule.createNode();
relationship = startNode.createRelationshipTo(endNode, type);
tx.success();
}
// WHEN
AtomicReference<String> deletedToString = new AtomicReference<>();
dbRule.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {
@Override
public void afterCommit(TransactionData data, Object state) {
for (Relationship relationship : data.deletedRelationships()) {
deletedToString.set(relationship.toString());
}
}
});
try (Transaction tx = dbRule.beginTx()) {
relationship.delete();
tx.success();
}
// THEN
assertNotNull(deletedToString.get());
assertThat(deletedToString.get(), containsString(type.name()));
assertThat(deletedToString.get(), containsString(format("(%d)", startNode.getId())));
assertThat(deletedToString.get(), containsString(format("(%d)", endNode.getId())));
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class TestShortStringProperties method canUpdateShortStringInplace.
@Test
public void canUpdateShortStringInplace() throws Exception {
try {
long recordCount = dynamicRecordsInUse();
long propCount = propertyRecordsInUse();
Node node = graphdb.getGraphDatabaseAPI().createNode();
node.setProperty("key", "value");
newTx();
assertEquals(recordCount, dynamicRecordsInUse());
assertEquals(propCount + 1, propertyRecordsInUse());
assertEquals("value", node.getProperty("key"));
node.setProperty("key", "other");
commit();
assertEquals(recordCount, dynamicRecordsInUse());
assertEquals(propCount + 1, propertyRecordsInUse());
assertThat(node, inTx(graphdb.getGraphDatabaseAPI(), hasProperty("key").withValue("other")));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
Aggregations