Search in sources :

Example 1 with SailChangedEvent

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

the class SailTest method testSailChangedListeners.

@Test
public void testSailChangedListeners() throws Exception {
    if (sail instanceof NotifyingSail) {
        final Collection<SailChangedEvent> events = new LinkedList<SailChangedEvent>();
        SailChangedListener listener = new SailChangedListener() {

            public void sailChanged(final SailChangedEvent event) {
                events.add(event);
            }
        };
        ((NotifyingSail) sail).addSailChangedListener(listener);
        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");
        SailConnection sc = sail.getConnection();
        try {
            sc.begin();
            sc.clear();
            sc.commit();
            sc.begin();
            events.clear();
            assertEquals(0, events.size());
            sc.addStatement(uriA, uriB, uriC, uriA);
            sc.addStatement(uriB, uriC, uriA, uriA);
            // Events are buffered until the commit
            assertEquals(0, events.size());
            sc.commit();
            sc.begin();
            // Only one SailChangedEvent per commit
            assertEquals(1, events.size());
            SailChangedEvent event = events.iterator().next();
            assertTrue(event.statementsAdded());
            assertFalse(event.statementsRemoved());
            events.clear();
            assertEquals(0, events.size());
            sc.removeStatements(uriA, uriB, uriC, uriA);
            sc.commit();
            sc.begin();
            assertEquals(1, events.size());
            event = events.iterator().next();
            assertFalse(event.statementsAdded());
            assertTrue(event.statementsRemoved());
            events.clear();
            assertEquals(0, events.size());
            sc.clear();
            sc.commit();
            sc.begin();
            assertEquals(1, events.size());
            event = events.iterator().next();
            assertFalse(event.statementsAdded());
            assertTrue(event.statementsRemoved());
        } finally {
            sc.rollback();
            sc.close();
        }
    }
}
Also used : NotifyingSail(org.openrdf.sail.NotifyingSail) SailChangedEvent(org.openrdf.sail.SailChangedEvent) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) SailChangedListener(org.openrdf.sail.SailChangedListener) URI(org.openrdf.model.URI) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 URI (org.openrdf.model.URI)1 NotifyingSail (org.openrdf.sail.NotifyingSail)1 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)1 SailChangedEvent (org.openrdf.sail.SailChangedEvent)1 SailChangedListener (org.openrdf.sail.SailChangedListener)1 SailConnection (org.openrdf.sail.SailConnection)1