summaryrefslogtreecommitdiff
path: root/sfx2/qa/complex/sfx2/DocumentMetadataAccess.java
blob: 8f7071048fc3fa0e4a53d10ba164bd4c8c762a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

package complex.sfx2;

// import complexlib.ComplexTestCase;
import com.sun.star.beans.Pair;
import com.sun.star.rdf.Literal;
import com.sun.star.rdf.XLiteral;
import com.sun.star.rdf.XNamedGraph;
import com.sun.star.rdf.BlankNode;
import com.sun.star.rdf.XQuerySelectResult;
import com.sun.star.rdf.XNode;
import com.sun.star.rdf.XDocumentRepository;
import com.sun.star.rdf.XMetadatable;
import com.sun.star.rdf.Statement;
import com.sun.star.rdf.FileFormat;
import com.sun.star.rdf.URIs;
import com.sun.star.rdf.URI;
import com.sun.star.rdf.XDocumentMetadataAccess;
import com.sun.star.rdf.XRepositorySupplier;
import com.sun.star.rdf.XRepository;
import com.sun.star.rdf.XBlankNode;
import com.sun.star.rdf.XURI;
import helper.StreamSimulator;

import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XComponent;

import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.WrappedTargetRuntimeException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.StringPair;
import com.sun.star.container.XEnumerationAccess;
import com.sun.star.container.XEnumeration;
import com.sun.star.io.XInputStream;
import com.sun.star.util.XCloseable;
import com.sun.star.frame.XStorable;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XText;
import complex.sfx2.tools.TestDocument;
import lib.TestParameters;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openoffice.test.OfficeConnection;
import static org.junit.Assert.*;

/**
 * Test case for interface com.sun.star.rdf.XDocumentMetadataAccess
 * Currently, this service is implemented in
 * sfx2/source/doc/DocumentMetadataAccess.cxx
 *
 * Actually, this is not a service, so we need to create a document and
 * go from there...
 *
 * @author mst
 */
public class DocumentMetadataAccess
{
    XMultiServiceFactory xMSF;
    XComponentContext xContext;
    String tempDir;

    String nsRDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    String nsRDFS = "http://www.w3.org/2000/01/rdf-schema#";
    String nsPkg="http://docs.oasis-open.org/opendocument/meta/package/common#";
    String nsODF ="http://docs.oasis-open.org/opendocument/meta/package/odf#";

    XURI foo;
    XURI bar;
    XURI baz;

    static XURI rdf_type;
    static XURI rdfs_label;
    static XURI pkg_Document;
    static XURI pkg_hasPart;
    static XURI pkg_MetadataFile;
    static XURI odf_ContentFile;
    static XURI odf_StylesFile;
    static XURI odf_Element;
    static XBlankNode blank1;
    static XBlankNode blank2;
    static XBlankNode blank3;
    static XBlankNode blank4;
    static String manifestPath = "manifest.rdf";
    static String contentPath = "content.xml";
    static String stylesPath = "styles.xml";
    static String fooPath = "foo.rdf";
    static String fooBarPath = "meta/foo/bar.rdf";

    XRepository xRep;
    XRepositorySupplier xRS;
    XDocumentMetadataAccess xDMA;

    /**
     * The test parameters
     */
    private static TestParameters param = null;

