Search in sources :

Example 1 with NotifyingSailConnection

use of org.openrdf.sail.NotifyingSailConnection in project blueprints by tinkerpop.

the class SailTest method testSailConnectionListeners.

// listeners ///////////////////////////////////////////////////////////////
// (disabled for Sails which do not implement NotifyingSail)
@Test
public void testSailConnectionListeners() throws Exception {
    if (sail instanceof NotifyingSail) {
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        TestListener listener1 = new TestListener(), listener2 = new TestListener();
        NotifyingSailConnection sc = ((NotifyingSail) sail).getConnection();
        try {
            sc.begin();
            sc.clear();
            sc.commit();
            sc.begin();
            // Add a listener and add statements
            sc.addConnectionListener(listener1);
            sc.addStatement(uriA, uriB, uriC, uriA);
            sc.addStatement(uriB, uriC, uriA, uriA);
            sc.commit();
            sc.begin();
            // Add another listener and remove a statement
            sc.addConnectionListener(listener2);
            sc.removeStatements(uriA, null, null);
            sc.commit();
            sc.begin();
            assertEquals(2, listener1.getAdded());
            assertEquals(0, listener2.getAdded());
            assertEquals(1, listener1.getRemoved());
            assertEquals(1, listener2.getRemoved());
            // Remove a listener and clear
            sc.removeConnectionListener(listener1);
            sc.clear();
            sc.commit();
            sc.begin();
            assertEquals(1, listener1.getRemoved());
            assertEquals(2, listener2.getRemoved());
        } finally {
            sc.rollback();
            sc.close();
        }
    }
}
Also used : NotifyingSail(org.openrdf.sail.NotifyingSail) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 URI (org.openrdf.model.URI)1 NotifyingSail (org.openrdf.sail.NotifyingSail)1 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)1