Update ooo320-m1
[ooovba.git] / unoxml / qa / complex / RDFRepositoryTest.java
blob7719a766d650f7b1299efc7a45662916fd46078c
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: RDFRepositoryTest.java,v $
10 * $Revision: 1.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
30 package complex.unoxml;
32 import complexlib.ComplexTestCase;
33 import helper.StreamSimulator;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XComponentContext;
37 import com.sun.star.uno.Any;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.lang.XInitialization;
40 import com.sun.star.lang.XEventListener;
41 import com.sun.star.lang.XServiceInfo;
42 import com.sun.star.lang.IllegalArgumentException;
43 import com.sun.star.lang.WrappedTargetException;
44 import com.sun.star.lang.WrappedTargetRuntimeException;
45 import com.sun.star.beans.XPropertySet;
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.beans.StringPair;
48 import com.sun.star.container.XEnumeration;
49 import com.sun.star.container.ElementExistException;
50 import com.sun.star.container.NoSuchElementException;
51 import com.sun.star.io.XInputStream;
52 import com.sun.star.io.XOutputStream;
53 import com.sun.star.text.XTextRange;
54 import com.sun.star.text.XText;
55 import com.sun.star.rdf.*;
57 /**
58 * Test case for service com.sun.star.rdf.Repository
59 * Currently, this service is implemented in
60 * unoxml/source/rdf/librdf_repository.cxx
62 * @author mst
64 public class RDFRepositoryTest extends ComplexTestCase
66 XComponentContext xContext;
67 String tempDir;
69 XDocumentRepository xRep;
70 XURI foo;
71 XURI bar;
72 XURI baz;
73 XURI uint;
74 XURI rdfslabel;
75 XURI manifest;
76 XURI uuid;
77 XURI base;
78 XBlankNode blank;
79 XLiteral lit;
80 XLiteral litlang;
81 XLiteral littype;
82 String rdfs = "http://www.w3.org/2000/01/rdf-schema#";
84 public String[] getTestMethodNames ()
86 return new String[] { "check", "checkSPARQL", "checkRDFa" };
89 public void before()
91 try {
92 XMultiServiceFactory xMSF = (XMultiServiceFactory) param.getMSF();
93 assure("could not create MultiServiceFactory.", xMSF != null);
94 XPropertySet xPropertySet = (XPropertySet)
95 UnoRuntime.queryInterface(XPropertySet.class, xMSF);
96 Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext");
97 xContext = (XComponentContext)
98 UnoRuntime.queryInterface(XComponentContext.class, defaultCtx);
99 assure("could not get component context.", xContext != null);
101 tempDir = util.utils.getOfficeTemp/*Dir*/(xMSF);
102 log.println("tempdir: " + tempDir);
104 foo = URI.create(xContext, "uri:foo");
105 assure("foo", null != foo);
106 bar = URI.create(xContext, "uri:bar");
107 assure("bar", null != bar);
108 baz = URI.create(xContext, "uri:baz");
109 assure("baz", null != baz);
110 uint = URI.create(xContext, "uri:int");
111 assure("uint", null != uint);
112 blank = BlankNode.create(xContext, "_:uno");
113 assure("blank", null != blank);
114 lit = Literal.create(xContext, "i am the literal");
115 assure("lit", null != lit);
116 litlang = Literal.createWithLanguage(xContext,
117 "i am the literal", "en");
118 assure("litlang", null != litlang);
119 littype = Literal.createWithType(xContext, "42", uint);
120 assure("littype", null != littype);
122 rdfslabel = URI.create(xContext, rdfs + "label");
123 assure("rdfslabel", null != rdfslabel);
124 manifest = URI.create(xContext, "manifest:manifest"); //FIXME
125 assure("manifest", null != manifest);
126 uuid = URI.create(xContext,
127 "urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3");
128 assure("uuid", null != uuid);
129 base = URI.create(xContext, "base-uri:"); //FIXME
130 assure("base", null != base);
131 } catch (Exception e) {
132 report(e);
136 public void after()
138 xRep = null;
141 public void check()
143 try {
145 log.println("Creating service Repository...");
147 //FIXME: ?
148 // xRep = Repository.create(xContext);
149 xRep = (XDocumentRepository) UnoRuntime.queryInterface(
150 XDocumentRepository.class, Repository.create(xContext));
151 assure("null", null != xRep);
153 log.println("...done");
155 log.println("Checking that new repository is really empty...");
156 assure("empty: graphs", 0 == xRep.getGraphNames().length);
158 XEnumeration stmts;
159 stmts = xRep.getStatements(null, null, null);
160 assure("empty: stmts", !stmts.hasMoreElements());
162 log.println("...done");
164 log.println("Checking graph creation...");
166 XNamedGraph xFooGraph = xRep.createGraph(foo);
167 assure("foo graph", null != xFooGraph);
169 try {
170 xRep.createGraph(foo);
171 assure("creating duplicate graph was allowed", false);
172 } catch (ElementExistException e) {
173 // ignore
176 try {
177 xRep.createGraph(null);
178 assure("invalid graph name was allowed", false);
179 } catch (IllegalArgumentException e) {
180 // ignore
183 XURI[] names = xRep.getGraphNames();
184 assure("no foo graph in getGraphNames",
185 1 == names.length && eq(names[0], foo));
186 assure("no foo graph", null != xRep.getGraph(foo));
188 stmts = xFooGraph.getStatements(null, null, null);
189 assure("stmts in foo graph", !stmts.hasMoreElements());
191 XOutputStream xFooOut =
192 new StreamSimulator(tempDir + "empty.rdf", false, param);
193 xRep.exportGraph(FileFormat.RDF_XML, xFooOut, foo, base);
194 xFooOut.closeOutput();
196 XInputStream xFooIn =
197 new StreamSimulator(tempDir + "empty.rdf", true, param);
198 xRep.importGraph(FileFormat.RDF_XML, xFooIn, bar, base);
199 assure("no bar graph", null != xRep.getGraph(bar));
201 log.println("...done");
203 log.println("Checking graph manipulation...");
205 XEnumeration xFooEnum;
207 Statement xFoo_FooBarBaz = new Statement(foo, bar, baz, foo);
208 xFooGraph.addStatement(foo, bar, baz);
209 xFooEnum = xFooGraph.getStatements(null, null, null);
210 assure("addStatement(foo,bar,baz)",
211 eq(xFooEnum, new Statement[] { xFoo_FooBarBaz }));
213 Statement xFoo_FooBarBlank = new Statement(foo, bar, blank, foo);
214 xFooGraph.addStatement(foo, bar, blank);
215 xFooEnum = xFooGraph.getStatements(null, null, null);
216 assure("addStatement(foo,bar,blank)",
217 eq(xFooEnum,
218 new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank }));
219 xFooEnum = xRep.getStatements(null, null, null);
220 assure("addStatement(foo,bar,blank) (global)",
221 eq(xFooEnum,
222 new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank }));
224 Statement xFoo_BazBarLit = new Statement(baz, bar, lit, foo);
225 xFooGraph.addStatement(baz, bar, lit);
226 xFooEnum = xFooGraph.getStatements(null, null, null);
227 assure("addStatement(baz,bar,lit)",
228 eq(xFooEnum, new Statement[] {
229 xFoo_FooBarBaz, xFoo_FooBarBlank, xFoo_BazBarLit }));
230 xFooEnum = xFooGraph.getStatements(baz, bar, null);
231 assure("addStatement(baz,bar,lit) (baz,bar)",
232 eq(xFooEnum, new Statement[] { xFoo_BazBarLit }));
234 Statement xFoo_BazBarLitlang =
235 new Statement(baz, bar, litlang, foo);
236 xFooGraph.addStatement(baz, bar, litlang);
237 xFooEnum = xFooGraph.getStatements(null, null, null);
238 assure("addStatement(baz,bar,litlang)",
239 eq(xFooEnum, new Statement[] {
240 xFoo_FooBarBaz, xFoo_FooBarBlank, xFoo_BazBarLit,
241 xFoo_BazBarLitlang }));
242 xFooEnum = xFooGraph.getStatements(null, null, baz);
243 assure("addStatement(baz,bar,litlang) (baz)",
244 eq(xFooEnum, new Statement[] { xFoo_FooBarBaz }));
246 Statement xFoo_BazBarLittype =
247 new Statement(baz, bar, littype, foo);
248 xFooGraph.addStatement(baz, bar, littype);
249 xFooEnum = xFooGraph.getStatements(null, null, null);
250 assure("addStatement(baz,bar,littype)",
251 eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank,
252 xFoo_BazBarLit, xFoo_BazBarLitlang, xFoo_BazBarLittype }));
254 xFooGraph.removeStatements(baz, bar, litlang);
255 xFooEnum = xFooGraph.getStatements(null, null, null);
256 assure("removeStatement(baz,bar,litlang)",
257 eq(xFooEnum, new Statement[] { xFoo_FooBarBaz, xFoo_FooBarBlank,
258 xFoo_BazBarLit, xFoo_BazBarLittype }));
260 xFooGraph.removeStatements(foo, bar, null);
261 xFooEnum = xFooGraph.getStatements(null, null, null);
262 assure("removeStatement(foo,bar,null)",
263 eq(xFooEnum, new Statement[] {
264 xFoo_BazBarLit, xFoo_BazBarLittype }));
266 xFooGraph.addStatement(foo, bar, baz);
267 xFooEnum = xFooGraph.getStatements(null, null, null);
268 assure("addStatement(foo,bar,baz) (re-add)",
269 eq(xFooEnum, new Statement[] { xFoo_FooBarBaz,
270 xFoo_BazBarLit, xFoo_BazBarLittype }));
272 xFooGraph.addStatement(foo, bar, baz);
273 xFooEnum = xFooGraph.getStatements(null, null, null);
274 assure("addStatement(foo,bar,baz) (duplicate)",
275 eq(xFooEnum, new Statement[] { xFoo_FooBarBaz,
276 xFoo_BazBarLit, xFoo_BazBarLittype }));
278 xFooGraph.addStatement(xFooGraph, bar, baz);
279 xFooEnum = xFooGraph.getStatements(null, null, null);
280 assure("addStatement(foo,bar,baz) (triplicate, as graph)",
281 eq(xFooEnum, new Statement[] { xFoo_FooBarBaz,
282 xFoo_BazBarLit, xFoo_BazBarLittype }));
284 log.println("...done");
286 log.println("Checking graph import/export...");
288 xFooOut = new StreamSimulator(tempDir + "foo.rdf", false, param);
289 xRep.exportGraph(FileFormat.RDF_XML, xFooOut, foo, base);
290 xFooOut.closeOutput();
292 xFooIn = new StreamSimulator(tempDir + "foo.rdf", true, param);
293 try {
294 xRep.importGraph(FileFormat.RDF_XML, xFooIn, bar, base);
295 assure("importing existing graph did not fail", false);
296 } catch (ElementExistException e) {
297 // ignore
300 xFooIn = new StreamSimulator(tempDir + "foo.rdf", true, param);
301 xRep.importGraph(FileFormat.RDF_XML, xFooIn, baz, base);
302 XNamedGraph xBazGraph = xRep.getGraph(baz);
303 assure("no baz graph", null != xBazGraph);
305 Statement xBaz_FooBarBaz = new Statement(foo, bar, baz, baz);
306 Statement xBaz_BazBarLit = new Statement(baz, bar, lit, baz);
307 Statement xBaz_BazBarLittype =
308 new Statement(baz, bar, littype, baz);
309 XEnumeration xBazEnum = xBazGraph.getStatements(null, null, null);
310 assure("importing exported graph",
311 eq(xBazEnum, new Statement[] {
312 xBaz_FooBarBaz, xBaz_BazBarLit, xBaz_BazBarLittype }));
314 log.println("...done");
316 log.println("Checking graph deletion...");
318 xFooGraph.clear();
319 xFooEnum = xFooGraph.getStatements(null, null, null);
320 assure("clear", eq(xFooEnum, new Statement[] { }));
322 xRep.destroyGraph(baz);
323 assure("baz graph zombie", null == xRep.getGraph(baz));
325 try {
326 xBazGraph.clear();
327 assure("deleted graph not invalid (clear)", false);
328 } catch (NoSuchElementException e) {
329 // ignore
332 try {
333 xBazGraph.addStatement(foo, foo, foo);
334 assure("deleted graph not invalid (add)", false);
335 } catch (NoSuchElementException e) {
336 // ignore
339 try {
340 xBazGraph.removeStatements(null, null, null);
341 assure("deleted graph not invalid (remove)", false);
342 } catch (NoSuchElementException e) {
343 // ignore
346 try {
347 xBazGraph.getStatements(null, null, null);
348 assure("deleted graph not invalid (get)", false);
349 } catch (NoSuchElementException e) {
350 // ignore
353 log.println("...done");
355 } catch (Exception e) {
356 report(e);
360 public void checkSPARQL()
362 try {
364 log.println("Checking SPARQL queries...");
366 XInputStream xIn = new StreamSimulator(
367 util.utils.getFullTestDocName("example.rdf"), true, param);
368 xRep.importGraph(FileFormat.RDF_XML, xIn, manifest, base);
370 String query;
371 query = "SELECT ?p WHERE { ?p rdf:type pkg:Package . }";
372 XQuerySelectResult result = xRep.querySelect(mkNss() + query);
373 assure("query: package-id\n" + query,
374 eq(result, new String[] { "p" },
375 new XNode[][] { { uuid } }));
377 query = "SELECT ?part ?path FROM <" + manifest +
378 "> WHERE { ?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part ."
379 + " ?part pkg:path ?path . ?part rdf:type odf:ContentFile. }";
380 result = xRep.querySelect(mkNss() + query);
381 assure("query: contentfile",
382 eq(result, new String[] { "part", "path" },
383 new XNode[][] { { BlankNode.create(xContext, "whatever"),
384 Literal.create(xContext, "content.xml") } }));
386 query = "SELECT ?pkg ?path FROM <" + toS(manifest) + "> WHERE { "
387 + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . "
388 + "?part pkg:path ?path . ?part rdf:type odf:ContentFile. }";
389 result = xRep.querySelect(mkNss() + query);
390 assure("query: contentfile\n" + query,
391 eq(result, new String[] { "pkg", "path" },
392 new XNode[][] { { uuid ,
393 Literal.create(xContext, "content.xml") } }));
395 query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { "
396 + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . "
397 + "?part pkg:path ?path . ?part rdf:type odf:StylesFile. }";
398 result = xRep.querySelect(mkNss() + query);
399 assure("query: stylesfile\n" + query,
400 eq(result, new String[] { "part", "path" },
401 new XNode[][] { }));
403 query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { "
404 + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . "
405 + "?part pkg:path ?path . ?part rdf:type odf:MetadataFile. }";
406 result = xRep.querySelect(mkNss() + query);
407 assure("query: metadatafile\n" + query,
408 eq(result, new String[] { "part", "path" },
409 new XNode[][] { {
410 URI.create(xContext, "http://hospital-employee/doctor"),
411 Literal.create(xContext,
412 "meta/hospital/doctor.rdf") } }));
414 //FIXME redland BUG
415 String uri = "uri:example-element-2";
416 query = "SELECT ?path ?idref FROM <" + toS(manifest) + "> WHERE { "
417 // + "<" + toS(uuid) + "> rdf:type pkg:Package ; "
418 // + " pkg:hasPart ?part . "
419 + "<" + toS(uuid) + "> pkg:hasPart ?part . "
420 + "?part pkg:path ?path ; "
421 + " rdf:type ?type ; "
422 + " pkg:hasPart <" + uri + "> . "
423 // + "<" + uri + "> rdf:type odf:Element ; "
424 + "<" + uri + "> "
425 + " pkg:idref ?idref . "
426 + " FILTER (?type = odf:ContentFile || ?type = odf:StylesFile)"
427 + " }";
428 //log.println(query);
429 result = xRep.querySelect(mkNss() + query);
430 assure("query: example-element-2\n" + query,
431 eq(result, new String[] { "path", "idref" },
432 new XNode[][] { {
433 Literal.create(xContext, "content.xml"),
434 Literal.create(xContext, "ID_B") } }));
436 // CONSTRUCT result triples have no graph!
437 Statement x_PkgFooLit = new Statement(uuid, foo, lit, null);
438 query = "CONSTRUCT { ?pkg <" + toS(foo) + "> \"" +
439 lit.getStringValue() + "\" } FROM <" + toS(manifest) +
440 "> WHERE { ?pkg rdf:type pkg:Package . } ";
441 XEnumeration xResultEnum = xRep.queryConstruct(mkNss() + query);
442 assure("query: construct\n" + query,
443 eq(xResultEnum, new Statement[] { x_PkgFooLit }));
445 query = "ASK { ?pkg rdf:type pkg:Package . }";
446 boolean bResult = xRep.queryAsk(mkNss() + query);
447 assure("query: ask\n" + query, bResult);
449 log.println("...done");
451 } catch (Exception e) {
452 report(e);
456 public void checkRDFa()
458 try {
459 log.println("Checking RDFa gunk...");
461 String content = "behold, for i am the content.";
462 XTextRange xTR = new TestRange(content);
463 XMetadatable xM = (XMetadatable) xTR;
465 Statement[] result = xRep.getStatementRDFa((XMetadatable)xTR);
466 assure("RDFa: get: not empty (initial)",
467 0 == result.length);
469 try {
470 xRep.setStatementRDFa(foo, new XURI[] {}, xM, "", null);
471 assure("RDFa: set: no predicate", false);
472 } catch (IllegalArgumentException e) {
473 // ignore
476 try {
477 xRep.setStatementRDFa(foo, new XURI[] {bar}, null, "", null);
478 assure("RDFa: set: null", false);
479 } catch (IllegalArgumentException e) {
480 // ignore
483 XLiteral trlit = Literal.create(xContext, content);
484 Statement x_FooBarTRLit = new Statement(foo, bar, trlit, null);
485 xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "", null);
487 result = xRep.getStatementRDFa((XMetadatable)xTR);
488 assure("RDFa: get: without content",
489 1 == result.length && eq((Statement)result[0], x_FooBarTRLit));
491 //FIXME: do this?
492 xTR.setString(lit.getStringValue());
494 Statement xFooBarLit = new Statement(foo, bar, lit, null);
495 result = xRep.getStatementRDFa((XMetadatable)xTR);
496 assure("RDFa: get: change",
497 eq((Statement)result.First, xFooBarLit) && null == result.Second);
500 Statement x_FooBarLittype = new Statement(foo, bar, littype, null);
501 Statement x_FooLabelLit = new Statement(foo, rdfslabel, lit, null);
502 xRep.setStatementRDFa(foo, new XURI[] { bar }, xM, "42", uint);
504 result = xRep.getStatementRDFa((XMetadatable)xTR);
505 assure("RDFa: get: with content",
506 2 == result.length && eq((Statement)result[0], x_FooLabelLit)
507 && eq((Statement)result[1], x_FooBarLittype));
509 //FIXME: do this?
510 xTR.setString(content);
512 Statement xFooLabelTRLit = new Statement(foo, rdfslabel, trlit, null);
513 result = xRep.getStatementRDFa((XMetadatable)xTR);
514 assure("RDFa: get: change (label)",
515 eq((Statement)result.First, xFooBarLittype) &&
516 eq((Statement)result.Second, xFooLabelTRLit));
519 xRep.removeStatementRDFa((XMetadatable)xTR);
521 result = xRep.getStatementRDFa((XMetadatable)xTR);
522 assure("RDFa: get: not empty (removed)",
523 0 == result.length);
525 xRep.setStatementRDFa(foo, new XURI[] { foo, bar, baz }, xM,
526 "", null);
528 Statement x_FooFooTRLit = new Statement(foo, foo, trlit, null);
529 Statement x_FooBazTRLit = new Statement(foo, baz, trlit, null);
530 result = xRep.getStatementRDFa((XMetadatable) xTR);
531 assure("RDFa: get: without content (multiple predicates, reinsert)",
532 eq(result, new Statement[] {
533 x_FooFooTRLit, x_FooBarTRLit, x_FooBazTRLit }));
535 xRep.removeStatementRDFa((XMetadatable)xTR);
537 result = xRep.getStatementRDFa((XMetadatable) xTR);
538 assure("RDFa: get: not empty (re-removed)",
539 0 == result.length);
541 log.println("...done");
543 } catch (Exception e) {
544 report(e);
548 // utilities -------------------------------------------------------------
550 public void report2(Exception e)
552 if (e instanceof WrappedTargetException)
554 log.println("Cause:");
555 Exception cause = (Exception)
556 (((WrappedTargetException)e).TargetException);
557 log.println(cause.toString());
558 report2(cause);
559 } else if (e instanceof WrappedTargetRuntimeException) {
560 log.println("Cause:");
561 Exception cause = (Exception)
562 (((WrappedTargetRuntimeException)e).TargetException);
563 log.println(cause.toString());
564 report2(cause);
568 public void report(Exception e) {
569 log.println("Exception occurred:");
570 e.printStackTrace((java.io.PrintWriter) log);
571 report2(e);
572 failed();
575 public static String toS(XNode n) {
576 if (null == n) return "< null >";
577 return n.getStringValue();
580 static boolean isBlank(XNode i_node)
582 XBlankNode blank = (XBlankNode) UnoRuntime.queryInterface(
583 XBlankNode.class, i_node);
584 return blank != null;
588 static class Statement implements XStatement
590 XResource m_Subject;
591 XResource m_Predicate;
592 XNode m_Object;
593 XURI m_Graph;
595 Statement(XResource i_Subject, XResource i_Predicate, XNode i_Object,
596 XURI i_Graph)
598 m_Subject = i_Subject;
599 m_Predicate = i_Predicate;
600 m_Object = i_Object;
601 m_Graph = i_Graph;
604 public XResource getSubject() { return m_Subject; }
605 public XResource getPredicate() { return m_Predicate; }
606 public XNode getObject() { return m_Object; }
607 public XURI getGraph() { return m_Graph; }
611 static Statement[] toSeq(XEnumeration i_Enum) throws Exception
613 java.util.Collection c = new java.util.Vector();
614 while (i_Enum.hasMoreElements()) {
615 Statement s = (Statement) i_Enum.nextElement();
616 //log.println("toSeq: " + s.getSubject().getStringValue() + " " + s.getPredicate().getStringValue() + " " + s.getObject().getStringValue() + ".");
617 c.add(s);
619 // return (Statement[]) c.toArray();
620 // java sucks
621 Object[] arr = c.toArray();
622 Statement[] ret = new Statement[arr.length];
623 for (int i = 0; i < arr.length; ++i) {
624 ret[i] = (Statement) arr[i];
626 return ret;
629 static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception
631 java.util.Collection c = new java.util.Vector();
632 while (i_Enum.hasMoreElements()) {
633 XNode[] s = (XNode[]) i_Enum.nextElement();
634 c.add(s);
636 // return (XNode[][]) c.toArray();
637 Object[] arr = c.toArray();
638 XNode[][] ret = new XNode[arr.length][];
639 for (int i = 0; i < arr.length; ++i) {
640 ret[i] = (XNode[]) arr[i];
642 return ret;
645 static class BindingComp implements java.util.Comparator
647 public int compare(Object i_Left, Object i_Right)
649 XNode[] left = (XNode[]) i_Left;
650 XNode[] right = (XNode[]) i_Right;
651 if (left.length != right.length) throw new RuntimeException();
652 for (int i = 0; i < left.length; ++i) {
653 int eq = (left[i].getStringValue().compareTo(
654 right[i].getStringValue()));
655 if (eq != 0) return eq;
657 return 0;
661 static class StmtComp implements java.util.Comparator
663 public int compare(Object i_Left, Object i_Right)
665 int eq;
666 Statement left = (Statement) i_Left;
667 Statement right = (Statement) i_Right;
668 if ((eq = cmp(left.Graph, right.Graph )) != 0) return eq;
669 if ((eq = cmp(left.Subject, right.Subject )) != 0) return eq;
670 if ((eq = cmp(left.Predicate, right.Predicate)) != 0) return eq;
671 if ((eq = cmp(left.Object, right.Object )) != 0) return eq;
672 return 0;
675 public int cmp(XNode i_Left, XNode i_Right)
677 if (isBlank(i_Left)) {
678 return isBlank(i_Right) ? 0 : 1;
679 } else {
680 if (isBlank(i_Right)) {
681 return -1;
682 } else {
683 return toS(i_Left).compareTo(toS(i_Right));
689 static boolean eq(Statement i_Left, Statement i_Right)
691 XURI lG = i_Left.Graph;
692 XURI rG = i_Right.Graph;
693 if (!eq(lG, rG)) {
694 log.println("Graphs differ: " + toS(lG) + " != " + toS(rG));
695 return false;
697 if (!eq(i_Left.Subject, i_Right.Subject)) {
698 log.println("Subjects differ: " +
699 i_Left.Subject.getStringValue() + " != " +
700 i_Right.Subject.getStringValue());
701 return false;
703 if (!eq(i_Left.Predicate, i_Right.Predicate)) {
704 log.println("Predicates differ: " +
705 i_Left.Predicate.getStringValue() + " != " +
706 i_Right.Predicate.getStringValue());
707 return false;
709 if (!eq(i_Left.Object, i_Right.Object)) {
710 log.println("Objects differ: " +
711 i_Left.Object.getStringValue() + " != " +
712 i_Right.Object.getStringValue());
713 return false;
715 return true;
718 static boolean eq(Statement[] i_Result, Statement[] i_Expected)
720 if (i_Result.length != i_Expected.length) {
721 log.println("eq: different lengths: " + i_Result.length + " " +
722 i_Expected.length);
723 return false;
725 Statement[] expected = (Statement[])
726 java.util.Arrays.asList(i_Expected).toArray();
727 java.util.Arrays.sort(i_Result, new StmtComp());
728 java.util.Arrays.sort(expected, new StmtComp());
729 for (int i = 0; i < expected.length; ++i) {
730 if (!eq(i_Result[i], expected[i])) return false;
732 return true;
735 static boolean eq(XEnumeration i_Enum, Statement[] i_Expected)
736 throws Exception
738 Statement[] current = toSeq(i_Enum);
739 return eq(current, i_Expected);
742 static boolean eq(XNode i_Left, XNode i_Right)
744 if (i_Left == null) {
745 return (i_Right == null);
746 } else {
747 return (i_Right != null) &&
748 (i_Left.getStringValue().equals(i_Right.getStringValue())
749 // FIXME: hack: blank nodes considered equal
750 || (isBlank(i_Left) && isBlank(i_Right)));
754 static boolean eq(XQuerySelectResult i_Result,
755 String[] i_Vars, XNode[][] i_Bindings) throws Exception
757 String[] vars = (String[]) i_Result.getBindingNames();
758 XEnumeration iter = (XEnumeration) i_Result;
759 XNode[][] bindings = toSeqs(iter);
760 if (vars.length != i_Vars.length) {
761 log.println("var lengths differ");
762 return false;
764 if (bindings.length != i_Bindings.length) {
765 log.println("binding lengths differ: " + i_Bindings.length +
766 " vs " + bindings.length );
767 return false;
769 java.util.Arrays.sort(bindings, new BindingComp());
770 java.util.Arrays.sort(i_Bindings, new BindingComp());
771 for (int i = 0; i < i_Bindings.length; ++i) {
772 if (i_Bindings[i].length != i_Vars.length) {
773 log.println("TEST ERROR!");
774 throw new Exception();
776 if (bindings[i].length != i_Vars.length) {
777 log.println("binding length and var length differ");
778 return false;
780 for (int j = 0; j < i_Vars.length; ++j) {
781 if (!eq(bindings[i][j], i_Bindings[i][j])) {
782 log.println("bindings differ: " +
783 toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j]));
784 return false;
788 for (int i = 0; i < i_Vars.length; ++i) {
789 if (!vars[i].equals(i_Vars[i])) {
790 log.println("variable names differ: " +
791 vars[i] + " != " + i_Vars[i]);
792 return false;
795 return true;
798 static String mkNamespace(String i_prefix, String i_namespace)
800 return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n";
803 static String mkNss()
805 String namespaces = mkNamespace("rdf",
806 "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
807 namespaces += mkNamespace("pkg",
808 "http://docs.oasis-open.org/opendocument/meta/package/common#");
809 namespaces += mkNamespace("odf",
810 "http://docs.oasis-open.org/opendocument/meta/package/odf#");
811 return namespaces;
814 class TestRange implements XTextRange, XMetadatable, XServiceInfo
816 String m_Stream;
817 String m_XmlId;
818 String m_Text;
819 TestRange(String i_Str) { m_Text = i_Str; }
821 public String getStringValue() { return ""; }
822 public String getNamespace() { return ""; }
823 public String getLocalName() { return ""; }
825 public StringPair getMetadataReference()
826 { return new StringPair(m_Stream, m_XmlId); }
827 public void setMetadataReference(StringPair i_Ref)
828 throws IllegalArgumentException
829 { m_Stream = (String)i_Ref.First; m_XmlId = (String)i_Ref.Second; }
830 public void ensureMetadataReference()
831 { m_Stream = "content.xml"; m_XmlId = "42"; }
833 public String getImplementationName() { return null; }
834 public String[] getSupportedServiceNames() { return null; }
835 public boolean supportsService(String i_Svc)
836 { return i_Svc.equals("com.sun.star.text.Paragraph"); }
838 public XText getText() { return null; }
839 public XTextRange getStart() { return null; }
840 public XTextRange getEnd() { return null; }
841 public String getString() { return m_Text; }
842 public void setString(String i_Str) { m_Text = i_Str; }