    @Before public void before()
    {
        try {

            xMSF = getMSF();
            param = new TestParameters();
            param.put("ServiceFactory", xMSF);  // important for param.getMSF()

            assertNotNull("could not create MultiServiceFactory.", xMSF);
            XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xMSF);
            Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext");
            xContext = UnoRuntime.queryInterface(XComponentContext.class, defaultCtx);
            assertNotNull("could not get component context.", xContext);

            tempDir = util.utils.getOfficeTemp/*Dir*/(xMSF);
            System.out.println("tempdir: " + tempDir);

            foo = URI.create(xContext, "uri:foo");
            assertNotNull("foo", foo);
            bar = URI.create(xContext, "uri:bar");
            assertNotNull("bar", bar);
            baz = URI.create(xContext, "uri:baz");
            assertNotNull("baz", baz);

            blank1 = BlankNode.create(xContext, "_:1");
            assertNotNull("blank1", blank1);
            blank2 = BlankNode.create(xContext, "_:2");
            assertNotNull("blank2", blank2);
            blank3 = BlankNode.create(xContext, "_:3");
            assertNotNull("blank3", blank3);
            blank4 = BlankNode.create(xContext, "_:4");
            assertNotNull("blank4", blank4);
            rdf_type = URI.createKnown(xContext, URIs.RDF_TYPE);
            assertNotNull("rdf_type", rdf_type);
            rdfs_label = URI.createKnown(xContext, URIs.RDFS_LABEL);
            assertNotNull("rdfs_label", rdfs_label);
            pkg_Document = URI.createKnown(xContext, URIs.PKG_DOCUMENT);
            assertNotNull("pkg_Document", pkg_Document);
            pkg_hasPart = URI.createKnown(xContext, URIs.PKG_HASPART);
            assertNotNull("pkg_hasPart", pkg_hasPart);
            pkg_MetadataFile = URI.createKnown(xContext, URIs.PKG_METADATAFILE);
            assertNotNull("pkg_MetadataFile", pkg_MetadataFile);
            odf_ContentFile = URI.createKnown(xContext, URIs.ODF_CONTENTFILE);
            assertNotNull("odf_ContentFile", odf_ContentFile);
            odf_StylesFile = URI.createKnown(xContext, URIs.ODF_STYLESFILE);
            assertNotNull("odf_StylesFile", odf_StylesFile);
            odf_Element = URI.createKnown(xContext, URIs.ODF_ELEMENT);
            assertNotNull("odf_Element", odf_Element);

        } catch (Exception e) {
            report(e);
        }
    }

    @After public void after()
    {
        xRep = null;
        xRS  = null;
        xDMA = null;
    }

    @Test public void check()
    {
        XComponent xComp = null;
        XComponent xComp2 = null;
        try {
            XEnumeration xStmtsEnum;
            XNamedGraph xManifest;

            System.out.println("Creating document with Repository...");

            // we cannot create a XDMA directly, we must create
            // a document and get it from there :(
            // create document
            PropertyValue[] loadProps = new PropertyValue[1];
            loadProps[0] = new PropertyValue();
            loadProps[0].Name = "Hidden";
            loadProps[0].Value = true;
            xComp = util.DesktopTools.openNewDoc(xMSF, "swriter", loadProps);
            XTextDocument xText = UnoRuntime.queryInterface(XTextDocument.class, xComp);

            XRepositorySupplier xRepoSupplier = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp);
            assertNotNull("xRS null", xRepoSupplier);
            XDocumentMetadataAccess xDocMDAccess = UnoRuntime.queryInterface(XDocumentMetadataAccess.class, xRepoSupplier);
            assertNotNull("xDMA null", xDocMDAccess);
            xRep = xRepoSupplier.getRDFRepository();
            assertNotNull("xRep null", xRep);

            System.out.println("...done");

            System.out.println("Checking that new repository is initialized...");

            XURI xBaseURI = (XURI) xDocMDAccess;
            String baseURI = xBaseURI.getStringValue();
            assertNotNull("new: baseURI", xBaseURI );
            assertTrue("new: baseURI", !xBaseURI.getStringValue().equals(""));

            assertTrue("new: # graphs", 1 == xRep.getGraphNames().length);
            XURI manifest = URI.createNS(xContext, xBaseURI.getStringValue(),
                manifestPath);
            xManifest = xRep.getGraph(manifest);
            assertTrue("new: manifest graph", null != xManifest);

            Statement[] manifestStmts = getManifestStmts(xBaseURI);
            xStmtsEnum = xRep.getStatements(null, null, null);
            assertTrue("new: manifest graph", eq(xStmtsEnum, manifestStmts));

            System.out.println("...done");

            System.out.println("Checking some invalid args...");

            String content = "behold, for i am the content.";
            XTextRange xTR = new TestRange(content);
            XMetadatable xM = (XMetadatable) xTR;

            try {
                xDocMDAccess.getElementByURI(null);
                fail("getElementByURI: null allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.getMetadataGraphsWithType(null);
                fail("getMetadataGraphsWithType: null URI allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("", new XURI[0]);
                fail("addMetadataFile: empty filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("/foo", new XURI[0]);
                fail("addMetadataFile: absolute filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("fo\"o", new XURI[0]);
                fail("addMetadataFile: invalid filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("../foo", new XURI[0]);
                fail("addMetadataFile: filename with .. allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("foo/../../bar", new XURI[0]);
                fail("addMetadataFile: filename with nest .. allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("foo/././bar", new XURI[0]);
                fail("addMetadataFile: filename with nest . allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("content.xml", new XURI[0]);
                fail("addMetadataFile: content.xml allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("styles.xml", new XURI[0]);
                fail("addMetadataFile: styles.xml allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("meta.xml", new XURI[0]);
                fail("addMetadataFile: meta.xml allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addMetadataFile("settings.xml", new XURI[0]);
                fail("addMetadataFile: settings.xml allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, null, "foo",
                    foo, new XURI[0]);
                fail("importMetadataFile: null stream allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }

            final String sEmptyRDF = TestDocument.getUrl("empty.rdf");
            try {
                XInputStream xFooIn = new StreamSimulator(sEmptyRDF, true, param);
                xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, xFooIn, "",
                    foo, new XURI[0]);
                fail("importMetadataFile: empty filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                XInputStream xFooIn =
                    new StreamSimulator(sEmptyRDF, true, param);
                xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, xFooIn, "meta.xml",
                    foo, new XURI[0]);
                fail("importMetadataFile: meta.xml filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                XInputStream xFooIn =
                    new StreamSimulator(sEmptyRDF, true, param);
                xDocMDAccess.importMetadataFile(FileFormat.RDF_XML,
                    xFooIn, "foo", null, new XURI[0]);
                fail("importMetadataFile: null base URI allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                XInputStream xFooIn =
                    new StreamSimulator(sEmptyRDF, true, param);
                xDocMDAccess.importMetadataFile(FileFormat.RDF_XML,
                    xFooIn, "foo", rdf_type, new XURI[0]);
                fail("importMetadataFile: non-absolute base URI allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.removeMetadataFile(null);
                fail("removeMetadataFile: null URI allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addContentOrStylesFile("");
                fail("addContentOrStylesFile: empty filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addContentOrStylesFile("/content.xml");
                fail("addContentOrStylesFile: absolute filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.addContentOrStylesFile("foo.rdf");
                fail("addContentOrStylesFile: invalid filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.removeContentOrStylesFile("");
                fail("removeContentOrStylesFile: empty filename allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.loadMetadataFromStorage(null, foo, null);
                fail("loadMetadataFromStorage: null storage allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.storeMetadataToStorage(null/*, base*/);
                fail("storeMetadataToStorage: null storage allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.loadMetadataFromMedium(new PropertyValue[0]);
                fail("loadMetadataFromMedium: empty medium allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }
            try {
                xDocMDAccess.storeMetadataToMedium(new PropertyValue[0]);
                fail("storeMetadataToMedium: empty medium allowed");
            } catch (IllegalArgumentException e) {
                // ignore
            }

            System.out.println("...done");

            System.out.println("Checking file addition/removal...");

            xDocMDAccess.removeContentOrStylesFile(contentPath);
            xStmtsEnum = xManifest.getStatements(null, null, null);
            assertTrue("removeContentOrStylesFile (content)",
                eq(xStmtsEnum, new Statement[] {
                        manifestStmts[0], manifestStmts[2], manifestStmts[4]
                    }));

            xDocMDAccess.addContentOrStylesFile(contentPath);
            xStmtsEnum = xManifest.getStatements(null, null, null);
            assertTrue("addContentOrStylesFile (content)",
                eq(xStmtsEnum, manifestStmts));

            xDocMDAccess.removeContentOrStylesFile(stylesPath);
            xStmtsEnum = xManifest.getStatements(null, null, null);
            assertTrue("removeContentOrStylesFile (styles)",
                eq(xStmtsEnum, new Statement[] {
                        manifestStmts[0], manifestStmts[1], manifestStmts[3]
                    }));

            xDocMDAccess.addContentOrStylesFile(stylesPath);
            xStmtsEnum = xManifest.getStatements(null, null, null);
            assertTrue("addContentOrStylesFile (styles)",
                eq(xStmtsEnum, manifestStmts));

            XURI xFoo = URI.createNS(xContext, xBaseURI.getStringValue(),
                fooPath);
            Statement xM_BaseHaspartFoo =
                new Statement(xBaseURI, pkg_hasPart, xFoo, manifest);
            Statement xM_FooTypeMetadata =
                new Statement(xFoo, rdf_type, pkg_MetadataFile, manifest);
            Statement xM_FooTypeBar =
                new Statement(xFoo, rdf_type, bar, manifest);
            xDocMDAccess.addMetadataFile(fooPath, new XURI[] { bar });
            xStmtsEnum = xManifest.getStatements(null, null, null);
            assertTrue("addMetadataFile",
                eq(xStmtsEnum, merge(manifestStmts, new Statement[] {
                        xM_BaseHaspartFoo, xM_FooTypeMetadata, xM_FooTypeBar
                    })));

            XURI[] graphsBar = xDocMDAccess.getMetadataGraphsWithType(bar);
            assertTrue("getMetadataGraphsWithType",
                graphsBar.length == 1 && eq(graphsBar[0], xFoo));


            xDocMDAccess.removeMetadataFile(xFoo);
            xStmtsEnum = xManifest.getStatements(null, null, null);
            assertTrue("removeMetadataFile",
                eq(xStmtsEnum, manifestStmts));

            System.out.println("...done");

            System.out.println("Checking mapping...");

            XEnumerationAccess xTextEnum = UnoRuntime.queryInterface(XEnumerationAccess.class, xText.getText());
            Object o = xTextEnum.createEnumeration().nextElement();
            XMetadatable xMeta1 = UnoRuntime.queryInterface(XMetadatable.class, o);

            XURI uri;
            XMetadatable xMeta;
            xMeta = xDocMDAccess.getElementByURI(xMeta1);
            assertTrue("getElementByURI: null", null != xMeta);
            String XmlId = xMeta.getMetadataReference().Second;
            String XmlId1 = xMeta1.getMetadataReference().Second;
            assertTrue("getElementByURI: no xml id", !XmlId.equals(""));
            assertTrue("getElementByURI: different xml id", XmlId.equals(XmlId1));

            System.out.println("...done");

            System.out.println("Checking storing and loading...");

            XURI xFoobar = URI.createNS(xContext, xBaseURI.getStringValue(),
                fooBarPath);
            Statement[] metadataStmts = getMetadataFileStmts(xBaseURI,
                fooBarPath);
            xDocMDAccess.addMetadataFile(fooBarPath, new XURI[0]);
            xStmtsEnum = xRep.getStatements(null, null, null);
            assertTrue("addMetadataFile",
                eq(xStmtsEnum, merge(manifestStmts, metadataStmts )));

            Statement xFoobar_FooBarFoo =
                new Statement(foo, bar, foo, xFoobar);
            xRep.getGraph(xFoobar).addStatement(foo, bar, foo);
            xStmtsEnum = xRep.getStatements(null, null, null);
            assertTrue("addStatement",
                eq(xStmtsEnum, merge(manifestStmts, merge(metadataStmts,
                    new Statement[] { xFoobar_FooBarFoo }))));

            PropertyValue noMDNoContentFile = new PropertyValue();
            noMDNoContentFile.Name = "URL";
            noMDNoContentFile.Value = TestDocument.getUrl("CUSTOM.odt");
            PropertyValue noMDFile = new PropertyValue();
            noMDFile.Name = "URL";
            noMDFile.Value = TestDocument.getUrl("TEST.odt");
            PropertyValue file = new PropertyValue();
            file.Name = "URL";
            file.Value = tempDir + "TESTDMA.odt";
            PropertyValue mimetype = new PropertyValue();
            mimetype.Name = "MediaType";
            mimetype.Value = "application/vnd.oasis.opendocument.text";
            PropertyValue[] argsEmptyNoContent = { mimetype, noMDNoContentFile};
            PropertyValue[] argsEmpty = { mimetype, noMDFile };
            PropertyValue[] args = { mimetype, file };

            xStmtsEnum = xRep.getStatements(null, null, null);
            XURI[] graphs = xRep.getGraphNames();

            xDocMDAccess.storeMetadataToMedium(args);

            // this should re-init
            xDocMDAccess.loadMetadataFromMedium(argsEmptyNoContent);
            xRep = xRepoSupplier.getRDFRepository();
            assertTrue("xRep null", null != xRep);
            assertTrue("baseURI still tdoc?",
                !baseURI.equals(xDocMDAccess.getStringValue()));
            Statement[] manifestStmts2 = getManifestStmts((XURI) xDocMDAccess);
            xStmtsEnum = xRep.getStatements(null, null, null);
            // there is no content or styles file in here, so we have just
            // the package stmt
            assertTrue("loadMetadataFromMedium (no metadata, no content)",
                eq(xStmtsEnum, new Statement[] { manifestStmts2[0] }));

            // this should re-init
            xDocMDAccess.loadMetadataFromMedium(argsEmpty);
            xRep = xRepoSupplier.getRDFRepository();
            assertTrue("xRep null", null != xRep);
            assertTrue("baseURI still tdoc?",
                !baseURI.equals(xDocMDAccess.getStringValue()));
            Statement[] manifestStmts3 = getManifestStmts((XURI) xDocMDAccess);

            xStmtsEnum = xRep.getStatements(null, null, null);
            assertTrue("loadMetadataFromMedium (no metadata)",
                eq(xStmtsEnum, manifestStmts3));

            xDocMDAccess.loadMetadataFromMedium(args);
            xRep = xRepoSupplier.getRDFRepository();
            assertTrue("xRep null", null != xRep);
            Statement[] manifestStmts4 = getManifestStmts((XURI) xDocMDAccess);
            Statement[] metadataStmts4 = getMetadataFileStmts((XURI) xDocMDAccess,
                fooBarPath);

            xStmtsEnum = xRep.getStatements(null, null, null);
            assertTrue("some graph(s) not reloaded",
                graphs.length == xRep.getGraphNames().length);

            XURI xFoobar4 = URI.createNS(xContext, xDocMDAccess.getStringValue(),
                fooBarPath);
            Statement xFoobar_FooBarFoo4 =
                new Statement(foo, bar, foo, xFoobar4);
            assertTrue("loadMetadataFromMedium (re-load)",
                eq(xStmtsEnum, merge(manifestStmts4, merge(metadataStmts4,
                        new Statement[] { xFoobar_FooBarFoo4 }))));

            System.out.println("...done");

            System.out.println("Checking storing and loading via model...");

            String f = tempDir + "TESTPARA.odt";

            XStorable xStor = UnoRuntime.queryInterface(XStorable.class, xRepoSupplier);

            xStor.storeToURL(f, new PropertyValue[0]);

            xComp2 = util.DesktopTools.loadDoc(xMSF, f, loadProps);

            XDocumentMetadataAccess xDMA2 = UnoRuntime.queryInterface(XDocumentMetadataAccess.class, xComp2);
            assertTrue("xDMA2 null", null != xDMA2);

            XRepositorySupplier xRS2 = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp2);
            assertTrue("xRS2 null", null != xRS2);

            XRepository xRep2 = xRS2.getRDFRepository();
            assertTrue("xRep2 null", null != xRep2);

            Statement[] manifestStmts5 = getManifestStmts((XURI) xDMA2);
            Statement[] metadataStmts5 = getMetadataFileStmts((XURI) xDMA2,
                fooBarPath);
            XURI xFoobar5 = URI.createNS(xContext, xDMA2.getStringValue(),
                fooBarPath);
            Statement xFoobar_FooBarFoo5 =
                new Statement(foo, bar, foo, xFoobar5);
            xStmtsEnum = xRep.getStatements(null, null, null);
            XEnumeration xStmtsEnum2 = xRep2.getStatements(null, null, null);
            assertTrue("load: repository differs",
                eq(xStmtsEnum2, merge(manifestStmts5, merge(metadataStmts5,
                        new Statement[] { xFoobar_FooBarFoo5 }))));

            System.out.println("...done");

        } catch (Exception e) {
            report(e);
        } finally {
            close(xComp);
            close(xComp2);
        }
    }

    @Test public void checkRDFa()
    {
        XComponent xComp = null;
        try {
            final String file = TestDocument.getUrl("TESTRDFA.odt");
            xComp = loadRDFa(file);
            if (xComp != null)
            {
                final String sNewFile = tempDir + "TESTRDFA.odt";
                storeRDFa(xComp, sNewFile);
                close(xComp);

                xComp = loadRDFa(sNewFile);
            }
        } finally {
            close(xComp);
        }
    }

    private void storeRDFa(XComponent xComp, String file)
    {
        try {

            System.out.println("Storing test document...");

            XStorable xStor = UnoRuntime.queryInterface(XStorable.class, xComp);

            xStor.storeToURL(file, new PropertyValue[0]);

            System.out.println("...done");

        } catch (Exception e) {
            report(e);
        }
    }

    private XComponent loadRDFa(String file)
    {
        XComponent xComp = null;
        try {

            System.out.println("Loading test document...");

            PropertyValue[] loadProps = new PropertyValue[1];
            loadProps[0] = new PropertyValue();
            loadProps[0].Name = "Hidden";
            loadProps[0].Value = true;



            xComp = util.DesktopTools.loadDoc(xMSF, file, loadProps);

            XRepositorySupplier xRepoSupplier = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp);
            assertTrue("xRS null", null != xRepoSupplier);

            XDocumentRepository xDocRepository = UnoRuntime.queryInterface(XDocumentRepository.class, xRepoSupplier.getRDFRepository());
            assertTrue("xRep null", null != xDocRepository);

            XTextDocument xTextDoc = UnoRuntime.queryInterface(XTextDocument.class, xComp);

            XText xText = xTextDoc.getText();

            XEnumerationAccess xEA = UnoRuntime.queryInterface(XEnumerationAccess.class, xText);
            XEnumeration xEnum = xEA.createEnumeration();

            System.out.println("...done");

            System.out.println("Checking RDFa in loaded test document...");

            XMetadatable xPara;
            Pair<Statement[], Boolean> result;

            Statement x_FooBarLit1 = new Statement(foo, bar, mkLit("1"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 1",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit1
                    }));

            Statement x_FooBarLit2 = new Statement(foo, bar, mkLit("2"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 2",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit2
                    }));

            Statement x_BlankBarLit3 =
                new Statement(blank1, bar, mkLit("3"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 3",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_BlankBarLit3
                    }));
            XBlankNode b3 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject);

            Statement x_BlankBarLit4 =
                new Statement(blank2, bar, mkLit("4"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 4",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_BlankBarLit4
                    }));
            XBlankNode b4 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject);

            Statement x_BlankBarLit5 =
                new Statement(blank1, bar, mkLit("5"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 5",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_BlankBarLit5
                    }));
            XBlankNode b5 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject);

            assertTrue("RDFa: 3 != 4",
                !b3.getStringValue().equals(b4.getStringValue()));
            assertTrue("RDFa: 3 == 5",
                 b3.getStringValue().equals(b5.getStringValue()));

            Statement x_FooBarLit6 = new Statement(foo, bar, mkLit("6"), null);
            Statement x_FooBazLit6 = new Statement(foo, baz, mkLit("6"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 6",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit6, x_FooBazLit6
                    }));

            Statement x_FooBarLit7 = new Statement(foo, bar, mkLit("7"), null);
            Statement x_FooBazLit7 = new Statement(foo, baz, mkLit("7"), null);
            Statement x_FooFooLit7 = new Statement(foo, foo, mkLit("7"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 7",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit7, x_FooBazLit7, x_FooFooLit7
                    }));

            XNode lit = mkLit("a fooish bar");
            XNode lit_type= mkLit("a fooish bar", bar);
            Statement x_FooBarLit = new Statement(foo, bar, lit, null);
            Statement x_FooBarLittype = new Statement(foo, bar, lit_type, null);

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 8",
                result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit
                    }));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 9",
                result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit
                    }));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 10",
                result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLittype
                    }));

            Statement x_FooBarLit11
                = new Statement(foo, bar, mkLit("11", bar), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 11",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit11
                    }));

            XURI xFile = URI.createNS(xContext, file, "/" + contentPath);
            Statement x_FileBarLit12 =
                new Statement(xFile, bar, mkLit("12"), null);
              xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 12",
                !result.Second &&
                eq(result.First, new Statement[] {
                        x_FileBarLit12
                    }));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 13",
                result.Second &&
                eq(result.First, new Statement[] {
                        x_FooBarLit
                    }));

            Statement x_FooLabelLit14 =
                new Statement(foo, rdfs_label, mkLit("14"), null);
            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 14",
                result.Second &&
                eq(result.First, new Statement[] {
                        /* x_FooLabelLit14 */ x_FooBarLit
                    }));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 15", eq(result.First, new Statement[] { } ));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 16", eq(result.First, new Statement[] { } ));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 17", eq(result.First, new Statement[] { } ));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 18", eq(result.First, new Statement[] { } ));

            xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 19", eq(result.First, new Statement[] { } ));

            xPara = UnoRuntime.queryInterface(
                XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 20", eq(result.First, new Statement[] { } ));

            xPara = UnoRuntime.queryInterface(
                XMetadatable.class, xEnum.nextElement());
            result = xDocRepository.getStatementRDFa(xPara);
            assertTrue("RDFa: 21", eq(result.First, new Statement[] { } ));

            System.out.println("...done");

        } catch (Exception e) {
            report(e);
            close(xComp);
        }
        return xComp;
    }


