-
Notifications
You must be signed in to change notification settings - Fork 14
/
SelectResultSetTest.java
587 lines (511 loc) · 21.4 KB
/
SelectResultSetTest.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* 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.
*
* 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.jena.query;
import cz.cvut.kbss.ontodriver.exception.VariableNotBoundException;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.NoSuchElementException;
import static org.apache.jena.rdf.model.ResourceFactory.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class SelectResultSetTest {
private static final Resource SUBJECT = createResource(Generator.generateUri().toString());
private static final Resource TYPE_ONE = createResource(Generator.generateUri().toString());
private static final Property PROPERTY = createProperty(Generator.generateUri().toString());
private static final String QUERY = "SELECT * WHERE { ?x ?y ?z . }";
private static final String OPTIONAL_QUERY =
"SELECT ?x ?y ?z WHERE { ?x a ?y . OPTIONAL { ?x <" + PROPERTY.getURI() + "> ?z . }}";
@Rule
public ExpectedException thrown = ExpectedException.none();
@Mock
private JenaStatement statement;
private Model model;
private QueryExecution execution;
private ResultSet resultSet;
private SelectResultSet selectResult;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.model = ModelFactory.createDefaultModel();
}
@After
public void tearDown() {
if (!execution.isClosed()) {
execution.close();
}
}
private SelectResultSet resultFor(String query) {
this.execution = spy(QueryExecutionFactory.create(query, model));
this.resultSet = spy(execution.execSelect());
final SelectResultSet rs = new SelectResultSet(execution, resultSet);
rs.setStatement(statement);
return rs;
}
@Test
public void closeClosesUnderlyingJenaResultSet() throws Exception {
this.selectResult = resultFor(QUERY);
assertTrue(selectResult.isOpen());
selectResult.close();
verify(execution).close();
assertFalse(selectResult.isOpen());
}
@Test
public void findColumnReturnsIndexOfColumnWithSpecifiedName() {
this.selectResult = resultFor(QUERY);
assertEquals(0, selectResult.findColumn("x"));
}
@Test
public void getColumnCountReturnsNumberOfBindings() {
this.selectResult = resultFor(QUERY);
assertEquals(3, selectResult.getColumnCount());
}
@Test
public void hasNextReturnsTrueWhenResultSetHasMoreElements() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
assertTrue(selectResult.hasNext());
}
@Test
public void hasNextReturnsFalseWhenNoMoreResultsAreFound() {
this.selectResult = resultFor(QUERY);
assertFalse(selectResult.hasNext());
}
@Test
public void nextMovesResultSetCursorToNextRow() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
assertTrue(selectResult.hasNext());
selectResult.next();
assertFalse(selectResult.hasNext());
}
@Test
public void nextThrowsNoSuchElementExceptionWhenNextIsCalledButNoMoreRowsAreAvailable() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
thrown.expect(NoSuchElementException.class);
selectResult.next();
selectResult.next();
}
@Test
public void relativeMovesSpecifiedNumberOfRowsForward() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
this.selectResult = resultFor(QUERY);
selectResult.next();
assertEquals(0, selectResult.getRowIndex());
reset(resultSet);
selectResult.relative(2);
verify(resultSet, times(2)).next();
assertEquals(2, selectResult.getRowIndex());
}
@Test
public void relativeThrowsUnsupportedOperationForNegativeArgument() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
this.selectResult = resultFor(QUERY);
selectResult.next();
assertEquals(0, selectResult.getRowIndex());
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage(containsString("Moving back is not supported by this result set."));
selectResult.relative(-2);
}
@Test
public void setRowIndexMovesToTargetRowIndex() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
this.selectResult = resultFor(QUERY);
selectResult.next();
assertEquals(0, selectResult.getRowIndex());
reset(resultSet);
selectResult.setRowIndex(1);
assertEquals(1, selectResult.getRowIndex());
verify(resultSet).next();
}
@Test
public void setRowIndexThrowsUnsupportedOperationForIndexLessThanCurrent() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
this.selectResult = resultFor(QUERY);
selectResult.next();
selectResult.next();
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage(containsString("Moving back is not supported by this result set."));
selectResult.setRowIndex(0);
}
@Test
public void setRowIndexThrowsNoSuchElementWhenTargetIndexExceedsRowCount() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
this.selectResult = resultFor(QUERY);
thrown.expect(NoSuchElementException.class);
selectResult.setRowIndex(10);
}
@Test
public void lastMovesResultToLastExistingRow() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
this.selectResult = resultFor(QUERY);
assertTrue(selectResult.hasNext());
selectResult.last();
assertFalse(selectResult.hasNext());
}
@Test
public void isFirstReturnsTrueForFirstRowInResult() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
model.add(SUBJECT, RDF.type, createResource(Generator.generateUri().toString()));
this.selectResult = resultFor(QUERY);
assertFalse(selectResult.isFirst());
selectResult.next();
assertTrue(selectResult.isFirst());
selectResult.next();
assertFalse(selectResult.isFirst());
}
@Test
public void getStringByIndexReturnsLiteralAsString() {
final int value = 117;
model.add(SUBJECT, PROPERTY, createTypedLiteral(value));
this.selectResult = resultFor(QUERY);
selectResult.next();
assertEquals(Integer.toString(value), selectResult.getString(2));
}
@Test
public void getStringByUnknownIndexThrowsIllegalArgumentException() {
final int value = 117;
model.add(SUBJECT, PROPERTY, createTypedLiteral(value));
this.selectResult = resultFor(QUERY);
selectResult.next();
thrown.expect(IllegalArgumentException.class);
int index = 4;
thrown.expectMessage(containsString("Variable index " + index + " is out of bounds."));
selectResult.getString(index);
}
@Test
public void getStringByIndexReturnsResourceAsString() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
selectResult.next();
assertEquals(TYPE_ONE.getURI(), selectResult.getString(2));
}
@Test
public void getStringByVariableReturnsLiteralAsString() {
final int value = 117;
model.add(SUBJECT, PROPERTY, createTypedLiteral(value));
this.selectResult = resultFor(QUERY);
selectResult.next();
assertEquals(Integer.toString(value), selectResult.getString("z"));
}
@Test
public void getStringByVariableNameThrowsIllegalArgumentExceptionForUnknownVariableName() {
final int value = 117;
model.add(SUBJECT, PROPERTY, createTypedLiteral(value));
this.selectResult = resultFor(QUERY);
selectResult.next();
final String name = "unknown";
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(containsString("Variable \'" + name + "\' not found in the result set."));
selectResult.getString(name);
}
@Test
public void getStringByVariableReturnsResourceAsString() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
selectResult.next();
assertEquals(TYPE_ONE.getURI(), selectResult.getString("z"));
}
@Test
public void getStringByIndexThrowsIllegalStateExceptionWhenCalledBeforeNext() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Must call next before getting any values.");
selectResult.getString(0);
}
@Test
public void getBooleanByIndexReturnsBooleanValue() throws JenaDriverException {
saveValueAndExecuteQuery(true);
assertTrue(selectResult.getBoolean(2));
}
private void saveValueAndExecuteQuery(Object value) {
model.add(SUBJECT, PROPERTY, createTypedLiteral(value));
this.selectResult = resultFor(QUERY);
selectResult.next();
}
@Test
public void getBooleanByVariableReturnsBooleanValue() throws JenaDriverException {
saveValueAndExecuteQuery(true);
assertTrue(selectResult.getBoolean("z"));
}
@Test
public void getLiteralThrowsJenaDriverExceptionWhenResourceIsEncountered() throws JenaDriverException {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
selectResult.next();
thrown.expect(JenaDriverException.class);
thrown.expectMessage("Expected value " + RDF.type + " to be a literal.");
selectResult.getBoolean("y");
}
@Test
public void getByteByIndexReturnsByteValue() throws JenaDriverException {
saveValueAndExecuteQuery(Byte.MAX_VALUE);
assertEquals(Byte.MAX_VALUE, selectResult.getByte(2));
}
@Test
public void getByteByVariableReturnsByValue() throws JenaDriverException {
saveValueAndExecuteQuery(Byte.MAX_VALUE);
assertEquals(Byte.MAX_VALUE, selectResult.getByte("z"));
}
@Test
public void getDoubleByIndexReturnsDoubleValue() throws JenaDriverException {
saveValueAndExecuteQuery(Math.PI);
assertEquals(Math.PI, selectResult.getDouble(2), 0.01);
}
@Test
public void getDoubleByVariableReturnsDoubleValue() throws JenaDriverException {
saveValueAndExecuteQuery(Math.PI);
assertEquals(Math.PI, selectResult.getDouble("z"), 0.01);
}
@Test
public void getFloatByIndexReturnsFloatValue() throws JenaDriverException {
saveValueAndExecuteQuery(117.0f);
assertEquals(117.0f, selectResult.getFloat(2), 0.01f);
}
@Test
public void getFloatByVariableReturnsFloatValue() throws JenaDriverException {
saveValueAndExecuteQuery(117.0f);
assertEquals(117.0f, selectResult.getFloat("z"), 0.01f);
}
@Test
public void getIntByIndexReturnsIntValue() throws JenaDriverException {
saveValueAndExecuteQuery(117);
assertEquals(117, selectResult.getInt(2));
}
@Test
public void getIntByVariableReturnsIntValue() throws JenaDriverException {
saveValueAndExecuteQuery(117);
assertEquals(117, selectResult.getInt("z"));
}
@Test
public void getLongByIndexReturnsLongValue() throws JenaDriverException {
final long value = System.currentTimeMillis();
saveValueAndExecuteQuery(value);
assertEquals(value, selectResult.getLong(2));
}
@Test
public void getLongByVariableReturnsLongValue() throws JenaDriverException {
final long value = System.currentTimeMillis();
saveValueAndExecuteQuery(value);
assertEquals(value, selectResult.getLong("z"));
}
@Test
public void getShortByIndexReturnsShortValue() throws JenaDriverException {
final short value = 117;
saveValueAndExecuteQuery(value);
assertEquals(value, selectResult.getShort(2));
}
@Test
public void getShortByVariableReturnsShortValue() throws JenaDriverException {
final short value = 117;
saveValueAndExecuteQuery(value);
assertEquals(value, selectResult.getShort("z"));
}
@Test
public void getShortRoundsDoubleValueToShort() throws JenaDriverException {
saveValueAndExecuteQuery(Math.PI);
final short result = selectResult.getShort("z");
assertEquals((short) Math.round(Math.PI), result);
}
@Test
public void getIntThrowsNumberFormatExceptionForStringValue() throws JenaDriverException {
// This test is more for documentation purposes - it shows how Jena type resolution works
saveValueAndExecuteQuery("Test");
thrown.expect(NumberFormatException.class);
selectResult.getInt(2);
}
@Test
public void getObjectByIndexReturnsLiteralValue() {
saveValueAndExecuteQuery(117);
final Object result = selectResult.getObject(2);
assertTrue(result instanceof Integer);
assertEquals(117, result);
}
@Test
public void getObjectByIndexReturnsURIForNamedResource() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
selectResult.next();
final Object result = selectResult.getObject(1);
assertTrue(result instanceof URI);
assertEquals(URI.create(RDF.type.getURI()), result);
}
@Test
public void getObjectByIndexReturnsStringValueForAnonymousResource() {
final Resource resource = createResource();
model.add(SUBJECT, RDF.type, resource);
this.selectResult = resultFor(QUERY);
selectResult.next();
final Object result = selectResult.getObject(2);
assertTrue(result instanceof String);
assertEquals(resource.getId().getLabelString(), result);
}
@Test
public void getObjectByVariableReturnsLiteralValue() {
saveValueAndExecuteQuery(117);
final Object result = selectResult.getObject("z");
assertTrue(result instanceof Integer);
assertEquals(117, result);
}
@Test
public void getObjectByIndexAndClassReturnsObjectWhenItCanBeCastToExpectedClass() throws JenaDriverException {
saveValueAndExecuteQuery(117);
final Literal result = selectResult.getObject(2, Literal.class);
assertNotNull(result);
assertEquals(117, result.getInt());
}
@Test
public void getObjectByIndexAndClassReturnsLiteralValueWhenItCanBeCastToExpectedClass() throws JenaDriverException {
saveValueAndExecuteQuery(117);
final Integer result = selectResult.getObject(2, Integer.class);
assertNotNull(result);
assertEquals(117, result.intValue());
}
@Test
public void getObjectByIndexAndClassReturnsURIWhenResourceIsExtractedAndURIIsExpected() throws JenaDriverException {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
selectResult.next();
final URI result = selectResult.getObject(2, URI.class);
assertEquals(URI.create(TYPE_ONE.getURI()), result);
}
@Test
public void getObjectByIndexReturnsStringWhenBlankNodeIsExtractedAndStringIsExpected() throws JenaDriverException {
final Resource resource = createResource();
model.add(SUBJECT, RDF.type, resource);
this.selectResult = resultFor(QUERY);
selectResult.next();
final String result = selectResult.getObject(2, String.class);
assertEquals(resource.getId().getLabelString(), result);
}
@Test
public void getObjectByIndexUsesConstructorWithJenaBasedParameterToBuildResult() throws JenaDriverException {
saveValueAndExecuteQuery(117);
final ResultJenaBased result = selectResult.getObject(2, ResultJenaBased.class);
assertEquals(117, result.value);
}
public static class ResultJenaBased {
private Object value;
public ResultJenaBased(RDFNode node) {
this.value = node.isLiteral() ? node.asLiteral().getValue() : URI.create(node.asResource().getURI());
}
}
@Test
public void getObjectByIndexUsesConstructorWithJavaBasedParameterToBuildResult() throws JenaDriverException {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
selectResult.next();
final ResultJavaBased result = selectResult.getObject(2, ResultJavaBased.class);
assertEquals(URI.create(TYPE_ONE.getURI()), result.value);
}
public static class ResultJavaBased {
private URI value;
public ResultJavaBased(URI value) {
this.value = value;
}
}
@Test
public void getObjectByIndexUsesStringBasedConstructorWhenPresent() throws JenaDriverException {
final Resource resource = createResource();
model.add(SUBJECT, RDF.type, resource);
this.selectResult = resultFor(QUERY);
selectResult.next();
final ResultStringBased result = selectResult.getObject(2, ResultStringBased.class);
assertEquals(resource.getId().getLabelString(), result.value);
}
public static class ResultStringBased {
private String value;
public ResultStringBased(String value) {
this.value = value;
}
}
@Test
public void getObjectByIndexAndClassThrowsJenaDriverExceptionWhenNoMatchingConstructorIsFound() throws Exception {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(QUERY);
selectResult.next();
thrown.expect(JenaDriverException.class);
thrown.expectMessage(
"No suitable constructor for value " + TYPE_ONE + " found in type " + SelectResultSetTest.class);
selectResult.getObject(2, SelectResultSetTest.class);
}
@Test
public void getObjectByVariableAndClassRetrievesValueAsCorrectType() throws JenaDriverException {
saveValueAndExecuteQuery(117);
final ResultJenaBased result = selectResult.getObject("z", ResultJenaBased.class);
assertEquals(117, result.value);
}
@Test
public void getObjectThrowsVariableNotBoundForNonPresentOptionalValue() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(OPTIONAL_QUERY);
selectResult.next();
thrown.expect(VariableNotBoundException.class);
thrown.expectMessage(containsString("not bound in the current result row"));
assertNull(selectResult.getObject(2));
}
@Test
public void isBoundReturnsTrueForBoundVariableIndex() {
saveValueAndExecuteQuery(117);
assertTrue(selectResult.isBound(1));
}
@Test
public void isBoundReturnsFalseForUnboundVariableIndex() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(OPTIONAL_QUERY);
selectResult.next();
assertFalse(selectResult.isBound(2));
}
@Test
public void isBoundReturnsTrueForBoundVariableName() {
saveValueAndExecuteQuery(117);
assertTrue(selectResult.isBound("x"));
}
@Test
public void isBoundReturnsFalseForUnboundVariableName() {
model.add(SUBJECT, RDF.type, TYPE_ONE);
this.selectResult = resultFor(OPTIONAL_QUERY);
selectResult.next();
assertFalse(selectResult.isBound("z"));
}
}