49
|
1 package Test::DOM::Builder;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
166
|
5 use parent qw(IMPL::Test::Unit);
|
49
|
6 __PACKAGE__->PassThroughArgs;
|
|
7
|
|
8 use IMPL::Class::Property;
|
|
9 use IMPL::Test qw(test failed shared);
|
|
10
|
|
11 require IMPL::DOM::Schema;
|
|
12 require IMPL::DOM::Navigator::Builder;
|
|
13 require IMPL::DOM::Navigator::SimpleBuilder;
|
|
14 require IMPL::DOM::Document;
|
|
15 require IMPL::DOM::XMLReader;
|
|
16
|
|
17 BEGIN {
|
|
18 public property schemaDoc => prop_all;
|
|
19 }
|
|
20
|
|
21 sub CTOR {
|
|
22 my ($this) = @_;
|
|
23
|
|
24 my $schema = new IMPL::DOM::Schema;
|
|
25 $schema->appendRange(
|
|
26 IMPL::DOM::Schema::ComplexNode->new( name => 'personInfo' )->appendRange(
|
|
27 IMPL::DOM::Schema::NodeSet->new()->appendRange(
|
232
|
28 IMPL::DOM::Schema::SimpleNode->new( name => 'firstName' ),
|
|
29 IMPL::DOM::Schema::SimpleNode->new( name => 'lastName' ),
|
|
30 IMPL::DOM::Schema::ComplexNode->new( name => 'address', maxOccur => 'unbounded' )->appendRange(
|
49
|
31 IMPL::DOM::Schema::NodeSet->new()->appendRange(
|
232
|
32 IMPL::DOM::Schema::SimpleNode->new( name => 'street' ),
|
|
33 IMPL::DOM::Schema::SimpleNode->new( name => 'line', minOccur => 0 )
|
49
|
34 )
|
|
35 )
|
|
36 )
|
|
37 )
|
|
38 );
|
|
39
|
|
40 $this->schemaDoc( $schema );
|
|
41 }
|
|
42
|
|
43 test CreateBuilder => sub {
|
|
44 my ($this) = @_;
|
|
45
|
|
46 my $builder = IMPL::DOM::Navigator::Builder->new(
|
|
47 new IMPL::DOM::Document(nodeName => 'personInfo'),
|
|
48 $this->schemaDoc
|
|
49 );
|
|
50 };
|
|
51
|
|
52 test BuildDocument => sub {
|
|
53 my ($this) = @_;
|
|
54
|
|
55 my $builder = IMPL::DOM::Navigator::Builder->new(
|
|
56 'IMPL::DOM::Document',
|
|
57 $this->schemaDoc
|
|
58 );
|
|
59
|
|
60 $builder->NavigateCreate('personInfo', version => '1');
|
|
61 $builder->NavigateCreate('firstName')->nodeValue('Nemo');
|
|
62 $builder->Back();
|
|
63 $builder->NavigateCreate('lastName')->nodeValue('Nobel');
|
|
64 $builder->Back();
|
|
65 for(1..10) {
|
|
66 $builder->NavigateCreate('address', local => 1);
|
|
67 $builder->NavigateCreate('street')->nodeValue('Hellroad');
|
|
68 $builder->Back();
|
|
69 $builder->NavigateCreate('line')->nodeValue($_);
|
|
70 $builder->Back();
|
|
71 $builder->Back();
|
|
72 }
|
|
73 $builder->Back();
|
|
74
|
|
75 my @errors = $this->schemaDoc->Validate($builder->Document);
|
|
76 failed ("The built document doesn't pass a validation",@errors) if @errors;
|
|
77
|
|
78 return 1;
|
|
79 };
|
|
80
|
|
81 test BuildSimpleDocument => sub {
|
|
82 my ($this) = @_;
|
|
83
|
|
84 my $builder = IMPL::DOM::Navigator::SimpleBuilder->new();
|
|
85
|
|
86 $builder->NavigateCreate('personInfo', version => '1');
|
|
87 $builder->NavigateCreate('firstName')->nodeValue('Nemo');
|
|
88 $builder->Back();
|
|
89 $builder->NavigateCreate('lastName')->nodeValue('Nobel');
|
|
90 $builder->Back();
|
|
91 for(1..10) {
|
|
92 $builder->NavigateCreate('address', local => 1);
|
|
93 $builder->NavigateCreate('street')->nodeValue('Hellroad');
|
|
94 $builder->Back();
|
|
95 $builder->NavigateCreate('line')->nodeValue($_);
|
|
96 $builder->Back();
|
|
97 $builder->Back();
|
|
98 }
|
|
99 $builder->Back();
|
|
100
|
|
101 my @errors = $this->schemaDoc->Validate($builder->Document);
|
|
102 failed ("The built document doesn't pass a validation",@errors) if @errors;
|
|
103
|
|
104 return 1;
|
|
105 };
|
|
106
|
|
107 test BuildDocumentFromXml => sub {
|
|
108 my ($this) = @_;
|
|
109
|
|
110 my $builder = IMPL::DOM::Navigator::SimpleBuilder->new();
|
|
111 my $reader = IMPL::DOM::XMLReader->new( Navigator => $builder );
|
|
112
|
|
113 $reader->ParseFile("Resources/person_info.xml");
|
|
114
|
|
115 my $doc = $builder->Document() or failed("No document was constrcuted");
|
|
116
|
|
117 my @errors = $this->schemaDoc->Validate($doc);
|
|
118 failed("The document isn't correct", @errors) if @errors;
|
|
119 my $name = ($doc->selectNodes("firstName"))[0]->nodeValue;
|
|
120 failed("The firstName has a wrong value", "Expected: Norman", "Got: $name") unless $name eq "Norman";
|
|
121
|
|
122 };
|
|
123
|
|
124
|
|
125 1;
|