Project

Profile

Help

How to connect?
Download (27.2 KB) Statistics
| Branch: | Revision:

he / src / test / nunit / SaxonNUnit / SaxonNUnit / TestCollection.cs @ 3df04590

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using Saxon.Api;
5
using NUnit.Framework;
6
using System.Diagnostics;
7

    
8
namespace SaxonNUnit
9
{
10
    class TestCollection
11
    {
12
        [Test]
13
    public void TestDirectoryNoRecurse()
14
        {
15
            try
16
            {
17
                Processor p = new Processor(true);
18
                XPathCompiler c = p.NewXPathCompiler();
19
                XPathExecutable exec = c.Compile("collection(.)!document-uri(.)");
20
                XPathSelector eval = exec.Load();
21
                eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "trax/xml/?select=*.xml"));
22
                XdmValue val = eval.Evaluate();
23
                Assert.AreEqual(5, val.Count);
24
            }
25
            catch (Exception e)
26
            {
27
                Assert.Fail(e.Message);
28
            }
29
        }
30

    
31
        [Test]
32
    public void TestDirectoryMatch()
33
        {
34
            try
35
            {
36
                Processor p = new Processor(true);
37
                XPathCompiler c = p.NewXPathCompiler();
38
                c.XPathLanguageVersion = "3.1";
39
                c.DeclareVariable(new QName("a"));
40
                c.DeclareVariable(new QName("b"));
41
                XPathExecutable exec = c.Compile("collection($a||'trax/xml/?match='||encode-for-uri($b))!document-uri(.)");
42
                XPathSelector eval = exec.Load();
43
                eval.SetVariable(new QName("a"), new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR));
44
                eval.SetVariable(new QName("b"), new XdmAtomicValue("[A-Za-z]+\\.xml"));
45
                XdmValue val = eval.Evaluate();
46
                Assert.AreEqual(5, val.Count);
47
            } catch (Exception e)
48
            {
49
                Assert.Fail(e.Message);
50
            }
51
        }
52

    
53
        [Test]
54
    public void TestDirectoryNoRecurse30()
55
        {
56
            try
57
            {
58
                Processor p = new Processor(true);
59
                XPathCompiler c = p.NewXPathCompiler();
60
                c.XPathLanguageVersion = ("3.0");
61
                XPathExecutable exec = c.Compile("collection(.)!document-uri(.)");
62
                XPathSelector eval = exec.Load();
63
                eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "trax/xml/?select=*.xml"));
64
                XdmValue val = eval.Evaluate();
65
                Assert.AreEqual(5, val.Count);
66
            }
67
            catch (Exception e)
68
            {
69
                Assert.Fail(e.Message);
70
            }
71
        }
72

    
73
        [Test]
74
    public void TestDirectoryRecurse()
75
        {
76
            try
77
            {
78
                Processor p = new Processor(true);
79
                XPathCompiler c = p.NewXPathCompiler();
80
                XPathExecutable exec = c.Compile("collection(.)!document-uri(.)");
81
                XPathSelector eval = exec.Load();
82
                eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "trax/xml/?select=*.xml;recurse=yes"));
83
                XdmValue val = eval.Evaluate();
84
                Assert.AreEqual(7, val.Count, "found7");
85
            }
86
            catch (Exception e)
87
            {
88
                Assert.Fail(e.Message);
89
            }
90
        }
91

    
92
        [Test]
93
    public void TestDirectoryWithContentType()
94
        {
95
            try
96
            {
97
                Processor p = new Processor(true);
98
                XPathCompiler c = p.NewXPathCompiler();
99
                XPathExecutable exec = c.Compile("collection(.)");
100
                XPathSelector eval = exec.Load();
101
                eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "trax/xml/?select=*.xml;recurse=yes;content-type=text/plain"));
102
                XdmValue val = eval.Evaluate();
103
                Assert.AreEqual(7, val.Count, "found7");
104
                Assert.True(val.ItemAt(0) is XdmAtomicValue, "isString");
105
            }
106
            catch (Exception e)
107
            {
108
                Assert.Fail(e.Message);
109
            }
110
        }
111

    
112
        [Test]
113
    public void TestDirectoryUriNoRecurse()
