KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junit.framework.* ;
9
10 import java.util.* ;
11
12 import com.hp.hpl.jena.rdf.model.* ;
13 import com.hp.hpl.jena.rdql.*;
14
15 /** Bunch of other tests.
16  * Workign with associated triples
17  *
18  * @author Andy Seaborne
19  * @version $Id: QueryTestsMisc.java,v 1.5 2005/02/21 12:16:08 andy_seaborne Exp $
20  */

21
22 public class QueryTestsMisc extends TestSuite
23 {
24     static final String JavaDoc testSetName = "RDQL - Query - Other" ;
25     public static String JavaDoc baseURI = "http://rdql/" ;
26
27     public static TestSuite suite()
28     {
29         return new QueryTestsMisc(testSetName) ;
30     }
31         
32         
33     private QueryTestsMisc(String JavaDoc name)
34     {
35         super(name) ;
36         
37         try {
38             //suite.addTest(new TestQuery("RDQL Query "));
39
addTest(new TestQueryTriplesMerge()) ;
40             addTest(new TestQueryGetTriples()) ;
41             addTest(new TestQueryGetTriples2()) ;
42         } catch (Exception JavaDoc ex)
43         {
44             System.err.println("Problems making RDQL test") ;
45             ex.printStackTrace(System.err) ;
46             return ;
47         }
48         // Tests of templates
49
}
50
51     static abstract class TestQueryTriples extends TestCase
52     {
53         Model model ;
54         Resource r ;
55         
56         TestQueryTriples(String JavaDoc testName)
57         {
58             super(testName) ;
59             model = ModelFactory.createDefaultModel() ;
60             r = model.createResource(baseURI+"r") ;
61             r.addProperty(model.createProperty(baseURI+"p1"), "v1") ;
62             r.addProperty(model.createProperty(baseURI+"p2"), "v2") ;
63         }
64     }
65     
66     static class TestQueryTriplesMerge extends TestQueryTriples
67     {
68         
69         TestQueryTriplesMerge() { super("TestQueryTriplesMerge" ) ; }
70         
71         protected void runTest() throws Throwable JavaDoc
72         {
73             Model model2 = ModelFactory.createDefaultModel() ;
74             
75             String JavaDoc queryString = "SELECT * WHERE (<"+r.getURI()+"> ?p ?v)" ;
76             
77             Query query = new Query(queryString) ;
78             query.setSource(model);
79             QueryExecution qe = new QueryEngine(query) ;
80             QueryResults results = qe.exec() ;
81
82             for ( ; results.hasNext() ; )
83             {
84                  ResultBindingImpl rb = (ResultBindingImpl)results.next() ;
85                  rb.mergeTriples(model2) ;
86             }
87             results.close() ;
88             
89             assertTrue(getName()+": merged rsults not the same as target model",
90                        model.isIsomorphicWith(model2)) ;
91         }
92     }
93     
94     static class TestQueryGetTriples extends TestQueryTriples
95     {
96         
97         TestQueryGetTriples() { super("TestQueryGetTriples" ) ; }
98         
99         protected void runTest() throws Throwable JavaDoc
100         {
101             Model model2 = ModelFactory.createDefaultModel() ;
102             
103             String JavaDoc queryString = "SELECT * WHERE (<"+r.getURI()+"> ?p ?v)" ;
104             
105             Query query = new Query(queryString) ;
106             query.setSource(model);
107             QueryExecution qe = new QueryEngine(query) ;
108             QueryResults results = qe.exec() ;
109
110             int i = 0 ;
111             for ( ; results.hasNext() ; )
112             {
113                 i++ ;
114                 ResultBindingImpl rb = (ResultBindingImpl)results.next() ;
115                 assertTrue(getName()+": getTriples(loop "+i+")", rb.getTriples().size() == 1) ;
116             }
117             results.close() ;
118         }
119     }
120
121     static class TestQueryGetTriples2 extends TestQueryTriples
122     {
123         
124         TestQueryGetTriples2() { super("TestQueryGetTriples2" ) ; }
125         
126         protected void runTest() throws Throwable JavaDoc
127         {
128             Model model2 = ModelFactory.createDefaultModel() ;
129             
130             String JavaDoc queryString = "SELECT * WHERE (<"+r.getURI()+"> ?p ?v)" ;
131             
132             Query query = new Query(queryString) ;
133             query.setSource(model);
134             QueryExecution qe = new QueryEngine(query) ;
135             QueryResults results = qe.exec() ;
136
137             int i = 0 ;
138             for ( ; results.hasNext() ; )
139             {
140                 i++ ;
141                 ResultBindingImpl rb = (ResultBindingImpl)results.next() ;
142                 Set s = rb.getTriples() ;
143                 // Try again - ensure caching works.
144
assertTrue(getName()+": getTriples2(loop "+i+")",
145                            rb.getTriples().size() == 1) ;
146             }
147             results.close() ;
148         }
149     }
150
151 }
152     
153 /*
154  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
155  * All rights reserved.
156  *
157  * Redistribution and use in source and binary forms, with or without
158  * modification, are permitted provided that the following conditions
159  * are met:
160  * 1. Redistributions of source code must retain the above copyright
161  * notice, this list of conditions and the following disclaimer.
162  * 2. Redistributions in binary form must reproduce the above copyright
163  * notice, this list of conditions and the following disclaimer in the
164  * documentation and/or other materials provided with the distribution.
165  * 3. The name of the author may not be used to endorse or promote products
166  * derived from this software without specific prior written permission.
167  *
168  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
169  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
170  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
172  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
173  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
174  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
175  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
176  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
177  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178  */

179
180
Popular Tags