merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / qa / complex / DocumentMetadataAccessTest.java
blob86848ac4bf09ddb98f76df3f7e52f1b7b3115d50
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: DocumentMetadataAccessTest.java,v $
10 * $Revision: 1.1.2.9 $
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.framework;
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.XComponent;
40 import com.sun.star.lang.XInitialization;
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.XEnumerationAccess;
49 import com.sun.star.container.XEnumeration;
50 import com.sun.star.container.ElementExistException;
51 import com.sun.star.container.NoSuchElementException;
52 import com.sun.star.io.XInputStream;
53 import com.sun.star.io.XOutputStream;
54 import com.sun.star.util.XCloseable;
55 import com.sun.star.frame.XStorable;
56 import com.sun.star.frame.XLoadable;
57 import com.sun.star.text.XTextDocument;
58 import com.sun.star.text.XTextRange;
59 import com.sun.star.text.XText;
60 import com.sun.star.rdf.*;
62 /**
63 * Test case for interface com.sun.star.rdf.XDocumentMetadataAccess
64 * Currently, this service is implemented in
65 * sfx2/source/doc/DocumentMetadataAccess.cxx
67 * Actually, this is not a service, so we need to create a document and
68 * go from there...
70 * @author mst
72 public class DocumentMetadataAccessTest extends ComplexTestCase
74 XMultiServiceFactory xMSF;
75 XComponentContext xContext;
76 String tempDir;
78 String nsRDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
79 String nsRDFS = "http://www.w3.org/2000/01/rdf-schema#";
80 String nsPkg="http://docs.oasis-open.org/opendocument/meta/package/common#";
81 String nsODF ="http://docs.oasis-open.org/opendocument/meta/package/odf#";
83 XURI foo;
84 XURI bar;
85 XURI baz;
87 static XURI rdf_type;
88 static XURI rdfs_label;
89 static XURI pkg_Document;
90 static XURI pkg_hasPart;
91 static XURI pkg_MetadataFile;
92 static XURI odf_ContentFile;
93 static XURI odf_StylesFile;
94 static XURI odf_Element;
95 static XBlankNode blank1;
96 static XBlankNode blank2;
97 static XBlankNode blank3;
98 static XBlankNode blank4;
99 static String manifestPath = "manifest.rdf";
100 static String contentPath = "content.xml";
101 static String stylesPath = "styles.xml";
102 static String fooPath = "foo.rdf";
103 static String fooBarPath = "meta/foo/bar.rdf";
105 XRepository xRep;
106 XRepositorySupplier xRS;
107 XDocumentMetadataAccess xDMA;
109 public String[] getTestMethodNames ()
111 return new String[] { "check", "checkRDFa" };
114 public void before()
116 try {
118 xMSF = (XMultiServiceFactory) param.getMSF();
119 assure("could not create MultiServiceFactory.", xMSF != null);
120 XPropertySet xPropertySet = (XPropertySet)
121 UnoRuntime.queryInterface(XPropertySet.class, xMSF);
122 Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext");
123 xContext = (XComponentContext)
124 UnoRuntime.queryInterface(XComponentContext.class, defaultCtx);
125 assure("could not get component context.", xContext != null);
127 tempDir = util.utils.getOfficeTemp/*Dir*/(xMSF);
128 log.println("tempdir: " + tempDir);
130 foo = URI.create(xContext, "uri:foo");
131 assure("foo", null != foo);
132 bar = URI.create(xContext, "uri:bar");
133 assure("bar", null != bar);
134 baz = URI.create(xContext, "uri:baz");
135 assure("baz", null != baz);
137 blank1 = BlankNode.create(xContext, "_:1");
138 assure("blank1", null != blank1);
139 blank2 = BlankNode.create(xContext, "_:2");
140 assure("blank2", null != blank2);
141 blank3 = BlankNode.create(xContext, "_:3");
142 assure("blank3", null != blank3);
143 blank4 = BlankNode.create(xContext, "_:4");
144 assure("blank4", null != blank4);
145 rdf_type = URI.createKnown(xContext, URIs.RDF_TYPE);
146 assure("rdf_type", null != rdf_type);
147 rdfs_label = URI.createKnown(xContext, URIs.RDFS_LABEL);
148 assure("rdfs_label", null != rdfs_label);
149 pkg_Document = URI.createKnown(xContext, URIs.PKG_DOCUMENT);
150 assure("pkg_Document", null != pkg_Document);
151 pkg_hasPart = URI.createKnown(xContext, URIs.PKG_HASPART);
152 assure("pkg_hasPart", null != pkg_hasPart);
153 pkg_MetadataFile = URI.createKnown(xContext, URIs.PKG_METADATAFILE);
154 assure("pkg_MetadataFile", null != pkg_MetadataFile);
155 odf_ContentFile = URI.createKnown(xContext, URIs.ODF_CONTENTFILE);
156 assure("odf_ContentFile", null != odf_ContentFile);
157 odf_StylesFile = URI.createKnown(xContext, URIs.ODF_STYLESFILE);
158 assure("odf_StylesFile", null != odf_StylesFile);
159 odf_Element = URI.createKnown(xContext, URIs.ODF_ELEMENT);
160 assure("odf_Element", null != odf_Element);
162 } catch (Exception e) {
163 report(e);
167 public void after()
169 xRep = null;
170 xRS = null;
171 xDMA = null;
174 public void check()
176 XComponent xComp = null;
177 XComponent xComp2 = null;
178 try {
179 XEnumeration xStmtsEnum;
180 XNamedGraph xManifest;
182 log.println("Creating document with Repository...");
184 // we cannot create a XDMA directly, we must create
185 // a document and get it from there :(
186 // create document
187 PropertyValue[] loadProps = new PropertyValue[1];
188 loadProps[0] = new PropertyValue();
189 loadProps[0].Name = "Hidden";
190 loadProps[0].Value = new Boolean(true);
191 xComp = util.DesktopTools.openNewDoc(xMSF, "swriter", loadProps);
192 XTextDocument xText = (XTextDocument) UnoRuntime.queryInterface(
193 XTextDocument.class, xComp);
195 XRepositorySupplier xRS = (XRepositorySupplier)
196 UnoRuntime.queryInterface(XRepositorySupplier.class, xComp);
197 assure("xRS null", null != xRS);
198 XDocumentMetadataAccess xDMA = (XDocumentMetadataAccess)
199 UnoRuntime.queryInterface(XDocumentMetadataAccess.class, xRS);
200 assure("xDMA null", null != xDMA);
201 xRep = xRS.getRDFRepository();
202 assure("xRep null", null != xRep);
204 log.println("...done");
206 log.println("Checking that new repository is initialized...");
208 XURI xBaseURI = (XURI) xDMA;
209 String baseURI = xBaseURI.getStringValue();
210 assure("new: baseURI",
211 null != xBaseURI && !xBaseURI.getStringValue().equals(""));
213 assure("new: # graphs", 1 == xRep.getGraphNames().length);
214 XURI manifest = URI.createNS(xContext, xBaseURI.getStringValue(),
215 manifestPath);
216 xManifest = xRep.getGraph(manifest);
217 assure("new: manifest graph", null != xManifest);
219 Statement[] manifestStmts = getManifestStmts(xBaseURI);
220 xStmtsEnum = xRep.getStatements(null, null, null);
221 assure("new: manifest graph", eq(xStmtsEnum, manifestStmts));
223 log.println("...done");
225 log.println("Checking some invalid args...");
227 String content = "behold, for i am the content.";
228 XTextRange xTR = new TestRange(content);
229 XMetadatable xM = (XMetadatable) xTR;
231 try {
232 xDMA.getElementByURI(null);
233 assure("getElementByURI: null allowed", false);
234 } catch (IllegalArgumentException e) {
235 // ignore
237 try {
238 xDMA.getMetadataGraphsWithType(null);
239 assure("getMetadataGraphsWithType: null URI allowed", false);
240 } catch (IllegalArgumentException e) {
241 // ignore
243 try {
244 xDMA.addMetadataFile("", new XURI[0]);
245 assure("addMetadataFile: empty filename allowed", false);
246 } catch (IllegalArgumentException e) {
247 // ignore
249 try {
250 xDMA.addMetadataFile("/foo", new XURI[0]);
251 assure("addMetadataFile: absolute filename allowed", false);
252 } catch (IllegalArgumentException e) {
253 // ignore
255 try {
256 xDMA.addMetadataFile("fo\"o", new XURI[0]);
257 assure("addMetadataFile: invalid filename allowed", false);
258 } catch (IllegalArgumentException e) {
259 // ignore
261 try {
262 xDMA.addMetadataFile("../foo", new XURI[0]);
263 assure("addMetadataFile: filename with .. allowed", false);
264 } catch (IllegalArgumentException e) {
265 // ignore
267 try {
268 xDMA.addMetadataFile("foo/../../bar", new XURI[0]);
269 assure("addMetadataFile: filename with nest .. allowed", false);
270 } catch (IllegalArgumentException e) {
271 // ignore
273 try {
274 xDMA.addMetadataFile("foo/././bar", new XURI[0]);
275 assure("addMetadataFile: filename with nest . allowed", false);
276 } catch (IllegalArgumentException e) {
277 // ignore
279 try {
280 xDMA.addMetadataFile("content.xml", new XURI[0]);
281 assure("addMetadataFile: content.xml allowed", false);
282 } catch (IllegalArgumentException e) {
283 // ignore
285 try {
286 xDMA.addMetadataFile("styles.xml", new XURI[0]);
287 assure("addMetadataFile: styles.xml allowed", false);
288 } catch (IllegalArgumentException e) {
289 // ignore
291 try {
292 xDMA.addMetadataFile("meta.xml", new XURI[0]);
293 assure("addMetadataFile: meta.xml allowed", false);
294 } catch (IllegalArgumentException e) {
295 // ignore
297 try {
298 xDMA.addMetadataFile("settings.xml", new XURI[0]);
299 assure("addMetadataFile: settings.xml allowed", false);
300 } catch (IllegalArgumentException e) {
301 // ignore
303 try {
304 xDMA.importMetadataFile(FileFormat.RDF_XML, null, "foo",
305 foo, new XURI[0]);
306 assure("importMetadataFile: null stream allowed", false);
307 } catch (IllegalArgumentException e) {
308 // ignore
310 try {
311 XInputStream xFooIn =
312 new StreamSimulator(tempDir + "empty.rdf", true, param);
313 xDMA.importMetadataFile(FileFormat.RDF_XML, xFooIn, "",
314 foo, new XURI[0]);
315 assure("importMetadataFile: empty filename allowed", false);
316 } catch (IllegalArgumentException e) {
317 // ignore
319 try {
320 XInputStream xFooIn =
321 new StreamSimulator(tempDir + "empty.rdf", true, param);
322 xDMA.importMetadataFile(FileFormat.RDF_XML, xFooIn, "meta.xml",
323 foo, new XURI[0]);
324 assure("importMetadataFile: meta.xml filename allowed", false);
325 } catch (IllegalArgumentException e) {
326 // ignore
328 try {
329 XInputStream xFooIn =
330 new StreamSimulator(tempDir + "empty.rdf", true, param);
331 xDMA.importMetadataFile(FileFormat.RDF_XML,
332 xFooIn, "foo", null, new XURI[0]);
333 assure("importMetadataFile: null base URI allowed", false);
334 } catch (IllegalArgumentException e) {
335 // ignore
337 try {
338 XInputStream xFooIn =
339 new StreamSimulator(tempDir + "empty.rdf", true, param);
340 xDMA.importMetadataFile(FileFormat.RDF_XML,
341 xFooIn, "foo", rdf_type, new XURI[0]);
342 assure("importMetadataFile: non-absolute base URI allowed",
343 false);
344 } catch (IllegalArgumentException e) {
345 // ignore
347 try {
348 xDMA.removeMetadataFile(null);
349 assure("removeMetadataFile: null URI allowed", false);
350 } catch (IllegalArgumentException e) {
351 // ignore
353 try {
354 xDMA.addContentOrStylesFile("");
355 assure("addContentOrStylesFile: empty filename allowed",
356 false);
357 } catch (IllegalArgumentException e) {
358 // ignore
360 try {
361 xDMA.addContentOrStylesFile("/content.xml");
362 assure("addContentOrStylesFile: absolute filename allowed",
363 false);
364 } catch (IllegalArgumentException e) {
365 // ignore
367 try {
368 xDMA.addContentOrStylesFile("foo.rdf");
369 assure("addContentOrStylesFile: invalid filename allowed",
370 false);
371 } catch (IllegalArgumentException e) {
372 // ignore
374 try {
375 xDMA.removeContentOrStylesFile("");
376 assure("removeContentOrStylesFile: empty filename allowed",
377 false);
378 } catch (IllegalArgumentException e) {
379 // ignore
381 try {
382 xDMA.loadMetadataFromStorage(null, foo, null);
383 assure("loadMetadataFromStorage: null storage allowed", false);
384 } catch (IllegalArgumentException e) {
385 // ignore
387 try {
388 xDMA.storeMetadataToStorage(null/*, base*/);
389 assure("storeMetadataToStorage: null storage allowed", false);
390 } catch (IllegalArgumentException e) {
391 // ignore
393 try {
394 xDMA.loadMetadataFromMedium(new PropertyValue[0]);
395 assure("loadMetadataFromMedium: empty medium allowed", false);
396 } catch (IllegalArgumentException e) {
397 // ignore
399 try {
400 xDMA.storeMetadataToMedium(new PropertyValue[0]);
401 assure("storeMetadataToMedium: empty medium allowed", false);
402 } catch (IllegalArgumentException e) {
403 // ignore
406 log.println("...done");
408 log.println("Checking file addition/removal...");
410 xDMA.removeContentOrStylesFile(contentPath);
411 xStmtsEnum = xManifest.getStatements(null, null, null);
412 assure("removeContentOrStylesFile (content)",
413 eq(xStmtsEnum, new Statement[] {
414 manifestStmts[0], manifestStmts[2], manifestStmts[4]
415 }));
417 xDMA.addContentOrStylesFile(contentPath);
418 xStmtsEnum = xManifest.getStatements(null, null, null);
419 assure("addContentOrStylesFile (content)",
420 eq(xStmtsEnum, manifestStmts));
422 xDMA.removeContentOrStylesFile(stylesPath);
423 xStmtsEnum = xManifest.getStatements(null, null, null);
424 assure("removeContentOrStylesFile (styles)",
425 eq(xStmtsEnum, new Statement[] {
426 manifestStmts[0], manifestStmts[1], manifestStmts[3]
427 }));
429 xDMA.addContentOrStylesFile(stylesPath);
430 xStmtsEnum = xManifest.getStatements(null, null, null);
431 assure("addContentOrStylesFile (styles)",
432 eq(xStmtsEnum, manifestStmts));
434 XURI xFoo = URI.createNS(xContext, xBaseURI.getStringValue(),
435 fooPath);
436 Statement xM_BaseHaspartFoo =
437 new Statement(xBaseURI, pkg_hasPart, xFoo, manifest);
438 Statement xM_FooTypeMetadata =
439 new Statement(xFoo, rdf_type, pkg_MetadataFile, manifest);
440 Statement xM_FooTypeBar =
441 new Statement(xFoo, rdf_type, bar, manifest);
442 xDMA.addMetadataFile(fooPath, new XURI[] { bar });
443 xStmtsEnum = xManifest.getStatements(null, null, null);
444 assure("addMetadataFile",
445 eq(xStmtsEnum, merge(manifestStmts, new Statement[] {
446 xM_BaseHaspartFoo, xM_FooTypeMetadata, xM_FooTypeBar
447 })));
449 XURI[] graphsBar = xDMA.getMetadataGraphsWithType(bar);
450 assure("getMetadataGraphsWithType",
451 graphsBar.length == 1 && eq(graphsBar[0], xFoo));
454 xDMA.removeMetadataFile(xFoo);
455 xStmtsEnum = xManifest.getStatements(null, null, null);
456 assure("removeMetadataFile",
457 eq(xStmtsEnum, manifestStmts));
459 log.println("...done");
461 log.println("Checking mapping...");
463 XEnumerationAccess xTextEnum = (XEnumerationAccess)
464 UnoRuntime.queryInterface(XEnumerationAccess.class,
465 xText.getText());
466 Object o = xTextEnum.createEnumeration().nextElement();
467 XMetadatable xMeta1 = (XMetadatable) UnoRuntime.queryInterface(
468 XMetadatable.class, o);
470 XURI uri;
471 XMetadatable xMeta;
472 xMeta = xDMA.getElementByURI(xMeta1);
473 assure("getElementByURI: null", null != xMeta);
474 String XmlId = xMeta.getMetadataReference().Second;
475 String XmlId1 = xMeta1.getMetadataReference().Second;
476 assure("getElementByURI: no xml id", !XmlId.equals(""));
477 assure("getElementByURI: different xml id", XmlId.equals(XmlId1));
479 log.println("...done");
481 log.println("Checking storing and loading...");
483 XURI xFoobar = URI.createNS(xContext, xBaseURI.getStringValue(),
484 fooBarPath);
485 Statement[] metadataStmts = getMetadataFileStmts(xBaseURI,
486 fooBarPath);
487 xDMA.addMetadataFile(fooBarPath, new XURI[0]);
488 xStmtsEnum = xRep.getStatements(null, null, null);
489 assure("addMetadataFile",
490 eq(xStmtsEnum, merge(manifestStmts, metadataStmts )));
492 Statement xFoobar_FooBarFoo =
493 new Statement(foo, bar, foo, xFoobar);
494 xRep.getGraph(xFoobar).addStatement(foo, bar, foo);
495 xStmtsEnum = xRep.getStatements(null, null, null);
496 assure("addStatement",
497 eq(xStmtsEnum, merge(manifestStmts, merge(metadataStmts,
498 new Statement[] { xFoobar_FooBarFoo }))));
500 PropertyValue noMDNoContentFile = new PropertyValue();
501 noMDNoContentFile.Name = "URL";
502 noMDNoContentFile.Value = util.utils.getFullTestURL("CUSTOM.odt");
503 PropertyValue noMDFile = new PropertyValue();
504 noMDFile.Name = "URL";
505 noMDFile.Value = util.utils.getFullTestURL("TEST.odt");
506 PropertyValue file = new PropertyValue();
507 file.Name = "URL";
508 file.Value = tempDir + "TESTDMA.odt";
510 PropertyValue baseURL = new PropertyValue();
511 baseURL.Name = "DocumentBaseURL";
512 baseURL.Value = tempDir + "TMP.odt";
514 PropertyValue mimetype = new PropertyValue();
515 mimetype.Name = "MediaType";
516 mimetype.Value = "application/vnd.oasis.opendocument.text";
517 PropertyValue[] argsEmptyNoContent = { mimetype, noMDNoContentFile};
518 PropertyValue[] argsEmpty = { mimetype, noMDFile };
519 PropertyValue[] args = { mimetype, file };
521 xStmtsEnum = xRep.getStatements(null, null, null);
522 XURI[] graphs = xRep.getGraphNames();
524 xDMA.storeMetadataToMedium(args);
526 // this should re-init
527 xDMA.loadMetadataFromMedium(argsEmptyNoContent);
528 xRep = xRS.getRDFRepository();
529 assure("xRep null", null != xRep);
530 assure("baseURI still tdoc?",
531 !baseURI.equals(xDMA.getStringValue()));
532 Statement[] manifestStmts2 = getManifestStmts((XURI) xDMA);
533 xStmtsEnum = xRep.getStatements(null, null, null);
534 // there is no content or styles file in here, so we have just
535 // the package stmt
536 assure("loadMetadataFromMedium (no metadata, no content)",
537 eq(xStmtsEnum, new Statement[] { manifestStmts2[0] }));
539 // this should re-init
540 xDMA.loadMetadataFromMedium(argsEmpty);
541 xRep = xRS.getRDFRepository();
542 assure("xRep null", null != xRep);
543 assure("baseURI still tdoc?",
544 !baseURI.equals(xDMA.getStringValue()));
545 Statement[] manifestStmts3 = getManifestStmts((XURI) xDMA);
547 xStmtsEnum = xRep.getStatements(null, null, null);
548 assure("loadMetadataFromMedium (no metadata)",
549 eq(xStmtsEnum, manifestStmts3));
551 xDMA.loadMetadataFromMedium(args);
552 xRep = xRS.getRDFRepository();
553 assure("xRep null", null != xRep);
554 Statement[] manifestStmts4 = getManifestStmts((XURI) xDMA);
555 Statement[] metadataStmts4 = getMetadataFileStmts((XURI) xDMA,
556 fooBarPath);
558 xStmtsEnum = xRep.getStatements(null, null, null);
559 assure("some graph(s) not reloaded",
560 graphs.length == xRep.getGraphNames().length);
562 XURI xFoobar4 = URI.createNS(xContext, xDMA.getStringValue(),
563 fooBarPath);
564 Statement xFoobar_FooBarFoo4 =
565 new Statement(foo, bar, foo, xFoobar4);
566 assure("loadMetadataFromMedium (re-load)",
567 eq(xStmtsEnum, merge(manifestStmts4, merge(metadataStmts4,
568 new Statement[] { xFoobar_FooBarFoo4 }))));
570 log.println("...done");
572 log.println("Checking storing and loading via model...");
574 String f = tempDir + "TESTPARA.odt";
576 XStorable xStor = (XStorable) UnoRuntime.queryInterface(
577 XStorable.class, xRS);
579 xStor.storeToURL(f, new PropertyValue[0]);
581 xComp2 = util.DesktopTools.loadDoc(xMSF, f, loadProps);
583 XDocumentMetadataAccess xDMA2 = (XDocumentMetadataAccess)
584 UnoRuntime.queryInterface(XDocumentMetadataAccess.class,
585 xComp2);
586 assure("xDMA2 null", null != xDMA2);
588 XRepositorySupplier xRS2 = (XRepositorySupplier)
589 UnoRuntime.queryInterface(XRepositorySupplier.class, xComp2);
590 assure("xRS2 null", null != xRS2);
592 XRepository xRep2 = xRS2.getRDFRepository();
593 assure("xRep2 null", null != xRep2);
595 Statement[] manifestStmts5 = getManifestStmts((XURI) xDMA2);
596 Statement[] metadataStmts5 = getMetadataFileStmts((XURI) xDMA2,
597 fooBarPath);
598 XURI xFoobar5 = URI.createNS(xContext, xDMA2.getStringValue(),
599 fooBarPath);
600 Statement xFoobar_FooBarFoo5 =
601 new Statement(foo, bar, foo, xFoobar5);
602 xStmtsEnum = xRep.getStatements(null, null, null);
603 XEnumeration xStmtsEnum2 = xRep2.getStatements(null, null, null);
604 assure("load: repository differs",
605 eq(xStmtsEnum2, merge(manifestStmts5, merge(metadataStmts5,
606 new Statement[] { xFoobar_FooBarFoo5 }))));
608 log.println("...done");
610 } catch (Exception e) {
611 report(e);
612 } finally {
613 close(xComp);
614 close(xComp2);
618 public void checkRDFa()
620 XComponent xComp = null;
621 String file;
622 try {
623 file = util.utils.getFullTestURL("TESTRDFA.odt");
624 xComp = loadRDFa(file);
625 if (xComp != null)
627 file = tempDir + "TESTRDFA.odt";
628 storeRDFa(xComp, file);
629 close(xComp);
630 xComp = loadRDFa(file);
632 } finally {
633 close(xComp);
637 public void storeRDFa(XComponent xComp, String file)
639 try {
641 log.println("Storing test document...");
643 XStorable xStor = (XStorable) UnoRuntime.queryInterface(
644 XStorable.class, xComp);
646 xStor.storeToURL(file, new PropertyValue[0]);
648 log.println("...done");
650 } catch (Exception e) {
651 report(e);
655 public XComponent loadRDFa(String file)
657 XComponent xComp = null;
658 try {
660 log.println("Loading test document...");
662 PropertyValue[] loadProps = new PropertyValue[1];
663 loadProps[0] = new PropertyValue();
664 loadProps[0].Name = "Hidden";
665 loadProps[0].Value = new Boolean(true);
669 xComp = util.DesktopTools.loadDoc(xMSF, file, loadProps);
671 XRepositorySupplier xRS = (XRepositorySupplier)
672 UnoRuntime.queryInterface(XRepositorySupplier.class, xComp);
673 assure("xRS null", null != xRS);
675 XDocumentRepository xRep = (XDocumentRepository)
676 UnoRuntime.queryInterface(XDocumentRepository.class,
677 xRS.getRDFRepository());
678 assure("xRep null", null != xRep);
680 XTextDocument xTextDoc = (XTextDocument)
681 UnoRuntime.queryInterface(XTextDocument.class, xComp);
683 XText xText = xTextDoc.getText();
685 XEnumerationAccess xEA = (XEnumerationAccess)
686 UnoRuntime.queryInterface(XEnumerationAccess.class, xText);
687 XEnumeration xEnum = xEA.createEnumeration();
689 log.println("...done");
691 log.println("Checking RDFa in loaded test document...");
693 XMetadatable xPara;
694 Statement[] stmts;
696 Statement x_FooBarLit1 = new Statement(foo, bar, mkLit("1"), null);
697 xPara = (XMetadatable) UnoRuntime.queryInterface(
698 XMetadatable.class, xEnum.nextElement());
699 stmts = xRep.getStatementRDFa(xPara);
700 assure("RDFa: 1",
701 eq(stmts, new Statement[] {
702 x_FooBarLit1
703 }));
705 Statement x_FooBarLit2 = new Statement(foo, bar, mkLit("2"), null);
706 xPara = (XMetadatable) UnoRuntime.queryInterface(
707 XMetadatable.class, xEnum.nextElement());
708 stmts = xRep.getStatementRDFa(xPara);
709 assure("RDFa: 2",
710 eq(stmts, new Statement[] {
711 x_FooBarLit2
712 }));
714 Statement x_BlankBarLit3 =
715 new Statement(blank1, bar, mkLit("3"), null);
716 xPara = (XMetadatable) UnoRuntime.queryInterface(
717 XMetadatable.class, xEnum.nextElement());
718 stmts = xRep.getStatementRDFa(xPara);
719 assure("RDFa: 3",
720 eq(stmts, new Statement[] {
721 x_BlankBarLit3
722 }));
723 XBlankNode b3 = (XBlankNode) UnoRuntime.queryInterface(
724 XBlankNode.class, stmts[0].Subject);
726 Statement x_BlankBarLit4 =
727 new Statement(blank2, bar, mkLit("4"), null);
728 xPara = (XMetadatable) UnoRuntime.queryInterface(
729 XMetadatable.class, xEnum.nextElement());
730 stmts = xRep.getStatementRDFa(xPara);
731 assure("RDFa: 4",
732 eq(stmts, new Statement[] {
733 x_BlankBarLit4
734 }));
735 XBlankNode b4 = (XBlankNode) UnoRuntime.queryInterface(
736 XBlankNode.class, stmts[0].Subject);
738 Statement x_BlankBarLit5 =
739 new Statement(blank1, bar, mkLit("5"), null);
740 xPara = (XMetadatable) UnoRuntime.queryInterface(
741 XMetadatable.class, xEnum.nextElement());
742 stmts = xRep.getStatementRDFa(xPara);
743 assure("RDFa: 5",
744 eq(stmts, new Statement[] {
745 x_BlankBarLit5
746 }));
747 XBlankNode b5 = (XBlankNode) UnoRuntime.queryInterface(
748 XBlankNode.class, stmts[0].Subject);
750 assure("RDFa: 3 != 4",
751 !b3.getStringValue().equals(b4.getStringValue()));
752 assure("RDFa: 3 == 5",
753 b3.getStringValue().equals(b5.getStringValue()));
755 Statement x_FooBarLit6 = new Statement(foo, bar, mkLit("6"), null);
756 Statement x_FooBazLit6 = new Statement(foo, baz, mkLit("6"), null);
757 xPara = (XMetadatable) UnoRuntime.queryInterface(
758 XMetadatable.class, xEnum.nextElement());
759 stmts = xRep.getStatementRDFa(xPara);
760 assure("RDFa: 6",
761 eq(stmts, new Statement[] {
762 x_FooBarLit6, x_FooBazLit6
763 }));
765 Statement x_FooBarLit7 = new Statement(foo, bar, mkLit("7"), null);
766 Statement x_FooBazLit7 = new Statement(foo, baz, mkLit("7"), null);
767 Statement x_FooFooLit7 = new Statement(foo, foo, mkLit("7"), null);
768 xPara = (XMetadatable) UnoRuntime.queryInterface(
769 XMetadatable.class, xEnum.nextElement());
770 stmts = xRep.getStatementRDFa(xPara);
771 assure("RDFa: 7",
772 eq(stmts, new Statement[] {
773 x_FooBarLit7, x_FooBazLit7, x_FooFooLit7
774 }));
776 XNode lit = mkLit("a fooish bar");
777 XNode lit_type= mkLit("a fooish bar", bar);
778 Statement x_FooBarLit = new Statement(foo, bar, lit, null);
779 Statement x_FooBarLittype = new Statement(foo, bar, lit_type, null);
781 Statement x_FooLabelLit8 =
782 new Statement(foo, rdfs_label, mkLit("8"), null);
783 xPara = (XMetadatable) UnoRuntime.queryInterface(
784 XMetadatable.class, xEnum.nextElement());
785 stmts = xRep.getStatementRDFa(xPara);
786 assure("RDFa: 8",
787 eq(stmts, new Statement[] {
788 x_FooBarLit, x_FooLabelLit8
789 }));
791 Statement x_FooLabelLit9 =
792 new Statement(foo, rdfs_label, mkLit("9"), null);
793 xPara = (XMetadatable) UnoRuntime.queryInterface(
794 XMetadatable.class, xEnum.nextElement());
795 stmts = xRep.getStatementRDFa(xPara);
796 assure("RDFa: 9",
797 eq(stmts, new Statement[] {
798 x_FooBarLit, x_FooLabelLit9
799 }));
801 Statement x_FooLabelLit10 =
802 new Statement(foo, rdfs_label, mkLit("10"), null);
803 xPara = (XMetadatable) UnoRuntime.queryInterface(
804 XMetadatable.class, xEnum.nextElement());
805 stmts = xRep.getStatementRDFa(xPara);
806 assure("RDFa: 10",
807 eq(stmts, new Statement[] {
808 x_FooBarLittype, x_FooLabelLit10
809 }));
811 Statement x_FooBarLit11
812 = new Statement(foo, bar, mkLit("11", bar), null);
813 xPara = (XMetadatable) UnoRuntime.queryInterface(
814 XMetadatable.class, xEnum.nextElement());
815 stmts = xRep.getStatementRDFa(xPara);
816 assure("RDFa: 11",
817 eq(stmts, new Statement[] {
818 x_FooBarLit11
819 }));
821 XURI xFile = URI.createNS(xContext, file, "/" + contentPath);
822 Statement x_FileBarLit12 =
823 new Statement(xFile, bar, mkLit("12"), null);
824 xPara = (XMetadatable) UnoRuntime.queryInterface(
825 XMetadatable.class, xEnum.nextElement());
826 stmts = xRep.getStatementRDFa(xPara);
827 assure("RDFa: 12",
828 eq(stmts, new Statement[] {
829 x_FileBarLit12
830 }));
832 Statement x_FooLabelLit13 =
833 new Statement(foo, rdfs_label, mkLit("13"), null);
834 xPara = (XMetadatable) UnoRuntime.queryInterface(
835 XMetadatable.class, xEnum.nextElement());
836 stmts = xRep.getStatementRDFa(xPara);
837 assure("RDFa: 13",
838 eq(stmts, new Statement[] {
839 x_FooBarLit, x_FooLabelLit13
840 }));
842 Statement x_FooLabelLit14 =
843 new Statement(foo, rdfs_label, mkLit("14"), null);
844 xPara = (XMetadatable) UnoRuntime.queryInterface(
845 XMetadatable.class, xEnum.nextElement());
846 stmts = xRep.getStatementRDFa(xPara);
847 assure("RDFa: 14",
848 eq(stmts, new Statement[] {
849 x_FooBarLit, x_FooLabelLit14
850 }));
852 xPara = (XMetadatable) UnoRuntime.queryInterface(
853 XMetadatable.class, xEnum.nextElement());
854 stmts = xRep.getStatementRDFa(xPara);
855 assure("RDFa: 15", eq(stmts, new Statement[] { } ));
857 xPara = (XMetadatable) UnoRuntime.queryInterface(
858 XMetadatable.class, xEnum.nextElement());
859 stmts = xRep.getStatementRDFa(xPara);
860 assure("RDFa: 16", eq(stmts, new Statement[] { } ));
862 xPara = (XMetadatable) UnoRuntime.queryInterface(
863 XMetadatable.class, xEnum.nextElement());
864 stmts = xRep.getStatementRDFa(xPara);
865 assure("RDFa: 17", eq(stmts, new Statement[] { } ));
867 xPara = (XMetadatable) UnoRuntime.queryInterface(
868 XMetadatable.class, xEnum.nextElement());
869 stmts = xRep.getStatementRDFa(xPara);
870 assure("RDFa: 18", eq(stmts, new Statement[] { } ));
872 xPara = (XMetadatable) UnoRuntime.queryInterface(
873 XMetadatable.class, xEnum.nextElement());
874 stmts = xRep.getStatementRDFa(xPara);
875 assure("RDFa: 19", eq(stmts, new Statement[] { } ));
877 xPara = (XMetadatable) UnoRuntime.queryInterface(
878 XMetadatable.class, xEnum.nextElement());
879 stmts = xRep.getStatementRDFa(xPara);
880 assure("RDFa: 20", eq(stmts, new Statement[] { } ));
882 xPara = (XMetadatable) UnoRuntime.queryInterface(
883 XMetadatable.class, xEnum.nextElement());
884 stmts = xRep.getStatementRDFa(xPara);
885 assure("RDFa: 21", eq(stmts, new Statement[] { } ));
887 log.println("...done");
889 } catch (Exception e) {
890 report(e);
891 close(xComp);
893 return xComp;
897 // utilities -------------------------------------------------------------
899 public void report2(Exception e)
901 if (e instanceof WrappedTargetException)
903 log.println("Cause:");
904 Exception cause = (Exception)
905 (((WrappedTargetException)e).TargetException);
906 log.println(cause.toString());
907 report2(cause);
908 } else if (e instanceof WrappedTargetRuntimeException) {
909 log.println("Cause:");
910 Exception cause = (Exception)
911 (((WrappedTargetRuntimeException)e).TargetException);
912 log.println(cause.toString());
913 report2(cause);
917 public void report(Exception e) {
918 log.println("Exception occurred:");
919 e.printStackTrace((java.io.PrintWriter) log);
920 report2(e);
921 failed();
924 static void close(XComponent i_comp)
926 try {
927 XCloseable xClos = (XCloseable) UnoRuntime.queryInterface(
928 XCloseable.class, i_comp);
929 if (xClos != null) xClos.close(true);
930 } catch (Exception e) {
934 XLiteral mkLit(String i_content)
936 return Literal.create(xContext, i_content);
939 XLiteral mkLit(String i_content, XURI i_uri)
941 return Literal.createWithType(xContext, i_content, i_uri);
944 static Statement[] merge(Statement[] i_A1, Statement[] i_A2)
946 // bah, java sucks...
947 Statement[] ret = new Statement[i_A1.length + i_A2.length];
948 for (int i = 0; i < i_A1.length; ++i) {
949 ret[i] = i_A1[i];
951 for (int i = 0; i < i_A2.length; ++i) {
952 ret[i+i_A1.length] = i_A2[i];
954 return ret;
957 public static String toS(XNode n) {
958 if (null == n) return "< null >";
959 return n.getStringValue();
962 static boolean isBlank(XNode i_node)
964 XBlankNode blank = (XBlankNode) UnoRuntime.queryInterface(
965 XBlankNode.class, i_node);
966 return blank != null;
970 static class Statement implements XStatement
972 XResource m_Subject;
973 XResource m_Predicate;
974 XNode m_Object;
975 XURI m_Graph;
977 Statement(XResource i_Subject, XResource i_Predicate, XNode i_Object,
978 XURI i_Graph)
980 m_Subject = i_Subject;
981 m_Predicate = i_Predicate;
982 m_Object = i_Object;
983 m_Graph = i_Graph;
986 public XResource getSubject() { return m_Subject; }
987 public XResource getPredicate() { return m_Predicate; }
988 public XNode getObject() { return m_Object; }
989 public XURI getGraph() { return m_Graph; }
993 static Statement[] toSeq(XEnumeration i_Enum) throws Exception
995 java.util.Collection c = new java.util.Vector();
996 while (i_Enum.hasMoreElements()) {
997 Statement s = (Statement) i_Enum.nextElement();
998 //log.println("toSeq: " + s.getSubject().getStringValue() + " " + s.getPredicate().getStringValue() + " " + s.getObject().getStringValue() + ".");
999 c.add(s);
1001 // return (Statement[]) c.toArray();
1002 // java sucks
1003 Object[] arr = c.toArray();
1004 Statement[] ret = new Statement[arr.length];
1005 for (int i = 0; i < arr.length; ++i) {
1006 ret[i] = (Statement) arr[i];
1008 return ret;
1011 static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception
1013 java.util.Collection c = new java.util.Vector();
1014 while (i_Enum.hasMoreElements()) {
1015 XNode[] s = (XNode[]) i_Enum.nextElement();
1016 c.add(s);
1018 // return (XNode[][]) c.toArray();
1019 Object[] arr = c.toArray();
1020 XNode[][] ret = new XNode[arr.length][];
1021 for (int i = 0; i < arr.length; ++i) {
1022 ret[i] = (XNode[]) arr[i];
1024 return ret;
1027 static class BindingComp implements java.util.Comparator
1029 public int compare(Object i_Left, Object i_Right)
1031 XNode[] left = (XNode[]) i_Left;
1032 XNode[] right = (XNode[]) i_Right;
1033 if (left.length != right.length) throw new RuntimeException();
1034 for (int i = 0; i < left.length; ++i) {
1035 int eq = (left[i].getStringValue().compareTo(
1036 right[i].getStringValue()));
1037 if (eq != 0) return eq;
1039 return 0;
1043 static class StmtComp implements java.util.Comparator
1045 public int compare(Object i_Left, Object i_Right)
1047 int eq;
1048 Statement left = (Statement) i_Left;
1049 Statement right = (Statement) i_Right;
1050 if ((eq = cmp(left.Graph, right.Graph )) != 0) return eq;
1051 if ((eq = cmp(left.Subject, right.Subject )) != 0) return eq;
1052 if ((eq = cmp(left.Predicate, right.Predicate)) != 0) return eq;
1053 if ((eq = cmp(left.Object, right.Object )) != 0) return eq;
1054 return 0;
1057 public int cmp(XNode i_Left, XNode i_Right)
1059 if (isBlank(i_Left)) {
1060 return isBlank(i_Right) ? 0 : 1;
1061 } else {
1062 if (isBlank(i_Right)) {
1063 return -1;
1064 } else {
1065 return toS(i_Left).compareTo(toS(i_Right));
1071 static boolean eq(Statement i_Left, Statement i_Right)
1073 XURI lG = i_Left.Graph;
1074 XURI rG = i_Right.Graph;
1075 if (!eq(lG, rG)) {
1076 log.println("Graphs differ: " + toS(lG) + " != " + toS(rG));
1077 return false;
1079 if (!eq(i_Left.Subject, i_Right.Subject)) {
1080 log.println("Subjects differ: " +
1081 i_Left.Subject.getStringValue() + " != " +
1082 i_Right.Subject.getStringValue());
1083 return false;
1085 if (!eq(i_Left.Predicate, i_Right.Predicate)) {
1086 log.println("Predicates differ: " +
1087 i_Left.Predicate.getStringValue() + " != " +
1088 i_Right.Predicate.getStringValue());
1089 return false;
1091 if (!eq(i_Left.Object, i_Right.Object)) {
1092 log.println("Objects differ: " +
1093 i_Left.Object.getStringValue() + " != " +
1094 i_Right.Object.getStringValue());
1095 return false;
1097 return true;
1100 static boolean eq(Statement[] i_Result, Statement[] i_Expected)
1102 if (i_Result.length != i_Expected.length) {
1103 log.println("eq: different lengths: " + i_Result.length + " " +
1104 i_Expected.length);
1105 return false;
1107 Statement[] expected = (Statement[])
1108 java.util.Arrays.asList(i_Expected).toArray();
1109 java.util.Arrays.sort(i_Result, new StmtComp());
1110 java.util.Arrays.sort(expected, new StmtComp());
1111 for (int i = 0; i < expected.length; ++i) {
1112 if (!eq(i_Result[i], expected[i])) return false;
1114 return true;
1117 static boolean eq(XEnumeration i_Enum, Statement[] i_Expected)
1118 throws Exception
1120 Statement[] current = toSeq(i_Enum);
1121 return eq(current, i_Expected);
1124 static boolean eq(XNode i_Left, XNode i_Right)
1126 if (i_Left == null) {
1127 return (i_Right == null);
1128 } else {
1129 return (i_Right != null) &&
1130 (i_Left.getStringValue().equals(i_Right.getStringValue())
1131 // FIXME: hack: blank nodes considered equal
1132 || (isBlank(i_Left) && isBlank(i_Right)));
1136 static boolean eq(XQuerySelectResult i_Result,
1137 String[] i_Vars, XNode[][] i_Bindings) throws Exception
1139 String[] vars = (String[]) i_Result.getBindingNames();
1140 XEnumeration iter = (XEnumeration) i_Result;
1141 XNode[][] bindings = toSeqs(iter);
1142 if (vars.length != i_Vars.length) {
1143 log.println("var lengths differ");
1144 return false;
1146 if (bindings.length != i_Bindings.length) {
1147 log.println("binding lengths differ: " + i_Bindings.length +
1148 " vs " + bindings.length );
1149 return false;
1151 java.util.Arrays.sort(bindings, new BindingComp());
1152 java.util.Arrays.sort(i_Bindings, new BindingComp());
1153 for (int i = 0; i < i_Bindings.length; ++i) {
1154 if (i_Bindings[i].length != i_Vars.length) {
1155 log.println("TEST ERROR!");
1156 throw new Exception();
1158 if (bindings[i].length != i_Vars.length) {
1159 log.println("binding length and var length differ");
1160 return false;
1162 for (int j = 0; j < i_Vars.length; ++j) {
1163 if (!eq(bindings[i][j], i_Bindings[i][j])) {
1164 log.println("bindings differ: " +
1165 toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j]));
1166 return false;
1170 for (int i = 0; i < i_Vars.length; ++i) {
1171 if (!vars[i].equals(i_Vars[i])) {
1172 log.println("variable names differ: " +
1173 vars[i] + " != " + i_Vars[i]);
1174 return false;
1177 return true;
1180 static boolean eq(StringPair i_Left, StringPair i_Right)
1182 return ((i_Left.First).equals(i_Right.First)) &&
1183 ((i_Left.Second).equals(i_Right.Second));
1186 static String mkNamespace(String i_prefix, String i_namespace)
1188 return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n";
1191 static String mkNss()
1193 String namespaces = mkNamespace("rdf",
1194 "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
1195 namespaces += mkNamespace("pkg",
1196 "http://docs.oasis-open.org/opendocument/meta/package/common#");
1197 namespaces += mkNamespace("odf",
1198 "http://docs.oasis-open.org/opendocument/meta/package/odf#");
1199 return namespaces;
1202 Statement[] getManifestStmts(XURI xBaseURI) throws Exception
1204 XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(),
1205 manifestPath);
1206 XURI xContent = URI.createNS(xContext, xBaseURI.getStringValue(),
1207 contentPath);
1208 XURI xStyles = URI.createNS(xContext, xBaseURI.getStringValue(),
1209 stylesPath);
1210 Statement xM_BaseTypeDoc =
1211 new Statement(xBaseURI, rdf_type, pkg_Document, xManifest);
1212 Statement xM_BaseHaspartContent =
1213 new Statement(xBaseURI, pkg_hasPart, xContent, xManifest);
1214 Statement xM_BaseHaspartStyles =
1215 new Statement(xBaseURI, pkg_hasPart, xStyles, xManifest);
1216 Statement xM_ContentTypeContent =
1217 new Statement(xContent, rdf_type, odf_ContentFile, xManifest);
1218 Statement xM_StylesTypeStyles =
1219 new Statement(xStyles, rdf_type, odf_StylesFile, xManifest);
1220 return new Statement[] {
1221 xM_BaseTypeDoc, xM_BaseHaspartContent, xM_BaseHaspartStyles,
1222 xM_ContentTypeContent, xM_StylesTypeStyles
1226 Statement[] getMetadataFileStmts(XURI xBaseURI, String Path)
1227 throws Exception
1229 XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(),
1230 manifestPath);
1231 XURI xGraph = URI.createNS(xContext, xBaseURI.getStringValue(), Path);
1232 Statement xM_BaseHaspartGraph =
1233 new Statement(xBaseURI, pkg_hasPart, xGraph, xManifest);
1234 Statement xM_GraphTypeMetadata =
1235 new Statement(xGraph, rdf_type, pkg_MetadataFile, xManifest);
1236 return new Statement[] { xM_BaseHaspartGraph, xM_GraphTypeMetadata };
1239 class TestRange implements XTextRange, XMetadatable, XServiceInfo
1241 String m_Stream;
1242 String m_XmlId;
1243 String m_Text;
1244 TestRange(String i_Str) { m_Text = i_Str; }
1246 public String getStringValue() { return ""; }
1247 public String getNamespace() { return ""; }
1248 public String getLocalName() { return ""; }
1250 public StringPair getMetadataReference()
1251 { return new StringPair(m_Stream, m_XmlId); }
1252 public void setMetadataReference(StringPair i_Ref)
1253 throws IllegalArgumentException
1254 { m_Stream = (String)i_Ref.First; m_XmlId = (String)i_Ref.Second; }
1255 public void ensureMetadataReference()
1256 { m_Stream = "content.xml"; m_XmlId = "42"; }
1258 public String getImplementationName() { return null; }
1259 public String[] getSupportedServiceNames() { return null; }
1260 public boolean supportsService(String i_Svc)
1261 { return i_Svc.equals("com.sun.star.text.Paragraph"); }
1263 public XText getText() { return null; }
1264 public XTextRange getStart() { return null; }
1265 public XTextRange getEnd() { return null; }
1266 public String getString() { return m_Text; }
1267 public void setString(String i_Str) { m_Text = i_Str; }