1*cfcf00c6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*cfcf00c6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*cfcf00c6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*cfcf00c6SAndrew Rist * distributed with this work for additional information 6*cfcf00c6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*cfcf00c6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*cfcf00c6SAndrew Rist * "License"); you may not use this file except in compliance 9*cfcf00c6SAndrew Rist * with the License. You may obtain a copy of the License at 10*cfcf00c6SAndrew Rist * 11*cfcf00c6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*cfcf00c6SAndrew Rist * 13*cfcf00c6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*cfcf00c6SAndrew Rist * software distributed under the License is distributed on an 15*cfcf00c6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*cfcf00c6SAndrew Rist * KIND, either express or implied. See the License for the 17*cfcf00c6SAndrew Rist * specific language governing permissions and limitations 18*cfcf00c6SAndrew Rist * under the License. 19*cfcf00c6SAndrew Rist * 20*cfcf00c6SAndrew Rist *************************************************************/ 21*cfcf00c6SAndrew Rist 22*cfcf00c6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package complex.unoxml; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import helper.StreamSimulator; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 29cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 30cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 31cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 32cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 33cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException; 34cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetRuntimeException; 35cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 36cdf0e10cSrcweir import com.sun.star.beans.Pair; 37cdf0e10cSrcweir import com.sun.star.beans.StringPair; 38cdf0e10cSrcweir import com.sun.star.container.XEnumeration; 39cdf0e10cSrcweir import com.sun.star.container.ElementExistException; 40cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 41cdf0e10cSrcweir import com.sun.star.io.XInputStream; 42cdf0e10cSrcweir import com.sun.star.io.XOutputStream; 43cdf0e10cSrcweir import com.sun.star.text.XTextRange; 44cdf0e10cSrcweir import com.sun.star.text.XText; 45cdf0e10cSrcweir import com.sun.star.rdf.*; 46cdf0e10cSrcweir import lib.TestParameters; 47cdf0e10cSrcweir 48cdf0e10cSrcweir import org.junit.After; 49cdf0e10cSrcweir import org.junit.AfterClass; 50cdf0e10cSrcweir import org.junit.Before; 51cdf0e10cSrcweir import org.junit.BeforeClass; 52cdf0e10cSrcweir import org.junit.Test; 53cdf0e10cSrcweir import org.openoffice.test.OfficeConnection; 54cdf0e10cSrcweir import static org.junit.Assert.*; 55cdf0e10cSrcweir 56cdf0e10cSrcweir /** 57cdf0e10cSrcweir * Test case for service com.sun.star.rdf.Repository 58cdf0e10cSrcweir * Currently, this service is implemented in 59cdf0e10cSrcweir * unoxml/source/rdf/librdf_repository.cxx 60cdf0e10cSrcweir * 61cdf0e10cSrcweir * @author mst 62cdf0e10cSrcweir */ 63cdf0e10cSrcweir public class RDFRepositoryTest 64cdf0e10cSrcweir { 65cdf0e10cSrcweir XComponentContext xContext; 66cdf0e10cSrcweir String tempDir; 67cdf0e10cSrcweir 68cdf0e10cSrcweir XDocumentRepository xRep; 69cdf0e10cSrcweir XURI foo; 70cdf0e10cSrcweir XURI bar; 71cdf0e10cSrcweir XURI baz; 72cdf0e10cSrcweir XURI uint; 73cdf0e10cSrcweir XURI rdfslabel; 74cdf0e10cSrcweir XURI manifest; 75cdf0e10cSrcweir XURI uuid; 76cdf0e10cSrcweir XURI base; 77cdf0e10cSrcweir XBlankNode blank; 78cdf0e10cSrcweir XLiteral lit; 79cdf0e10cSrcweir XLiteral litlang; 80cdf0e10cSrcweir XLiteral littype; 81cdf0e10cSrcweir String rdfs = "http://www.w3.org/2000/01/rdf-schema#"; 82cdf0e10cSrcweir 83cdf0e10cSrcweir /** 84cdf0e10cSrcweir * The test parameters 85cdf0e10cSrcweir */ 86cdf0e10cSrcweir private static TestParameters param = null; 87cdf0e10cSrcweir 88cdf0e10cSrcweir // public String[] getTestMethodNames () 89cdf0e10cSrcweir // { 90cdf0e10cSrcweir // return new String[] { "check", "checkSPARQL", "checkRDFa" }; 91cdf0e10cSrcweir // } 92cdf0e10cSrcweir before()93cdf0e10cSrcweir @Before public void before() 94cdf0e10cSrcweir { 95cdf0e10cSrcweir try { 96cdf0e10cSrcweir XMultiServiceFactory xMSF = getMSF(); 97cdf0e10cSrcweir param = new TestParameters(); 98cdf0e10cSrcweir param.put("ServiceFactory", xMSF); 99cdf0e10cSrcweir 100cdf0e10cSrcweir assertNotNull("could not create MultiServiceFactory.", xMSF); 101cdf0e10cSrcweir XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xMSF); 102cdf0e10cSrcweir Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext"); 103cdf0e10cSrcweir xContext = UnoRuntime.queryInterface(XComponentContext.class, defaultCtx); 104cdf0e10cSrcweir assertNotNull("could not get component context.", xContext); 105cdf0e10cSrcweir 106cdf0e10cSrcweir tempDir = util.utils.getOfficeTemp/*Dir*/(xMSF); 107cdf0e10cSrcweir System.out.println("tempdir: " + tempDir); 108cdf0e10cSrcweir 109cdf0e10cSrcweir foo = URI.create(xContext, "uri:foo"); 110cdf0e10cSrcweir assertNotNull("foo", foo); 111cdf0e10cSrcweir bar = URI.create(xContext, "uri:bar"); 112cdf0e10cSrcweir assertNotNull("bar", bar); 113cdf0e10cSrcweir baz = URI.create(xContext, "uri:baz"); 114cdf0e10cSrcweir assertNotNull("baz", baz); 115cdf0e10cSrcweir uint = URI.create(xContext, "uri:int"); 116cdf0e10cSrcweir assertNotNull("uint", uint); 117cdf0e10cSrcweir blank = BlankNode.create(xContext, "_:uno"); 118cdf0e10cSrcweir assertNotNull("blank", blank); 119cdf0e10cSrcweir lit = Literal.create(xContext, "i am the literal"); 120cdf0e10cSrcweir assertNotNull("lit", lit); 121cdf0e10cSrcweir litlang = Literal.createWithLanguage(xContext, 122cdf0e10cSrcweir "i am the literal", "en"); 123cdf0e10cSrcweir assertNotNull("litlang", litlang); 124cdf0e10cSrcweir littype = Literal.createWithType(xContext, "42", uint); 125cdf0e10cSrcweir assertNotNull("littype", littype); 126cdf0e10cSrcweir 127cdf0e10cSrcweir rdfslabel = URI.create(xContext, rdfs + "label"); 128cdf0e10cSrcweir assertNotNull("rdfslabel", rdfslabel); 129cdf0e10cSrcweir manifest = URI.create(xContext, "manifest:manifest"); //FIXME 130cdf0e10cSrcweir assertNotNull("manifest", manifest); 131cdf0e10cSrcweir uuid = URI.create(xContext, 132cdf0e10cSrcweir "urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"); 133cdf0e10cSrcweir assertNotNull("uuid", uuid); 134cdf0e10cSrcweir base = URI.create(xContext, "base-uri:"); //FIXME 135cdf0e10cSrcweir assertNotNull("base", base); 136cdf0e10cSrcweir } catch (Exception e) { 137cdf0e10cSrcweir report(e); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir //FIXME: ? 140cdf0e10cSrcweir // xRep = Repository.create(xContext); 141cdf0e10cSrcweir System.out.println("Creating service Repository..."); 142cdf0e10cSrcweir xRep = UnoRuntime.queryInterface(XDocumentRepository.class, Repository.create(xContext)); 143cdf0e10cSrcweir assertNotNull("null", xRep); 144cdf0e10cSrcweir System.out.println("...done"); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir after()147cdf0e10cSrcweir @After public void after() 148cdf0e10cSrcweir { 149cdf0e10cSrcweir xRep = null; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir check()152cdf0e10cSrcweir @Test public void check() 153cdf0e10cSrcweir { 154cdf0e10cSrcweir try { 155cdf0e10cSrcweir System.out.println("Checking that new repository is really empty..."); 156cdf0e10cSrcweir assertTrue("empty: graphs", 0 == xRep.getGraphNames().length); 157cdf0e10cSrcweir 158cdf0e10cSrcweir XEnumeration stmts; 159cdf0e10cSrcweir stmts = xRep.getStatements(null, null, null); 160cdf0e10cSrcweir assertTrue("empty: stmts", !stmts.hasMoreElements()); 161cdf0e10cSrcweir 162cdf0e10cSrcweir System.out.println("...done"); 163cdf0e10cSrcweir 164cdf0e10cSrcweir System.out.println("Checking graph creation..."); 165cdf0e10cSrcweir 166cdf0e10cSrcweir XNamedGraph xFooGraph = xRep.createGraph(foo); 167cdf0e10cSrcweir assertNotNull("foo graph", xFooGraph); 168cdf0e10cSrcweir 169cdf0e10cSrcweir try { 170cdf0e10cSrcweir xRep.createGraph(foo); 171cdf0e10cSrcweir fail("creating duplicate graph was allowed"); 172cdf0e10cSrcweir } catch (ElementExistException e) { 173cdf0e10cSrcweir // ignore 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir try { 177cdf0e10cSrcweir xRep.createGraph(null); 178cdf0e10cSrcweir fail("invalid graph name was allowed"); 179cdf0e10cSrcweir } catch (IllegalArgumentException e) { 180cdf0e10cSrcweir // ignore 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir XURI[] names = xRep.getGraphNames(); 184cdf0e10cSrcweir assertTrue("no foo graph in getGraphNames", 185cdf0e10cSrcweir 1 == names.length && eq(names[0], foo)); 186cdf0e10cSrcweir assertNotNull("no foo graph", xRep.getGraph(foo)); 187cdf0e10cSrcweir 188cdf0e10cSrcweir stmts = xFooGraph.getStatements(null, null, null); 189cdf0e10cSrcweir assertTrue("stmts in foo graph", !stmts.hasMoreElements()); 190cdf0e10cSrcweir 191cdf0e10cSrcweir XOutputStream xFooOut = 192cdf0e10cSrcweir new StreamSimulator(tempDir + "empty.rdf", false, param); 193cdf0e10cSrcweir xRep.exportGraph(FileFormat.RDF_XML, xFooOut, foo, base); 194cdf0e10cSrcweir xFooOut.closeOutput(); 195cdf0e10cSrcweir 196cdf0e10cSrcweir XInputStream xFooIn = 197cdf0e10cSrcweir new StreamSimulator(tempDir + "empty.rdf", true, param); 198cdf0e10cSrcweir xRep.importGraph(FileFormat.RDF_XML, xFooIn, bar, base); 199cdf0e10cSrcweir assertNotNull("no bar graph", xRep.getGraph(bar)); 200cdf0e10cSrcweir 201cdf0e10cSrcweir System.out.println("...done"); 202cdf0e10cSrcweir 203cdf0e10cSrcweir System.out.println("Checking graph manipulation..."); 204cdf0e10cSrcweir 205cdf0e10cSrcweir XEnumeration xFooEnum; 206cdf0e10cSrcweir 207cdf0e10cSrcweir Statement xFoo_FooBarBaz = new Statement(foo, bar, baz, foo); 208cdf0e10cSrcweir xFooGraph.addStatement(foo, bar, baz); 209cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 210cdf0e10cSrcweir assertTrue("addStatement(foo,bar,baz)", 211cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_FooBarBaz })); 212cdf0e10cSrcweir 213cdf0e10cSrcweir Statement xFoo_FooBarBlank = new Statement(foo, bar, blank, foo); 214cdf0e10cSrcweir xFooGraph.addStatement(foo, bar, blank); 215cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 216cdf0e10cSrcweir assertTrue("addStatement(foo,bar,blank)", 217cdf0e10cSrcweir eq(xFooEnum, 218cdf0e10cSrcweir new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank })); 219cdf0e10cSrcweir xFooEnum = xRep.getStatements(null, null, null); 220cdf0e10cSrcweir assertTrue("addStatement(foo,bar,blank) (global)", 221cdf0e10cSrcweir eq(xFooEnum, 222cdf0e10cSrcweir new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank })); 223cdf0e10cSrcweir 224cdf0e10cSrcweir Statement xFoo_BazBarLit = new Statement(baz, bar, lit, foo); 225cdf0e10cSrcweir xFooGraph.addStatement(baz, bar, lit); 226cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 227cdf0e10cSrcweir assertTrue("addStatement(baz,bar,lit)", 228cdf0e10cSrcweir eq(xFooEnum, new Statement[] { 229cdf0e10cSrcweir xFoo_FooBarBaz, xFoo_FooBarBlank, xFoo_BazBarLit })); 230cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(baz, bar, null); 231cdf0e10cSrcweir assertTrue("addStatement(baz,bar,lit) (baz,bar)", 232cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_BazBarLit })); 233cdf0e10cSrcweir 234cdf0e10cSrcweir Statement xFoo_BazBarLitlang = 235cdf0e10cSrcweir new Statement(baz, bar, litlang, foo); 236cdf0e10cSrcweir xFooGraph.addStatement(baz, bar, litlang); 237cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 238cdf0e10cSrcweir assertTrue("addStatement(baz,bar,litlang)", 239cdf0e10cSrcweir eq(xFooEnum, new Statement[] { 240cdf0e10cSrcweir xFoo_FooBarBaz, xFoo_FooBarBlank, xFoo_BazBarLit, 241cdf0e10cSrcweir xFoo_BazBarLitlang })); 242cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, baz); 243cdf0e10cSrcweir assertTrue("addStatement(baz,bar,litlang) (baz)", 244cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_FooBarBaz })); 245cdf0e10cSrcweir 246cdf0e10cSrcweir Statement xFoo_BazBarLittype = 247cdf0e10cSrcweir new Statement(baz, bar, littype, foo); 248cdf0e10cSrcweir xFooGraph.addStatement(baz, bar, littype); 249cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 250cdf0e10cSrcweir assertTrue("addStatement(baz,bar,littype)", 251cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank, 252cdf0e10cSrcweir xFoo_BazBarLit, xFoo_BazBarLitlang, xFoo_BazBarLittype })); 253cdf0e10cSrcweir 254cdf0e10cSrcweir xFooGraph.removeStatements(baz, bar, litlang); 255cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 256cdf0e10cSrcweir assertTrue("removeStatement(baz,bar,litlang)", 257cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank, 258cdf0e10cSrcweir xFoo_BazBarLit, xFoo_BazBarLittype })); 259cdf0e10cSrcweir 260cdf0e10cSrcweir xFooGraph.removeStatements(foo, bar, null); 261cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 262cdf0e10cSrcweir assertTrue("removeStatement(foo,bar,null)", 263cdf0e10cSrcweir eq(xFooEnum, new Statement[] { 264cdf0e10cSrcweir xFoo_BazBarLit, xFoo_BazBarLittype })); 265cdf0e10cSrcweir 266cdf0e10cSrcweir xFooGraph.addStatement(foo, bar, baz); 267cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 268cdf0e10cSrcweir assertTrue("addStatement(foo,bar,baz) (re-add)", 269cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, 270cdf0e10cSrcweir xFoo_BazBarLit, xFoo_BazBarLittype })); 271cdf0e10cSrcweir 272cdf0e10cSrcweir xFooGraph.addStatement(foo, bar, baz); 273cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 274cdf0e10cSrcweir assertTrue("addStatement(foo,bar,baz) (duplicate)", 275cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, 276cdf0e10cSrcweir xFoo_BazBarLit, xFoo_BazBarLittype })); 277cdf0e10cSrcweir 278cdf0e10cSrcweir xFooGraph.addStatement(xFooGraph, bar, baz); 279cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 280cdf0e10cSrcweir assertTrue("addStatement(foo,bar,baz) (triplicate, as graph)", 281cdf0e10cSrcweir eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, 282cdf0e10cSrcweir xFoo_BazBarLit, xFoo_BazBarLittype })); 283cdf0e10cSrcweir 284cdf0e10cSrcweir System.out.println("...done"); 285cdf0e10cSrcweir 286cdf0e10cSrcweir System.out.println("Checking graph import/export..."); 287cdf0e10cSrcweir 288cdf0e10cSrcweir xFooOut = new StreamSimulator(tempDir + "foo.rdf", false, param); 289cdf0e10cSrcweir xRep.exportGraph(FileFormat.RDF_XML, xFooOut, foo, base); 290cdf0e10cSrcweir xFooOut.closeOutput(); 291cdf0e10cSrcweir 292cdf0e10cSrcweir xFooIn = new StreamSimulator(tempDir + "foo.rdf", true, param); 293cdf0e10cSrcweir try { 294cdf0e10cSrcweir xRep.importGraph(FileFormat.RDF_XML, xFooIn, bar, base); 295cdf0e10cSrcweir fail("importing existing graph did not fail"); 296cdf0e10cSrcweir } catch (ElementExistException e) { 297cdf0e10cSrcweir // ignore 298cdf0e10cSrcweir } 299cdf0e10cSrcweir 300cdf0e10cSrcweir xFooIn = new StreamSimulator(tempDir + "foo.rdf", true, param); 301cdf0e10cSrcweir xRep.importGraph(FileFormat.RDF_XML, xFooIn, baz, base); 302cdf0e10cSrcweir XNamedGraph xBazGraph = xRep.getGraph(baz); 303cdf0e10cSrcweir assertNotNull("no baz graph", xBazGraph); 304cdf0e10cSrcweir 305cdf0e10cSrcweir Statement xBaz_FooBarBaz = new Statement(foo, bar, baz, baz); 306cdf0e10cSrcweir Statement xBaz_BazBarLit = new Statement(baz, bar, lit, baz); 307cdf0e10cSrcweir Statement xBaz_BazBarLittype = 308cdf0e10cSrcweir new Statement(baz, bar, littype, baz); 309cdf0e10cSrcweir XEnumeration xBazEnum = xBazGraph.getStatements(null, null, null); 310cdf0e10cSrcweir assertTrue("importing exported graph", 311cdf0e10cSrcweir eq(xBazEnum, new Statement[] { 312cdf0e10cSrcweir xBaz_FooBarBaz, xBaz_BazBarLit, xBaz_BazBarLittype })); 313cdf0e10cSrcweir 314cdf0e10cSrcweir System.out.println("...done"); 315cdf0e10cSrcweir 316cdf0e10cSrcweir System.out.println("Checking graph deletion..."); 317cdf0e10cSrcweir 318cdf0e10cSrcweir xFooGraph.clear(); 319cdf0e10cSrcweir xFooEnum = xFooGraph.getStatements(null, null, null); 320cdf0e10cSrcweir assertTrue("clear", eq(xFooEnum, new Statement[] { })); 321cdf0e10cSrcweir 322cdf0e10cSrcweir xRep.destroyGraph(baz); 323cdf0e10cSrcweir assertNull("baz graph zombie", xRep.getGraph(baz)); 324cdf0e10cSrcweir 325cdf0e10cSrcweir try { 326cdf0e10cSrcweir xBazGraph.clear(); 327cdf0e10cSrcweir fail("deleted graph not invalid (clear)"); 328cdf0e10cSrcweir } catch (NoSuchElementException e) { 329cdf0e10cSrcweir // ignore 330cdf0e10cSrcweir } 331cdf0e10cSrcweir 332cdf0e10cSrcweir try { 333cdf0e10cSrcweir xBazGraph.addStatement(foo, foo, foo); 334cdf0e10cSrcweir fail("deleted graph not invalid (add)"); 335cdf0e10cSrcweir } catch (NoSuchElementException e) { 336cdf0e10cSrcweir // ignore 337cdf0e10cSrcweir } 338cdf0e10cSrcweir 339cdf0e10cSrcweir try { 340cdf0e10cSrcweir xBazGraph.removeStatements(null, null, null); 341cdf0e10cSrcweir fail("deleted graph not invalid (remove)"); 342cdf0e10cSrcweir } catch (NoSuchElementException e) { 343cdf0e10cSrcweir // ignore 344cdf0e10cSrcweir } 345cdf0e10cSrcweir 346cdf0e10cSrcweir try { 347cdf0e10cSrcweir xBazGraph.getStatements(null, null, null); 348cdf0e10cSrcweir fail("deleted graph not invalid (get)"); 349cdf0e10cSrcweir } catch (NoSuchElementException e) { 350cdf0e10cSrcweir // ignore 351cdf0e10cSrcweir } 352cdf0e10cSrcweir 353cdf0e10cSrcweir System.out.println("...done"); 354cdf0e10cSrcweir 355cdf0e10cSrcweir } catch (Exception e) { 356cdf0e10cSrcweir report(e); 357cdf0e10cSrcweir } 358cdf0e10cSrcweir } 359cdf0e10cSrcweir checkSPARQL()360cdf0e10cSrcweir @Test public void checkSPARQL() 361cdf0e10cSrcweir { 362cdf0e10cSrcweir try { 363cdf0e10cSrcweir 364cdf0e10cSrcweir System.out.println("Checking SPARQL queries..."); 365cdf0e10cSrcweir 366cdf0e10cSrcweir XInputStream xIn = new StreamSimulator(TestDocument.getUrl("example.rdf"), true, param); 367cdf0e10cSrcweir // util.utils.getFullTestDocName("example.rdf"), true, param); 368cdf0e10cSrcweir xRep.importGraph(FileFormat.RDF_XML, xIn, manifest, base); 369cdf0e10cSrcweir 370cdf0e10cSrcweir String query; 371cdf0e10cSrcweir query = "SELECT ?p WHERE { ?p rdf:type pkg:Package . }"; 372cdf0e10cSrcweir XQuerySelectResult result = xRep.querySelect(mkNss() + query); 373cdf0e10cSrcweir assertTrue("query: package-id\n" + query, 374cdf0e10cSrcweir eq(result, new String[] { "p" }, 375cdf0e10cSrcweir new XNode[][] { { uuid } })); 376cdf0e10cSrcweir 377cdf0e10cSrcweir query = "SELECT ?part ?path FROM <" + manifest + 378cdf0e10cSrcweir "> WHERE { ?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part ." 379cdf0e10cSrcweir + " ?part pkg:path ?path . ?part rdf:type odf:ContentFile. }"; 380cdf0e10cSrcweir result = xRep.querySelect(mkNss() + query); 381cdf0e10cSrcweir assertTrue("query: contentfile", 382cdf0e10cSrcweir eq(result, new String[] { "part", "path" }, 383cdf0e10cSrcweir new XNode[][] { { BlankNode.create(xContext, "whatever"), 384cdf0e10cSrcweir Literal.create(xContext, "content.xml") } })); 385cdf0e10cSrcweir 386cdf0e10cSrcweir query = "SELECT ?pkg ?path FROM <" + toS(manifest) + "> WHERE { " 387cdf0e10cSrcweir + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " 388cdf0e10cSrcweir + "?part pkg:path ?path . ?part rdf:type odf:ContentFile. }"; 389cdf0e10cSrcweir result = xRep.querySelect(mkNss() + query); 390cdf0e10cSrcweir assertTrue("query: contentfile\n" + query, 391cdf0e10cSrcweir eq(result, new String[] { "pkg", "path" }, 392cdf0e10cSrcweir new XNode[][] { { uuid , 393cdf0e10cSrcweir Literal.create(xContext, "content.xml") } })); 394cdf0e10cSrcweir 395cdf0e10cSrcweir query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { " 396cdf0e10cSrcweir + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " 397cdf0e10cSrcweir + "?part pkg:path ?path . ?part rdf:type odf:StylesFile. }"; 398cdf0e10cSrcweir result = xRep.querySelect(mkNss() + query); 399cdf0e10cSrcweir assertTrue("query: stylesfile\n" + query, 400cdf0e10cSrcweir eq(result, new String[] { "part", "path" }, 401cdf0e10cSrcweir new XNode[][] { })); 402cdf0e10cSrcweir 403cdf0e10cSrcweir query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { " 404cdf0e10cSrcweir + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " 405cdf0e10cSrcweir + "?part pkg:path ?path . ?part rdf:type odf:MetadataFile. }"; 406cdf0e10cSrcweir result = xRep.querySelect(mkNss() + query); 407cdf0e10cSrcweir assertTrue("query: metadatafile\n" + query, 408cdf0e10cSrcweir eq(result, new String[] { "part", "path" }, 409cdf0e10cSrcweir new XNode[][] { { 410cdf0e10cSrcweir URI.create(xContext, "http://hospital-employee/doctor"), 411cdf0e10cSrcweir Literal.create(xContext, 412cdf0e10cSrcweir "meta/hospital/doctor.rdf") } })); 413cdf0e10cSrcweir 414cdf0e10cSrcweir //FIXME redland BUG 415cdf0e10cSrcweir String uri = "uri:example-element-2"; 416cdf0e10cSrcweir query = "SELECT ?path ?idref FROM <" + toS(manifest) + "> WHERE { " 417cdf0e10cSrcweir // + "<" + toS(uuid) + "> rdf:type pkg:Package ; " 418cdf0e10cSrcweir // + " pkg:hasPart ?part . " 419cdf0e10cSrcweir + "<" + toS(uuid) + "> pkg:hasPart ?part . " 420cdf0e10cSrcweir + "?part pkg:path ?path ; " 421cdf0e10cSrcweir + " rdf:type ?type ; " 422cdf0e10cSrcweir + " pkg:hasPart <" + uri + "> . " 423cdf0e10cSrcweir // + "<" + uri + "> rdf:type odf:Element ; " 424cdf0e10cSrcweir + "<" + uri + "> " 425cdf0e10cSrcweir + " pkg:idref ?idref . " 426cdf0e10cSrcweir + " FILTER (?type = odf:ContentFile || ?type = odf:StylesFile)" 427cdf0e10cSrcweir + " }"; 428cdf0e10cSrcweir //System.out.println(query); 429cdf0e10cSrcweir result = xRep.querySelect(mkNss() + query); 430cdf0e10cSrcweir assertTrue("query: example-element-2\n" + query, 431cdf0e10cSrcweir eq(result, new String[] { "path", "idref" }, 432cdf0e10cSrcweir new XNode[][] { { 433cdf0e10cSrcweir Literal.create(xContext, "content.xml"), 434cdf0e10cSrcweir Literal.create(xContext, "ID_B") } })); 435cdf0e10cSrcweir 436cdf0e10cSrcweir // CONSTRUCT result triples have no graph! 437cdf0e10cSrcweir Statement x_PkgFooLit = new Statement(uuid, foo, lit, null); 438cdf0e10cSrcweir query = "CONSTRUCT { ?pkg <" + toS(foo) + "> \"" + 439cdf0e10cSrcweir lit.getStringValue() + "\" } FROM <" + toS(manifest) + 440cdf0e10cSrcweir "> WHERE { ?pkg rdf:type pkg:Package . } "; 441cdf0e10cSrcweir XEnumeration xResultEnum = xRep.queryConstruct(mkNss() + query); 442cdf0e10cSrcweir assertTrue("query: construct\n" + query, 443cdf0e10cSrcweir eq(xResultEnum, new Statement[] { x_PkgFooLit })); 444cdf0e10cSrcweir 445cdf0e10cSrcweir query = "ASK { ?pkg rdf:type pkg:Package . }"; 446cdf0e10cSrcweir boolean bResult = xRep.queryAsk(mkNss() + query); 447cdf0e10cSrcweir assertTrue("query: ask\n" + query, bResult); 448cdf0e10cSrcweir 449cdf0e10cSrcweir System.out.println("...done"); 450cdf0e10cSrcweir 451cdf0e10cSrcweir } catch (Exception e) { 452cdf0e10cSrcweir report(e); 453cdf0e10cSrcweir } 454cdf0e10cSrcweir } 455cdf0e10cSrcweir checkRDFa()456cdf0e10cSrcweir @Test public void checkRDFa() 457cdf0e10cSrcweir { 458cdf0e10cSrcweir try { 459cdf0e10cSrcweir System.out.println("Checking RDFa gunk..."); 460cdf0e10cSrcweir 461cdf0e10cSrcweir String content = "behold, for i am the content."; 462cdf0e10cSrcweir XTextRange xTR = new TestRange(content); 463cdf0e10cSrcweir XMetadatable xM = (XMetadatable) xTR; 464cdf0e10cSrcweir 465cdf0e10cSrcweir Pair<Statement[], Boolean> result = 466cdf0e10cSrcweir xRep.getStatementRDFa((XMetadatable)xTR); 467cdf0e10cSrcweir assertTrue("RDFa: get: not empty (initial)", 468cdf0e10cSrcweir 0 == result.First.length); 469cdf0e10cSrcweir 470cdf0e10cSrcweir try { 471cdf0e10cSrcweir xRep.setStatementRDFa(foo, new XURI[] {}, xM, "", null); 472cdf0e10cSrcweir fail("RDFa: set: no predicate"); 473cdf0e10cSrcweir } catch (IllegalArgumentException e) { 474cdf0e10cSrcweir // ignore 475cdf0e10cSrcweir } 476cdf0e10cSrcweir 477cdf0e10cSrcweir try { 478cdf0e10cSrcweir xRep.setStatementRDFa(foo, new XURI[] {bar}, null, "", null); 479cdf0e10cSrcweir fail("RDFa: set: null"); 480cdf0e10cSrcweir } catch (IllegalArgumentException e) { 481cdf0e10cSrcweir // ignore 482cdf0e10cSrcweir } 483cdf0e10cSrcweir 484cdf0e10cSrcweir XLiteral trlit = Literal.create(xContext, content); 485cdf0e10cSrcweir Statement x_FooBarTRLit = new Statement(foo, bar, trlit, null); 486cdf0e10cSrcweir xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "", null); 487cdf0e10cSrcweir 488cdf0e10cSrcweir result = xRep.getStatementRDFa((XMetadatable)xTR); 489cdf0e10cSrcweir assertTrue("RDFa: get: without content", 490cdf0e10cSrcweir !result.Second && (1 == result.First.length) 491cdf0e10cSrcweir && eq(result.First[0], x_FooBarTRLit)); 492cdf0e10cSrcweir 493cdf0e10cSrcweir //FIXME: do this? 494cdf0e10cSrcweir xTR.setString(lit.getStringValue()); 495cdf0e10cSrcweir /* 496cdf0e10cSrcweir Statement xFooBarLit = new Statement(foo, bar, lit, null); 497cdf0e10cSrcweir result = xRep.getStatementRDFa((XMetadatable)xTR); 498cdf0e10cSrcweir assertTrue("RDFa: get: change", 499cdf0e10cSrcweir eq((Statement)result.First, xFooBarLit) && null == result.Second); 500cdf0e10cSrcweir */ 501cdf0e10cSrcweir 502cdf0e10cSrcweir Statement x_FooBarLittype = new Statement(foo, bar, littype, null); 503cdf0e10cSrcweir xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "42", uint); 504cdf0e10cSrcweir 505cdf0e10cSrcweir result = xRep.getStatementRDFa((XMetadatable)xTR); 506cdf0e10cSrcweir assertTrue("RDFa: get: with content", 507cdf0e10cSrcweir result.Second && 508cdf0e10cSrcweir (1 == result.First.length) && 509cdf0e10cSrcweir eq(result.First[0], x_FooBarLittype)); 510cdf0e10cSrcweir 511cdf0e10cSrcweir //FIXME: do this? 512cdf0e10cSrcweir xTR.setString(content); 513cdf0e10cSrcweir /* 514cdf0e10cSrcweir Statement xFooLabelTRLit = new Statement(foo, rdfslabel, trlit, null); 515cdf0e10cSrcweir result = xRep.getStatementRDFa((XMetadatable)xTR); 516cdf0e10cSrcweir assertTrue("RDFa: get: change (label)", 517cdf0e10cSrcweir eq((Statement)result.First, xFooBarLittype) && 518cdf0e10cSrcweir eq((Statement)result.Second, xFooLabelTRLit)); 519cdf0e10cSrcweir */ 520cdf0e10cSrcweir 521cdf0e10cSrcweir xRep.removeStatementRDFa((XMetadatable)xTR); 522cdf0e10cSrcweir 523cdf0e10cSrcweir result = xRep.getStatementRDFa((XMetadatable)xTR); 524cdf0e10cSrcweir assertTrue("RDFa: get: not empty (removed)", 525cdf0e10cSrcweir 0 == result.First.length); 526cdf0e10cSrcweir 527cdf0e10cSrcweir xRep.setStatementRDFa(foo, new XURI[] { foo, bar, baz }, xM, 528cdf0e10cSrcweir "", null); 529cdf0e10cSrcweir 530cdf0e10cSrcweir Statement x_FooFooTRLit = new Statement(foo, foo, trlit, null); 531cdf0e10cSrcweir Statement x_FooBazTRLit = new Statement(foo, baz, trlit, null); 532cdf0e10cSrcweir result = xRep.getStatementRDFa((XMetadatable) xTR); 533cdf0e10cSrcweir assertTrue("RDFa: get: without content (multiple predicates, reinsert)", 534cdf0e10cSrcweir !result.Second && 535cdf0e10cSrcweir eq(result.First, new Statement[] { 536cdf0e10cSrcweir x_FooFooTRLit, x_FooBarTRLit, x_FooBazTRLit })); 537cdf0e10cSrcweir 538cdf0e10cSrcweir xRep.removeStatementRDFa((XMetadatable)xTR); 539cdf0e10cSrcweir 540cdf0e10cSrcweir result = xRep.getStatementRDFa((XMetadatable) xTR); 541cdf0e10cSrcweir assertTrue("RDFa: get: not empty (re-removed)", 542cdf0e10cSrcweir 0 == result.First.length); 543cdf0e10cSrcweir 544cdf0e10cSrcweir System.out.println("...done"); 545cdf0e10cSrcweir 546cdf0e10cSrcweir } catch (Exception e) { 547cdf0e10cSrcweir report(e); 548cdf0e10cSrcweir } 549cdf0e10cSrcweir } 550cdf0e10cSrcweir 551cdf0e10cSrcweir // utilities ------------------------------------------------------------- 552cdf0e10cSrcweir report2(Exception e)553cdf0e10cSrcweir public void report2(Exception e) 554cdf0e10cSrcweir { 555cdf0e10cSrcweir if (e instanceof WrappedTargetException) 556cdf0e10cSrcweir { 557cdf0e10cSrcweir System.out.println("Cause:"); 558cdf0e10cSrcweir Exception cause = (Exception) 559cdf0e10cSrcweir (((WrappedTargetException)e).TargetException); 560cdf0e10cSrcweir System.out.println(cause.toString()); 561cdf0e10cSrcweir report2(cause); 562cdf0e10cSrcweir } else if (e instanceof WrappedTargetRuntimeException) { 563cdf0e10cSrcweir System.out.println("Cause:"); 564cdf0e10cSrcweir Exception cause = (Exception) 565cdf0e10cSrcweir (((WrappedTargetRuntimeException)e).TargetException); 566cdf0e10cSrcweir System.out.println(cause.toString()); 567cdf0e10cSrcweir report2(cause); 568cdf0e10cSrcweir } 569cdf0e10cSrcweir } 570cdf0e10cSrcweir report(Exception e)571cdf0e10cSrcweir public void report(Exception e) { 572cdf0e10cSrcweir System.out.println("Exception occurred:"); 573cdf0e10cSrcweir e.printStackTrace(); 574cdf0e10cSrcweir report2(e); 575cdf0e10cSrcweir fail(); 576cdf0e10cSrcweir } 577cdf0e10cSrcweir toS(XNode n)578cdf0e10cSrcweir public static String toS(XNode n) { 579cdf0e10cSrcweir if (null == n) 580cdf0e10cSrcweir { 581cdf0e10cSrcweir return "< null >"; 582cdf0e10cSrcweir } 583cdf0e10cSrcweir return n.getStringValue(); 584cdf0e10cSrcweir } 585cdf0e10cSrcweir isBlank(XNode i_node)586cdf0e10cSrcweir static boolean isBlank(XNode i_node) 587cdf0e10cSrcweir { 588cdf0e10cSrcweir XBlankNode blank = UnoRuntime.queryInterface(XBlankNode.class, i_node); 589cdf0e10cSrcweir return blank != null; 590cdf0e10cSrcweir } 591cdf0e10cSrcweir 592cdf0e10cSrcweir /* 593cdf0e10cSrcweir static class Statement implements XStatement 594cdf0e10cSrcweir { 595cdf0e10cSrcweir XResource m_Subject; 596cdf0e10cSrcweir XResource m_Predicate; 597cdf0e10cSrcweir XNode m_Object; 598cdf0e10cSrcweir XURI m_Graph; 599cdf0e10cSrcweir 600cdf0e10cSrcweir Statement(XResource i_Subject, XResource i_Predicate, XNode i_Object, 601cdf0e10cSrcweir XURI i_Graph) 602cdf0e10cSrcweir { 603cdf0e10cSrcweir m_Subject = i_Subject; 604cdf0e10cSrcweir m_Predicate = i_Predicate; 605cdf0e10cSrcweir m_Object = i_Object; 606cdf0e10cSrcweir m_Graph = i_Graph; 607cdf0e10cSrcweir } 608cdf0e10cSrcweir 609cdf0e10cSrcweir public XResource getSubject() { return m_Subject; } 610cdf0e10cSrcweir public XResource getPredicate() { return m_Predicate; } 611cdf0e10cSrcweir public XNode getObject() { return m_Object; } 612cdf0e10cSrcweir public XURI getGraph() { return m_Graph; } 613cdf0e10cSrcweir } 614cdf0e10cSrcweir */ 615cdf0e10cSrcweir toSeq(XEnumeration i_Enum)616cdf0e10cSrcweir static Statement[] toSeq(XEnumeration i_Enum) throws Exception 617cdf0e10cSrcweir { 618cdf0e10cSrcweir java.util.Collection c = new java.util.Vector(); 619cdf0e10cSrcweir while (i_Enum.hasMoreElements()) { 620cdf0e10cSrcweir Statement s = (Statement) i_Enum.nextElement(); 621cdf0e10cSrcweir //System.out.println("toSeq: " + s.getSubject().getStringValue() + " " + s.getPredicate().getStringValue() + " " + s.getObject().getStringValue() + "."); 622cdf0e10cSrcweir c.add(s); 623cdf0e10cSrcweir } 624cdf0e10cSrcweir // return (Statement[]) c.toArray(); 625cdf0e10cSrcweir // java sucks 626cdf0e10cSrcweir Object[] arr = c.toArray(); 627cdf0e10cSrcweir Statement[] ret = new Statement[arr.length]; 628cdf0e10cSrcweir for (int i = 0; i < arr.length; ++i) { 629cdf0e10cSrcweir ret[i] = (Statement) arr[i]; 630cdf0e10cSrcweir } 631cdf0e10cSrcweir return ret; 632cdf0e10cSrcweir } 633cdf0e10cSrcweir toSeqs(XEnumeration i_Enum)634cdf0e10cSrcweir static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception 635cdf0e10cSrcweir { 636cdf0e10cSrcweir java.util.Collection c = new java.util.Vector(); 637cdf0e10cSrcweir while (i_Enum.hasMoreElements()) { 638cdf0e10cSrcweir XNode[] s = (XNode[]) i_Enum.nextElement(); 639cdf0e10cSrcweir c.add(s); 640cdf0e10cSrcweir } 641cdf0e10cSrcweir // return (XNode[][]) c.toArray(); 642cdf0e10cSrcweir Object[] arr = c.toArray(); 643cdf0e10cSrcweir XNode[][] ret = new XNode[arr.length][]; 644cdf0e10cSrcweir for (int i = 0; i < arr.length; ++i) { 645cdf0e10cSrcweir ret[i] = (XNode[]) arr[i]; 646cdf0e10cSrcweir } 647cdf0e10cSrcweir return ret; 648cdf0e10cSrcweir } 649cdf0e10cSrcweir 650cdf0e10cSrcweir static class BindingComp implements java.util.Comparator 651cdf0e10cSrcweir { compare(Object i_Left, Object i_Right)652cdf0e10cSrcweir public int compare(Object i_Left, Object i_Right) 653cdf0e10cSrcweir { 654cdf0e10cSrcweir XNode[] left = (XNode[]) i_Left; 655cdf0e10cSrcweir XNode[] right = (XNode[]) i_Right; 656cdf0e10cSrcweir if (left.length != right.length) 657cdf0e10cSrcweir { 658cdf0e10cSrcweir throw new RuntimeException(); 659cdf0e10cSrcweir } 660cdf0e10cSrcweir for (int i = 0; i < left.length; ++i) { 661cdf0e10cSrcweir int eq = (left[i].getStringValue().compareTo( 662cdf0e10cSrcweir right[i].getStringValue())); 663cdf0e10cSrcweir if (eq != 0) return eq; 664cdf0e10cSrcweir } 665cdf0e10cSrcweir return 0; 666cdf0e10cSrcweir } 667cdf0e10cSrcweir } 668cdf0e10cSrcweir 669cdf0e10cSrcweir static class StmtComp implements java.util.Comparator 670cdf0e10cSrcweir { compare(Object i_Left, Object i_Right)671cdf0e10cSrcweir public int compare(Object i_Left, Object i_Right) 672cdf0e10cSrcweir { 673cdf0e10cSrcweir int eq; 674cdf0e10cSrcweir Statement left = (Statement) i_Left; 675cdf0e10cSrcweir Statement right = (Statement) i_Right; 676cdf0e10cSrcweir if ((eq = cmp(left.Graph, right.Graph )) != 0) 677cdf0e10cSrcweir { 678cdf0e10cSrcweir return eq; 679cdf0e10cSrcweir } 680cdf0e10cSrcweir if ((eq = cmp(left.Subject, right.Subject )) != 0) 681cdf0e10cSrcweir { 682cdf0e10cSrcweir return eq; 683cdf0e10cSrcweir } 684cdf0e10cSrcweir if ((eq = cmp(left.Predicate, right.Predicate)) != 0) 685cdf0e10cSrcweir { 686cdf0e10cSrcweir return eq; 687cdf0e10cSrcweir } 688cdf0e10cSrcweir if ((eq = cmp(left.Object, right.Object )) != 0) 689cdf0e10cSrcweir { 690cdf0e10cSrcweir return eq; 691cdf0e10cSrcweir } 692cdf0e10cSrcweir return 0; 693cdf0e10cSrcweir } 694cdf0e10cSrcweir cmp(XNode i_Left, XNode i_Right)695cdf0e10cSrcweir public int cmp(XNode i_Left, XNode i_Right) 696cdf0e10cSrcweir { 697cdf0e10cSrcweir if (isBlank(i_Left)) { 698cdf0e10cSrcweir return isBlank(i_Right) ? 0 : 1; 699cdf0e10cSrcweir } else { 700cdf0e10cSrcweir if (isBlank(i_Right)) { 701cdf0e10cSrcweir return -1; 702cdf0e10cSrcweir } else { 703cdf0e10cSrcweir return toS(i_Left).compareTo(toS(i_Right)); 704cdf0e10cSrcweir } 705cdf0e10cSrcweir } 706cdf0e10cSrcweir } 707cdf0e10cSrcweir } 708cdf0e10cSrcweir eq(Statement i_Left, Statement i_Right)709cdf0e10cSrcweir static boolean eq(Statement i_Left, Statement i_Right) 710cdf0e10cSrcweir { 711cdf0e10cSrcweir XURI lG = i_Left.Graph; 712cdf0e10cSrcweir XURI rG = i_Right.Graph; 713cdf0e10cSrcweir if (!eq(lG, rG)) { 714cdf0e10cSrcweir System.out.println("Graphs differ: " + toS(lG) + " != " + toS(rG)); 715cdf0e10cSrcweir return false; 716cdf0e10cSrcweir } 717cdf0e10cSrcweir if (!eq(i_Left.Subject, i_Right.Subject)) { 718cdf0e10cSrcweir System.out.println("Subjects differ: " + 719cdf0e10cSrcweir i_Left.Subject.getStringValue() + " != " + 720cdf0e10cSrcweir i_Right.Subject.getStringValue()); 721cdf0e10cSrcweir return false; 722cdf0e10cSrcweir } 723cdf0e10cSrcweir if (!eq(i_Left.Predicate, i_Right.Predicate)) { 724cdf0e10cSrcweir System.out.println("Predicates differ: " + 725cdf0e10cSrcweir i_Left.Predicate.getStringValue() + " != " + 726cdf0e10cSrcweir i_Right.Predicate.getStringValue()); 727cdf0e10cSrcweir return false; 728cdf0e10cSrcweir } 729cdf0e10cSrcweir if (!eq(i_Left.Object, i_Right.Object)) { 730cdf0e10cSrcweir System.out.println("Objects differ: " + 731cdf0e10cSrcweir i_Left.Object.getStringValue() + " != " + 732cdf0e10cSrcweir i_Right.Object.getStringValue()); 733cdf0e10cSrcweir return false; 734cdf0e10cSrcweir } 735cdf0e10cSrcweir return true; 736cdf0e10cSrcweir } 737cdf0e10cSrcweir eq(Statement[] i_Result, Statement[] i_Expected)738cdf0e10cSrcweir static boolean eq(Statement[] i_Result, Statement[] i_Expected) 739cdf0e10cSrcweir { 740cdf0e10cSrcweir if (i_Result.length != i_Expected.length) { 741cdf0e10cSrcweir System.out.println("eq: different lengths: " + i_Result.length + " " + 742cdf0e10cSrcweir i_Expected.length); 743cdf0e10cSrcweir return false; 744cdf0e10cSrcweir } 745cdf0e10cSrcweir Statement[] expected = (Statement[]) 746cdf0e10cSrcweir java.util.Arrays.asList(i_Expected).toArray(); 747cdf0e10cSrcweir java.util.Arrays.sort(i_Result, new StmtComp()); 748cdf0e10cSrcweir java.util.Arrays.sort(expected, new StmtComp()); 749cdf0e10cSrcweir for (int i = 0; i < expected.length; ++i) { 750cdf0e10cSrcweir if (!eq(i_Result[i], expected[i])) 751cdf0e10cSrcweir { 752cdf0e10cSrcweir return false; 753cdf0e10cSrcweir } 754cdf0e10cSrcweir } 755cdf0e10cSrcweir return true; 756cdf0e10cSrcweir } 757cdf0e10cSrcweir eq(XEnumeration i_Enum, Statement[] i_Expected)758cdf0e10cSrcweir static boolean eq(XEnumeration i_Enum, Statement[] i_Expected) 759cdf0e10cSrcweir throws Exception 760cdf0e10cSrcweir { 761cdf0e10cSrcweir Statement[] current = toSeq(i_Enum); 762cdf0e10cSrcweir return eq(current, i_Expected); 763cdf0e10cSrcweir } 764cdf0e10cSrcweir eq(XNode i_Left, XNode i_Right)765cdf0e10cSrcweir static boolean eq(XNode i_Left, XNode i_Right) 766cdf0e10cSrcweir { 767cdf0e10cSrcweir if (i_Left == null) { 768cdf0e10cSrcweir return (i_Right == null); 769cdf0e10cSrcweir } else { 770cdf0e10cSrcweir return (i_Right != null) && 771cdf0e10cSrcweir (i_Left.getStringValue().equals(i_Right.getStringValue()) 772cdf0e10cSrcweir // FIXME: hack: blank nodes considered equal 773cdf0e10cSrcweir || (isBlank(i_Left) && isBlank(i_Right))); 774cdf0e10cSrcweir } 775cdf0e10cSrcweir } 776cdf0e10cSrcweir eq(XQuerySelectResult i_Result, String[] i_Vars, XNode[][] i_Bindings)777cdf0e10cSrcweir static boolean eq(XQuerySelectResult i_Result, 778cdf0e10cSrcweir String[] i_Vars, XNode[][] i_Bindings) throws Exception 779cdf0e10cSrcweir { 780cdf0e10cSrcweir String[] vars = (String[]) i_Result.getBindingNames(); 781cdf0e10cSrcweir XEnumeration iter = (XEnumeration) i_Result; 782cdf0e10cSrcweir XNode[][] bindings = toSeqs(iter); 783cdf0e10cSrcweir if (vars.length != i_Vars.length) { 784cdf0e10cSrcweir System.out.println("var lengths differ"); 785cdf0e10cSrcweir return false; 786cdf0e10cSrcweir } 787cdf0e10cSrcweir if (bindings.length != i_Bindings.length) { 788cdf0e10cSrcweir System.out.println("binding lengths differ: " + i_Bindings.length + 789cdf0e10cSrcweir " vs " + bindings.length ); 790cdf0e10cSrcweir return false; 791cdf0e10cSrcweir } 792cdf0e10cSrcweir java.util.Arrays.sort(bindings, new BindingComp()); 793cdf0e10cSrcweir java.util.Arrays.sort(i_Bindings, new BindingComp()); 794cdf0e10cSrcweir for (int i = 0; i < i_Bindings.length; ++i) { 795cdf0e10cSrcweir if (i_Bindings[i].length != i_Vars.length) { 796cdf0e10cSrcweir System.out.println("TEST ERROR!"); 797cdf0e10cSrcweir throw new Exception(); 798cdf0e10cSrcweir } 799cdf0e10cSrcweir if (bindings[i].length != i_Vars.length) { 800cdf0e10cSrcweir System.out.println("binding length and var length differ"); 801cdf0e10cSrcweir return false; 802cdf0e10cSrcweir } 803cdf0e10cSrcweir for (int j = 0; j < i_Vars.length; ++j) { 804cdf0e10cSrcweir if (!eq(bindings[i][j], i_Bindings[i][j])) { 805cdf0e10cSrcweir System.out.println("bindings differ: " + 806cdf0e10cSrcweir toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j])); 807cdf0e10cSrcweir return false; 808cdf0e10cSrcweir } 809cdf0e10cSrcweir } 810cdf0e10cSrcweir } 811cdf0e10cSrcweir for (int i = 0; i < i_Vars.length; ++i) { 812cdf0e10cSrcweir if (!vars[i].equals(i_Vars[i])) { 813cdf0e10cSrcweir System.out.println("variable names differ: " + 814cdf0e10cSrcweir vars[i] + " != " + i_Vars[i]); 815cdf0e10cSrcweir return false; 816cdf0e10cSrcweir } 817cdf0e10cSrcweir } 818cdf0e10cSrcweir return true; 819cdf0e10cSrcweir } 820cdf0e10cSrcweir mkNamespace(String i_prefix, String i_namespace)821cdf0e10cSrcweir static String mkNamespace(String i_prefix, String i_namespace) 822cdf0e10cSrcweir { 823cdf0e10cSrcweir return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n"; 824cdf0e10cSrcweir } 825cdf0e10cSrcweir mkNss()826cdf0e10cSrcweir static String mkNss() 827cdf0e10cSrcweir { 828cdf0e10cSrcweir String namespaces = mkNamespace("rdf", 829cdf0e10cSrcweir "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); 830cdf0e10cSrcweir namespaces += mkNamespace("pkg", 831cdf0e10cSrcweir "http://docs.oasis-open.org/opendocument/meta/package/common#"); 832cdf0e10cSrcweir namespaces += mkNamespace("odf", 833cdf0e10cSrcweir "http://docs.oasis-open.org/opendocument/meta/package/odf#"); 834cdf0e10cSrcweir return namespaces; 835cdf0e10cSrcweir } 836cdf0e10cSrcweir 837cdf0e10cSrcweir class TestRange implements XTextRange, XMetadatable, XServiceInfo 838cdf0e10cSrcweir { 839cdf0e10cSrcweir String m_Stream; 840cdf0e10cSrcweir String m_XmlId; 841cdf0e10cSrcweir String m_Text; TestRange(String i_Str)842cdf0e10cSrcweir TestRange(String i_Str) { m_Text = i_Str; } 843cdf0e10cSrcweir getStringValue()844cdf0e10cSrcweir public String getStringValue() { return ""; } getNamespace()845cdf0e10cSrcweir public String getNamespace() { return ""; } getLocalName()846cdf0e10cSrcweir public String getLocalName() { return ""; } 847cdf0e10cSrcweir getMetadataReference()848cdf0e10cSrcweir public StringPair getMetadataReference() 849cdf0e10cSrcweir { return new StringPair(m_Stream, m_XmlId); } setMetadataReference(StringPair i_Ref)850cdf0e10cSrcweir public void setMetadataReference(StringPair i_Ref) 851cdf0e10cSrcweir throws IllegalArgumentException 852cdf0e10cSrcweir { m_Stream = i_Ref.First; m_XmlId = i_Ref.Second; } ensureMetadataReference()853cdf0e10cSrcweir public void ensureMetadataReference() 854cdf0e10cSrcweir { m_Stream = "content.xml"; m_XmlId = "42"; } 855cdf0e10cSrcweir getImplementationName()856cdf0e10cSrcweir public String getImplementationName() { return null; } getSupportedServiceNames()857cdf0e10cSrcweir public String[] getSupportedServiceNames() { return null; } supportsService(String i_Svc)858cdf0e10cSrcweir public boolean supportsService(String i_Svc) 859cdf0e10cSrcweir { return i_Svc.equals("com.sun.star.text.Paragraph"); } 860cdf0e10cSrcweir getText()861cdf0e10cSrcweir public XText getText() { return null; } getStart()862cdf0e10cSrcweir public XTextRange getStart() { return null; } getEnd()863cdf0e10cSrcweir public XTextRange getEnd() { return null; } getString()864cdf0e10cSrcweir public String getString() { return m_Text; } setString(String i_Str)865cdf0e10cSrcweir public void setString(String i_Str) { m_Text = i_Str; } 866cdf0e10cSrcweir } 867cdf0e10cSrcweir getMSF()868cdf0e10cSrcweir private XMultiServiceFactory getMSF() 869cdf0e10cSrcweir { 870cdf0e10cSrcweir final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 871cdf0e10cSrcweir return xMSF1; 872cdf0e10cSrcweir } 873cdf0e10cSrcweir 874cdf0e10cSrcweir // setup and close connections setUpConnection()875cdf0e10cSrcweir @BeforeClass public static void setUpConnection() throws Exception { 876cdf0e10cSrcweir System.out.println("setUpConnection()"); 877cdf0e10cSrcweir connection.setUp(); 878cdf0e10cSrcweir } 879cdf0e10cSrcweir tearDownConnection()880cdf0e10cSrcweir @AfterClass public static void tearDownConnection() 881cdf0e10cSrcweir throws InterruptedException, com.sun.star.uno.Exception 882cdf0e10cSrcweir { 883cdf0e10cSrcweir System.out.println("tearDownConnection()"); 884cdf0e10cSrcweir connection.tearDown(); 885cdf0e10cSrcweir } 886cdf0e10cSrcweir 887cdf0e10cSrcweir private static final OfficeConnection connection = new OfficeConnection(); 888cdf0e10cSrcweir } 889cdf0e10cSrcweir 890