KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > test > TestRDQLMatch


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

6
7 package com.hp.hpl.jena.db.test;
8
9 /**
10  *
11  * This tests string match operations on the modelRDB.
12  *
13 */

14
15
16 import com.hp.hpl.jena.db.*;
17 import com.hp.hpl.jena.db.impl.IRDBDriver;
18 import com.hp.hpl.jena.rdf.model.*;
19 import com.hp.hpl.jena.rdql.Query;
20 import com.hp.hpl.jena.rdql.QueryEngine;
21 import com.hp.hpl.jena.rdql.QueryExecution;
22 import com.hp.hpl.jena.rdql.QueryResults;
23 import com.hp.hpl.jena.shared.*;
24
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28
29 public class TestRDQLMatch extends TestCase {
30         
31     public TestRDQLMatch( String JavaDoc name )
32         { super( name ); }
33     
34     public static TestSuite suite()
35         { return new TestSuite( TestRDQLMatch.class ); }
36     
37     protected void setUp() throws java.lang.Exception JavaDoc {
38     }
39     
40     protected void tearDown() throws java.lang.Exception JavaDoc {
41     }
42     
43     String JavaDoc pfxQueryString = "SELECT * WHERE " +
44        "(?s, <ex:prop>, ?o) and ?o =~ /^hi/" +
45        "USING ex FOR <http://example.org/> ";
46          
47     String JavaDoc sfxQueryString = "SELECT * WHERE " +
48          "(?s, <ex:prop>, ?o) and ?o =~ /there$/ " +
49          "USING ex FOR <http://example.org/> ";
50
51       String JavaDoc ipfxQueryString = "SELECT * WHERE " +
52          "(?s, <ex:prop>, ?o) and ?o =~ /^hi/i" +
53          "USING ex FOR <http://example.org/> ";
54          
55       String JavaDoc isfxQueryString = "SELECT * WHERE " +
56            "(?s, <ex:prop>, ?o) and ?o =~ /there$/i " +
57            "USING ex FOR <http://example.org/> ";
58
59     String JavaDoc epfxQueryString = "SELECT * WHERE " +
60          "(?s, <ex:prop>, ?o) and ?o =~ /^yo_their/ " +
61          "USING ex FOR <http://example.org/> ";
62          
63     String JavaDoc unboundQueryString = "SELECT * WHERE " +
64        "(?s, ?p, ?o) and ?o =~ /hi/";
65
66     String JavaDoc lobjQueryString = "SELECT * WHERE " +
67          "(?s, <ex:prop>, ?o) and ?o =~ /789obj/ " +
68          "USING ex FOR <http://example.org/> ";
69          
70     String JavaDoc lsubjQueryString = "SELECT * WHERE " +
71          "(?s, <ex:prop>, ?o) and ?s =~ /789subj/ " +
72          "USING ex FOR <http://example.org/> ";
73
74     String JavaDoc lpredQueryString = "SELECT * WHERE " +
75          "(<ex:subj>, ?p, ?o) and ?p =~ /789pred/ " +
76          "USING ex FOR <http://example.org/> ";
77
78
79
80     private static void loadClass()
81         {
82         try { Class.forName(TestPackage.M_DBDRIVER_CLASS); }
83         catch (Exception JavaDoc e) { throw new JenaException( e ); }
84         }
85         
86     public static IDBConnection makeTestConnection()
87         {
88         loadClass();
89         return new DBConnection
90             (
91             TestPackage.M_DB_URL,
92             TestPackage.M_DB_USER,
93             TestPackage.M_DB_PASSWD,
94             TestPackage.M_DB
95             );
96         }
97         
98     public static IDBConnection makeAndCleanTestConnection()
99         {
100             DBConnection result;
101             try{
102         result = (DBConnection) makeTestConnection();
103         // result.getConnection().setAutoCommit(false);
104
result.cleanDB();
105         } catch (Exception JavaDoc e) { throw new JenaException( e ); }
106         return result;
107         }
108         
109     public void testMatches() throws java.lang.Exception JavaDoc {
110
111         Class.forName(TestPackage.M_DBDRIVER_CLASS);
112         IDBConnection conn = makeAndCleanTestConnection();
113         ModelRDB model = ModelRDB.createModel(conn);
114         
115         IRDBDriver dbDriver = null;
116         dbDriver = conn.getDriver();
117
118         Resource subj =
119             ResourceFactory.createResource("http://example.org/subj");
120         Property pred =
121             ResourceFactory.createProperty("http://example.org/prop");
122         Resource obj = ResourceFactory.createResource("http://example.org/obj");
123         Statement s1 = ResourceFactory.createStatement(subj, pred, obj);
124
125         Literal lit1 = ResourceFactory.createPlainLiteral("hi there");
126         Statement s2 = ResourceFactory.createStatement(subj, pred, lit1);
127
128         Literal lit2 = ResourceFactory.createPlainLiteral("hi there again");
129         Statement s3 = ResourceFactory.createStatement(subj, pred, lit2);
130
131         Literal lit3 = ResourceFactory.createPlainLiteral("HI THERE");
132         Statement s4 = ResourceFactory.createStatement(subj, pred, lit3);
133
134         Literal lit4 = ResourceFactory.createPlainLiteral("HI THERE AGAIN");
135         Statement s5 = ResourceFactory.createStatement(subj, pred, lit4);
136
137         Literal lit5 = ResourceFactory.createPlainLiteral("yo_their");
138         Statement s6 = ResourceFactory.createStatement(subj, pred, lit5);
139
140         Literal lit6 = ResourceFactory.createPlainLiteral("yo their");
141         Statement s7 = ResourceFactory.createStatement(subj, pred, lit6);
142
143         // create three statements with long URIs and long literals
144
String JavaDoc sfx = "0123456789";
145         String JavaDoc longURI = "http://example.org/long#";
146         long longLen = dbDriver.getLongObjectLength();
147         // make long prefix
148
while ( longURI.length() < longLen )
149             longURI += sfx;
150         Resource subjLong = ResourceFactory.createResource(longURI+"subj");
151         Property predLong = ResourceFactory.createProperty(longURI+"pred");
152         Resource objLong = ResourceFactory.createResource(longURI+"obj");
153         Literal litLong = ResourceFactory.createPlainLiteral(longURI+"obj");
154         Statement s8 = ResourceFactory.createStatement(subjLong,pred,objLong);
155         Statement s9 = ResourceFactory.createStatement(subjLong,pred,litLong);
156         Statement s10 = ResourceFactory.createStatement(subj,predLong,litLong);
157
158
159         model.add(s1);
160         model.add(s2);
161         model.add(s3);
162         model.add(s4);
163         model.add(s5);
164         model.add(s6);
165         model.add(s7);
166         model.add(s8);
167         model.add(s9);
168         model.add(s10);
169
170         Query query;
171         QueryExecution qe;
172         QueryResults results;
173         int i;
174
175         query = new Query(pfxQueryString);
176         query.setSource(model);
177         qe = new QueryEngine(query);
178         results = qe.exec();
179
180         i = 0;
181         while (results.hasNext()) {
182             results.next(); i++;
183         }
184         assertTrue(i == 2);
185
186         query = new Query(ipfxQueryString);
187         query.setSource(model);
188         qe = new QueryEngine(query);
189         results = qe.exec();
190
191         i = 0;
192         while (results.hasNext()) {
193             results.next(); i++;
194         }
195         assertTrue(i == 4);
196
197         query = new Query(sfxQueryString);
198         query.setSource(model);
199         qe = new QueryEngine(query);
200         results = qe.exec();
201
202         i = 0;
203         while (results.hasNext()) {
204             results.next(); i++;
205         }
206         assertTrue(i == 1);
207
208         query = new Query(isfxQueryString);
209         query.setSource(model);
210         qe = new QueryEngine(query);
211         results = qe.exec();
212
213         i = 0;
214         while (results.hasNext()) {
215             results.next(); i++;
216         }
217         assertTrue(i == 2);
218     
219         query = new Query(epfxQueryString);
220         query.setSource(model);
221         qe = new QueryEngine(query);
222         results = qe.exec();
223
224         i = 0;
225         while (results.hasNext()) {
226             results.next(); i++;
227         }
228         assertTrue(i == 1);
229         
230         query = new Query(unboundQueryString);
231         query.setSource(model);
232         qe = new QueryEngine(query);
233         results = qe.exec();
234
235         i = 0;
236         while (results.hasNext()) {
237             results.next(); i++;
238         }
239         assertTrue(i == 2);
240         
241         query = new Query(lsubjQueryString);
242         query.setSource(model);
243         qe = new QueryEngine(query);
244         results = qe.exec();
245
246         i = 0;
247         while (results.hasNext()) {
248             results.next(); i++;
249         }
250         assertTrue(i == 2);
251         
252         query = new Query(lpredQueryString);
253         query.setSource(model);
254         qe = new QueryEngine(query);
255         results = qe.exec();
256
257         i = 0;
258         while (results.hasNext()) {
259             results.next(); i++;
260         }
261         assertTrue(i == 1);
262         
263         query = new Query(lobjQueryString);
264         query.setSource(model);
265         qe = new QueryEngine(query);
266         results = qe.exec();
267
268         i = 0;
269         while (results.hasNext()) {
270             results.next(); i++;
271         }
272         assertTrue(i == 2);
273
274     }
275
276
277 }
278         
279
280 /*
281     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
282     All rights reserved.
283
284     Redistribution and use in source and binary forms, with or without
285     modification, are permitted provided that the following conditions
286     are met:
287
288     1. Redistributions of source code must retain the above copyright
289        notice, this list of conditions and the following disclaimer.
290
291     2. Redistributions in binary form must reproduce the above copyright
292        notice, this list of conditions and the following disclaimer in the
293        documentation and/or other materials provided with the distribution.
294
295     3. The name of the author may not be used to endorse or promote products
296        derived from this software without specific prior written permission.
297
298     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
299     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
300     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
301     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
302     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
303     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
304     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
305     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
306     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
308 */

309
Popular Tags