View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Test RDF implementation (and thus its RDFTerm implementations)
32   * <p>
33   * To add to your implementation's tests, create a subclass with a name ending
34   * in <code>Test</code> and provide {@link #createFactory()} which minimally
35   * supports one of the operations, but ideally supports all operations.
36   *
37   * @see RDF
38   */
39  public abstract class AbstractRDFTest {
40  
41      private RDF factory;
42  
43      /**
44       * 
45       * This method must be overridden by the implementing test to provide a
46       * factory for the test to create {@link Literal}, {@link IRI} etc.
47       * 
48       * @return {@link RDF} instance to be tested.
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              // Expected exception
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          // We don't know what the identifier is, but it MUST be the same
87          assertEquals(bnode1.uniqueReference(), bnode2.uniqueReference());
88          // We don't know what the ntriplesString is, but it MUST be the same
89          assertEquals(bnode1.ntriplesString(), bnode2.ntriplesString());
90          // and here it MUST differ
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          // it MUST differ from a second factory
100         differentFactory = createFactory().createBlankNode();
101 
102         // NOTE: We can't make similar assumption if we provide a
103         // name to createBlankNode(String) as its documentation
104         // only says:
105         //
106         // * BlankNodes created using this method with the same parameter, for
107         // * different instances of RDFFactory, SHOULD NOT be equivalent.
108         //
109         // https://github.com/apache/incubator-commonsrdf/pull/7#issuecomment-92312779
110         assertNotEquals(bnode1, differentFactory);
111         assertNotEquals(bnode1.uniqueReference(), differentFactory.uniqueReference());
112         // but we can't require:
113         // assertNotEquals(bnode1.ntriplesString(),
114         // differentFactory.ntriplesString());
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         // and now for the international fun!
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         // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html
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         // see https://issues.apache.org/jira/browse/JENA-827
190         final Literal vls = factory.createLiteral("Herbert Van de Sompel", "vls"); // JENA-827
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         // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html
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         // bnode equivalence should be OK as we used the same
216         // factory and have not yet inserted Triple into a Graph
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         // bnode equivalence should be OK as we used the same
230         // factory and have not yet inserted Triple into a Graph
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         // bnode equivalence should be OK as we used the same
244         // factory and have not yet inserted Triple into a Graph
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             // Good!
257             return;
258         }
259         // Factory allows :colon, which is OK as long as it's not causing an
260         // invalid ntriplesString
261         assertFalse(withColon.ntriplesString().contains("with:colon"));
262 
263         // and creating it twice gets the same ntriplesString
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 }