1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
64
65 allToAll.add(new Object[] { from, to });
66 }
67 }
68 return allToAll;
69 }
70
71
72
73
74
75
76
77
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
89 assertTrue(g.contains(s, p, o));
90 final Triple t1 = g.stream().findAny().get();
91
92
93
94
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
104
105
106
107 final Triple t2 = g.stream(s2, p, null).findAny().get();
108
109 final BlankNode bnode = (BlankNode) t2.getObject();
110
111
112 assertEquals(bnode, t1.getSubject());
113
114 final Triple t3 = g.stream(bnode, p, null).findAny().get();
115 assertEquals(t1, t3);
116 }
117 }
118
119
120
121
122
123
124
125
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
136
137 assertEquals(s, srcT1.getSubject());
138 assertEquals(p, srcT1.getPredicate());
139 assertEquals(o, srcT1.getObject());
140 g.add(srcT1);
141
142
143 assertTrue(g.contains(srcT1));
144 final Triple t1 = g.stream().findAny().get();
145
146
147
148
149
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
159
160
161
162 final Triple t2 = g.stream(s2, p, null).findAny().get();
163
164 final BlankNode bnode = (BlankNode) t2.getObject();
165
166
167 assertEquals(bnode, t1.getSubject());
168
169 final Triple t3 = g.stream(bnode, p, null).findAny().get();
170 assertEquals(t1, t3);
171 }
172 }
173 }