File: 062xtmdeserialize.t

package info (click to toggle)
libtm-perl 1.56-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,852 kB
  • sloc: perl: 35,234; sh: 565; makefile: 47
file content (506 lines) | stat: -rw-r--r-- 13,739 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;

use Test::More qw(no_plan);

use Data::Dumper;
$Data::Dumper::Indent = 1;
use Class::Trait;
use TM;

sub _chomp {
    my $s = shift;
    chomp $s;
    return $s;
}

#==================================================================

{ # xtm explicit namespace
    my $content = q|<topicMap
   xmlns="http://www.topicmaps.org/xtm/"
   version="2.0">
  <topic id="rumsti" />
</topicMap>
|;

    my $tm2 = new TM (baseuri=>"tm://");
    Class::Trait->apply ($tm2, "TM::Serializable::XTM");
    $tm2->deserialize ($content);
#warn Dumper $tm2;
    is ($tm2->tids ('rumsti'), 'tm://rumsti', 'explicit namespace: topic found');
}

{ # xtm default namespace
    my $content = q|<topicMap
   version="2.0">
  <topic id="rumsti" />
</topicMap>
|;
    my $tm2 = new TM (baseuri=>"tm://");
    Class::Trait->apply ($tm2, "TM::Serializable::XTM");
    $tm2->deserialize ($content);
#warn Dumper $tm2;
    is ($tm2->tids ('rumsti'), 'tm://rumsti', 'default namespace: topic found');
}

__END__

{ # explicit namespace + prefix
    my $content = q|<xtm:topicMap
   xmlns:xtm="http://www.topicmaps.org/xtm/" version='2.0'
>
  <xtm:topic id="rumsti" />
</xtm:topicMap>
|;

    my $tm2 = new TM (baseuri=>"tm://");
    Class::Trait->apply ($tm2, "TM::Serializable::XTM");
    $tm2->deserialize ($content);
#warn Dumper $tm2;
    is ($tm2->tids ('rumsti'), 'tm://rumsti', 'prefixed namespace: xtm: topic found');
}

{ # explicit namespace + prefix
    my $content = q|<bamsti:topicMap
   xmlns:bamsti="http://www.topicmaps.org/xtm/" version="2.0"
>
  <bamsti:topic id="rumsti" />
</bamsti:topicMap>
|;

    my $tm2 = new TM (baseuri=>"tm://");
    Class::Trait->apply ($tm2, "TM::Serializable::XTM");
    $tm2->deserialize ($content);
#warn Dumper $tm2;
    is ($tm2->tids ('rumsti'), 'tm://rumsti', 'prefixed namespace: bamsti: topic found');
}

# eval {
#     my $content = q|<xxtopicMap xmlns='http://www.topicmaps.org/xtm/' version="2.0">
# </xxtopicMap>
# |;

#     my $tm2 = new TM (baseuri=>"tm://");
#     Class::Trait->apply ($tm2, "TM::Serializable::XTM");
#     $tm2->deserialize ($content);
#     ok (0);
# }; like ($@, qr/element/, 'topicMap missing');

#-- start with actual parsing

sub _parse {
    my $xtm = shift;
    unless ($xtm =~ /<\?/) {
	$xtm = qq|<?xml version="1.0"?>
<topicMap xmlns='http://www.topicmaps.org/xtm/'
          xmlns:xlink = 'http://www.w3.org/1999/xlink'
          version="2.0">$xtm</topicMap>
|;
    }
    my $tm = new TM (baseuri => "tm:");
    Class::Trait->apply ($tm, "TM::Serializable::XTM");
    $tm->deserialize ($xtm);
    return $tm;
}

#--

my $npa = scalar keys %{$TM::infrastructure->{assertions}};
my $npt = scalar keys %{$TM::infrastructure->{mid2iid}};

#-- empty maps, attrs

{
    my $ms = _parse (q|<?xml version="1.0"?>
<topicMap xmlns='http://www.topicmaps.org/xtm/' version="2.0">
</topicMap>
|);

#warn Dumper $ms;

    is (scalar $ms->match(), $npa, 'empty map 1 (assertions)');
    is ($ms->toplets,        $npt, 'empty map 2 (toplets)');
}

{ # topic with basename
  my $ms = _parse (q|
<topic id="aaa">
  <name>
     <value>AAA</value>
  </name>
</topic>
|);
#warn Dumper $ms;
  is (scalar $ms->match (TM->FORALL, type => 'name',        irole => 'thing',    iplayer => 'tm:aaa' ), 1, 'basenames for aaa');
}