114
        {
115
            try
116
            {
117
                Processor p = new Processor(true);
118
                XPathCompiler c = p.NewXPathCompiler();
119
                XPathExecutable exec = c.Compile("uri-collection(.)");
120
                XPathSelector eval = exec.Load();
121
                eval.ContextItem = (new XdmAtomicValue(ConfigTest.DATA_DIR2 + "trax/xml/?select=*.xml"));
122
                XdmValue val = eval.Evaluate();
123
                Assert.AreEqual(5, val.Count);
124
            }
125
            catch (Exception e)
126
            {
127
                Assert.Fail(e.Message);
128
            }
129
        }
130

    
131
        [Test]
132
    public void TestDirectoryUriRecurse()
133
        {
134
            try
135
            {
136
                Processor p = new Processor(true);
137
                XPathCompiler c = p.NewXPathCompiler();
138
                XPathExecutable exec = c.Compile("uri-collection(.)");
139
                XPathSelector eval = exec.Load();
140
                eval.ContextItem = (new XdmAtomicValue(ConfigTest.DATA_DIR2 + "trax/xml/?select=*.xml;recurse=yes"));
141
                XdmValue val = eval.Evaluate();
142
                Assert.AreEqual(7, val.Count);
143
            }
144
            catch (Exception e)
145
            {
146
                Assert.Fail(e.Message);
147
            }
148
        }
149

    
150
        [Test]
151
    public void TestParallelSpeed()
152
        {
153
            try
154
            {
155
                for (int i = 0; i < 5; i++)
156
                {
157
                    speedTest(true);
158
                    speedTest(false);
159
                }
160
                TimeSpan serial = speedTest(false);
161
                Console.WriteLine("Serial processing (HE): " + serial.Milliseconds + "ms");
162
                TimeSpan parallel = speedTest(true);
163
                Console.WriteLine("Parallel processing (EE): " + parallel.Milliseconds + "ms");
164
                Assert.True(parallel < serial, "not faster!");
165
            }
166
            catch (Exception e)
167
            {
168
                Assert.Fail(e.Message);
169
            }
170
        }
171

    
172
        private TimeSpan speedTest(bool parallel)
173
        {
174
            Processor p = new Processor(parallel);
175
            Stopwatch stopwatch = Stopwatch.StartNew();
176
            TimeSpan startTime = stopwatch.Elapsed;
177

    
178
            XPathCompiler c = p.NewXPathCompiler();
179
            XPathExecutable exec = c.Compile("sum(collection(.)!count(//*))");
180
            XPathSelector eval = exec.Load();
181
            eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "collection/?select=*.xml;recurse=yes"));
182
            XdmValue val = eval.Evaluate();
183
            stopwatch.Stop();
184
            TimeSpan apiTime = stopwatch.Elapsed;
185
            Console.WriteLine("Found " + val + " nodes");
186
            return apiTime;
187
    }
188

    
189
    [Test]
190
    public void TestStable()
191
    {
192
        try
193
        {
194
            Processor p = new Processor(true);
195
            XPathCompiler c = p.NewXPathCompiler();
196
            XPathExecutable exec = c.Compile("count(collection(.)//* | collection(.)//*)");
197
            XPathSelector eval = exec.Load();
198
            eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "collection/?select=*.xml;recurse=yes;stable=yes"));
199
            XdmValue val = eval.Evaluate();
200
            Assert.AreEqual(10680, ((XdmAtomicValue)val).GetLongValue());
201
        }
202
        catch (Exception e)
203
        {
204
            Assert.Fail(e.Message);
205
        }
206
    }
207

    
208
    [Test]
209
    public void TestUnstable()
210
    {
211
        try
212
        {
213
            Processor p = new Processor(true);
214
            XPathCompiler c = p.NewXPathCompiler();
215
            XPathExecutable exec = c.Compile("count(collection(.)//* | collection(.)//*)");
216
            XPathSelector eval = exec.Load();
217
            eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "collection/?select=*.xml;recurse=yes;stable=no"));
218
            XdmValue val = eval.Evaluate();
219
            Assert.AreEqual(21360, ((XdmAtomicValue)val).GetLongValue());
220
        }
221
        catch (Exception e)
222
        {
223
            Assert.Fail(e.Message);
224
        }
225
    }
226

    
227
    [Test]
228
    public void TestStrictValidation()
