1313import static org.assertj.core.api.Assertions.assertThat;
1414import static org.junit.jupiter.api.Assertions.assertEquals;
1515
16+ import java.util.ArrayList;
17+ import java.util.HashSet;
18+ import java.util.List;
19+ import java.util.Set;
20+
21+ import org.eclipse.rdf4j.model.Statement;
1622import org.eclipse.rdf4j.model.vocabulary.RDF;
1723import org.eclipse.rdf4j.model.vocabulary.RDFS;
24+ import org.eclipse.rdf4j.repository.sail.SailRepository;
25+ import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
1826import org.eclipse.rdf4j.sail.NotifyingSail;
27+ import org.eclipse.rdf4j.sail.NotifyingSailConnection;
1928import org.eclipse.rdf4j.sail.SailChangedEvent;
2029import org.eclipse.rdf4j.sail.SailChangedListener;
30+ import org.eclipse.rdf4j.sail.SailConnectionListener;
2131import org.eclipse.rdf4j.sail.SailException;
2232import org.junit.jupiter.api.BeforeEach;
2333import org.junit.jupiter.api.Test;
@@ -36,6 +46,7 @@ public abstract class RDFNotifyingStoreTest extends RDFStoreTest implements Sail
3646 private int removeEventCount;
3747
3848 private int addEventCount;
49+ private SailRepository repo;
3950
4051 /*---------*
4152 * Methods *
@@ -54,7 +65,9 @@ public abstract class RDFNotifyingStoreTest extends RDFStoreTest implements Sail
5465 public void addSailChangedListener() {
5566 // set self as listener
5667 ((NotifyingSail) sail).addSailChangedListener(this);
57-
68+ removeEventCount = 0;
69+ addEventCount = 0;
70+ this.repo = new SailRepository(sail);
5871 }
5972
6073 @Test
@@ -99,6 +112,154 @@ public void testNotifyingRemoveAndClear() {
99112 assertEquals(3, removeEventCount, "There should have been 3 events in which statements were removed");
100113 }
101114
115+ @Test
116+ public void testUpdateQuery() {
117+
118+ try (SailRepositoryConnection connection = repo.getConnection()) {
119+ connection.begin();
120+ connection.add(painter, RDF.TYPE, RDFS.CLASS);
121+ connection.add(painting, RDF.TYPE, RDFS.CLASS);
122+ connection.add(picasso, RDF.TYPE, painter);
123+ connection.add(guernica, RDF.TYPE, painting);
124+ connection.add(picasso, paints, guernica);
125+ connection.commit();
126+
127+ }
128+
129+ addEventCount = 0;
130+ removeEventCount = 0;
131+
132+ try (SailRepositoryConnection connection = repo.getConnection()) {
133+ Set<Statement> added = new HashSet<>();
134+ Set<Statement> removed = new HashSet<>();
135+
136+ List<Statement> addedRaw = new ArrayList<>();
137+ List<Statement> removedRaw = new ArrayList<>();
138+
139+ ((NotifyingSailConnection) connection.getSailConnection())
140+ .addConnectionListener(new SailConnectionListener() {
141+ @Override
142+ public void statementAdded(Statement st) {
143+ boolean add = added.add(st);
144+ if (!add) {
145+ removed.remove(st);
146+ }
147+
148+ addedRaw.add(st);
149+ }
150+
151+ @Override
152+ public void statementRemoved(Statement st) {
153+ boolean add = removed.add(st);
154+ if (!add) {
155+ added.remove(st);
156+ }
157+
158+ removedRaw.add(st);
159+ }
160+ }
161+ );
162+
163+ connection.prepareUpdate("" +
164+ "DELETE {?a ?b ?c}" +
165+ "INSERT {?a ?b ?c}" +
166+ "WHERE {?a ?b ?c}").execute();
167+
168+ System.out.println("Added Raw Size: " + addedRaw.size());
169+ System.out.println("Removed Raw Size: " + removedRaw.size());
170+ System.out.println("Added Raw: " + addedRaw);
171+ System.out.println("Removed Raw: " + removedRaw);
172+ System.out.println("Added Size: " + added.size());
173+ System.out.println("Removed Size: " + removed.size());
174+ System.out.println("Added: " + added);
175+ System.out.println("Removed: " + removed);
176+
177+ assertEquals(5, added.size());
178+ assertEquals(5, removed.size());
179+ assertEquals(5, addedRaw.size());
180+ assertEquals(5, removedRaw.size());
181+
182+ assertEquals(added, removed);
183+
184+ }
185+
186+ assertEquals(5, con.size());
187+
188+ }
189+
190+ @Test
191+ public void testUpdateQuery2() {
192+
193+ try (SailRepositoryConnection connection = repo.getConnection()) {
194+ connection.begin();
195+ connection.add(painter, RDF.TYPE, RDFS.CLASS);
196+ connection.commit();
197+
198+ }
199+
200+ String statement = "<" + painter + "> <" + RDF.TYPE + "> <" + RDFS.CLASS + "> .";
201+
202+ addEventCount = 0;
203+ removeEventCount = 0;
204+
205+ try (SailRepositoryConnection connection = repo.getConnection()) {
206+ Set<Statement> added = new HashSet<>();
207+ Set<Statement> removed = new HashSet<>();
208+
209+ List<Statement> addedRaw = new ArrayList<>();
210+ List<Statement> removedRaw = new ArrayList<>();
211+
212+ ((NotifyingSailConnection) connection.getSailConnection())
213+ .addConnectionListener(new SailConnectionListener() {
214+ @Override
215+ public void statementAdded(Statement st) {
216+ boolean add = added.add(st);
217+ if (!add) {
218+ removed.remove(st);
219+ }
220+
221+ addedRaw.add(st);
222+ }
223+
224+ @Override
225+ public void statementRemoved(Statement st) {
226+ boolean add = removed.add(st);
227+ if (!add) {
228+ added.remove(st);
229+ }
230+
231+ removedRaw.add(st);
232+ }
233+ }
234+ );
235+
236+ connection.prepareUpdate("" +
237+ "DELETE {" + statement + "}" +
238+ "INSERT {" + statement + "}" +
239+ "WHERE {?a ?b ?c}").execute();
240+
241+ System.out.println("Added Raw Size: " + addedRaw.size());
242+ System.out.println("Removed Raw Size: " + removedRaw.size());
243+ System.out.println("Added Raw: " + addedRaw);
244+ System.out.println("Removed Raw: " + removedRaw);
245+ System.out.println("Added Size: " + added.size());
246+ System.out.println("Removed Size: " + removed.size());
247+ System.out.println("Added: " + added);
248+ System.out.println("Removed: " + removed);
249+
250+ assertEquals(1, added.size());
251+ assertEquals(1, removed.size());
252+ assertEquals(1, addedRaw.size());
253+ assertEquals(1, removedRaw.size());
254+
255+ assertEquals(added, removed);
256+
257+ }
258+
259+ assertEquals(1, con.size());
260+
261+ }
262+
102263 @Override
103264 public void sailChanged(SailChangedEvent event) {
104265 if (event.statementsAdded()) {
0 commit comments