{ # topic with typed basename
  my $ms = _parse (q|
<topic id="aaa">
  <name>
     <type><topicRef href="#bbb"/></type>
     <value>AAA</value>
  </name>
</topic>
|);
#warn Dumper $ms;
  is (scalar $ms->match (TM->FORALL, type => 'name',   irole => 'thing',    iplayer => 'tm:aaa' ), 1, 'basenames for aaa (via bbb)');
  is (scalar $ms->match (TM->FORALL, type => 'tm:bbb', irole => 'thing',    iplayer => 'tm:aaa' ), 1, 'basenames for aaa (via bbb)');
  ok ($ms->toplet ('tm:bbb'), 'found topic (implicit)');
}

{ # topic with typed, scoped basename
  my $ms = _parse (q|
<topic id="aaa">
  <name>
     <type><topicRef href="#bbb"/></type>
     <scope><topicRef href="#sss"/></scope>
     <value>AAA</value>
  </name>
</topic>
|);
#warn Dumper $ms;

  ok ($ms->toplet ('tm:sss'), 'found topic (implicit)');
  is (scalar $ms->match (TM->FORALL, type => 'tm:bbb',   irole => 'thing',  iplayer => 'tm:aaa' ), 1, 'scoped, typed basenames for aaa (via bbb)');
  is (scalar $ms->match (TM->FORALL, scope => 'tm:sss' ), 1,                                          'scoped, typed basenames for aaa (via bbb)');
}

{ # topic with resourceData occurrence
  my $ms = _parse (q|
<topic id="aaa">
  <occurrence>
     <resourceData>sldfsdlf</resourceData>
  </occurrence>
</topic>
|);
#  warn Dumper $ms;

  is (scalar $ms->match (TM->FORALL, type => 'name',           irole => 'thing',    iplayer => 'tm:aaa' ), 0, 'occ: no basenames for aaa');
  is (scalar $ms->match (TM->FORALL, type => 'occurrence',     irole => 'thing',    iplayer => 'tm:aaa' ), 1, 'occ: occurrence for aaa');

  my ($o) = $ms->match (TM->FORALL, type => 'occurrence',      irole => 'thing',    iplayer => 'tm:aaa' );
  like ($o->[TM->PLAYERS]->[1]->[0], qr/sldfsdlf/, 'occ: value');
}

{ #-- XML data as string
  my $ms = _parse (q|
<topic id="aaa">
    <occurrence>
       <resourceData>
           <x:some  xmlns:x="html">text</x:some>
           <x:some2 xmlns:x="xhtml">text2</x:some2>
       </resourceData>
    </occurrence>
</topic>
|);

#warn Dumper $ms;
  my ($o) = $ms->match (TM->FORALL, type => 'occurrence',    irole => 'thing',    iplayer => 'tm:aaa' );
  is ($o->[TM->PLAYERS]->[1]->[1], TM::Literal->STRING, 'occ: XML as string');
  like ($o->[TM->PLAYERS]->[1]->[0], qr/<x:some /,  'occ: XML value 1');
  like ($o->[TM->PLAYERS]->[1]->[0], qr/<x:some2 /, 'occ: XML value 2');
  like ($o->[TM->PLAYERS]->[1]->[0], qr/xmlns:x="xhtml"/, 'occ: XML value 3');
}

{ #-- XML data as XML
  my $ms = _parse (q|
<topic id="aaa">
    <occurrence>
       <resourceData datatype="http://www.w3.org/2001/XMLSchema#anyType">
           <x:some  xmlns:x="html">text</x:some>
           <x:some2 xmlns:x="xhtml">text2</x:some2>
       </resourceData>
    </occurrence>
</topic>
|);

#warn Dumper $ms;
  my ($o) = $ms->match (TM->FORALL, type => 'occurrence',    irole => 'thing',    iplayer => 'tm:aaa' );
  is ($o->[TM->PLAYERS]->[1]->[1], TM::Literal->ANY, 'occ: XML as XML');
  like ($o->[TM->PLAYERS]->[1]->[0], qr/<x:some /,  'occ: XML value 1');
}

{ #-- URI as resourceData
  my $ms = _parse (q|
<topic id="aaa">
    <occurrence>
       <resourceData datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://xxx.com/</resourceData>
    </occurrence>
</topic>
|);

#warn Dumper $ms;
  my ($o) = $ms->match (TM->FORALL, type => 'occurrence',    irole => 'thing',    iplayer => 'tm:aaa' );
  is ($o->[TM->PLAYERS]->[1]->[1], TM::Literal->URI, 'occ: URI');
  like ($o->[TM->PLAYERS]->[1]->[0], qr/http/,  'occ: URI');
}

