KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > test > JosekiClientLibraryTests


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

5  
6 package org.joseki.test;
7
8 import java.io.* ;
9 import org.apache.commons.logging.* ;
10 import junit.framework.*;
11 import com.hp.hpl.jena.joseki.* ;
12
13
14 import com.hp.hpl.jena.rdf.model.* ;
15 import com.hp.hpl.jena.shared.*;
16 import com.hp.hpl.jena.util.ModelLoader ;
17 import javax.servlet.http.HttpServletResponse JavaDoc ;
18
19 /** Tests of the client library
20  *
21  * Note: depends on external files (configuration, data for models)
22  * in the current directory and an already built RDF server.
23  * Tests must leave the server and its models unchanged.
24  *
25  * @author Andy Seaborne
26  * @version $Id: JosekiClientLibraryTests.java,v 1.17 2004/04/28 15:41:37 andy_seaborne Exp $
27  */

28 public class JosekiClientLibraryTests extends TestSuite
29 {
30     static Log log = LogFactory.getLog("JosekiClientLibraryTests") ;
31     
32     TestServer testServer = null ;
33     PrintWriter writer = null ;
34     
35     static final Model empty = ModelFactory.createDefaultModel() ;
36     
37     public JosekiClientLibraryTests(TestServer server)
38     {
39         super("Joseki Client Library Tests") ;
40         testServer = server ;
41
42         try {
43             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"))) ;
44         } catch (java.io.UnsupportedEncodingException JavaDoc ex) {}
45
46         Model testData1 = ModelLoader.loadModel("Data/Test/test_data_1.nt") ;
47         Model testData2 = ModelLoader.loadModel("Data/Test/test_data_2.nt") ;
48         Model testData1_BV = ModelLoader.loadModel("Data/Test/test_data_1-results.n3") ;
49         
50         String JavaDoc rdqlAll = "SELECT * WHERE (?x ?y ?z)";
51         //String rdqlPath2 = "SELECT * WHERE (?x, ?y, ?z), (?z, ?a, ?b)" ;
52
//String rdqlProp = "SELECT * WHERE (?x, <http://jena/property2>, ?z)" ;
53

54         int counter = 1 ;
55
56         // ------------------------------------------------
57

58         // Ping tests - also test "no such model", "no operation".
59
// Empty model has "ping" debug inline.
60
addTest(new PingTest( "Ping-"+(counter++), testServer.emptyModelURI, true)) ;
61         // Data model has "ping" dedebugd as a standard operation
62
addTest(new PingTest( "Ping-"+(counter++), testServer.testdata_URI, true)) ;
63         
64         // Model does not exist
65
addTest(new PingTest( "Ping-"+(counter++), testServer.noSuchModelURI, false)) ;
66         // Has no Ping operation
67
addTest(new PingTest( "Ping-"+(counter++), testServer.scratchModelURI, false)) ;
68
69
70         // ------------------------------------------------
71
// Fetch
72

73         addTest(new FetchTest("Fetch-"+(counter++), testServer.emptyModelURI, "http://anything/", empty)) ;
74         
75         Model m = ModelLoader.loadModel("Data/Test/fetch-result-1.n3") ;
76         // Fetch by URI
77
addTest(new FetchTest("Fetch-"+(counter++), testServer.fetchModelURI, "http://jena/object#1", m)) ;
78
79         // Fetch by property/value
80
addTest(new FetchTest("Fetch-"+(counter++), testServer.fetchModelURI,
81                 "http://jena/ns#name", "Object/1", false, m)) ;
82         
83         m = null ;
84
85         // ------------------------------------------------
86
// Queries that should succeed
87

88         // Plain GET
89
addTest(new QueryTest("GET-" + (counter++),
90                 null, null,
91                 testServer.testdata_URI, testData1));
92         
93         // Plain GET on the XML data model
94
addTest(new QueryTest("GET-" + (counter++),
95                 null, null,
96                 testServer.testdata2_URI, testData1));
97         
98         // Set the query language to GET
99
addTest(new QueryTest("GET-" + (counter++),
100                 "GET", null,
101                 testServer.testdata_URI, testData1));
102         
103         addTest(new QueryTest("GET-" + (counter++),
104                 "GET", null,
105                 testServer.emptyModelURI, empty));
106         
107         // RDQL query
108
addTest(new QueryTest("Query-" + (counter++) + "-RDQL",
109                 "RDQL", rdqlAll,
110                 testServer.testdata_URI, testData1));
111                 
112         addTest(new QueryTest("Query-" + (counter++) + "-RDQL",
113                 "rdql", rdqlAll,
114                 testServer.testdata_URI, testData1));
115                 
116         String JavaDoc p[] = { "format" } ;
117         String JavaDoc v[] = { "BV" } ;
118         // Same as before but ask for bound variable RDF graph result set.
119
addTest(new QueryTest("Query-" + (counter++) + "-RDQL",
120                 "rdql", rdqlAll, p, v,
121                 testServer.testdata_URI, testData1_BV));
122                 
123
124         // RDQL query forced over HTTP POST
125
QueryTest t1 = new QueryTest("Query-" + (counter++) + "-RDQL/POST",
126                             "RDQL", rdqlAll,
127                             testServer.testdata_URI, testData1);
128         t1.usePOST = true ;
129         addTest(t1) ;
130         
131         
132         // SPO tests
133
{
134             Model results = ModelLoader.loadModel("Data/Test/test_data_SPO_1.nt") ;
135             QueryTest t = new QueryTest("Query-" + (counter++) + "-SPO",
136                              "SPO", null,
137                              new String JavaDoc[]{"p"},new String JavaDoc[]{"http://jena/property1"},
138                              testServer.testdata2_URI, results) ;
139             
140             addTest(t) ;
141         }
142         // ------------------------------------------------
143
// Queries that should not work
144
addTest(new QueryTest("Query-" + (counter++) + "-GET",
145                 "GET", null,
146                 testServer.noSuchModelURI, HttpServletResponse.SC_NOT_FOUND)) ;
147
148         addTest(new QueryTest("Query-" + (counter++) + "-NoSuchLanguage",
149                 "ThisLanguageDoesNotExist", null,
150                 testServer.emptyModelURI, HttpServletResponse.SC_NOT_IMPLEMENTED)) ;
151
152         addTest(new QueryTest("Query-" + (counter++) + "-RDQL-NoQuery1",
153                 "RDQL", null,
154                 testServer.emptyModelURI, HttpServletResponse.SC_BAD_REQUEST)) ;
155
156         addTest(new QueryTest("Query-" + (counter++) + "-RDQL-NoQuery2",
157                 "RDQL", "",
158                 testServer.emptyModelURI, HttpServletResponse.SC_BAD_REQUEST)) ;
159
160         addTest(new QueryTest("Query-" + (counter++) + "-RDQL-ParseError",
161                 "RDQL", "SELECT * WHERE rubbish",
162                 testServer.emptyModelURI, HttpServletResponse.SC_BAD_REQUEST)) ;
163
164         // RDQL query forced over HTTP POST (model does not allow it)
165
QueryTest t2 = new QueryTest("Query-" + (counter++) + "-RDQL/POST-failure",
166                             "RDQL", rdqlAll,
167                             testServer.emptyModelURI, HttpServletResponse.SC_NOT_IMPLEMENTED);
168         t2.usePOST = true ;
169         addTest(t2) ;
170                 
171         addTest(new QueryTest("Query-" + (counter++) + "-NoLangGiven",
172                 null, "Anything",
173                 testServer.emptyModelURI, HttpServletResponse.SC_BAD_REQUEST)) ;
174
175
176         // ------------------------------------------------
177
// Options
178
addTest(new OptionsTest( "Options-"+(counter++), testServer.baseURI, true)) ;
179         addTest(new OptionsTest( "Options-"+(counter++), testServer.scratchModelURI, true)) ;
180
181         // ------------------------------------------------
182
// Operations
183

184         addTest(new AddTest( "Add-"+(counter++), testServer.scratchModelURI, testData1, testData1)) ;
185         // Must not use bNode data
186
addTest(new RemoveTest( "Remove-"+(counter++), testServer.scratchModelURI, testData2, empty)) ;
187         
188         // ------------------------------------------------
189
// Inference
190

191         Model infResult = null ;
192         
193         // Currently there is a Jena bug that causes a warning:
194
// Workaround for bugs 803804 and 858163: using RDF/XML (not RDF/XML-ABBREV) writer for inferred graph
195
// If the model is immutable (i.e. GET just hands out the underlying model).
196

197         infResult = ModelLoader.loadModel("Data/Test/inf-result-get.rdf") ;
198         addTest(new QueryTest("Inf-GET-" + (counter++), null, null, testServer.infModelURI, infResult)) ;
199         
200         infResult = ModelLoader.loadModel("Data/Test/inf-result-fetch.n3") ;
201         addTest(new FetchTest("Inf-fetch-" + (counter++),
202                     testServer.infModelURI, "http://example.com/resource", infResult)) ;
203         infResult = null ;
204         
205     }
206     
207 // public void run(TestResult result)
208
// {
209
// super.run(result) ;
210
// // Run and clearup.
211
// // TestCases have a clearup method - Tests (and TestSuites) don't
212
// if ( server != null )
213
// server.stop() ;
214
// }
215

216     // Test classes
217

218     class QueryTest extends ClientLibraryTest
219     {
220         String JavaDoc queryLang ;
221         String JavaDoc queryString ;
222         boolean successExpected = true ;
223         boolean usePOST = false ;
224         String JavaDoc[] queryParamNames = null ;
225         String JavaDoc[] queryParamValues = null ;
226         
227         
228         QueryTest(String JavaDoc testName, String JavaDoc qLang, String JavaDoc qString, String JavaDoc target, Model resultModel)
229         {
230             this(testName, qLang, qString, null, null, target, resultModel) ;
231         }
232         
233
234         // Same but with query parameters
235

236         QueryTest(String JavaDoc testName, String JavaDoc qLang, String JavaDoc qString,
237                                    String JavaDoc params[], String JavaDoc values[],
238                                    String JavaDoc target, Model resultModel)
239         {
240             super(writer, testName, target, resultModel) ;
241             queryLang = qLang ;
242             queryString = qString ;
243             queryParamNames = params ;
244             queryParamValues = values ;
245         }
246         
247         QueryTest(String JavaDoc testName, String JavaDoc qLang, String JavaDoc qString, String JavaDoc target, HttpException httpEx)
248         {
249             this( testName, qLang, qString, null, null, target, httpEx) ;
250         }
251             
252             
253
254         QueryTest(String JavaDoc testName, String JavaDoc qLang, String JavaDoc qString,
255                                    String JavaDoc params[], String JavaDoc values[],
256                                    String JavaDoc target, HttpException httpEx)
257         {
258             super(writer, testName, target, httpEx) ;
259             queryLang = qLang ;
260             queryString = qString ;
261             queryParamNames = params ;
262             queryParamValues = values ;
263         }
264
265         QueryTest(String JavaDoc testName, String JavaDoc qLang, String JavaDoc qString, String JavaDoc target, int responseCode)
266         {
267             this(testName, qLang, qString, null, null, target, responseCode) ;
268         }
269             
270         QueryTest(String JavaDoc testName, String JavaDoc qLang, String JavaDoc qString,
271                                    String JavaDoc params[], String JavaDoc values[],
272                                    String JavaDoc target, int responseCode)
273         {
274             super(writer, testName, target, responseCode) ;
275             queryLang = qLang ;
276             queryString = qString ;
277             queryParamNames = params ;
278             queryParamValues = values ;
279         }
280
281         protected Model performTest() throws Throwable JavaDoc
282         {
283             String JavaDoc tmp = (queryString==null?"<<null>>":queryString) ;
284             HttpQuery httpQuery = new HttpQuery(modelURI, queryLang) ;
285             if ( queryString != null )
286                 httpQuery.addParam("query", queryString) ;
287
288             if ( queryParamNames != null )
289             {
290                 if ( queryParamValues == null )
291                     throw new RuntimeException JavaDoc("Failed to set up test: no values for parameters.") ;
292                 if ( queryParamNames.length != queryParamValues.length )
293                     throw new RuntimeException JavaDoc("Failed to set up test: values and paramtyers mismatch.") ;
294                 for ( int i = 0 ; i < queryParamNames.length ; i++ )
295                 {
296                     httpQuery.addParam(queryParamNames[i], queryParamValues[i]) ;
297                 }
298             }
299             
300             if ( usePOST )
301                 httpQuery.setForcePOST() ;
302             try {
303                 Model model = httpQuery.exec() ;
304                 return model ;
305             } catch (JenaException jEx)
306             {
307                 log.warn(this.getName()+": "+tmp) ;
308                 jEx.printStackTrace(System.out) ;
309                 throw jEx ;
310             }
311         }
312         
313     }
314     
315     class FetchTest extends ClientLibraryTest
316     {
317         String JavaDoc modelURI ;
318         String JavaDoc resource ;
319         String JavaDoc predicate ;
320         String JavaDoc objectURI ;
321         String JavaDoc objectValue ;
322         boolean successExpected = true ;
323         
324         FetchTest(String JavaDoc testName, String JavaDoc target, String JavaDoc thing, Model resultModel)
325         {
326             super(writer, testName, target, resultModel) ;
327             modelURI = target ;
328             resource = thing ;
329             predicate = null ;
330             objectValue = null ;
331             objectURI = null ;
332         }
333         
334         // predicate/value form
335
FetchTest(String JavaDoc testName, String JavaDoc target, String JavaDoc predicateURI,
336                   String JavaDoc objURIorValue, boolean isURI, Model resultModel)
337         {
338             super(writer, testName, target, resultModel) ;
339             modelURI = target ;
340             resource = null ;
341             predicate = predicateURI ;
342             
343             objectValue = null ;
344             objectURI = null ;
345             if ( isURI )
346                 objectURI = objURIorValue;
347             else
348                 objectValue= objURIorValue ;
349         }
350         
351         protected Model performTest() throws Throwable JavaDoc
352         {
353             if ( resource != null )
354             {
355                 HttpFetch httpFetch = new HttpFetch(modelURI, resource) ;
356                 Model model = httpFetch.exec();
357                 return model;
358             }
359             if ( predicate != null )
360             {
361                 String JavaDoc node = null ;
362                 boolean isResource = false ;
363                 
364                 if ( objectURI != null )
365                 {
366                     node = objectURI ;
367                     isResource = true ;
368                 }
369                 if ( objectValue != null )
370                 {
371                     node = objectValue ;
372                     isResource = false ;
373                 }
374                 
375                 HttpFetch httpFetch = new HttpFetch(modelURI, predicate, node, isResource) ;
376                 Model model = httpFetch.exec();
377                 return model;
378             }
379             fail("Test setup failure: no resource or predciate for fetch") ;
380             return null ;
381         }
382     }
383     
384     class AddTest extends ClientLibraryTest
385     {
386         Model inputModel ;
387         
388         AddTest(String JavaDoc testName, String JavaDoc target, Model d, Model results)
389         {
390             super(writer, testName, target, results) ;
391             inputModel = d ;
392             modelURI = target ;
393         }
394
395         protected Model performTest() throws Throwable JavaDoc
396         {
397             HttpAdd httpAdd = new HttpAdd(modelURI);
398             httpAdd.add(inputModel);
399             httpAdd.exec();
400
401             HttpQuery httpGet = new HttpQuery(modelURI);
402             Model model1 = httpGet.exec();
403             return model1 ;
404         }
405         
406         protected void takeDownTest() throws Throwable JavaDoc
407         {
408             HttpClear httpClear = new HttpClear(modelURI);
409             httpClear.exec();
410             HttpQuery httpGetZ = new HttpQuery(modelURI);
411             Model modelZ = httpGetZ.exec();
412             assertTrue(modelZ.size() == 0);
413         }
414
415     }
416     
417     class RemoveTest extends ClientLibraryTest
418     {
419         Model inputModel ;
420         
421         RemoveTest(String JavaDoc testName, String JavaDoc target, Model d, Model results)
422         {
423             super(writer, testName, target, results) ;
424             inputModel = d ;
425         }
426
427         protected void setUpTest() throws Throwable JavaDoc
428         {
429             HttpAdd httpAdd = new HttpAdd(modelURI);
430             httpAdd.add(inputModel);
431             httpAdd.exec();
432
433             HttpQuery httpGet1 = new HttpQuery(modelURI);
434             Model model1 = httpGet1.exec();
435
436             boolean passesTest = model1.isIsomorphicWith(inputModel) ;
437             if (!passesTest)
438             {
439                 out.println(this.getName() + " Expected --------------------------");
440                 dumpModel(inputModel);
441                 out.println(this.getName() + " Got -------------------------------");
442                 dumpModel(model1);
443                 out.println(this.getName() + " -----------------------------------");
444             }
445             assertTrue("Target model not initialized correctly",passesTest) ;
446         }
447
448         protected Model performTest() throws Throwable JavaDoc
449         {
450             log.debug(getName()+" :: Remove("+modelURI+")") ;
451             HttpRemove httpRemove = new HttpRemove(modelURI) ;
452             httpRemove.remove(inputModel) ;
453             Model result = httpRemove.exec() ;
454             
455             HttpQuery httpGet2 = new HttpQuery(modelURI);
456             Model model2 = httpGet2.exec();
457             if ( model2.size() != 0 )
458             {
459                 out.println(this.getName() + " Expected --------------------------");
460                 out.println("Empty model") ;
461                 out.println(this.getName() + " Got -------------------------------");
462                 dumpModel(model2);
463                 out.println(this.getName() + " -----------------------------------");
464                 
465             }
466             assertTrue(model2.size() == 0) ;
467             StmtIterator sIter = inputModel.listStatements() ;
468             for ( ; sIter.hasNext() ; )
469             {
470                 Statement s = sIter.nextStatement() ;
471                 assertTrue( !model2.contains(s) ) ;
472             }
473             return result ;
474         }
475         
476         protected void takeDownTest() throws Throwable JavaDoc
477         {
478             // Ensure empty.
479
HttpClear httpClear = new HttpClear(modelURI);
480             httpClear.exec();
481             HttpQuery httpGetZ = new HttpQuery(modelURI);
482             Model modelZ = httpGetZ.exec();
483             assertTrue(modelZ.size() == 0);
484         }
485     }
486     
487     class OptionsTest extends ClientLibraryTest
488     {
489         OptionsTest(String JavaDoc testName, String JavaDoc target, boolean shouldWork)
490         {
491             super(writer, testName, target, shouldWork) ;
492         }
493         
494         protected Model performTest() throws Throwable JavaDoc
495         {
496             HttpOptions httpOptions = new HttpOptions(modelURI) ;
497             Model resultModel = httpOptions.exec() ;
498             assertTrue(resultModel.size() != 0 ) ;
499             return resultModel ;
500         }
501     }
502
503     class PingTest extends ClientLibraryTest
504     {
505         String JavaDoc modelURI ;
506         Model data ;
507         boolean expectSuccess ;
508         
509         PingTest(String JavaDoc testName, String JavaDoc target, boolean shouldWork)
510         {
511             super(writer, testName, target, shouldWork) ;
512             modelURI = target ;
513             expectSuccess = shouldWork ;
514         }
515         
516         protected Model performTest() throws Throwable JavaDoc
517         {
518             HttpPing httpPing = new HttpPing(modelURI) ;
519             Model resultModel = httpPing.exec() ;
520             return resultModel ;
521         }
522     }
523 }
524
525 /*
526  * (c) Copyright 2003,2004 Hewlett-Packard Development Company, LP
527  * All rights reserved.
528  *
529  * Redistribution and use in source and binary forms, with or without
530  * modification, are permitted provided that the following conditions
531  * are met:
532  * 1. Redistributions of source code must retain the above copyright
533  * notice, this list of conditions and the following disclaimer.
534  * 2. Redistributions in binary form must reproduce the above copyright
535  * notice, this list of conditions and the following disclaimer in the
536  * documentation and/or other materials provided with the distribution.
537  * 3. The name of the author may not be used to endorse or promote products
538  * derived from this software without specific prior written permission.
539  *
540  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
541  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
542  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
543  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
544  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
545  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
546  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
547  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
548  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
549  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
550  */

551
Popular Tags