// utilities -------------------------------------------------------------

    public void report2(Exception e)
    {
        if (e instanceof WrappedTargetException)
        {
            System.out.println("Cause:");
            Exception cause = (Exception)
                (((WrappedTargetException)e).TargetException);
            System.out.println(cause.toString());
            report2(cause);
        } else if (e instanceof WrappedTargetRuntimeException) {
            System.out.println("Cause:");
            Exception cause = (Exception)
                (((WrappedTargetRuntimeException)e).TargetException);
            System.out.println(cause.toString());
            report2(cause);
        }
    }

    public void report(Exception e) {
        System.out.println("Exception occurred:");
        e.printStackTrace(System.out);
        report2(e);
        fail();
    }

    static void close(XComponent i_comp)
    {
        try {
            XCloseable xClos = UnoRuntime.queryInterface(XCloseable.class, i_comp);
            if (xClos != null)
            {
                xClos.close(true);
            }
        } catch (Exception e) {
        }
    }

    XLiteral mkLit(String i_content)
    {
        return Literal.create(xContext, i_content);
    }

    XLiteral mkLit(String i_content, XURI i_uri)
    {
        return Literal.createWithType(xContext, i_content, i_uri);
    }

    static Statement[] merge(Statement[] i_A1, Statement[] i_A2)
    {
        // bah, java sucks...
        Statement[] ret = new Statement[i_A1.length + i_A2.length];
        for (int i = 0; i < i_A1.length; ++i) {
            ret[i] = i_A1[i];
        }
        for (int i = 0; i < i_A2.length; ++i) {
            ret[i+i_A1.length] = i_A2[i];
        }
        return ret;
    }

    public static String toS(XNode n) {
        if (null == n)
        {
            return "< null >";
        }
        return n.getStringValue();
    }

    static boolean isBlank(XNode i_node)
    {
        XBlankNode blank = UnoRuntime.queryInterface(XBlankNode.class, i_node);
        return blank != null;
    }


    static Statement[] toSeq(XEnumeration i_Enum) throws Exception
    {
        java.util.Collection c = new java.util.Vector();
        while (i_Enum.hasMoreElements()) {
            Statement s = (Statement) i_Enum.nextElement();
            c.add(s);
        }
        // java sucks
        Object[] arr = c.toArray();
        Statement[] ret = new Statement[arr.length];
        for (int i = 0; i < arr.length; ++i) {
            ret[i] = (Statement) arr[i];
        }
        return ret;
    }

    static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception
    {
        java.util.Collection c = new java.util.Vector();
        while (i_Enum.hasMoreElements()) {
            XNode[] s = (XNode[]) i_Enum.nextElement();
            c.add(s);
        }
        Object[] arr = c.toArray();
        XNode[][] ret = new XNode[arr.length][];
        for (int i = 0; i < arr.length; ++i) {
            ret[i] = (XNode[]) arr[i];
        }
        return ret;
    }

    static class BindingComp implements java.util.Comparator
    {
        public int compare(Object i_Left, Object i_Right)
        {
            XNode[] left = (XNode[]) i_Left;
            XNode[] right = (XNode[]) i_Right;
            if (left.length != right.length)
            {
                throw new RuntimeException();
            }
            for (int i = 0; i < left.length; ++i) {
                int eq = (left[i].getStringValue().compareTo(
                            right[i].getStringValue()));
                if (eq != 0)
                {
                    return eq;
                }
            }
            return 0;
        }
    }

    static class StmtComp implements java.util.Comparator
    {
        public int compare(Object i_Left, Object i_Right)
        {
            int eq;
            Statement left = (Statement) i_Left;
            Statement right = (Statement) i_Right;
            if ((eq = cmp(left.Graph,     right.Graph    )) != 0) return eq;
            if ((eq = cmp(left.Subject,   right.Subject  )) != 0) return eq;
            if ((eq = cmp(left.Predicate, right.Predicate)) != 0) return eq;
            if ((eq = cmp(left.Object,    right.Object   )) != 0) return eq;
            return 0;
        }

        public int cmp(XNode i_Left, XNode i_Right)
        {
            if (isBlank(i_Left)) {
                return isBlank(i_Right) ? 0 : 1;
            } else {
                if (isBlank(i_Right)) {
                    return -1;
                } else {
                    return toS(i_Left).compareTo(toS(i_Right));
                }
            }
        }
    }

    static boolean eq(Statement i_Left, Statement i_Right)
    {
        XURI lG = i_Left.Graph;
        XURI rG = i_Right.Graph;
        if (!eq(lG, rG)) {
            System.out.println("Graphs differ: " + toS(lG) + " != " + toS(rG));
            return false;
        }
        if (!eq(i_Left.Subject, i_Right.Subject)) {
            System.out.println("Subjects differ: " +
                i_Left.Subject.getStringValue() + " != " +
                i_Right.Subject.getStringValue());
            return false;
        }
        if (!eq(i_Left.Predicate, i_Right.Predicate)) {
            System.out.println("Predicates differ: " +
                i_Left.Predicate.getStringValue() + " != " +
                i_Right.Predicate.getStringValue());
            return false;
        }
        if (!eq(i_Left.Object, i_Right.Object)) {
            System.out.println("Objects differ: " +
                i_Left.Object.getStringValue() + " != " +
                i_Right.Object.getStringValue());
            return false;
        }
        return true;
    }

    static boolean eq(Statement[] i_Result, Statement[] i_Expected)
    {
        if (i_Result.length != i_Expected.length) {
            System.out.println("eq: different lengths: " + i_Result.length + " " +
                i_Expected.length);
            return false;
        }
        Statement[] expected = (Statement[])
            java.util.Arrays.asList(i_Expected).toArray();
        java.util.Arrays.sort(i_Result, new StmtComp());
        java.util.Arrays.sort(expected, new StmtComp());
        for (int i = 0; i < expected.length; ++i)
        {
            // This is better for debug!
            final Statement a = i_Result[i];
            final Statement b = expected[i];
            final boolean cond = eq(a, b);
            if (!cond) return false;
        }
        return true;
    }

    static boolean eq(XEnumeration i_Enum, Statement[] i_Expected)
        throws Exception
    {
        Statement[] current = toSeq(i_Enum);
        return eq(current, i_Expected);
    }

    static boolean eq(XNode i_Left, XNode i_Right)
    {
        if (i_Left == null) {
            return (i_Right == null);
        } else {
            return (i_Right != null) &&
                (i_Left.getStringValue().equals(i_Right.getStringValue())
                // FIXME: hack: blank nodes considered equal
                || (isBlank(i_Left) && isBlank(i_Right)));
        }
    }

    static boolean eq(XQuerySelectResult i_Result,
            String[] i_Vars, XNode[][] i_Bindings) throws Exception
    {
        String[] vars =  i_Result.getBindingNames();
        XEnumeration iter = (XEnumeration) i_Result;
        XNode[][] bindings = toSeqs(iter);
        if (vars.length != i_Vars.length) {
            System.out.println("var lengths differ");
            return false;
        }
        if (bindings.length != i_Bindings.length) {
            System.out.println("binding lengths differ: " + i_Bindings.length +
                " vs " + bindings.length );
            return false;
        }
        java.util.Arrays.sort(bindings, new BindingComp());
        java.util.Arrays.sort(i_Bindings, new BindingComp());
        for (int i = 0; i < i_Bindings.length; ++i) {
            if (i_Bindings[i].length != i_Vars.length) {
                System.out.println("TEST ERROR!");
                throw new Exception();
            }
            if (bindings[i].length != i_Vars.length) {
                System.out.println("binding length and var length differ");
                return false;
            }
            for (int j = 0; j < i_Vars.length; ++j) {
                if (!eq(bindings[i][j], i_Bindings[i][j])) {
                    System.out.println("bindings differ: " +
                        toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j]));
                    return false;
                }
            }
        }
        for (int i = 0; i < i_Vars.length; ++i) {
            if (!vars[i].equals(i_Vars[i])) {
                System.out.println("variable names differ: " +
                    vars[i] + " != " + i_Vars[i]);
                return false;
            }
        }
        return true;
    }

    static boolean eq(StringPair i_Left, StringPair i_Right)
    {
        return ((i_Left.First).equals(i_Right.First)) &&
            ((i_Left.Second).equals(i_Right.Second));
    }

    static String mkNamespace(String i_prefix, String i_namespace)
    {
        return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n";
    }

    static String mkNss()
    {
        String namespaces = mkNamespace("rdf",
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        namespaces += mkNamespace("pkg",
            "http://docs.oasis-open.org/opendocument/meta/package/common#");
        namespaces += mkNamespace("odf",
            "http://docs.oasis-open.org/opendocument/meta/package/odf#");
        return namespaces;
    }

    Statement[] getManifestStmts(XURI xBaseURI) throws Exception
    {
        XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(),
            manifestPath);
        XURI xContent = URI.createNS(xContext, xBaseURI.getStringValue(),
            contentPath);
        XURI xStyles  = URI.createNS(xContext, xBaseURI.getStringValue(),
            stylesPath);
        Statement xM_BaseTypeDoc =
            new Statement(xBaseURI, rdf_type, pkg_Document, xManifest);
        Statement xM_BaseHaspartContent =
            new Statement(xBaseURI, pkg_hasPart, xContent, xManifest);
        Statement xM_BaseHaspartStyles =
            new Statement(xBaseURI, pkg_hasPart, xStyles, xManifest);
        Statement xM_ContentTypeContent =
            new Statement(xContent, rdf_type, odf_ContentFile, xManifest);
        Statement xM_StylesTypeStyles =
            new Statement(xStyles, rdf_type, odf_StylesFile, xManifest);
        return new Statement[] {
                xM_BaseTypeDoc, xM_BaseHaspartContent, xM_BaseHaspartStyles,
                xM_ContentTypeContent, xM_StylesTypeStyles
            };
    }

    Statement[] getMetadataFileStmts(XURI xBaseURI, String Path)
        throws Exception
    {
        XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(),
            manifestPath);
        XURI xGraph = URI.createNS(xContext, xBaseURI.getStringValue(), Path);
        Statement xM_BaseHaspartGraph =
            new Statement(xBaseURI, pkg_hasPart, xGraph, xManifest);
        Statement xM_GraphTypeMetadata =
            new Statement(xGraph, rdf_type, pkg_MetadataFile, xManifest);
        return new Statement[] { xM_BaseHaspartGraph, xM_GraphTypeMetadata };
    }

    class TestRange implements XTextRange, XMetadatable, XServiceInfo
    {
        String m_Stream;
        String m_XmlId;
        String m_Text;
        TestRange(String i_Str) { m_Text = i_Str; }

        public String getStringValue() { return ""; }
        public String getNamespace() { return ""; }
        public String getLocalName() { return ""; }

        public StringPair getMetadataReference()
            {
                return new StringPair(m_Stream, m_XmlId);
            }
        public void setMetadataReference(StringPair i_Ref)
            throws IllegalArgumentException
            {
                m_Stream = i_Ref.First;
                m_XmlId = i_Ref.Second;
            }
        public void ensureMetadataReference()
            {
                m_Stream = "content.xml";
                m_XmlId = "42";
            }

        public String getImplementationName() { return null; }
        public String[] getSupportedServiceNames() { return null; }
        public boolean supportsService(String i_Svc)
            {
                return i_Svc.equals("com.sun.star.text.Paragraph");
            }

        public XText getText() { return null; }
        public XTextRange getStart() { return null; }
        public XTextRange getEnd() { return null; }
        public String getString() { return m_Text; }
        public void setString(String i_Str) { m_Text = i_Str; }
    }



    private XMultiServiceFactory getMSF()
    {
        final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
        return xMSF1;
    }

    // setup and close connections
    @BeforeClass public static void setUpConnection() throws Exception {
        System.out.println( "------------------------------------------------------------" );
        System.out.println( "starting class: " + DocumentMetadataAccess.class.getName() );
        System.out.println( "------------------------------------------------------------" );
        connection.setUp();
    }

    @AfterClass public static void tearDownConnection()
        throws InterruptedException, com.sun.star.uno.Exception
    {
        System.out.println( "------------------------------------------------------------" );
        System.out.println( "finishing class: " + DocumentMetadataAccess.class.getName() );
        System.out.println( "------------------------------------------------------------" );
        connection.tearDown();
    }

    private static final OfficeConnection connection = new OfficeConnection();

}