1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rdf.api;
19
20 import static org.junit.Assert.*;
21
22 import java.util.Locale;
23 import java.util.Objects;
24 import java.util.Optional;
25
26 import org.junit.Assume;
27 import org.junit.Before;
28 import org.junit.Test;
29
30
31
32
33
34
35
36
37
38
39 public abstract class AbstractRDFTest {
40
41 private RDF factory;
42
43
44
45
46
47
48
49
50 protected abstract RDF createFactory();
51
52 @Before
53 public void setUp() {
54 factory = createFactory();
55 }
56
57 @Test
58 public void testCreateBlankNode() throws Exception {
59 final BlankNode bnode = factory.createBlankNode();
60
61 final BlankNode bnode2 = factory.createBlankNode();
62 assertNotEquals("Second blank node has not got a unique internal identifier", bnode.uniqueReference(),
63 bnode2.uniqueReference());
64 }
65
66 @Test
67 public void testCreateBlankNodeIdentifierEmpty() throws Exception {
68 try {
69 factory.createBlankNode("");
70 } catch (final IllegalArgumentException e) {
71
72 }
73 }
74
75 @Test
76 public void testCreateBlankNodeIdentifier() throws Exception {
77 factory.createBlankNode("example1");
78 }
79
80 @Test
81 public void testCreateBlankNodeIdentifierTwice() throws Exception {
82 BlankNode bnode1, bnode2, bnode3;
83 bnode1 = factory.createBlankNode("example1");
84 bnode2 = factory.createBlankNode("example1");
85 bnode3 = factory.createBlankNode("differ");
86
87 assertEquals(bnode1.uniqueReference(), bnode2.uniqueReference());
88
89 assertEquals(bnode1.ntriplesString(), bnode2.ntriplesString());
90
91 assertNotEquals(bnode1.uniqueReference(), bnode3.uniqueReference());
92 assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString());
93 }
94
95 @Test
96 public void testCreateBlankNodeIdentifierTwiceDifferentFactories() throws Exception {
97 BlankNode bnode1, differentFactory;
98 bnode1 = factory.createBlankNode();
99
100 differentFactory = createFactory().createBlankNode();
101
102
103
104
105
106
107
108
109
110 assertNotEquals(bnode1, differentFactory);
111 assertNotEquals(bnode1.uniqueReference(), differentFactory.uniqueReference());
112
113
114
115 }
116
117 @Test
118 public void testCreateGraph() throws Exception {
119 try (final Graph graph = factory.createGraph(); final Graph graph2 = factory.createGraph()) {
120
121 assertEquals("Graph was not empty", 0, graph.size());
122 graph.add(factory.createBlankNode(), factory.createIRI("http://example.com/"), factory.createBlankNode());
123
124 assertNotSame(graph, graph2);
125 assertEquals("Graph was empty after adding", 1, graph.size());
126 assertEquals("New graph was not empty", 0, graph2.size());
127 }
128 }
129
130 @Test
131 public void testCreateIRI() throws Exception {
132 final IRI example = factory.createIRI("http://example.com/");
133
134 assertEquals("http://example.com/", example.getIRIString());
135 assertEquals("<http://example.com/>", example.ntriplesString());
136
137 final IRI term = factory.createIRI("http://example.com/vocab#term");
138 assertEquals("http://example.com/vocab#term", term.getIRIString());
139 assertEquals("<http://example.com/vocab#term>", term.ntriplesString());
140
141
142
143 final IRI latin1 = factory.createIRI("http://accént.example.com/première");
144 assertEquals("http://accént.example.com/première", latin1.getIRIString());
145 assertEquals("<http://accént.example.com/première>", latin1.ntriplesString());
146
147 final IRI cyrillic = factory.createIRI("http://example.испытание/Кириллица");
148 assertEquals("http://example.испытание/Кириллица", cyrillic.getIRIString());
149 assertEquals("<http://example.испытание/Кириллица>", cyrillic.ntriplesString());
150
151 final IRI deseret = factory.createIRI("http://𐐀.example.com/𐐀");
152 assertEquals("http://𐐀.example.com/𐐀", deseret.getIRIString());
153 assertEquals("<http://𐐀.example.com/𐐀>", deseret.ntriplesString());
154 }
155
156 @Test
157 public void testCreateLiteral() throws Exception {
158 final Literal example = factory.createLiteral("Example");
159 assertEquals("Example", example.getLexicalForm());
160 assertFalse(example.getLanguageTag().isPresent());
161 assertEquals("http://www.w3.org/2001/XMLSchema#string", example.getDatatype().getIRIString());
162
163 assertEquals("\"Example\"", example.ntriplesString());
164 }
165
166 @Test
167 public void testCreateLiteralDateTime() throws Exception {
168 final Literal dateTime = factory.createLiteral("2014-12-27T00:50:00T-0600",
169 factory.createIRI("http://www.w3.org/2001/XMLSchema#dateTime"));
170 assertEquals("2014-12-27T00:50:00T-0600", dateTime.getLexicalForm());
171 assertFalse(dateTime.getLanguageTag().isPresent());
172 assertEquals("http://www.w3.org/2001/XMLSchema#dateTime", dateTime.getDatatype().getIRIString());
173 assertEquals("\"2014-12-27T00:50:00T-0600\"^^<http://www.w3.org/2001/XMLSchema#dateTime>",
174 dateTime.ntriplesString());
175 }
176
177 @Test
178 public void testCreateLiteralLang() throws Exception {
179 final Literal example = factory.createLiteral("Example", "en");
180
181 assertEquals("Example", example.getLexicalForm());
182 assertEquals("en", example.getLanguageTag().get());
183 assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", example.getDatatype().getIRIString());
184 assertEquals("\"Example\"@en", example.ntriplesString());
185 }
186
187 @Test
188 public void testCreateLiteralLangISO693_3() throws Exception {
189
190 final Literal vls = factory.createLiteral("Herbert Van de Sompel", "vls");
191
192 assertEquals("vls", vls.getLanguageTag().get());
193 assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", vls.getDatatype().getIRIString());
194 assertEquals("\"Herbert Van de Sompel\"@vls", vls.ntriplesString());
195 }
196
197
198 private void assertEqualsBothWays(final Object a, final Object b) {
199 assertEquals(a, b);
200 assertEquals(b, a);
201
202 assertEquals(a.hashCode(), b.hashCode());
203 }
204
205 @Test
206 public void testCreateLiteralLangCaseInsensitive() throws Exception {
207
208
209
210
211
212 final Literal upper = factory.createLiteral("Hello", "EN-GB");
213 final Literal lower = factory.createLiteral("Hello", "en-gb");
214 final Literal mixed = factory.createLiteral("Hello", "en-GB");
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 assertEquals("en-gb",
232 lower.getLanguageTag().get().toLowerCase(Locale.ROOT));
233 assertEquals("en-gb",
234 upper.getLanguageTag().get().toLowerCase(Locale.ROOT));
235 assertEquals("en-gb",
236 mixed.getLanguageTag().get().toLowerCase(Locale.ROOT));
237
238
239 assertEquals(lower, lower);
240 assertEqualsBothWays(lower, upper);
241 assertEqualsBothWays(lower, mixed);
242 assertEquals(upper, upper);
243 assertEqualsBothWays(upper, mixed);
244 assertEquals(mixed, mixed);
245
246
247 }
248
249 @Test
250 public void testCreateLiteralLangCaseInsensitiveOther() throws Exception {
251
252
253
254 final Literal upper = factory.createLiteral("Hello", "EN-GB");
255 final Literal lower = factory.createLiteral("Hello", "en-gb");
256 final Literal mixed = factory.createLiteral("Hello", "en-GB");
257
258 final Literal otherLiteral = new Literal() {
259 @Override
260 public String ntriplesString() {
261 return "Hello@eN-Gb";
262 }
263 @Override
264 public String getLexicalForm() {
265 return "Hello";
266 }
267 @Override
268 public Optional<String> getLanguageTag() {
269 return Optional.of("eN-Gb");
270 }
271 @Override
272 public IRI getDatatype() {
273 return factory.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
274 }
275 @Override
276 public boolean equals(final Object obj) {
277 throw new RuntimeException("Wrong way comparison of literal");
278 }
279 };
280
281
282
283 assertEquals(mixed, otherLiteral);
284 assertEquals(lower, otherLiteral);
285 assertEquals(upper, otherLiteral);
286 }
287
288 @Test
289 public void testCreateLiteralLangCaseInsensitiveInTurkish() throws Exception {
290
291
292
293
294 final Locale defaultLocale = Locale.getDefault();
295 try {
296 Locale.setDefault(Locale.ROOT);
297 final Literal mixedROOT = factory.createLiteral("moi", "fI");
298 final Literal lowerROOT = factory.createLiteral("moi", "fi");
299 final Literal upperROOT = factory.createLiteral("moi", "FI");
300
301 final Locale turkish = Locale.forLanguageTag("TR");
302 Locale.setDefault(turkish);
303
304
305
306 Assume.assumeFalse("FI".toLowerCase().equals("fi"));
307
308 final Literal mixed = factory.createLiteral("moi", "fI");
309 final Literal lower = factory.createLiteral("moi", "fi");
310 final Literal upper = factory.createLiteral("moi", "FI");
311
312 assertEquals(lower, lower);
313 assertEqualsBothWays(lower, upper);
314 assertEqualsBothWays(lower, mixed);
315
316 assertEquals(upper, upper);
317 assertEqualsBothWays(upper, mixed);
318
319 assertEquals(mixed, mixed);
320
321
322
323
324 assertEqualsBothWays(lower, lowerROOT);
325 assertEqualsBothWays(upper, lowerROOT);
326 assertEqualsBothWays(mixed, lowerROOT);
327
328 assertEqualsBothWays(lower, upperROOT);
329 assertEqualsBothWays(upper, upperROOT);
330 assertEqualsBothWays(mixed, upperROOT);
331
332 assertEqualsBothWays(lower, mixedROOT);
333 assertEqualsBothWays(upper, mixedROOT);
334 assertEqualsBothWays(mixed, mixedROOT);
335 } finally {
336 Locale.setDefault(defaultLocale);
337 }
338 }
339
340 @Test
341 public void testCreateLiteralString() throws Exception {
342 final Literal example = factory.createLiteral("Example",
343 factory.createIRI("http://www.w3.org/2001/XMLSchema#string"));
344 assertEquals("Example", example.getLexicalForm());
345 assertFalse(example.getLanguageTag().isPresent());
346 assertEquals("http://www.w3.org/2001/XMLSchema#string", example.getDatatype().getIRIString());
347
348 assertEquals("\"Example\"", example.ntriplesString());
349 }
350
351 @Test
352 public void testCreateTripleBnodeBnode() {
353 final BlankNode subject = factory.createBlankNode("b1");
354 final IRI predicate = factory.createIRI("http://example.com/pred");
355 final BlankNode object = factory.createBlankNode("b2");
356 final Triple triple = factory.createTriple(subject, predicate, object);
357
358
359
360 assertEquals(subject, triple.getSubject());
361 assertEquals(predicate, triple.getPredicate());
362 assertEquals(object, triple.getObject());
363 }
364
365 @Test
366 public void testCreateTripleBnodeIRI() {
367 final BlankNode subject = factory.createBlankNode("b1");
368 final IRI predicate = factory.createIRI("http://example.com/pred");
369 final IRI object = factory.createIRI("http://example.com/obj");
370 final Triple triple = factory.createTriple(subject, predicate, object);
371
372
373
374 assertEquals(subject, triple.getSubject());
375 assertEquals(predicate, triple.getPredicate());
376 assertEquals(object, triple.getObject());
377 }
378
379 @Test
380 public void testCreateTripleBnodeTriple() {
381 final BlankNode subject = factory.createBlankNode();
382 final IRI predicate = factory.createIRI("http://example.com/pred");
383 final Literal object = factory.createLiteral("Example", "en");
384 final Triple triple = factory.createTriple(subject, predicate, object);
385
386
387
388 assertEquals(subject, triple.getSubject());
389 assertEquals(predicate, triple.getPredicate());
390 assertEquals(object, triple.getObject());
391 }
392
393 @Test
394 public void testPossiblyInvalidBlankNode() throws Exception {
395 BlankNode withColon;
396 try {
397 withColon = factory.createBlankNode("with:colon");
398 } catch (final IllegalArgumentException ex) {
399
400 return;
401 }
402
403
404 assertFalse(withColon.ntriplesString().contains("with:colon"));
405
406
407 assertEquals(withColon.ntriplesString(), factory.createBlankNode("with:colon").ntriplesString());
408 }
409
410 @Test(expected = IllegalArgumentException.class)
411 public void testInvalidIRI() throws Exception {
412 factory.createIRI("<no_brackets>");
413 }
414
415 @Test(expected = IllegalArgumentException.class)
416 public void testInvalidLiteralLang() throws Exception {
417 factory.createLiteral("Example", "with space");
418 }
419
420 @Test(expected = Exception.class)
421 public void testInvalidTriplePredicate() {
422 final BlankNode subject = factory.createBlankNode("b1");
423 final BlankNode predicate = factory.createBlankNode("b2");
424 final BlankNode object = factory.createBlankNode("b3");
425 factory.createTriple(subject, (IRI) predicate, object);
426 }
427
428 @Test
429 public void hashCodeBlankNode() throws Exception {
430 final BlankNode bnode1 = factory.createBlankNode();
431 assertEquals(bnode1.uniqueReference().hashCode(), bnode1.hashCode());
432 }
433
434 @Test
435 public void hashCodeIRI() throws Exception {
436 final IRI iri = factory.createIRI("http://example.com/");
437 assertEquals(iri.getIRIString().hashCode(), iri.hashCode());
438 }
439
440 @Test
441 public void hashCodeLiteral() throws Exception {
442 final Literal literal = factory.createLiteral("Hello");
443 assertEquals(Objects.hash(literal.getLexicalForm(), literal.getDatatype(), literal.getLanguageTag()),
444 literal.hashCode());
445 }
446
447 @Test
448 public void hashCodeTriple() throws Exception {
449 final IRI iri = factory.createIRI("http://example.com/");
450 final Triple triple = factory.createTriple(iri, iri, iri);
451 assertEquals(Objects.hash(iri, iri, iri), triple.hashCode());
452 }
453
454 }