1 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 ; 18 19 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 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 rdqlAll = "SELECT * WHERE (?x ?y ?z)"; 51 54 int counter = 1 ; 55 56 58 addTest(new PingTest( "Ping-"+(counter++), testServer.emptyModelURI, true)) ; 61 addTest(new PingTest( "Ping-"+(counter++), testServer.testdata_URI, true)) ; 63 64 addTest(new PingTest( "Ping-"+(counter++), testServer.noSuchModelURI, false)) ; 66 addTest(new PingTest( "Ping-"+(counter++), testServer.scratchModelURI, false)) ; 68 69 70 73 addTest(new FetchTest("Fetch-"+(counter++), testServer.emptyModelURI, "http://anything/", empty)) ; 74 75 Model m = ModelLoader.loadModel("Data/Test/fetch-result-1.n3") ; 76 addTest(new FetchTest("Fetch-"+(counter++), testServer.fetchModelURI, "http://jena/object#1", m)) ; 78 79 addTest(new FetchTest("Fetch-"+(counter++), testServer.fetchModelURI, 81 "http://jena/ns#name", "Object/1", false, m)) ; 82 83 m = null ; 84 85 88 addTest(new QueryTest("GET-" + (counter++), 90 null, null, 91 testServer.testdata_URI, testData1)); 92 93 addTest(new QueryTest("GET-" + (counter++), 95 null, null, 96 testServer.testdata2_URI, testData1)); 97 98 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 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 p[] = { "format" } ; 117 String v[] = { "BV" } ; 118 addTest(new QueryTest("Query-" + (counter++) + "-RDQL", 120 "rdql", rdqlAll, p, v, 121 testServer.testdata_URI, testData1_BV)); 122 123 124 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 { 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 []{"p"},new String []{"http://jena/property1"}, 138 testServer.testdata2_URI, results) ; 139 140 addTest(t) ; 141 } 142 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 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 addTest(new OptionsTest( "Options-"+(counter++), testServer.baseURI, true)) ; 179 addTest(new OptionsTest( "Options-"+(counter++), testServer.scratchModelURI, true)) ; 180 181 184 addTest(new AddTest( "Add-"+(counter++), testServer.scratchModelURI, testData1, testData1)) ; 185 addTest(new RemoveTest( "Remove-"+(counter++), testServer.scratchModelURI, testData2, empty)) ; 187 188 191 Model infResult = null ; 192 193 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 216 218 class QueryTest extends ClientLibraryTest 219 { 220 String queryLang ; 221 String queryString ; 222 boolean successExpected = true ; 223 boolean usePOST = false ; 224 String [] queryParamNames = null ; 225 String [] queryParamValues = null ; 226 227 228 QueryTest(String testName, String qLang, String qString, String target, Model resultModel) 229 { 230 this(testName, qLang, qString, null, null, target, resultModel) ; 231 } 232 233 234 236 QueryTest(String testName, String qLang, String qString, 237 String params[], String values[], 238 String 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 testName, String qLang, String qString, String target, HttpException httpEx) 248 { 249 this( testName, qLang, qString, null, null, target, httpEx) ; 250 } 251 252 253 254 QueryTest(String testName, String qLang, String qString, 255 String params[], String values[], 256 String 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 testName, String qLang, String qString, String target, int responseCode) 266 { 267 this(testName, qLang, qString, null, null, target, responseCode) ; 268 } 269 270 QueryTest(String testName, String qLang, String qString, 271 String params[], String values[], 272 String 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 282 { 283 String 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 ("Failed to set up test: no values for parameters.") ; 292 if ( queryParamNames.length != queryParamValues.length ) 293 throw new RuntimeException ("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 modelURI ; 318 String resource ; 319 String predicate ; 320 String objectURI ; 321 String objectValue ; 322 boolean successExpected = true ; 323 324 FetchTest(String testName, String target, String 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 FetchTest(String testName, String target, String predicateURI, 336 String 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 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 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 testName, String 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 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 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 testName, String target, Model d, Model results) 422 { 423 super(writer, testName, target, results) ; 424 inputModel = d ; 425 } 426 427 protected void setUpTest() throws Throwable 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 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 477 { 478 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 testName, String target, boolean shouldWork) 490 { 491 super(writer, testName, target, shouldWork) ; 492 } 493 494 protected Model performTest() throws Throwable 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 modelURI ; 506 Model data ; 507 boolean expectSuccess ; 508 509 PingTest(String testName, String target, boolean shouldWork) 510 { 511 super(writer, testName, target, shouldWork) ; 512 modelURI = target ; 513 expectSuccess = shouldWork ; 514 } 515 516 protected Model performTest() throws Throwable 517 { 518 HttpPing httpPing = new HttpPing(modelURI) ; 519 Model resultModel = httpPing.exec() ; 520 return resultModel ; 521 } 522 } 523 } 524 525 551 | Popular Tags |