{ #scoped, typed resourceData occurrence
  my $ms = _parse (q|
<topic id="aaa">
  <occurrence>
     <type><topicRef href="#bbb"/></type>
     <scope><topicRef href="#sss"/></scope>
     <resourceData>sldfsdlf</resourceData>
  </occurrence>
</topic>
|);
#warn Dumper $ms;

  ok ($ms->toplet ('tm:bbb'), 'found topic (implicit)');
  ok ($ms->toplet ('tm:sss'), 'found topic (implicit)');
  is (scalar $ms->match (TM->FORALL, type => 'tm:bbb',      irole => 'thing',    iplayer => 'tm:aaa' ), 1, 'occ: occurrence for aaa (via bbb)');
  is (scalar $ms->match (TM->FORALL, type => 'occurrence',  irole => 'thing',    iplayer => 'tm:aaa' ), 1, 'occ: occurrence for aaa');
}

{ # unscoped assoc
  my $ms = _parse (q|
<association>
  <type><topicRef href="#atype"/></type>
  <role>
     <type><topicRef href="#role2"/></type>
     <topicRef href="#player1"/>
  </role>
  <role>
     <type><topicRef href="#role1"/></type>
     <topicRef    href="#player2"/>
  </role>
</association>
|);
#warn Dumper $ms;

  my @m = $ms->match (TM->FORALL, type => 'tm:atype');
  ok (@m == 1, 'assoc: exactly one added');

#warn Dumper \@m;
  ok (eq_array (
		$m[0]->[TM->ROLES],
		[ 'tm:role1', 'tm:role2' ]
		), 'assoc: roles ok');
  ok (eq_array (
                $m[0]->[TM->PLAYERS],
                [ 'tm:player2', 'tm:player1' ]
                ), 'assoc: players ok');
  is ($m[0]->[TM->SCOPE], 'us', 'assoc: default scope');
  ok ($ms->toplet ('tm:role1'),   'assoc: found topic (implicit)');
  ok ($ms->toplet ('tm:role2'),   'assoc: found topic (implicit)');
  ok ($ms->toplet ('tm:player1'), 'assoc: found topic (implicit)');
  ok ($ms->toplet ('tm:player2'), 'assoc: found topic (implicit)');
}

{ # scoped assoc, defaults, id
  my $ms = _parse (q|
<association id="rumsti">
  <type><topicRef href="#atype"/></type>
  <scope><topicRef href="#ascope"/></scope>
  <role>
     <type><topicRef href="#role1"/></type>
     <topicRef href="#player1"/>
  </role>
  <role>
     <type><topicRef href="#role2"/></type>
     <topicRef href="#player2"/>
  </role>
</association>
|);
#warn Dumper $ms;

  my @m = $ms->match (TM->FORALL, type => 'tm:atype');
  ok (@m == 1, 'assoc: exactly one added');

#warn Dumper \@m;
  ok (eq_array (
		$m[0]->[TM->ROLES],
		[ 'tm:role1', 'tm:role2' ]
		), 'assoc: roles ok');
  ok (eq_array (
                $m[0]->[TM->PLAYERS],
                [ 'tm:player1', 'tm:player2' ]
                ), 'assoc: players ok');

  is ($m[0]->[TM->SCOPE],  'tm:ascope', 'assoc: explicit scope');
  @m = $ms->match (TM->FORALL, scope => 'tm:ascope');
  ok (@m == 1, 'assoc: exactly one scoped');
}

{ # topic with subject indicator
  my $ms = _parse (q|
<topic id="aaa">
   <subjectIdentifier href="http://rumsti"/>
   <subjectIdentifier href="http://remsti"/>
   <subjectLocator    href="http://ramsti"/>
</topic>
|);
##  warn Dumper $ms;

  my $t = $ms->toplet ('tm:aaa');
  is ($t->[TM->ADDRESS], 'http://ramsti', 'subject address');
  ok (eq_set (
	      $t->[TM->INDICATORS],
	      ['http://rumsti', 'http://remsti' ]), 'subject indicators');
}

{ # mergeMap, die Kraetzn
# create tmp file
    my $tmp;
    use IO::File;
    use POSIX qw(tmpnam);
    do { $tmp = tmpnam().".xtm" ;  } until IO::File->new ($tmp, O_RDWR|O_CREAT|O_EXCL);

##warn "tmp is $tmp";

    my $fh = IO::File->new ("> $tmp") || die "so what?";
print $fh q|<topicMap xmlns='http://www.topicmaps.org/xtm/1.0/' xml:base="tm:" xmlns:xlink='http://www.w3.org/1999/xlink' version="1.1">
  <topic id="xxx"/>
  <topic id="yyy"/>
</topicMap>
|;
    $fh->close;

#    warn "# sleeping for 2 secs";
#    sleep 2;

    my $ms = _parse (qq|
<topic id="aaa"/>

<mergeMap href="file:$tmp"/>

|);
#  warn Dumper $ms;

    ok ($ms->toplet ('tm:aaa'),    'mergeMap: found topic');
    ok ($ms->toplet ('tm:xxx'),    'mergeMap: found topic');
    ok ($ms->toplet ('tm:yyy'),    'mergeMap: found topic');

    unlink ($tmp) || die "cannot unlink '$tmp' file";
}

