-
Notifications
You must be signed in to change notification settings - Fork 14
/
JenaConnectionTest.java
278 lines (238 loc) · 9.17 KB
/
JenaConnectionTest.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cz.cvut.kbss.ontodriver.jena;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.jena.util.ConnectionListener;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class JenaConnectionTest {
private static final NamedResource SUBJECT = NamedResource.create(Generator.generateUri());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Mock
private JenaAdapter adapterMock;
private JenaConnection connection;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.connection = new JenaConnection(adapterMock);
}
@Test
public void setAutoCommitThrowsIllegalStateForClosedConnection() throws Exception {
connection.close();
expectClosedException();
connection.setAutoCommit(false);
}
private void expectClosedException() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage(containsString("connection is closed"));
}
@Test
public void isAutoCommitThrowsIllegalStateForClosedConnection() throws Exception {
connection.close();
expectClosedException();
connection.isAutoCommit();
}
@Test
public void closeClosesUnderlyingAdapter() throws Exception {
connection.close();
assertFalse(connection.isOpen());
verify(adapterMock).close();
}
@Test
public void commitCommitsUnderlyingAdapter() throws Exception {
connection.commit();
verify(adapterMock).commit();
}
@Test
public void commitDoesNothingForAutoCommitConnection() throws Exception {
connection.setAutoCommit(true);
connection.commit();
verify(adapterMock, never()).commit();
}
@Test
public void commitThrowsIllegalStateExceptionForClosedConnection() throws Exception {
connection.close();
expectClosedException();
connection.commit();
}
@Test
public void rollbackRollsBackChangesInAdapter() {
connection.rollback();
verify(adapterMock).rollback();
}
@Test
public void rollbackDoesNothingForAutoCommitConnection() {
connection.setAutoCommit(true);
connection.rollback();
verify(adapterMock, never()).rollback();
}
@Test
public void rollbackThrowsIllegalStateExceptionForClosedConnection() throws Exception {
connection.close();
expectClosedException();
connection.rollback();
}
@Test
public void persistPassesValueDescriptorToUnderlyingAdapter() throws Exception {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
connection.persist(descriptor);
verify(adapterMock).persist(descriptor);
}
@Test
public void persistCommitsTransactionIfConnectionIsAutoCommit() throws Exception {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
connection.setAutoCommit(true);
connection.persist(descriptor);
final InOrder inOrder = Mockito.inOrder(adapterMock);
inOrder.verify(adapterMock).persist(descriptor);
inOrder.verify(adapterMock).commit();
}
@Test
public void persistThrowsIllegalStateExceptionForClosedConnection() throws Exception {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
connection.close();
expectClosedException();
connection.persist(descriptor);
}
@Test
public void closeNotifiesRegisteredListeners() throws Exception {
final ConnectionListener listener = mock(ConnectionListener.class);
connection.registerListener(listener);
connection.close();
verify(listener).connectionClosed(connection);
}
@Test
public void unwrapReturnsConnectionInstanceIfClassMatches() throws Exception {
final JenaConnection result = connection.unwrap(JenaConnection.class);
assertSame(connection, result);
}
@Test
public void unwrapPassesCallToAdapterWhenClassDoesNotMatch() throws Exception {
connection.unwrap(StorageConnector.class);
verify(adapterMock).unwrap(StorageConnector.class);
}
@Test
public void unwrapThrowsIllegalStateExceptionForClosedConnection() throws Exception {
connection.close();
expectClosedException();
connection.unwrap(StorageConnector.class);
}
@Test
public void containsCallsAdapterWithArguments() throws Exception {
final Axiom<?> axiom = new AxiomImpl<>(SUBJECT, Assertion.createClassAssertion(false),
new Value<>(NamedResource.create(Generator.generateUri())));
final URI context = Generator.generateUri();
connection.contains(axiom, context);
verify(adapterMock).contains(axiom, context);
}
@Test
public void containsThrowsIllegalStateExceptionForClosedConnection() throws Exception {
final Axiom<?> axiom = new AxiomImpl<>(SUBJECT, Assertion.createClassAssertion(false),
new Value<>(NamedResource.create(Generator.generateUri())));
final URI context = Generator.generateUri();
connection.close();
expectClosedException();
connection.contains(axiom, context);
}
@Test
public void getContextsCallsAdapter() {
final List<URI> contexts = Collections.singletonList(Generator.generateUri());
when(adapterMock.getContext()).thenReturn(contexts);
final List<URI> result = connection.getContexts();
verify(adapterMock).getContext();
assertEquals(contexts, result);
}
@Test
public void getContextsThrowsIllegalStateExceptionForClosedConnection() throws Exception {
connection.close();
expectClosedException();
try {
connection.getContexts();
} finally {
verify(adapterMock, never()).getContext();
}
}
@Test
public void generateIdentifierCallsAdapter() {
final URI uri = Generator.generateUri();
when(adapterMock.generateIdentifier(uri)).thenReturn(uri);
final URI result = connection.generateIdentifier(uri);
assertNotNull(result);
verify(adapterMock).generateIdentifier(uri);
}
@Test
public void generateIdentifierThrowsIllegalStateExceptionForClosedConnection() throws Exception {
connection.close();
expectClosedException();
connection.generateIdentifier(Generator.generateUri());
}
@Test
public void findCallsAdapterWithDescriptor() throws Exception {
final AxiomDescriptor descriptor = new AxiomDescriptor(SUBJECT);
connection.find(descriptor);
verify(adapterMock).find(descriptor);
}
@Test
public void findThrowsIllegalStateExceptionForClosedConnection() throws Exception {
connection.close();
expectClosedException();
final AxiomDescriptor descriptor = new AxiomDescriptor(SUBJECT);
connection.find(descriptor);
}
@Test
public void typesReturnsTypesHandler() {
final JenaTypes types = connection.types();
assertNotNull(types);
}
@Test
public void removeCallsAdapterWithDescriptor() throws Exception {
final AxiomDescriptor descriptor = new AxiomDescriptor(SUBJECT);
connection.remove(descriptor);
verify(adapterMock).remove(descriptor);
}
@Test
public void removeCommitsTransactionWhenConnectionIsAutoCommit() throws Exception {
connection.setAutoCommit(true);
final AxiomDescriptor descriptor = new AxiomDescriptor(SUBJECT);
connection.remove(descriptor);
final InOrder inOrder = inOrder(adapterMock);
inOrder.verify(adapterMock).remove(descriptor);
inOrder.verify(adapterMock).commit();
}
@Test
public void updatePassesDataToAdapter() throws Exception {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
connection.update(descriptor);
verify(adapterMock).update(descriptor);
}
@Test
public void updateCommitsTransactionWhenConnectionIsAutoCommit() throws Exception {
final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT);
connection.setAutoCommit(true);
connection.update(descriptor);
final InOrder inOrder = inOrder(adapterMock);
inOrder.verify(adapterMock).update(descriptor);
inOrder.verify(adapterMock).commit();
}
@Test
public void updateThrowsIllegalStateExceptionForClosedConnection() throws Exception {
connection.close();
expectClosedException();
connection.update(new AxiomValueDescriptor(SUBJECT));
}
}