229
    {
230
        try
231
        {
232
            Processor p = new Processor(true);
233
            XPathCompiler c = p.NewXPathCompiler();
234
            XPathExecutable exec = c.Compile("collection(.)/* instance of element(*, xs:untyped)");
235
            XPathSelector eval = exec.Load();
236
            eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "?select=books.xml;recurse=no;validation=strict"));
237
            XdmValue val = eval.Evaluate();
238
            Assert.AreEqual(false, ((XdmAtomicValue)val).GetBooleanValue());
239
        }
240
        catch (Exception e)
241
        {
242
            Assert.Fail(e.Message);
243
        }
244
    }
245

    
246
    [Test]
247
    public void TestNoValidation()
248
    {
249
        try
250
        {
251
            Processor p = new Processor(true);
252
            XPathCompiler c = p.NewXPathCompiler();
253
            XPathExecutable exec = c.Compile("collection(.)/* instance of element(*, xs:untyped)");
254
            XPathSelector eval = exec.Load();
255
            eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "?select=books.xml;recurse=no;validation=skip"));
256
            XdmValue val = eval.Evaluate();
257
            Assert.AreEqual(true, ((XdmAtomicValue)val).GetBooleanValue());
258
        }
259
        catch (Exception e)
260
        {
261
            Assert.Fail(e.Message);
262
        }
263
    }
264

    
265
    [Test]
266
    public void TestJSONCollection()
267
    {
268
        try
269
        {
270
            Processor p = new Processor(true);
271
            p.SetProperty(Feature<bool>.ALLOW_MULTITHREADING,false);
272
            XPathCompiler c = p.NewXPathCompiler();
273
            c.XPathLanguageVersion = "3.1";
274
            XPathExecutable exec = c.Compile("for $X in (collection(.))" +
275
                "return (if ($X instance of node()) then name($X/*) else $X('id'))");
276
            XPathSelector eval = exec.Load();
277
            eval.ContextItem = (new XdmAtomicValue("file:///" + ConfigTest.DATA_DIR + "collection.xml"));
278
            XdmValue val = eval.Evaluate();
279
            Assert.AreEqual("1.0e0\n\"BOOKLIST\"", val.ToString());
280
        }
281
        catch (Exception e)
282
        {
283
           
284
            Assert.Fail(e.Message);
285
        }
286
    }
287
       