eval {
    _parse (q|

<mergeMap href="rumsti"/>

|);
    ok (0);
}; like ($@, qr/unable to load/i, "mergeMap: "._chomp $@);

eval {
    _parse (q|

<mergeMap xhref="rumsti"/>

|);
    ok (0);
}; like ($@, qr/missing/i, "mergeMap: "._chomp $@);

{ # assoc reification
  my $ms = _parse (q|
<association reifier="rumsti">
   <type><topicRef href="bumsti"/></type>
</association>
|);
# warn Dumper $ms;

  ok ($ms->toplet ('tm:rumsti'),    'reified assoc: found reifying topic');
  my $aid = $ms->toplet ('tm:rumsti')->[TM->ADDRESS];
  like ($aid, qr/.{32}/, 'assoc id sane');
  is ($ms->retrieve ($aid)->[TM->TYPE], 'tm:bumsti', 'assoc type')
}

{ # assoc reification with existing topics
  my $ms = _parse (q|
<topic id="rumsti"/>

<association reifier="rumsti">
   <type><topicRef href="bumsti"/></type>
</association>

<association reifier="ramsti">
   <type><topicRef href="bumsti"/></type>
</association>

<topic id="ramsti"/>

|);
# warn Dumper $ms;

  ok ($ms->toplet ('tm:rumsti'),    'reified assoc: found reifying topic');
  ok ($ms->toplet ('tm:ramsti'),    'reified assoc: found reifying topic');
  my $aid = $ms->toplet ('tm:rumsti')->[TM->ADDRESS];
  like ($aid, qr/.{32}/, 'assoc id sane');
  is ($ms->retrieve ($aid)->[TM->TYPE], 'tm:bumsti', 'assoc type');
  $aid = $ms->toplet ('tm:ramsti')->[TM->ADDRESS];
  like ($aid, qr/.{32}/, 'assoc id sane');
  is ($ms->retrieve ($aid)->[TM->TYPE], 'tm:bumsti', 'assoc type');
}

{ # name, occ reification with and without existing topics
  my $ms = _parse (q|
<topic id="ramsti"/>

<topic id="rumsti">
   <name reifier="ramsti"><value>AAA</value></name>
   <occurrence reifier="remsti"><resourceData>AAA</resourceData></occurrence>
</topic>

|);
# warn Dumper $ms;

  ok ($ms->toplet ('tm:rumsti'),    'reified chars: found topic');
  ok ($ms->toplet ('tm:ramsti'),    'reified assoc: found reifying topic');
  ok ($ms->toplet ('tm:remsti'),    'reified assoc: found reifying topic');

  my $aid = $ms->toplet ('tm:ramsti')->[TM->ADDRESS];
  like ($aid, qr/.{32}/, 'char id sane');
  is ($ms->retrieve ($aid)->[TM->KIND], TM->NAME, 'char kind');
  $aid = $ms->toplet ('tm:remsti')->[TM->ADDRESS];
  like ($aid, qr/.{32}/, 'char id sane');
  is ($ms->retrieve ($aid)->[TM->KIND], TM->OCC, 'char kind');
}

#-- errorneous situations

eval {
    _parse (q|<?xml version="1.0"?>
<topicMaps xmlns='http://www.topicmaps.org/xtm/1.0/'/>
|);
}; like ($@, qr/unsupported/, _chomp $@);

eval {
    _parse (q|<?xml version="1.0"?>
<topicMap xmlns='http://www.topicmaps.org/xtm/1.0/' version="2.2"/>
|);
    ok (0);
}; like ($@, qr/version/, _chomp $@);

eval {
    _parse (q|<?xml version="1.0"?>
<topicMap xmlns='http://wwwxxx.topicmaps.org/xtm/' version="2.0"/>
|);
    ok (0);
}; like ($@, qr/namespace/, _chomp $@);

eval {
    _parse (q|
<topic id="aaa">
  <instanceOf><topicRef xlink:href="#type"/></instanceOf>
  <baseName>
     <baseNameString>Testus</baseNameString>
  </baseName>
</toxxxxpic>
|);
    ok (0);
}; ok (1, scalar 'tag mismatch');



__END__


__END__



TODO: variants