-
Notifications
You must be signed in to change notification settings - Fork 14
/
SesameConnectionTest.java
249 lines (216 loc) · 8.58 KB
/
SesameConnectionTest.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
/**
* Copyright (C) 2020 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame;
import cz.cvut.kbss.ontodriver.Connection;
import cz.cvut.kbss.ontodriver.PreparedStatement;
import cz.cvut.kbss.ontodriver.Statement;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.sesame.query.SesamePreparedStatement;
import cz.cvut.kbss.ontodriver.sesame.query.SesameStatement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
public class SesameConnectionTest {
@Mock
private SesameAdapter adapterMock;
private Connection connection;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
this.connection = new SesameConnection(adapterMock);
}
@Test
public void testClose() throws Exception {
assertTrue(connection.isOpen());
connection.close();
assertFalse(connection.isOpen());
connection.close();
assertFalse(connection.isOpen());
verify(adapterMock).close();
}
@Test
public void testCommit() throws Exception {
connection.commit();
verify(adapterMock).commit();
}
@Test
public void testCommitClosed() throws Exception {
connection.close();
assertFalse(connection.isOpen());
assertThrows(IllegalStateException.class, () -> connection.commit());
verify(adapterMock, never()).commit();
}
@Test
public void testRollback() throws Exception {
connection.rollback();
verify(adapterMock).rollback();
}
@Test
public void testRollbackAutoCommit() throws Exception {
connection.setAutoCommit(true);
connection.rollback();
verify(adapterMock, never()).rollback();
}
@Test
public void testSetAutoCommit() {
connection.setAutoCommit(true);
assertTrue(connection.isAutoCommit());
connection.setAutoCommit(false);
assertFalse(connection.isAutoCommit());
}
@Test
public void testCreateStatement() throws Exception {
final Statement res = connection.createStatement();
assertNotNull(res);
assertTrue(res instanceof SesameStatement);
}
@Test
public void testCreateStatementOnClosed() throws Exception {
connection.close();
assertThrows(IllegalStateException.class, () -> connection.createStatement());
}
@Test
public void testPrepareStatement() throws Exception {
final PreparedStatement res = connection
.prepareStatement("SELECT ?x ? y ?z WHERE { ?x ?y ?z . }");
assertNotNull(res);
assertTrue(res instanceof SesamePreparedStatement);
}
@Test
public void testPrepareStatementEmpty() {
assertThrows(IllegalArgumentException.class, () -> connection.prepareStatement(""));
}
@Test
public void testIsConsistent() throws Exception {
final URI ctx = URI.create("http://contextUri");
when(adapterMock.isConsistent(ctx)).thenReturn(Boolean.TRUE);
boolean res = connection.isConsistent(ctx);
assertTrue(res);
verify(adapterMock).isConsistent(ctx);
}
@Test
public void testGetContexts() throws Exception {
final List<URI> contexts = new ArrayList<>();
contexts.add(URI.create("http://contextOne"));
contexts.add(URI.create("http://contextTwo"));
when(adapterMock.getContexts()).thenReturn(contexts);
final List<URI> res = connection.getContexts();
assertEquals(contexts, res);
verify(adapterMock).getContexts();
}
@Test
public void testGetContextsOnClosed() throws Exception {
connection.close();
assertThrows(IllegalStateException.class, () -> connection.getContexts());
verify(adapterMock, never()).getContexts();
}
@Test
public void testGenerateIdentifier() throws Exception {
final URI classUri = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/owlClassA");
final URI identifier = URI
.create("http://krizik.felk.cvut.cz/ontologies/jopa/owlClassA_instance_1");
when(adapterMock.generateIdentifier(classUri)).thenReturn(identifier);
final URI res = connection.generateIdentifier(classUri);
assertEquals(identifier, res);
verify(adapterMock).generateIdentifier(classUri);
}
@Test
public void testContainsAxiom() throws Exception {
final Axiom<?> ax = mock(Axiom.class);
when(adapterMock.contains(eq(ax), anySet())).thenReturn(Boolean.TRUE);
final boolean res = connection.contains(ax, Collections.emptySet());
assertTrue(res);
verify(adapterMock).contains(ax, Collections.emptySet());
}
@Test
public void testContainsAxiomInContext() throws Exception {
final Axiom<?> ax = mock(Axiom.class);
final Set<URI> context = Collections.singleton(URI.create("http://context.org"));
when(adapterMock.contains(ax, context)).thenReturn(Boolean.TRUE);
final boolean res = connection.contains(ax, context);
assertTrue(res);
verify(adapterMock).contains(ax, context);
}
@Test
public void testFind() throws Exception {
final AxiomDescriptor axDesc = mock(AxiomDescriptor.class);
final List<Axiom<?>> axioms = new ArrayList<>();
axioms.add(mock(Axiom.class));
axioms.add(mock(Axiom.class));
when(adapterMock.find(axDesc)).thenReturn(axioms);
final Collection<Axiom<?>> res = connection.find(axDesc);
assertEquals(axioms, res);
}
@Test
public void testFindNull() throws Exception {
assertThrows(NullPointerException.class, () -> connection.find(null));
verify(adapterMock, never()).find(any(AxiomDescriptor.class));
}
@Test
public void testPersist() throws Exception {
final AxiomValueDescriptor axDesc = mock(AxiomValueDescriptor.class);
connection.persist(axDesc);
verify(adapterMock).persist(axDesc);
}
@Test
public void testPersistAutoCommit() throws Exception {
final AxiomValueDescriptor axDesc = mock(AxiomValueDescriptor.class);
connection.setAutoCommit(true);
connection.persist(axDesc);
verify(adapterMock).persist(axDesc);
verify(adapterMock).commit();
}
@Test
public void testPersistOnClosed() throws Exception {
connection.close();
final AxiomValueDescriptor axDesc = mock(AxiomValueDescriptor.class);
assertThrows(IllegalStateException.class, () -> connection.persist(axDesc));
verify(adapterMock, never()).persist(axDesc);
}
@Test
public void testUpdate() throws Exception {
final AxiomValueDescriptor axDesc = mock(AxiomValueDescriptor.class);
connection.update(axDesc);
verify(adapterMock).update(axDesc);
}
@Test
public void testUpdateAutoCommit() throws Exception {
final AxiomValueDescriptor axDesc = mock(AxiomValueDescriptor.class);
connection.setAutoCommit(true);
connection.update(axDesc);
verify(adapterMock).update(axDesc);
verify(adapterMock).commit();
}
@Test
public void testRemove() throws Exception {
final AxiomDescriptor axDesc = mock(AxiomDescriptor.class);
connection.remove(axDesc);
verify(adapterMock).remove(axDesc);
}
@Test
public void unwrapReturnsConnectionIfItMatchesTheClass() throws Exception {
assertSame(connection, connection.unwrap(SesameConnection.class));
}
@Test
public void unwrapPassesCallAlongWhenConnectionDoesNotMatch() throws Exception {
connection.unwrap(SesameAdapter.class);
verify(adapterMock).unwrap(SesameAdapter.class);
}
}