288
        /*
289
    [Test]
290
    public void TestCustomMediaType()
291
    {
292
        try
293
        {
294
            Processor p = new Processor(true);
295
            Configuration config = p.Implementation;
296
            config.registerFileExtension("json", "personal/plain");
297
            config.registerMediaType("personal/plain", new ResourceFactory()
298
            {
299
                public Resource makeResource(Configuration config, AbstractResourceCollection.InputDetails details) throws XPathException {
300
                return new JSONResource(details);
301
            }
302
        });
303
        XPathCompiler c = p.NewXPathCompiler();
304
        c.XPathLanguageVersion = "3.1";
305
        XPathExecutable exec = c.Compile("for $X in (collection(.))" +
306
                                                 "return (if ($X instance of node()) then name($X/*) else $X('id'))");
307
        XPathSelector eval = exec.Load();
308
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection.xml"));
309
        XdmValue val = eval.Evaluate();
310
        Assert.AreEqual("1.0e0\n\"BOOKLIST\"", val.ToString());
311
    } catch (Exception e) {
312
           
313
            Assert.Fail(e.Message);
314
}
315
    }
316

    
317

    
318
    [Test]
319
    public void TestMixedCollection()
320
{
321
    try
322
    {
323
        Processor p = new Processor(true);
324
        XPathCompiler c = p.NewXPathCompiler();
325
        c.XPathLanguageVersion = "3.1";
326
        XPathExecutable exec = c.Compile("count(collection(.))");
327
        XPathSelector eval = exec.Load();
328
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection2/?select=*"));
329
        XdmValue val = eval.Evaluate();
330
        Assert.AreEqual(2, Integer.parseInt(val.ToString()));
331
    }
332
    catch (Exception e)
333
    {
334
        Assert.Fail(e.Message);
335
    }
336
}
337

    
338
[Test]
339
    public void TestMixedCollectionWithMetadata()
340
{
341
    try
342
    {
343
        Processor p = new Processor(true);
344
        XPathCompiler c = p.NewXPathCompiler();
345
        c.XPathLanguageVersion = "3.1";
346
        XPathExecutable exec = c.Compile("collection(.)");
347
        XPathSelector eval = exec.Load();
348
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection2/?select=*.xml;metadata=yes"));
349
        XdmValue val = eval.Evaluate();
350
        for (XdmItem item : val)
351
        {
352
            Assert.True(item is XdmFunctionItem);
353
            XdmFunctionItem map = (XdmFunctionItem)item;
354
            XdmAtomicValue name = (XdmAtomicValue)map.call(p, new XdmAtomicValue("canonical-path"));
355
            XdmAtomicValue length = (XdmAtomicValue)map.call(p, new XdmAtomicValue("length"));
356
            XdmAtomicValue lastModified = (XdmAtomicValue)map.call(p, new XdmAtomicValue("last-modified"));
357
            XdmAtomicValue contentType = (XdmAtomicValue)map.call(p, new XdmAtomicValue("content-type"));
358
            Assert.AreEqual("application/xml", contentType.ToString());
359
            Console.WriteLine(name + " " + lastModified + " " + length + " " + contentType);
360
        }
361
        Assert.AreEqual(1, val.Count);
362
    }
363
    catch (Exception e)
364
    {
365
        Assert.Fail(e.Message);
366
    }
367
}
368

    
369
[Test]
370
    public void TestMixedCollectionMetadataFetch()
371
{
372
    try
373
    {
374
        Processor p = new Processor(true);
375
        XPathCompiler c = p.NewXPathCompiler();
376
        c.XPathLanguageVersion = "3.1";
377
        String query = "for $m in collection(.||'?metadata=yes') " +
378
                       "return if ($m?content-type='application/xml') then $m?fetch() else ()";
379
        XPathExecutable exec = c.Compile(query);
380
        XPathSelector eval = exec.Load();
381
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection2"));
382
        XdmValue val = eval.Evaluate();
383
        foreach (XdmItem item in val)
384
        {
385
            Assert.True(item is XdmNode);
386
        }
387
        Assert.AreEqual(1, val.Count);
388
    }
389
    catch (Exception e)
390
    {
391
        Assert.Fail(e.Message);
392
    }
393
}
394

    
395

    
396
[Test]
397
    public void TestCollectionWithHttp()
398
{
399
    try
400
    {
401
        Processor p = new Processor(true);
402
        XPathCompiler c = p.NewXPathCompiler();
403
        c.XPathLanguageVersion = "3.1";
404
        XPathExecutable exec = c.Compile("count(collection(.))");
405
        XPathSelector eval = exec.Load();
406
        eval.ContextItem = (new XdmAtomicValue("https://dev.saxonica.com/tests/collection/catalog.xml"));
407
        XdmValue val = eval.Evaluate();
408
        Assert.AreEqual(2, Integer.parseInt(val.ToString()));
409
    }
410
    catch (Exception e)
411
    {
412
       
413
        Assert.Fail(e.Message);
414
    }
415
}
416

    
417
[Test]
418
    public void TestMixedWithHttpCollection()
419
{
420
    try
421
    {
422
        Processor p = new Processor(true);
423
        XQueryCompiler c = p.newXQueryCompiler();
424
        XQueryExecutable exec = c.Compile("for $X in (collection(.))" +
425
            "return if ($X instance of node()) then name($X/*) else $X('id')");
426
        XQueryEvaluator eval = exec.Load();
427
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection2.xml"));
428
        XdmValue val = eval.Evaluate();
429
        Assert.AreEqual("1.0e0\n\"html\"", val.ToString());
430
    }
431
    catch (Exception e)
432
    {
433
        Assert.Fail(e.Message);
434
    }
435
}
436

    
437
[Test]
438
    public void TestMixedWithHttpCollection2()
439
{
440
    try
441
    {
442
        Processor p = new Processor(true);
443
        XQueryCompiler c = p.newXQueryCompiler();
444
        XQueryExecutable exec = c.Compile("count(collection(.))");
445
        XQueryEvaluator eval = exec.Load();
446
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3.xml"));
447
        XdmValue val = eval.Evaluate();
448
        Assert.AreEqual(3, Integer.parseInt(val.ToString()));
449
    }
450
    catch (Exception e)
451
    {
452
        Assert.Fail(e.Message);
453
    }
454
}
455

    
456
[Test]
457
    public void TestJarCollection()
458
{
459
    try
460
    {
461
        Processor p = new Processor(true);
462
        XPathCompiler c = p.NewXPathCompiler();
463
        c.XPathLanguageVersion = "3.1";
464
        XPathExecutable exec = c.Compile("count(collection(.))");
465
        XPathSelector eval = exec.Load();
466
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3/saxon9-test.jar"));
467
        XdmValue val = eval.Evaluate();
468
        Assert.AreEqual(34, Integer.parseInt(val.ToString()));
469
    }
470
    catch (Exception e)
471
    {
472
        Assert.Fail(e.Message);
473
    }
474
}
475

    
476
[Test]
477
    public void TestJarCollection2()
478
{
479
    try
480
    {
481
        Processor p = new Processor(true);
482
        XQueryCompiler c = p.newXQueryCompiler();
483
        XQueryExecutable exec = c.Compile("for $c at $p in collection(.) return ($p || ' ' || local-name-from-QName(saxon:type-annotation($c)))");
484
        XQueryEvaluator eval = exec.Load();
485
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3/saxon9-test.jar"));
486
        XdmValue val = eval.Evaluate();
487
        Assert.True(val.ToString().contains("34 base64Binary"));
488
    }
489
    catch (Exception e)
490
    {
491
       
492
        Assert.Fail(e.Message);
493
    }
494
}
495

    
496
[Test]
497
    public void TestJarCollectionUri()
498
{
499
    try
500
    {
501
        Processor p = new Processor(true);
502
        XQueryCompiler c = p.newXQueryCompiler();
503
        XQueryExecutable exec = c.Compile("for $x at $p in uri-collection(.) return ($p || ' ' || xs:string($x))");
504
        XQueryEvaluator eval = exec.Load();
505
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3/saxon9-test.jar"));
506
        XdmValue val = eval.Evaluate();
507
        System.out.println(val.ToString());
508
        Assert.True(true, "Uri retrieved successfully: ");
509
    }
510
    catch (Exception e)
511
    {
512
        Assert.Fail(e.Message);
513
    }
514
}
515

    
516
[Test]
517
    public void TestJarCollectionUriWithOptions()
518
{
519
    try
520
    {
521
        Processor p = new Processor(true);
522
        XQueryCompiler c = p.newXQueryCompiler();
523
        XQueryExecutable exec = c.Compile("for $x in uri-collection(.) return switch(substring-after(xs:string($x), '!/META-INF/'))" +
524
            "case 'MANIFEST.MF' return true()" +
525
            "case 'SAXONICA.SF' return true()" +
526
            "case 'SAXONICA.RSA' return true()" +
527
            "default return false()");
528
        XQueryEvaluator eval = exec.Load();
529
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3/saxon9-test.jar?select=META-INF/*"));
530
        XdmValue val = eval.Evaluate();
531
        Assert.AreEqual(true, ((XdmAtomicValue)val.itemAt(1)).GetBooleanValue());
532
    }
533
    catch (Exception e)
534
    {
535
        Assert.Fail(e.Message);
536
    }
537
}
538

    
539
[Test]
540
    public void TestJarCollectionUriWithXmlOptions()
541
{
542
    try
543
    {
544
        Processor p = new Processor(true);
545
        XQueryCompiler c = p.newXQueryCompiler();
546
        XQueryExecutable exec = c.Compile("for $x in collection(.) return " +
547
            "if($x instance of node()) then true() else false()");
548
        XQueryEvaluator eval = exec.Load();
549
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3/saxon9he.jar?select=*.xsl;validation=skip"));
550
        XdmValue val = eval.Evaluate();
551
        Assert.AreEqual(true, ((XdmAtomicValue)val.itemAt(1)).GetBooleanValue());
552
    }
553
    catch (Exception e)
554
    {
555
        Assert.Fail(e.Message);
556
    }
557
}
558

    
559
[Test]
560
    public void TestJarCollectionUriWithXmlOptions2()
561
{
562
    try
563
    {
564
        Processor p = new Processor(true);
565
        XQueryCompiler c = p.newXQueryCompiler();
566
        XQueryExecutable exec = c.Compile("for $x in collection(.) return " +
567
                                                  "if($x instance of node()) then true() else false()");
568
        XQueryEvaluator eval = exec.Load();
569
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3/saxon9he.jar?select=*.xsl;validation=skip;content-type=text/plain"));
570
        XdmValue val = eval.Evaluate();
571
        Assert.AreEqual(false, ((XdmAtomicValue)val.itemAt(1)).GetBooleanValue());
572
    }
573
    catch (Exception e)
574
    {
575
        Assert.Fail(e.Message);
576
    }
577
}
578

    
579
[Test]
580
    public void TestJarMetadataCollection()
581
{
582
    try
583
    {
584
        Processor p = new Processor(true);
585
        XPathCompiler c = p.NewXPathCompiler();
586
        c.XPathLanguageVersion = "3.1";
587
        XPathExecutable exec = c.Compile("collection(.)");
588
        XPathSelector eval = exec.Load();
589
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "collection3/saxon9-test.jar?metadata=yes"));
590
        XdmValue val = eval.Evaluate();
591
        for (XdmItem item : val)
592
        {
593
            XdmFunctionItem map = (XdmFunctionItem)item;
594
            Assert.True(map.call(p, new XdmAtomicValue("entry-name")) != null);
595
            Assert.True(map.call(p, new XdmAtomicValue("last-modified")).getUnderlyingValue() is DateTimeValue);
596
        }
597

    
598
        Assert.AreEqual(34, val.Count);
599
    }
600
    catch (Exception e)
601
    {
602
        Assert.Fail(e.Message);
603
    }
604
}
605

    
606
[Test]
607
    public void TestCollectionFinder()
608
{
609
    try
610
    {
611
        Processor p = new Processor(true);
612
        Configuration config = p.Implementation;
613
        config.setCollectionFinder(new CollectionFinder()
614
        {
615
                public ResourceCollection findCollection(XPathContext context, String collectionURI) throws XPathException {
616
            return new DirectoryCollection(config, "http://example.com/collection", new File(ConfigTest.DATA_DIR + "trax/"),
617
                new URIQueryParameters("select=*.xml;recurse=yes", context.getConfiguration()));
618
        }
619
    });
620
    XQueryCompiler c = p.newXQueryCompiler();
621
    XQueryExecutable exec = c.Compile("for $x in collection(.) return " +
622
        "if($x instance of node()) then true() else false()");
623
    XQueryEvaluator eval = exec.Load();
624
    eval.ContextItem = (new XdmAtomicValue("http://example.com/collection"));
625
    XdmValue val = eval.Evaluate();
626
    Assert.AreEqual(7, val.Count);
627
} catch (Exception e) {
628
            Assert.Fail(e.Message);
629
        }
630
    }
631

    
632

    
633

    
634

    
635
    [Test]
636
    public void TestRegisterMediaType()
637
{
638
    try
639
    {
640
        Processor p = new Processor(true);
641
        final Configuration config = p.Implementation;
642
        config.registerFileExtension("java", "text/plain");
643
        config.registerFileExtension("xq", "text/plain");
644
        XQueryCompiler c = p.newXQueryCompiler();
645
        XQueryExecutable exec = c.Compile("for $x in collection(.) return " +
646
                                                  "if($x instance of xs:string) then true() else false()");
647
        XQueryEvaluator eval = exec.Load();
648
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "ganta"));
649
        XdmValue val = eval.Evaluate();
650
        Assert.AreEqual(6, val.Count);
651
        for (XdmItem item : val)
652
        {
653
            Assert.True(((XdmAtomicValue)item).GetBooleanValue(), "isText");
654
        }
655
    }
656
    catch (Exception e)
657
    {
658
        Assert.Fail(e.Message);
659
    }
660
}
661

    
662
[Test]
663
    public void TestMediaTypeConfigFile()
664
{
665
    try
666
    {
667
        String cf = "<configuration xmlns='http://saxon.sf.net/ns/configuration'\n" +
668
                "               edition='EE'\n" +
669
                "               label='Some label'>\n" +
670
                "          <resources>\n" +
671
                "            <fileExtension extension='xq' mediaType='text/plain'/>" +
672
                "            <fileExtension extension='java' mediaType='text/plain'/>" +
673
                "          </resources></configuration>";
674
        Processor p = new Processor(new StreamSource(new StringReader(cf)));
675
        XQueryCompiler c = p.newXQueryCompiler();
676
        XQueryExecutable exec = c.Compile("for $x in collection(.) return " +
677
                                                  "if($x instance of xs:string) then true() else false()");
678
        XQueryEvaluator eval = exec.Load();
679
        eval.ContextItem = (new XdmAtomicValue("file://" + ConfigTest.DATA_DIR + "ganta"));
680
        XdmValue val = eval.Evaluate();
681
        Assert.AreEqual(6, val.Count);
682
        for (XdmItem item : val)
683
        {
684
            Assert.True(((XdmAtomicValue)item).GetBooleanValue(), "isText");
685
        }
686
    }
687
    catch (Exception e)
688
    {
689
        Assert.Fail(e.Message);
690
    }
691
}
692

    
693
[Test]
694
    public void TestRegisteredCollection()
695
{
696
    try
697
    {
698
        Processor p = new Processor(true);
699
        Configuration config = p.Implementation;
700
        List<File> contents = new ArrayList<>();
701
        contents.add(new File(ConfigTest.DATA_DIR + "trax/xml/cities.xml"));
702
        contents.add(new File(ConfigTest.DATA_DIR + "trax/xml/baz.xml"));
703
        config.registerCollection("ftp://myCollection", new AbstractResourceCollection(config) {
704
                @Override
705
                public Iterator<String> getResourceURIs(XPathContext context) throws XPathException {
706
            return contents.stream().map(File::toURI).map(URI::toString).iterator();
707
        }
708

    
709
        @Override
710
                public Iterator<? extends Resource> getResources(XPathContext context) throws XPathException {
711
            return contents.stream().map(file-> {
712
                try
713
                {
714
                    StreamSource ss = new StreamSource(file);
715
                    NodeInfo doc = config.buildDocumentTree(ss).getRootNode();
716
                    return new XmlResource(config, doc);
717
                }
718
                catch (XPathException e)
719
                {
720
                    throw new UncheckedXPathException(e);
721
                }
722
            }).iterator();
723
        }
724
    });
725
    XPathCompiler c = p.NewXPathCompiler();
726
    c.XPathLanguageVersion = "3.1";
727
    XPathExecutable exec = c.Compile("count(uri-collection(.))");
728
    XPathSelector eval = exec.Load();
729
    eval.ContextItem = (new XdmAtomicValue("ftp://myCollection"));
730
    XdmValue val = eval.Evaluate();
731
    Assert.AreEqual(2, Integer.parseInt(val.ToString()));
732
    exec = c.Compile("collection(.)/* ! name()");
733
    eval = exec.Load();
734
    eval.ContextItem = (new XdmAtomicValue("ftp://myCollection"));
735
    val = eval.Evaluate();
736
    Assert.AreEqual("cities", val.itemAt(0).getStringValue(), "one");
737
    Assert.AreEqual("foo:document", val.itemAt(1).getStringValue(), "two");
738
} catch (Exception e) {
739
            Assert.Fail(e.Message);
740
        }
741
    }
742

    
743
    public static class Finder implements CollectionFinder
744
{
745
    @Override
746
        public ResourceCollection findCollection(XPathContext context, String collectionURI) throws XPathException
747
{
748
            if (collectionURI.equals("http://example.com/collection")) {
749
        File dir = new File(ConfigTest.DATA_DIR + "collection");
750
        URIQueryParameters params = new URIQueryParameters("select=*.xml", context.getConfiguration());
751
        return new DirectoryCollection(context.getConfiguration(), collectionURI, dir, params);
752
    } else {
753
        throw new XPathException("Collection " + collectionURI + " not found");
754
    }
755
}
756
    }
757

    
758
    [Test]
759
    public void TestCollectionFinderConfigFile()
760
{
761
    try
762
    {
763
        String cf = "<configuration xmlns='http://saxon.sf.net/ns/configuration'\n" +
764
                "               edition='EE'\n" +
765
                "               label='Some label'>\n" +
766
                "          <global collectionFinder='s9apitest.TestCollections$Finder'/>\n" +
767
                "    </configuration>";
768
        Processor p = new Processor(new StreamSource(new StringReader(cf)));
769
        XQueryCompiler c = p.newXQueryCompiler();
770
        XQueryExecutable exec = c.Compile("distinct-values(collection(\"http://example.com/collection\")!node-name(*))");
771
        XQueryEvaluator eval = exec.Load();
772
        XdmValue val = eval.Evaluate();
773
        Assert.AreEqual(39, val.Count);
774
    }
775
    catch (Exception e)
776
    {
777
        Assert.Fail(e.Message);
778
    }*/
779
}
780

    
781
}
(8-8/19)