001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestQuery.java,v 1.15 2008/01/02 12:04:43 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010:
011: import com.hp.hpl.jena.util.iterator.*;
012: import com.hp.hpl.jena.util.*;
013: import com.hp.hpl.jena.graph.*;
014: import com.hp.hpl.jena.graph.test.*;
015:
016: import java.util.*;
017:
018: import junit.framework.*;
019:
020: /**
021: @author kers
022: Test the query API. The query API is a doorway to the graph query SPI,
023: so rather than testing that query works in terms of getting results, we
024: test that the Model API translates into graph queries. This is a test of
025: ModelCom really. Dunno how to make it more general without having to
026: make it duplicate all the graph query tests.
027: */
028: public class TestQuery extends ModelTestBase {
029: public TestQuery(String name) {
030: super (name);
031: }
032:
033: public static TestSuite suite() {
034: return new TestSuite(TestQuery.class);
035: }
036:
037: public void testAAA() {
038: testQueryTranslates("", "");
039: testQueryTranslates("x R y", "x R y");
040: testQueryTranslates("x R _y; p S 99", "x R _y; p S 99");
041: testQueryTranslates("x R ?y", "x R ?y");
042: testQueryTranslates("jqv:x jqv:H y", "?x ?H y");
043: }
044:
045: public void testBBB() {
046: Model m = ModelFactory.createDefaultModel();
047: String[][] tests = { { "x", "x" }, { "jqv:x", "?x" } };
048: for (int i = 0; i < tests.length; i += 1)
049: testVariablesTranslate(resources(m, tests[i][0]),
050: nodeArray(tests[i][1]));
051: }
052:
053: private void testQueryTranslates(String model, String graph) {
054: String title = "must translate <" + model + "> to <" + graph
055: + ">";
056: Model m = modelWithStatements(model);
057: Graph g = GraphTestBase.graphWith(graph);
058: QueryMapper qm = new QueryMapper(m, new Resource[0]);
059: GraphTestBase.assertIsomorphic(title, g, qm.getGraph());
060: }
061:
062: public void testVariablesTranslate(Resource[] vIn, Node[] vOut) {
063: assertEquals("broken test", vIn.length, vOut.length);
064: QueryMapper qm = new QueryMapper(modelWithStatements(""), vIn);
065: Node[] result = qm.getVariables();
066: assertEquals(vOut.length, result.length);
067: for (int i = 0; i < result.length; i += 1)
068: assertEquals("variable did not convert", vOut[i], result[i]);
069: }
070:
071: public void testModelQuery() {
072: Model m = modelWithStatements("a R b; b S c; a R p; p T d");
073: Model q = modelWithStatements("jqv:x R jqv:y; jqv:y S jqv:z");
074: ExtendedIterator it = ModelQueryUtil.queryBindingsWith(m, q,
075: resources(q, "jqv:x jqv:z"));
076: assertTrue(it.hasNext());
077: assertEquals(Arrays.asList(resources(m, "a c b")), it.next());
078: assertFalse(it.hasNext());
079: }
080: }
081:
082: /*
083: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
084: All rights reserved.
085:
086: Redistribution and use in source and binary forms, with or without
087: modification, are permitted provided that the following conditions
088: are met:
089:
090: 1. Redistributions of source code must retain the above copyright
091: notice, this list of conditions and the following disclaimer.
092:
093: 2. Redistributions in binary form must reproduce the above copyright
094: notice, this list of conditions and the following disclaimer in the
095: documentation and/or other materials provided with the distribution.
096:
097: 3. The name of the author may not be used to endorse or promote products
098: derived from this software without specific prior written permission.
099:
100: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
101: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
102: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
103: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
104: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
105: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
106: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
107: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
108: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
109: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110: */
|