KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > test > QueryTestProgrammatic


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.rdql.test;
7
8 // Unsubtle JUnit-ization of some test code.
9
// This will be replaced by a more general testing of queries.
10

11 import junit.framework.* ;
12
13 import java.util.* ;
14
15 import com.hp.hpl.jena.rdf.model.* ;
16 import com.hp.hpl.jena.rdql.*;
17
18
19 /** Bunch of programmatic uses of query to complrment the script tests.
20  * @author Andy Seaborne
21  * @version $Id: QueryTestProgrammatic.java,v 1.9 2005/02/21 12:16:06 andy_seaborne Exp $
22  */

23
24 public class QueryTestProgrammatic extends TestSuite
25 {
26     static final String JavaDoc testSetName = "RDQL - Query - Programmatic" ;
27     
28     public static boolean dumpModel = false ;
29     public static boolean verbose = false ;
30
31     public static TestSuite suite()
32     {
33         return new QueryTestProgrammatic(testSetName) ;
34     }
35         
36         
37     private QueryTestProgrammatic(String JavaDoc name)
38     {
39         super(name) ;
40         
41         try {
42             Model model1 = makeModel1() ;
43             Model model2 = makeModel2() ;
44             //suite.addTest(new TestQuery("RDQL Query "));
45
addTest(new TestProgrammatic("RDQL-Test-Prog-1", model1, "SELECT ?x, ?z WHERE (?x, ?a, ?b), (?b, ?y, ?z)")) ;
46             addTest(new TestProgrammatic("RDQL-Test-Prog-2", model1, "SELECT ?z WHERE (?x, ?y, ?z) AND ?z == 1")) ;
47             addTest(new TestProgrammatic("RDQL-Test-Prog-3", model1, "SELECT ?z WHERE (<http://never/r-1>, <http://never/p-0>, ?z)")) ;
48
49             // Test with anon resources
50
addTest(new TestProgrammatic("RDQL-Test-Prog-4", model2, "SELECT ?x, ?a, ?b, ?y, ?z WHERE (?x, ?a, ?b), (?b, ?y, ?z)")) ;
51             addTest(new TestProgrammatic("RDQL-Test-Prog-5", model2, "SELECT ?z WHERE (?x, ?y, ?z) AND ?z == 1")) ;
52             addTest(new TestProgrammatic("RDQL-Test-Prog-6", model2, "SELECT ?z WHERE (<http://never/r-1>, <http://never/p-0>, ?z)")) ;
53             
54             // Test templates
55
// expect to get one result with ?z = 1+1 = 2
56

57             ResultBindingImpl rb = new ResultBindingImpl() ;
58             rb.add("x", model1.createResource("http://never/r-1")) ;
59             rb.add("y", model1.createResource("http://never/p-1")) ;
60             // No bindings
61
addTest(new TestQueryTemplate("RDQL-Test-Template-1", model1, null, "SELECT * WHERE (?x, ?y, ?z)", model1.size())) ;
62             // With bindings
63
addTest(new TestQueryTemplate("RDQL-Test-Template-2", model1, rb, "SELECT * WHERE (?x, ?y, ?z)", 1)) ;
64         } catch (Exception JavaDoc ex)
65         {
66             System.err.println("Problems making RDQL test") ;
67             ex.printStackTrace(System.err) ;
68             return ;
69         }
70         // Tests of templates
71
}
72
73     static class TestProgrammatic extends TestCase
74     {
75         Model model ;
76         String JavaDoc queryString ;
77         
78         TestProgrammatic(String JavaDoc testName, Model m, String JavaDoc q)
79         {
80             super(testName) ;
81             model = m ;
82             queryString = q ;
83         }
84         
85         protected void runTest() throws Throwable JavaDoc
86         {
87             if ( verbose )
88             {
89                 System.out.println() ;
90                 System.out.println("Query:") ;
91                 System.out.println(queryString) ;
92             }
93             // Currently "success" is executing the query at all!
94
Query query = new Query(queryString) ;
95             query.setSource(model);
96             QueryExecution qe = new QueryEngine(query) ;
97             QueryResults results = qe.exec() ;
98
99
100             for ( ; results.hasNext() ; )
101             {
102                  ResultBindingImpl rb = (ResultBindingImpl)results.next() ;
103                  for ( Iterator iter = rb.getTriples().iterator() ; iter.hasNext() ; )
104                  {
105                      Statement s = (Statement)iter.next() ;
106                      assertTrue("Statement not in model", model.contains(s)) ;
107                 }
108             }
109             results.close() ;
110         }
111     }
112     
113     static class TestQueryTemplate extends TestCase
114     {
115         Model model ;
116         String JavaDoc queryString ;
117         ResultBindingImpl binding ;
118         long numResults ;
119         
120         TestQueryTemplate(String JavaDoc testName, Model m, ResultBindingImpl b, String JavaDoc q, long num)
121         {
122             super(testName) ;
123             model = m ;
124             queryString = q ;
125             binding = b ;
126             numResults = num ;
127         }
128         
129         protected void runTest() throws Throwable JavaDoc
130         {
131             if ( verbose )
132             {
133                 System.out.println() ;
134                 System.out.println("Query:") ;
135                 System.out.println(queryString) ;
136             }
137             // Currently "success" is executing the query at all!
138
Query query = new Query(queryString) ;
139             query.setSource(model);
140             // Not query execution - we wish to prebind.
141
QueryEngine _qe = new QueryEngine(query) ;
142             QueryResults results = _qe.exec(binding) ;
143             QueryExecution qe = _qe ;
144             
145             long count = 0 ;
146             for ( ; results.hasNext() ; )
147             {
148                 ResultBinding rb = (ResultBinding)results.next() ;
149                 if ( rb == null )
150                     throw new Exception JavaDoc("TestQueryTemplate: found null result binding") ;
151                 // Check all the variables are there.
152
for ( Iterator iter = query.getResultVars().iterator() ;
153                       iter.hasNext() ; )
154                 {
155                     String JavaDoc varName = (String JavaDoc)iter.next() ;
156                     Object JavaDoc obj = rb.get(varName) ;
157                     assertNotNull("Variable: "+varName, obj) ;
158                     if ( binding != null )
159                     {
160                         Object JavaDoc original = binding.get(varName) ;
161                         if ( original != null )
162                             assertTrue("Variable: "+varName+" = "+original+" / "+obj, original.equals(obj)) ;
163                     }
164                 }
165                           
166                 count++ ;
167             }
168             results.close() ;
169             
170             if ( count != numResults )
171             {
172                 throw new Exception JavaDoc("TestQueryTemplate: mismatch in counts. Expected "+numResults+". Got "+count+" Query: "+queryString) ;
173             }
174             
175         }
176     }
177     
178     static public Model makeModel1() throws Exception JavaDoc
179     {
180         Model model = ModelFactory.createDefaultModel() ;
181
182         // Resource loop
183
for ( int i = 0 ; i < 2 ; i++ )
184         {
185             // Property loop
186
for ( int j = 0 ; j < 2 ; j++ )
187             {
188                 model.add(model.createResource("http://never/r-"+i) ,
189                           model.createProperty("http://never/p-"+j) ,
190                           //"val-r-"+i+"-p-"+j) ;
191
i+j) ;
192             }
193         }
194
195         // Bag statements are added to the model.
196
Bag bag = model.createBag("http://never/bag") ;
197         bag.add("11") ;
198         bag.add("22") ;
199
200         // Path.
201
model.add(model.createResource("http://never/path"),
202                   model.createProperty("http://never/path") ,
203                   model.createResource("http://never/r-0")) ;
204         return model ;
205     }
206     
207     
208     static public Model makeModel2() throws Exception JavaDoc
209     {
210         Model model = makeModel1() ;
211
212         // Add some stuff with anon. resources.
213
Resource anon1 = model.createResource() ;
214         Resource anon2 = model.createResource() ;
215
216         model.add(anon1,
217                   model.createProperty("http://never/p-anon-1") ,
218                   "p-anon-1") ;
219
220         model.add(anon2,
221                   model.createProperty("http://never/p-anon-2") ,
222                   "p-anon-2") ;
223
224         // Path from anon1 to anon2
225
model.add(anon1,
226                   model.createProperty("http://never/p-anon-1-2") ,
227                   anon2) ;
228
229         return model ;
230     }
231 }
232
233 /*
234  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
235  * All rights reserved.
236  *
237  * Redistribution and use in source and binary forms, with or without
238  * modification, are permitted provided that the following conditions
239  * are met:
240  * 1. Redistributions of source code must retain the above copyright
241  * notice, this list of conditions and the following disclaimer.
242  * 2. Redistributions in binary form must reproduce the above copyright
243  * notice, this list of conditions and the following disclaimer in the
244  * documentation and/or other materials provided with the distribution.
245  * 3. The name of the author may not be used to endorse or promote products
246  * derived from this software without specific prior written permission.
247  *
248  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
249  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
250  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
252  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
253  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
254  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
255  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
257  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258  */

259
260
Popular Tags