-
Notifications
You must be signed in to change notification settings - Fork 14
/
ChangeTrackingStorageConnectorTest.java
247 lines (223 loc) · 11.3 KB
/
ChangeTrackingStorageConnectorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.config.Configuration;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.*;
import static org.apache.jena.rdf.model.ResourceFactory.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
public class ChangeTrackingStorageConnectorTest {
private SharedStorageConnector centralConnector;
private ChangeTrackingStorageConnector connector;
@Before
public void setUp() {
final Configuration configuration = StorageTestUtil.createConfiguration("test:uri");
this.centralConnector = new SharedStorageConnector(configuration);
this.connector = new ChangeTrackingStorageConnector(centralConnector);
}
@After
public void tearDown() {
centralConnector.close();
}
@Test
public void beginCreatesNewLocalModel() throws Exception {
assertNull(getLocalModel());
connector.begin();
assertNotNull(getLocalModel());
assertTrue(connector.transaction.isActive());
}
private LocalModel getLocalModel() throws Exception {
final Field field = ChangeTrackingStorageConnector.class.getDeclaredField("localModel");
field.setAccessible(true);
return (LocalModel) field.get(connector);
}
@Test
public void findEnhancesResultFromCentralConnectorWithTransactionalChangesFromLocalModel() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
final Collection<Statement> result = connector.find(createResource(RESOURCE), null, null);
assertEquals(2, result.size());
assertTrue(result.contains(existing));
assertTrue(result.contains(added));
}
@Test
public void findEnhancesResultFromCentralConnectorWithTransactionalChangesFromLocalModel_Context()
throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
final Collection<Statement> result = connector.find(createResource(RESOURCE), null, null, NAMED_GRAPH);
assertEquals(1, result.size());
assertTrue(result.contains(added));
}
@Test
public void containsReturnsTrueForStatementsPresentInLocalChanges() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
assertTrue(connector.contains(null, null, added.getObject()));
}
@Test
public void containsChecksCentralConnectorWhenLocalChangesDoNotContainValue() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
assertTrue(connector.contains(null, null, createResource(TYPE_ONE)));
}
@Test
public void containsReturnsFalseForStatementLocallyRemoved() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
final Statement removed = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
getLocalModel().removeStatements(Collections.singletonList(removed));
assertFalse(connector.contains(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE), null));
}
@Test
public void containsWorksInContextAsWell() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing), NAMED_GRAPH);
centralConnector.commit();
connector.begin();
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
assertFalse(connector.contains(null, null, added.getObject(), NAMED_GRAPH));
}
@Test
public void addAddsStatementsToLocalModel() throws Exception {
final Statement statement = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.begin();
assertTrue(getLocalModel().getAdded().isEmpty());
connector.add(Collections.singletonList(statement));
assertTrue(getLocalModel().getAdded().getDefaultModel().contains(statement));
}
@Test
public void addAddsStatementsToCorrectContextInLocalModel() throws Exception {
final Statement statement = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.begin();
assertTrue(getLocalModel().getAdded().isEmpty());
connector.add(Collections.singletonList(statement), NAMED_GRAPH);
assertTrue(getLocalModel().getAdded().getNamedModel(NAMED_GRAPH).contains(statement));
}
@Test
public void removeAddsStatementsToLocalModelForRemoval() throws Exception {
final Statement statement = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
connector.begin();
assertTrue(getLocalModel().getRemoved().isEmpty());
connector.remove(Collections.singletonList(statement));
assertTrue(getLocalModel().getRemoved().getDefaultModel().contains(statement));
}
@Test
public void removeAddsStatementsForRemovalToCorrectContextInLocalModel() throws Exception {
final Statement statement = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
connector.begin();
assertTrue(getLocalModel().getRemoved().isEmpty());
connector.remove(Collections.singletonList(statement), NAMED_GRAPH);
assertTrue(getLocalModel().getRemoved().getNamedModel(NAMED_GRAPH).contains(statement));
}
@Test
public void rollbackDiscardsLocalModel() throws Exception {
final Statement statement = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.begin();
connector.add(Collections.singletonList(statement));
connector.rollback();
assertNull(getLocalModel());
assertFalse(connector.transaction.isActive());
}
@Test
public void commitAppliesChangesFromLocalModelToCentralConnector() throws Exception {
final Statement removed = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.begin();
centralConnector.add(Collections.singletonList(removed));
centralConnector.commit();
connector.begin();
getLocalModel().removeStatements(Collections.singletonList(removed));
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
connector.commit();
assertTrue(centralConnector.contains(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO)));
assertFalse(centralConnector.contains(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE)));
}
@Test
public void commitAppliesChangesInContexts() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
connector.commit();
assertTrue(centralConnector.contains(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO), NAMED_GRAPH));
}
@Test
public void commitDiscardsLocalModelOnSuccessfulFinish() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
connector.commit();
assertNull(getLocalModel());
assertFalse(connector.transaction.isActive());
}
@Test(expected = JenaDriverException.class)
public void commitRollsBackChangesOnException() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
centralConnector.storage = spy(centralConnector.storage);
doThrow(new JenaDriverException("Write failed.")).when(centralConnector.storage).writeChanges();
try {
connector.commit();
} finally {
assertFalse(centralConnector.contains(createResource(RESOURCE), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO), NAMED_GRAPH));
}
}
}