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.jena;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.commons.rdf.api.BlankNode;
24  import org.apache.jena.graph.Node;
25  import org.apache.jena.graph.NodeFactory;
26  import org.apache.jena.graph.Triple;
27  import org.junit.Test;
28  
29  public class GeneralizedRDFTripleTest {
30  
31      private final JenaRDF jena = new JenaRDF();
32  
33      @Test
34      public void bnodeProperty() throws Exception {
35          final BlankNode b1 = jena.createBlankNode("b1");
36          final JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
37          final JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
38  
39          final JenaGeneralizedTripleLike t = jena.createGeneralizedTriple(ex1, b1, ex2);
40          assertEquals(ex1, t.getSubject());
41          assertEquals(ex2, t.getObject());
42          assertEquals(b1, t.getPredicate()); // it's a bnode!
43          assertTrue(t.asJenaTriple().getPredicate().isBlank());
44      }
45  
46      @Test
47      public void literalPredicate() throws Exception {
48          final JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
49          final JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
50          final JenaLiteral lit = jena.createLiteral("Hello");
51  
52          final JenaGeneralizedTripleLike t = jena.createGeneralizedTriple(ex1, lit, ex2);
53          assertEquals(ex1, t.getSubject());
54          assertEquals(ex2, t.getObject());
55          assertEquals(lit, t.getPredicate()); // it's a literal!
56          assertTrue(t.asJenaTriple().getPredicate().isLiteral());
57      }
58  
59  
60      @Test
61      public void literalSubject() throws Exception {
62          final JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
63          final JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
64          final JenaLiteral lit = jena.createLiteral("Hello");
65  
66          final JenaGeneralizedTripleLike t = jena.createGeneralizedTriple(lit, ex1, ex2);
67          assertEquals(lit, t.getSubject()); // it's a literal!
68          assertEquals(ex1, t.getPredicate());
69          assertEquals(ex2, t.getObject());
70          assertTrue(t.asJenaTriple().getSubject().isLiteral());
71      }
72  
73      @Test
74      public void asGeneralizedTriple() throws Exception {
75          final Node s = NodeFactory.createLiteral("Hello");
76          final Node p = NodeFactory.createBlankNode();
77          final Node o = NodeFactory.createURI("http://example.com/ex");
78          final Triple jt = Triple.create(s, p, o);
79          final JenaTripleLike t = jena.asGeneralizedTriple(jt);
80          assertEquals(jena.createLiteral("Hello"), t.getSubject());
81          assertEquals(jena.asRDFTerm(p), t.getPredicate());
82          assertEquals(jena.createIRI("http://example.com/ex"), t.getObject());
83      }
84  
85  }