Search in sources :

Example 66 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestRelationship method testDeletedRelationship.

@Test
public void testDeletedRelationship() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship relationship = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    relationship.delete();
    try {
        relationship.setProperty("key1", new Integer(1));
        fail("Adding property to deleted rel should throw exception.");
    } catch (Exception e) {
    // good
    }
    node1.delete();
    node2.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 67 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestRelationship method testRelGetProperties.

@Test
public void testRelGetProperties() {
    Integer int1 = new Integer(1);
    Integer int2 = new Integer(2);
    String string = new String("3");
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    try {
        rel1.getProperty(key1);
        fail("get non existing property din't throw exception");
    } catch (NotFoundException e) {
    }
    try {
        rel1.getProperty(null);
        fail("get of null key din't throw exception");
    } catch (IllegalArgumentException e) {
    }
    assertTrue(!rel1.hasProperty(key1));
    assertTrue(!rel1.hasProperty(null));
    rel1.setProperty(key1, int1);
    rel1.setProperty(key2, int2);
    rel1.setProperty(key3, string);
    assertTrue(rel1.hasProperty(key1));
    assertTrue(rel1.hasProperty(key2));
    assertTrue(rel1.hasProperty(key3));
    try {
        rel1.removeProperty(key3);
    } catch (NotFoundException e) {
        fail("Remove of property failed.");
    }
    assertTrue(!rel1.hasProperty(key3));
    assertTrue(!rel1.hasProperty(null));
    rel1.delete();
    node2.delete();
    node1.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 68 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestTransactionEvents method shouldGetCorrectTransactionDataUponCommit.

@Test
public void shouldGetCorrectTransactionDataUponCommit() {
    makeSureRelationshipTypeIsCreated(RelTypes.TXEVENT);
    // Create new data, nothing modified, just added/created
    ExpectedTransactionData expectedData = new ExpectedTransactionData();
    VerifyingTransactionEventHandler handler = new VerifyingTransactionEventHandler(expectedData);
    getGraphDb().registerTransactionEventHandler(handler);
    newTransaction();
    Node node1 = null, node2 = null, node3 = null;
    Relationship rel1 = null, rel2 = null;
    try {
        node1 = getGraphDb().createNode();
        expectedData.expectedCreatedNodes.add(node1);
        node2 = getGraphDb().createNode();
        expectedData.expectedCreatedNodes.add(node2);
        rel1 = node1.createRelationshipTo(node2, RelTypes.TXEVENT);
        expectedData.expectedCreatedRelationships.add(rel1);
        node1.setProperty("name", "Mattias");
        expectedData.assignedProperty(node1, "name", "Mattias", null);
        node1.setProperty("last name", "Persson");
        expectedData.assignedProperty(node1, "last name", "Persson", null);
        node1.setProperty("counter", 10);
        expectedData.assignedProperty(node1, "counter", 10, null);
        rel1.setProperty("description", "A description");
        expectedData.assignedProperty(rel1, "description", "A description", null);
        rel1.setProperty("number", 4.5D);
        expectedData.assignedProperty(rel1, "number", 4.5D, null);
        node3 = getGraphDb().createNode();
        expectedData.expectedCreatedNodes.add(node3);
        rel2 = node3.createRelationshipTo(node2, RelTypes.TXEVENT);
        expectedData.expectedCreatedRelationships.add(rel2);
        node3.setProperty("name", "Node 3");
        expectedData.assignedProperty(node3, "name", "Node 3", null);
        newTransaction();
        assertTrue(handler.hasBeenCalled());
    } finally {
        getGraphDb().unregisterTransactionEventHandler(handler);
    }
    // Use the above data and modify it, change properties, delete stuff
    expectedData = new ExpectedTransactionData();
    handler = new VerifyingTransactionEventHandler(expectedData);
    getGraphDb().registerTransactionEventHandler(handler);
    newTransaction();
    try {
        Node tempNode = getGraphDb().createNode();
        Relationship tempRel = tempNode.createRelationshipTo(node1, RelTypes.TXEVENT);
        tempNode.setProperty("something", "Some value");
        tempRel.setProperty("someproperty", 101010);
        tempNode.removeProperty("nothing");
        node3.setProperty("test", "hello");
        node3.setProperty("name", "No name");
        node3.delete();
        expectedData.expectedDeletedNodes.add(node3);
        expectedData.removedProperty(node3, "name", null, "Node 3");
        node1.setProperty("new name", "A name");
        node1.setProperty("new name", "A better name");
        expectedData.assignedProperty(node1, "new name", "A better name", null);
        node1.setProperty("name", "Nothing");
        node1.setProperty("name", "Mattias Persson");
        expectedData.assignedProperty(node1, "name", "Mattias Persson", "Mattias");
        node1.removeProperty("counter");
        expectedData.removedProperty(node1, "counter", null, 10);
        node1.removeProperty("last name");
        node1.setProperty("last name", "Hi");
        expectedData.assignedProperty(node1, "last name", "Hi", "Persson");
        rel2.delete();
        expectedData.expectedDeletedRelationships.add(rel2);
        rel1.removeProperty("number");
        expectedData.removedProperty(rel1, "number", null, 4.5D);
        rel1.setProperty("description", "Ignored");
        rel1.setProperty("description", "New");
        expectedData.assignedProperty(rel1, "description", "New", "A description");
        tempRel.delete();
        tempNode.delete();
        newTransaction();
    } finally {
        getGraphDb().unregisterTransactionEventHandler(handler);
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 69 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestReadOnlyNeo4j method testReadOnlyOperationsAndNoTransaction.

@Test
public void testReadOnlyOperationsAndNoTransaction() {
    GraphDatabaseService db = new EmbeddedGraphDatabase(PATH);
    Transaction tx = db.beginTx();
    Node node1 = db.createNode();
    Node node2 = db.createNode();
    Relationship rel = node1.createRelationshipTo(node2, withName("TEST"));
    node1.setProperty("key1", "value1");
    rel.setProperty("key1", "value1");
    tx.success();
    tx.finish();
    // make sure write operations still throw exception
    try {
        db.createNode();
        fail("Write operation and no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node1.createRelationshipTo(node2, withName("TEST2"));
        fail("Write operation and no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node1.setProperty("key1", "value2");
        fail("Write operation and no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        rel.removeProperty("key1");
        fail("Write operation and no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    // clear caches and try reads
    ((AbstractGraphDatabase) db).getConfig().getGraphDbModule().getNodeManager().clearCache();
    assertEquals(node1, db.getNodeById(node1.getId()));
    assertEquals(node2, db.getNodeById(node2.getId()));
    assertEquals(rel, db.getRelationshipById(rel.getId()));
    ((AbstractGraphDatabase) db).getConfig().getGraphDbModule().getNodeManager().clearCache();
    assertEquals("value1", node1.getProperty("key1"));
    Relationship loadedRel = node1.getSingleRelationship(DynamicRelationshipType.withName("TEST"), Direction.OUTGOING);
    assertEquals(rel, loadedRel);
    assertEquals("value1", loadedRel.getProperty("key1"));
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 70 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestRelationship method testRelationshipAddProperty.

@Test
public void testRelationshipAddProperty() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    Relationship rel2 = node2.createRelationshipTo(node1, MyRelTypes.TEST);
    try {
        rel1.setProperty(null, null);
        fail("Null argument should result in exception.");
    } catch (IllegalArgumentException e) {
    }
    Integer int1 = new Integer(1);
    Integer int2 = new Integer(2);
    String string1 = new String("1");
    String string2 = new String("2");
    // add property
    rel1.setProperty(key1, int1);
    rel2.setProperty(key1, string1);
    rel1.setProperty(key2, string2);
    rel2.setProperty(key2, int2);
    assertTrue(rel1.hasProperty(key1));
    assertTrue(rel2.hasProperty(key1));
    assertTrue(rel1.hasProperty(key2));
    assertTrue(rel2.hasProperty(key2));
    assertTrue(!rel1.hasProperty(key3));
    assertTrue(!rel2.hasProperty(key3));
    assertEquals(int1, rel1.getProperty(key1));
    assertEquals(string1, rel2.getProperty(key1));
    assertEquals(string2, rel1.getProperty(key2));
    assertEquals(int2, rel2.getProperty(key2));
    getTransaction().failure();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Aggregations

Relationship (org.neo4j.graphdb.Relationship)490 Node (org.neo4j.graphdb.Node)370 Test (org.junit.Test)250 Transaction (org.neo4j.graphdb.Transaction)138 LinkedList (java.util.LinkedList)48 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)48 RelationshipType (org.neo4j.graphdb.RelationshipType)38 NotFoundException (org.neo4j.graphdb.NotFoundException)35 Path (org.neo4j.graphdb.Path)29 HashMap (java.util.HashMap)27 Direction (org.neo4j.graphdb.Direction)26 HashSet (java.util.HashSet)24 ArrayList (java.util.ArrayList)18 RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)18 Map (java.util.Map)14 WeightedPath (org.neo4j.graphalgo.WeightedPath)14 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)14 List (java.util.List)13 PropertyContainer (org.neo4j.graphdb.PropertyContainer)12 Graph (org.neo4j.test.GraphDescription.Graph)11