KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestQuery


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestQuery.java,v 1.11 2005/02/21 12:15:17 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.rdf.model.*;
10
11 import com.hp.hpl.jena.util.iterator.*;
12 import com.hp.hpl.jena.util.*;
13 import com.hp.hpl.jena.graph.*;
14 import com.hp.hpl.jena.graph.test.*;
15
16 import java.util.*;
17
18 import junit.framework.*;
19
20 /**
21     @author kers
22     Test the query API. The query API is a doorway to the graph query SPI,
23     so rather than testing that query works in terms of getting results, we
24     test that the Model API translates into graph queries. This is a test of
25     ModelCom really. Dunno how to make it more general without having to
26     make it duplicate all the graph query tests.
27 */

28 public class TestQuery extends ModelTestBase
29     {
30     public TestQuery( String JavaDoc name )
31         { super(name); }
32         
33     public static TestSuite suite()
34         { return new TestSuite( TestQuery.class ); }
35
36     public void testAAA()
37         {
38         testQueryTranslates( "", "" );
39         testQueryTranslates( "x R y", "x R y" );
40         testQueryTranslates( "x R _y; p S 99", "x R _y; p S 99" );
41         testQueryTranslates( "x R ?y", "x R ?y" );
42         testQueryTranslates( "jqv:x jqv:H y", "?x ?H y" );
43         }
44         
45     public void testBBB()
46         {
47         Model m = ModelFactory.createDefaultModel();
48         String JavaDoc [][] tests =
49             {
50                 {"x", "x"},
51                 {"jqv:x", "?x"}
52             };
53         for (int i = 0; i < tests.length; i += 1)
54             testVariablesTranslate( resources( m, tests[i][0] ), nodes( tests[i][1] ) );
55         }
56         
57     private void testQueryTranslates( String JavaDoc model, String JavaDoc graph )
58         {
59         String JavaDoc title = "must translate <" + model + "> to <" + graph + ">";
60         Model m = modelWithStatements( model );
61         Graph g = GraphTestBase.graphWith( graph );
62         QueryMapper qm = new QueryMapper( m, new Resource[0] );
63         GraphTestBase.assertIsomorphic( title, g, qm.getGraph() );
64         }
65     
66     public void testVariablesTranslate( Resource [] vIn, Node [] vOut )
67         {
68         assertEquals( "broken test", vIn.length, vOut.length );
69         QueryMapper qm = new QueryMapper( modelWithStatements( "" ), vIn );
70         Node [] result = qm.getVariables();
71         assertEquals( vOut.length, result.length );
72         for (int i = 0; i < result.length; i += 1)
73             assertEquals( "variable did not convert", vOut[i], result[i] );
74         }
75         
76     public void testModelQuery()
77         {
78         Model m = modelWithStatements( "a R b; b S c; a R p; p T d" );
79         Model q = modelWithStatements( "jqv:x R jqv:y; jqv:y S jqv:z" );
80         ExtendedIterator it = ModelQueryUtil.queryBindingsWith( m, q, resources( q, "jqv:x jqv:z") );
81         assertTrue( it.hasNext() );
82         assertEquals( Arrays.asList( resources( m, "a c b" ) ), it.next() );
83         assertFalse( it.hasNext() );
84         }
85     }
86
87
88 /*
89     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
90     All rights reserved.
91
92     Redistribution and use in source and binary forms, with or without
93     modification, are permitted provided that the following conditions
94     are met:
95
96     1. Redistributions of source code must retain the above copyright
97        notice, this list of conditions and the following disclaimer.
98
99     2. Redistributions in binary form must reproduce the above copyright
100        notice, this list of conditions and the following disclaimer in the
101        documentation and/or other materials provided with the distribution.
102
103     3. The name of the author may not be used to endorse or promote products
104        derived from this software without specific prior written permission.
105
106     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
107     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
108     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
109     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
110     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
111     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
112     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
113     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
114     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
115     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116 */
Popular Tags