Debian Codegraph @

commons-rdf-integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/AllToAllTest.java

raw
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.integrationtests;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.apache.commons.rdf.api.BlankNode;
29 import org.apache.commons.rdf.api.Graph;
30 import org.apache.commons.rdf.api.IRI;
31 import org.apache.commons.rdf.api.Literal;
32 import org.apache.commons.rdf.api.RDF;
33 import org.apache.commons.rdf.api.RDFTerm;
34 import org.apache.commons.rdf.api.Triple;
35 import org.apache.commons.rdf.jena.JenaRDF;
36 import org.apache.commons.rdf.jsonldjava.JsonLdRDF;
37 import org.apache.commons.rdf.rdf4j.RDF4J;
38 import org.apache.commons.rdf.simple.SimpleRDF;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.Parameterized;
42 import org.junit.runners.Parameterized.Parameters;
43
44 @RunWith(Parameterized.class)
45 public class AllToAllTest {
46
47 private final RDF nodeFactory;
48 private final RDF graphFactory;
49
50 public AllToAllTest(final Class<? extends RDF> from, final Class<? extends RDF> to)
51 throws InstantiationException, IllegalAccessException {
52 this.nodeFactory = from.newInstance();
53 this.graphFactory = to.newInstance();
54 }
55
56 @SuppressWarnings("rawtypes")
57 @Parameters(name = "{index}: {0}->{1}")
58 public static Collection<Object[]> data() {
59 final List<Class> factories = Arrays.asList(SimpleRDF.class, JenaRDF.class, RDF4J.class, JsonLdRDF.class);
60 final Collection<Object[]> allToAll = new ArrayList<>();
61 for (final Class from : factories) {
62 for (final Class to : factories) {
63 // NOTE: we deliberately include self-to-self here
64 // to test two instances of the same implementation
65 allToAll.add(new Object[] { from, to });
66 }
67 }
68 return allToAll;
69 }
70
71 /**
72 * This test creates a {@link Graph} with the first {@link RDF},
73 * then inserts/queries with triples using {@link RDFTerm}s created with the
74 * second factory.
75 *
76 * @throws Exception
77 * Just in case..
78 */
79 @Test
80 public void addTermsFromOtherFactory() throws Exception {
81 try (final Graph g = graphFactory.createGraph()) {
82 final BlankNode s = nodeFactory.createBlankNode();
83 final IRI p = nodeFactory.createIRI("http://example.com/p");
84 final Literal o = nodeFactory.createLiteral("Hello");
85
86 g.add(s, p, o);
87
88 // blankNode should still work with g.contains()
89 assertTrue(g.contains(s, p, o));
90 final Triple t1 = g.stream().findAny().get();
91
92 // Can't make assumptions about BlankNode equality - it might
93 // have been mapped to a different BlankNode.uniqueReference()
94 // assertEquals(s, t.getSubject());
95
96 assertEquals(p, t1.getPredicate());
97 assertEquals(o, t1.getObject());
98
99 final IRI s2 = nodeFactory.createIRI("http://example.com/s2");
100 g.add(s2, p, s);
101 assertTrue(g.contains(s2, p, s));
102
103 // This should be mapped to the same BlankNode
104 // (even if it has a different identifier), e.g.
105 // we should be able to do:
106
107 final Triple t2 = g.stream(s2, p, null).findAny().get();
108
109 final BlankNode bnode = (BlankNode) t2.getObject();
110 // And that (possibly adapted) BlankNode object should
111 // match the subject of t1 statement
112 assertEquals(bnode, t1.getSubject());
113 // And can be used as a key:
114 final Triple t3 = g.stream(bnode, p, null).findAny().get();
115 assertEquals(t1, t3);
116 }
117 }
118
119 /**
120 * This is a variation of {@link #addTermsFromOtherFactory()}, but here
121 * {@link Triple} is created in the "foreign" nodeFactory before adding to
122 * the graph.
123 *
124 * @throws Exception
125 * Just in case..
126 */
127 @Test
128 public void addTriplesFromOtherFactory() throws Exception {
129 try (final Graph g = graphFactory.createGraph()) {
130 final BlankNode s = nodeFactory.createBlankNode();
131 final IRI p = nodeFactory.createIRI("http://example.com/p");
132 final Literal o = nodeFactory.createLiteral("Hello");
133
134 final Triple srcT1 = nodeFactory.createTriple(s, p, o);
135 // This should work even with BlankNode as they are from the same
136 // factory
137 assertEquals(s, srcT1.getSubject());
138 assertEquals(p, srcT1.getPredicate());
139 assertEquals(o, srcT1.getObject());
140 g.add(srcT1);
141
142 // what about the blankNode within?
143 assertTrue(g.contains(srcT1));
144 final Triple t1 = g.stream().findAny().get();
145
146 // Can't make assumptions about BlankNode equality - it might
147 // have been mapped to a different BlankNode.uniqueReference()
148 // assertEquals(srcT1, t1);
149 // assertEquals(s, t1.getSubject());
150 assertEquals(p, t1.getPredicate());
151 assertEquals(o, t1.getObject());
152
153 final IRI s2 = nodeFactory.createIRI("http://example.com/s2");
154 final Triple srcT2 = nodeFactory.createTriple(s2, p, s);
155 g.add(srcT2);
156 assertTrue(g.contains(srcT2));
157
158 // This should be mapped to the same BlankNode
159 // (even if it has a different identifier), e.g.
160 // we should be able to do:
161
162 final Triple t2 = g.stream(s2, p, null).findAny().get();
163
164 final BlankNode bnode = (BlankNode) t2.getObject();
165 // And that (possibly adapted) BlankNode object should
166 // match the subject of t1 statement
167 assertEquals(bnode, t1.getSubject());
168 // And can be used as a key:
169 final Triple t3 = g.stream(bnode, p, null).findAny().get();
170 assertEquals(t1, t3);
171 }
172 }
173 }