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.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertNotSame;
24
25 import java.util.Objects;
26
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() {
119 final Graph graph = 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 final Graph graph2 = factory.createGraph();
125 assertNotSame(graph, graph2);
126 assertEquals("Graph was empty after adding", 1, graph.size());
127 assertEquals("New graph was not empty", 0, graph2.size());
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 @Test
198 public void testCreateLiteralString() throws Exception {
199 final Literal example = factory.createLiteral("Example",
200 factory.createIRI("http://www.w3.org/2001/XMLSchema#string"));
201 assertEquals("Example", example.getLexicalForm());
202 assertFalse(example.getLanguageTag().isPresent());
203 assertEquals("http://www.w3.org/2001/XMLSchema#string", example.getDatatype().getIRIString());
204
205 assertEquals("\"Example\"", example.ntriplesString());
206 }
207
208 @Test
209 public void testCreateTripleBnodeBnode() {
210 final BlankNode subject = factory.createBlankNode("b1");
211 final IRI predicate = factory.createIRI("http://example.com/pred");
212 final BlankNode object = factory.createBlankNode("b2");
213 final Triple triple = factory.createTriple(subject, predicate, object);
214
215
216
217 assertEquals(subject, triple.getSubject());
218 assertEquals(predicate, triple.getPredicate());
219 assertEquals(object, triple.getObject());
220 }
221
222 @Test
223 public void testCreateTripleBnodeIRI() {
224 final BlankNode subject = factory.createBlankNode("b1");
225 final IRI predicate = factory.createIRI("http://example.com/pred");
226 final IRI object = factory.createIRI("http://example.com/obj");
227 final Triple triple = factory.createTriple(subject, predicate, object);
228
229
230
231 assertEquals(subject, triple.getSubject());
232 assertEquals(predicate, triple.getPredicate());
233 assertEquals(object, triple.getObject());
234 }
235
236 @Test
237 public void testCreateTripleBnodeTriple() {
238 final BlankNode subject = factory.createBlankNode();
239 final IRI predicate = factory.createIRI("http://example.com/pred");
240 final Literal object = factory.createLiteral("Example", "en");
241 final Triple triple = factory.createTriple(subject, predicate, object);
242
243
244
245 assertEquals(subject, triple.getSubject());
246 assertEquals(predicate, triple.getPredicate());
247 assertEquals(object, triple.getObject());
248 }
249
250 @Test
251 public void testPossiblyInvalidBlankNode() throws Exception {
252 BlankNode withColon;
253 try {
254 withColon = factory.createBlankNode("with:colon");
255 } catch (final IllegalArgumentException ex) {
256
257 return;
258 }
259
260
261 assertFalse(withColon.ntriplesString().contains("with:colon"));
262
263
264 assertEquals(withColon.ntriplesString(), factory.createBlankNode("with:colon").ntriplesString());
265 }
266
267 @Test(expected = IllegalArgumentException.class)
268 public void testInvalidIRI() throws Exception {
269 factory.createIRI("<no_brackets>");
270 }
271
272 @Test(expected = IllegalArgumentException.class)
273 public void testInvalidLiteralLang() throws Exception {
274 factory.createLiteral("Example", "with space");
275 }
276
277 @Test(expected = Exception.class)
278 public void testInvalidTriplePredicate() {
279 final BlankNode subject = factory.createBlankNode("b1");
280 final BlankNode predicate = factory.createBlankNode("b2");
281 final BlankNode object = factory.createBlankNode("b3");
282 factory.createTriple(subject, (IRI) predicate, object);
283 }
284
285 @Test
286 public void hashCodeBlankNode() throws Exception {
287 final BlankNode bnode1 = factory.createBlankNode();
288 assertEquals(bnode1.uniqueReference().hashCode(), bnode1.hashCode());
289 }
290
291 @Test
292 public void hashCodeIRI() throws Exception {
293 final IRI iri = factory.createIRI("http://example.com/");
294 assertEquals(iri.getIRIString().hashCode(), iri.hashCode());
295 }
296
297 @Test
298 public void hashCodeLiteral() throws Exception {
299 final Literal literal = factory.createLiteral("Hello");
300 assertEquals(Objects.hash(literal.getLexicalForm(), literal.getDatatype(), literal.getLanguageTag()),
301 literal.hashCode());
302 }
303
304 @Test
305 public void hashCodeTriple() throws Exception {
306 final IRI iri = factory.createIRI("http://example.com/");
307 final Triple triple = factory.createTriple(iri, iri, iri);
308 assertEquals(Objects.hash(iri, iri, iri), triple.hashCode());
309 }
310 }