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 $
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
.*;
58 * Test case for service com.sun.star.rdf.Repository
59 * Currently, this service is implemented in
60 * unoxml/source/rdf/librdf_repository.cxx
64 public class RDFRepositoryTest
extends ComplexTestCase
66 XComponentContext xContext
;
69 XDocumentRepository xRep
;
82 String rdfs
= "http://www.w3.org/2000/01/rdf-schema#";
84 public String
[] getTestMethodNames ()
86 return new String
[] { "check", "checkSPARQL", "checkRDFa" };
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
) {
145 log
.println("Creating service Repository...");
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
);
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
);
170 xRep
.createGraph(foo
);
171 assure("creating duplicate graph was allowed", false);
172 } catch (ElementExistException e
) {
177 xRep
.createGraph(null);
178 assure("invalid graph name was allowed", false);
179 } catch (IllegalArgumentException e
) {
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)",
218 new Statement
[] { xFoo_FooBarBaz
, xFoo_FooBarBlank
}));
219 xFooEnum
= xRep
.getStatements(null, null, null);
220 assure("addStatement(foo,bar,blank) (global)",
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
);
294 xRep
.importGraph(FileFormat
.RDF_XML
, xFooIn
, bar
, base
);
295 assure("importing existing graph did not fail", false);
296 } catch (ElementExistException e
) {
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...");
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
));
327 assure("deleted graph not invalid (clear)", false);
328 } catch (NoSuchElementException e
) {
333 xBazGraph
.addStatement(foo
, foo
, foo
);
334 assure("deleted graph not invalid (add)", false);
335 } catch (NoSuchElementException e
) {
340 xBazGraph
.removeStatements(null, null, null);
341 assure("deleted graph not invalid (remove)", false);
342 } catch (NoSuchElementException e
) {
347 xBazGraph
.getStatements(null, null, null);
348 assure("deleted graph not invalid (get)", false);
349 } catch (NoSuchElementException e
) {
353 log
.println("...done");
355 } catch (Exception e
) {
360 public void checkSPARQL()
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
);
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" },
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" },
410 URI
.create(xContext
, "http://hospital-employee/doctor"),
411 Literal
.create(xContext
,
412 "meta/hospital/doctor.rdf") } }));
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 ; "
425 + " pkg:idref ?idref . "
426 + " FILTER (?type = odf:ContentFile || ?type = odf:StylesFile)"
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" },
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
) {
456 public void checkRDFa()
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)",
470 xRep
.setStatementRDFa(foo
, new XURI
[] {}, xM
, "", null);
471 assure("RDFa: set: no predicate", false);
472 } catch (IllegalArgumentException e
) {
477 xRep
.setStatementRDFa(foo
, new XURI
[] {bar
}, null, "", null);
478 assure("RDFa: set: null", false);
479 } catch (IllegalArgumentException e
) {
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
));
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
));
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)",
525 xRep
.setStatementRDFa(foo
, new XURI
[] { foo
, bar
, baz
}, xM
,
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)",
541 log
.println("...done");
543 } catch (Exception 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());
559 } else if (e
instanceof WrappedTargetRuntimeException
) {
560 log
.println("Cause:");
561 Exception cause
= (Exception
)
562 (((WrappedTargetRuntimeException
)e
).TargetException
);
563 log
.println(cause
.toString());
568 public void report(Exception e
) {
569 log
.println("Exception occurred:");
570 e
.printStackTrace((java
.io
.PrintWriter
) log
);
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
591 XResource m_Predicate;
595 Statement(XResource i_Subject, XResource i_Predicate, XNode i_Object,
598 m_Subject = i_Subject;
599 m_Predicate = i_Predicate;
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() + ".");
619 // return (Statement[]) c.toArray();
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
];
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();
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
];
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
;
661 static class StmtComp
implements java
.util
.Comparator
663 public int compare(Object i_Left
, Object i_Right
)
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
;
675 public int cmp(XNode i_Left
, XNode i_Right
)
677 if (isBlank(i_Left
)) {
678 return isBlank(i_Right
) ?
0 : 1;
680 if (isBlank(i_Right
)) {
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
;
694 log
.println("Graphs differ: " + toS(lG
) + " != " + toS(rG
));
697 if (!eq(i_Left
.Subject
, i_Right
.Subject
)) {
698 log
.println("Subjects differ: " +
699 i_Left
.Subject
.getStringValue() + " != " +
700 i_Right
.Subject
.getStringValue());
703 if (!eq(i_Left
.Predicate
, i_Right
.Predicate
)) {
704 log
.println("Predicates differ: " +
705 i_Left
.Predicate
.getStringValue() + " != " +
706 i_Right
.Predicate
.getStringValue());
709 if (!eq(i_Left
.Object
, i_Right
.Object
)) {
710 log
.println("Objects differ: " +
711 i_Left
.Object
.getStringValue() + " != " +
712 i_Right
.Object
.getStringValue());
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
+ " " +
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;
735 static boolean eq(XEnumeration i_Enum
, Statement
[] i_Expected
)
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);
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");
764 if (bindings
.length
!= i_Bindings
.length
) {
765 log
.println("binding lengths differ: " + i_Bindings
.length
+
766 " vs " + bindings
.length
);
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");
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
]));
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
]);
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#");
814 class TestRange
implements XTextRange
, XMetadatable
, XServiceInfo
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
; }