001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestTriple.java,v 1.23 2008/01/02 12:05:33 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: /**
010: @author bwm out of kers, then kers updates
011: */
012:
013: import com.hp.hpl.jena.graph.*;
014: import com.hp.hpl.jena.graph.impl.LiteralLabel;
015: import com.hp.hpl.jena.rdf.model.AnonId;
016: import com.hp.hpl.jena.shared.*;
017:
018: import junit.framework.*;
019:
020: public class TestTriple extends GraphTestBase {
021:
022: public TestTriple(String name) {
023: super (name);
024: }
025:
026: public static TestSuite suite() {
027: return new TestSuite(TestTriple.class);
028: }
029:
030: private static final String U = "http://some.domain.name/magic/spells.incant";
031: private static final String N = "Alice";
032: private static final LiteralLabel L = new LiteralLabel(
033: "ashes are burning", "en", false);
034:
035: public void testTripleEquals() {
036: try {
037: Node.cache(false);
038:
039: // create some nodes to test
040: AnonId id = AnonId.create();
041: LiteralLabel L2 = new LiteralLabel(id.toString(), "", false);
042: String U2 = id.toString();
043: String N2 = id.toString();
044:
045: Node[] nodes = new Node[] { Node.ANY, Node.createAnon(id),
046: Node.createAnon(), Node.createLiteral(L),
047: Node.createLiteral(L2), Node.createURI(U),
048: Node.createURI(U2), Node.createVariable(N),
049: Node.createVariable(N2) };
050:
051: Triple[] triples = new Triple[nodes.length * nodes.length
052: * nodes.length];
053: for (int i = 0; i < nodes.length; i++) {
054: for (int j = 0; j < nodes.length; j++) {
055: for (int k = 0; k < nodes.length; k++) {
056: triples[i * nodes.length * nodes.length + j
057: * nodes.length + k] = new Triple(
058: nodes[i], nodes[j], nodes[k]);
059: }
060: }
061: }
062:
063: // set up the expected results matrix
064: // a expected[i][j] is true if triples[i] equals triples[j]
065: // triples are expected to be equals if there components are equal
066: boolean[][] expected = new boolean[triples.length][triples.length];
067: for (int i1 = 0; i1 < nodes.length; i1++) {
068: for (int j1 = 0; j1 < nodes.length; j1++) {
069: for (int k1 = 0; k1 < nodes.length; k1++) {
070: for (int i2 = 0; i2 < nodes.length; i2++) {
071: for (int j2 = 0; j2 < nodes.length; j2++) {
072: for (int k2 = 0; k2 < nodes.length; k2++) {
073: expected[i1 * nodes.length
074: * nodes.length + j1
075: * nodes.length + k1][i2
076: * nodes.length
077: * nodes.length + j2
078: * nodes.length + k2] = nodes[i1]
079: .equals(nodes[i2])
080: && nodes[j1]
081: .equals(nodes[j2])
082: && nodes[k1]
083: .equals(nodes[k2]);
084: }
085: }
086: }
087: }
088: }
089: }
090:
091: assertEquals("triple, null", triples[0].equals(null), false);
092: assertDiffer("triple, string", triples[0], "string");
093:
094: // now compare each triple with each other triple
095: for (int i = 0; i < triples.length; i++) {
096: for (int j = 0; j < triples.length; j++) {
097: if (expected[i][j]) {
098: assertEquals("triples " + i + ", " + j,
099: triples[i], triples[j]);
100: } else {
101: assertDiffer("triples" + i + ", " + j,
102: triples[i], triples[j]);
103: }
104: }
105: }
106:
107: } finally {
108: Node.cache(true);
109: }
110: }
111:
112: public void testTripleCreate() {
113: Node S = NodeCreateUtils.create("s"), P = NodeCreateUtils
114: .create("p"), O = NodeCreateUtils.create("o");
115: assertEquals(new Triple(S, P, O), Triple.create(S, P, O));
116: }
117:
118: public void testTripleCreateFromString() {
119: Node S = NodeCreateUtils.create("a"), P = NodeCreateUtils
120: .create("_P"), O = NodeCreateUtils.create("?c");
121: assertEquals(new Triple(S, P, O), Triple.create("a _P ?c"));
122: }
123:
124: /**
125: Test that triple-creation respects prefixes, assuming that node creation
126: does.
127: */
128: public void testTriplePrefixes() {
129: Node S = NodeCreateUtils.create("rdf:alpha"), P = NodeCreateUtils
130: .create("dc:creator");
131: Node O = NodeCreateUtils.create("spoo:notmapped");
132: Triple t = Triple.create("rdf:alpha dc:creator spoo:notmapped");
133: assertEquals(new Triple(S, P, O), t);
134: }
135:
136: public void testTripleCreationMapped() {
137: PrefixMapping pm = PrefixMapping.Factory.create().setNsPrefix(
138: "a", "ftp://foo/").setNsPrefix("b", "http://spoo/");
139: Triple wanted = Triple.create("ftp://foo/x http://spoo/y c:z");
140: Triple got = Triple.create(pm, "a:x b:y c:z");
141: assertEquals(wanted, got);
142: }
143:
144: public void testPlainTripleMatches() {
145: testMatches("S P O");
146: testMatches("_S _P _O");
147: testMatches("1 2 3");
148: }
149:
150: public void testAnyTripleMatches() {
151: testMatches("?? P O", "Z P O");
152: testMatches("S ?? O", "S Q O");
153: testMatches("S P ??", "S P oh");
154: testMatches("?? ?? ??", "X Y Z");
155: testMatches("?? ?? ??", "X Y 1");
156: testMatches("?? ?? ??", "_X Y Z");
157: testMatches("?? ?? ??", "X _Y Z");
158: }
159:
160: private void testMatches(String triple) {
161: testMatches(triple, triple);
162: }
163:
164: private void testMatches(String pattern, String triple) {
165: assertTrue(Triple.create(pattern)
166: .matches(Triple.create(triple)));
167: }
168:
169: public void testPlainTripleDoesntMatch() {
170: testMatchFails("S P O", "Z P O");
171: testMatchFails("S P O", "S Q O");
172: testMatchFails("S P O", "S P oh");
173: }
174:
175: public void testAnyTripleDoesntMatch() {
176: testMatchFails("?? P O", "S P oh");
177: testMatchFails("S ?? O", "Z R O");
178: testMatchFails("S P ??", "Z P oh");
179: }
180:
181: public void testMatchFails(String pattern, String triple) {
182: assertFalse(Triple.create(pattern).matches(
183: Triple.create(triple)));
184: }
185:
186: public void testMatchesNodes() {
187: assertTrue(Triple.create("S P O").matches(node("S"), node("P"),
188: node("O")));
189: assertTrue(Triple.create("?? P O").matches(node("Z"),
190: node("P"), node("O")));
191: assertTrue(Triple.create("S ?? O").matches(node("S"),
192: node("Q"), node("O")));
193: assertTrue(Triple.create("S P ??").matches(node("S"),
194: node("P"), node("I")));
195: /* */
196: assertFalse(Triple.create("S P O").matches(node("Z"),
197: node("P"), node("O")));
198: assertFalse(Triple.create("S P O").matches(node("S"),
199: node("Q"), node("O")));
200: assertFalse(Triple.create("S P O").matches(node("Z"),
201: node("P"), node("I")));
202: }
203:
204: public void testElementMatches() {
205: assertTrue(Triple.create("S P O").subjectMatches(node("S")));
206: assertTrue(Triple.create("S P O").predicateMatches(node("P")));
207: assertTrue(Triple.create("S P O").objectMatches(node("O")));
208: /* */
209: assertFalse(Triple.create("S P O").subjectMatches(node("Z")));
210: assertFalse(Triple.create("S P O").predicateMatches(node("Q")));
211: assertFalse(Triple.create("S P O").objectMatches(node("I")));
212: /* */
213: assertTrue(Triple.create("?? P O").subjectMatches(node("SUB")));
214: assertTrue(Triple.create("S ?? O").predicateMatches(
215: node("PRED")));
216: assertTrue(Triple.create("S P ??").objectMatches(node("OBJ")));
217: }
218:
219: public void testConcrete() {
220: assertTrue(Triple.create("S P O").isConcrete());
221: assertTrue(Triple.create("S P 11").isConcrete());
222: assertTrue(Triple.create("S P _X").isConcrete());
223: assertTrue(Triple.create("S _P 11").isConcrete());
224: assertTrue(Triple.create("_S _P _O").isConcrete());
225: assertTrue(Triple.create("10 11 12").isConcrete());
226: assertTrue(Triple.create("S P 11").isConcrete());
227: assertFalse(Triple.create("?? P 11").isConcrete());
228: assertFalse(Triple.create("S ?? 11").isConcrete());
229: assertFalse(Triple.create("S P ??").isConcrete());
230: assertFalse(Triple.create("?S P 11").isConcrete());
231: assertFalse(Triple.create("S ?P 11").isConcrete());
232: assertFalse(Triple.create("S P ?O").isConcrete());
233: }
234:
235: /**
236: Primarily to make sure that literals get quoted and stuff comes out in some
237: kind of coherent order.
238: */
239: public void testTripleToStringOrdering() {
240: Triple t1 = Triple.create("subject predicate object");
241: assertTrue("subject must be present", t1.toString().indexOf(
242: "subject") >= 0);
243: assertTrue("subject must preceed predicate", t1.toString()
244: .indexOf("subject") < t1.toString()
245: .indexOf("predicate"));
246: assertTrue("predicate must preceed object", t1.toString()
247: .indexOf("predicate") < t1.toString().indexOf("object"));
248: }
249:
250: public void testTripleToStringQuoting() {
251: Triple t1 = Triple.create("subject predicate 'object'");
252: assertTrue(t1.toString().indexOf("\"object\"") > 0);
253: }
254:
255: public void testTripleToStringWithPrefixing() {
256: PrefixMapping pm = PrefixMapping.Factory.create();
257: pm.setNsPrefix("spoo", "eg://domain.dom/spoo#");
258: Triple t1 = Triple.create("eg://domain.dom/spoo#a b c");
259: assertEquals("spoo:a @eh:/b eh:/c", t1.toString(pm));
260: }
261:
262: }
263: /*
264: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
265: All rights reserved.
266:
267: Redistribution and use in source and binary forms, with or without
268: modification, are permitted provided that the following conditions
269: are met:
270:
271: 1. Redistributions of source code must retain the above copyright
272: notice, this list of conditions and the following disclaimer.
273:
274: 2. Redistributions in binary form must reproduce the above copyright
275: notice, this list of conditions and the following disclaimer in the
276: documentation and/or other materials provided with the distribution.
277:
278: 3. The name of the author may not be used to endorse or promote products
279: derived from this software without specific prior written permission.
280:
281: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
282: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
283: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
284: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
285: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
286: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
287: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
288: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
289: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
290: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291: */
|