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
|
use strict;
use warnings;
use Test::More;
use File::Spec;
use IO::File;
BEGIN {
unshift @INC, File::Spec->catfile('t', 'lib');
eval { require XML::SAX; };
if($@) {
plan skip_all => 'no XML::SAX';
}
}
use TagsToUpper;
# Initialise filenames and check they're there
my $SrcFile = File::Spec->catfile('t', 'desertnet.src');
my $XMLFile = File::Spec->catfile('t', 'desertnet7.xml');
my $CacheFile = File::Spec->catfile('t', 'desertnet.stor');
unless(-e $SrcFile) {
plan skip_all => 'test data missing';
}
# Make sure we can write to the filesystem and check it uses the same
# clock as the machine we're running on.
my $t0 = time();
unless(open(XML, '>', $XMLFile)) {
plan skip_all => "can't create test file '$XMLFile': $!";
}
close(XML);
my $t1 = (stat($XMLFile))[9];
my $t2 = time();
if($t1 < $t0 or $t2 < $t1) {
plan skip_all => 'time moved backwards!'
}
plan tests => 14;
##############################################################################
# S U P P O R T R O U T I N E S
##############################################################################
##############################################################################
# Copy a file
#
sub CopyFile {
my($src, $dst) = @_;
open(my $in, $src) or die "open(<$src): $!";
local($/) = undef;
my $data = <$in>;
close($in);
open(my $out, '>', $dst) or die "open(>$dst): $!";
print $out $data;
close($out);
return(1);
}
##############################################################################
# T E S T R O U T I N E S
##############################################################################
use XML::Simple;
# Initialise test data
my $Expected = {
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => '10.0.0.102'
},
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
my $xml = '';
# Force default behaviour of using SAX parser if it is available (which it
# is or we wouldn't be here).
$XML::Simple::PREFERRED_PARSER = '';
ok(CopyFile($SrcFile, $XMLFile), 'created source XML file');
if ('VMS' eq $^O) {
1 while (unlink($CacheFile));
} else {
unlink($CacheFile);
}
ok(! -e $CacheFile, 'deleted old cache files');
# Pass in a filename to check parse_uri()
my $opt = XMLin($XMLFile);
is_deeply($opt, $Expected, 'parsed expected value from file');
# Pass in an IO::File object to test parse_file()
my $fh = IO::File->new("<$XMLFile");
isa_ok($fh, 'IO::File', '$fh');
$opt = XMLin($fh);
is_deeply($opt, $Expected, 'parsed expected value from IO::File object');
$fh->close();
# Pass in a string to test parse_string()
if(open(XMLFILE, "<$XMLFile")) {
local($/) = undef;
$xml = <XMLFILE>;
close(XMLFILE);
}
$opt = XMLin($xml);
is_deeply($opt, $Expected, 'parsed expected value from string');
# Pass in '-' for STDIN
open(OLDSTDIN, "<&STDIN");
close(STDIN);
open(STDIN, "<$XMLFile");
$opt = XMLin('-');
is_deeply($opt, $Expected, "parsed expected value from STDIN ('-')");
open(STDIN, "<&OLDSTDIN");
close(OLDSTDIN);
# Try using XML:Simple object as a SAX handler
my $simple = XML::Simple->new();
my $parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$opt = $parser->parse_uri($XMLFile);
is_deeply($opt, $Expected,
'XML::Simple as a SAX handler returned expected value');
# Try again but make sure options from the constructor are being used
$simple = XML::Simple->new(
keyattr => { server => 'osname' },
forcearray => ['address'],
);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$opt = $parser->parse_uri($XMLFile);
my $Expected2 = {
'server' => {
'irix' => {
'address' => [ '10.0.0.102' ],
'osversion' => '6.5',
'name' => 'gobi'
},
'solaris' => {
'address' => [ '10.0.0.101', '10.0.1.101' ],
'osversion' => '2.6',
'name' => 'sahara'
},
'linux' => {
'address' => [ '10.0.0.103', '10.0.1.103' ],
'osversion' => '2.0.34',
'name' => 'kalahari'
}
}
};
is_deeply($opt, $Expected2, 'options passed to handler contructor work');
# Try using XML::Simple to drive a SAX pipeline
my $Expected3 = {
'SERVER' => {
'sahara' => {
'OSVERSION' => '2.6',
'OSNAME' => 'solaris',
'ADDRESS' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'OSVERSION' => '6.5',
'OSNAME' => 'irix',
'ADDRESS' => '10.0.0.102'
},
'kalahari' => {
'OSVERSION' => '2.0.34',
'OSNAME' => 'linux',
'ADDRESS' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
my $simple2 = XML::Simple->new(keyattr => [qw(NAME)]);
my $filter = TagsToUpper->new(Handler => $simple2);
my $opt2 = XMLout($opt,
keyattr => { server => 'osname' },
Handler => $filter,
);
is_deeply($opt2, $Expected3, 'driving a SAX pipeline with XML::Simple worked');
# Confirm that 'handler' is a synonym for 'Handler'
$simple2 = XML::Simple->new(keyattr => [qw(NAME)]);
$filter = TagsToUpper->new(Handler => $simple2);
$opt2 = XMLout($opt,
keyattr => { server => 'osname' },
handler => $filter,
);
is_deeply($opt2, $Expected3, "'handler' is a synonym for 'Handler'");
# Confirm that DataHandler routine gets called
$xml = q(<opt><anon>one</anon><anon>two</anon><anon>three</anon></opt>);
$simple = XML::Simple->new(
DataHandler => sub {
my $xs = shift;
my $data = shift;
return(join(',', @$data));
}
);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
my $result = $parser->parse_string($xml);
is($result, 'one,two,three', "'DataHandler' option works");
# Confirm that 'datahandler' is a synonym for 'DataHandler'
$simple = XML::Simple->new(
datahandler => sub {
my $xs = shift;
my $data = shift;
return(join(',', reverse(@$data)));
}
);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$result = $parser->parse_string($xml);
is($result, 'three,two,one', "'datahandler' is a synonym for 'DataHandler'");
# Confirm keeproot logic gets called
$simple = XML::Simple->new(keeproot => 1);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$opt = $parser->parse_string('<opt a="1" b="2" />');
is_deeply($opt, {opt => {a => 1, b => 2}}, "keeproot works with SAX pipelines");
# Clean up and go
unlink($CacheFile);
unlink($XMLFile);
exit(0);
|