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
|
use strict;
use warnings;
use Test::More;
use File::Spec;
eval { require Storable; };
unless($INC{'Storable.pm'}) {
plan skip_all => 'no Storable.pm';
}
unless(UNIVERSAL::can(Storable => 'lock_nstore')) {
plan skip_all => 'Storable.pm is too old - no file locking support';
}
# Initialise filenames and check they're there
my $SrcFile = File::Spec->catfile('t', 'desertnet.src');
my $XMLFile = File::Spec->catfile('t', 'desertnet3.xml');
my $CacheFile = File::Spec->catfile('t', 'desertnet3.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 => 23;
##############################################################################
# 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);
}
##############################################################################
# Delete a file - portably
#
sub DeleteFile {
my($Filename) = @_;
if ('VMS' eq $^O) {
1 while (unlink($Filename));
} else {
unlink($Filename);
}
}
##############################################################################
# Create a file, making sure that its timestamp is newer than another
# existing file.
#
sub MakeNewerFile {
my($File1, $File2, $CodeRef) = @_;
my $t0 = (stat($File1))[9];
while(1) {
unlink($File2);
$CodeRef->();
return if (stat($File2))[9] > $t0;
sleep(1);
}
}
##############################################################################
# Wait until the current time is greater than the supplied value
#
sub PassTime {
my($Target) = @_;
while(time <= $Target) {
sleep 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'
]
}
}
};
ok(CopyFile($SrcFile, $XMLFile), 'copied known good source file');
unlink($CacheFile);
ok(! -e $CacheFile, 'no cache files lying around');
my $opt = XMLin($XMLFile);
is_deeply($opt, $Expected, 'parsed expected data from file');
ok(! -e $CacheFile, 'and no cache file was created');
PassTime(time()); # Ensure cache file will be newer
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'parsed expected data from file (again)');
ok(-e $CacheFile, 'but this time a cache file was created');
$t0 = (stat($CacheFile))[9]; # Remember cache timestamp
PassTime($t0);
$opt = XMLin($XMLFile, cache => ['storable']);
is_deeply($opt, $Expected, 'got expected data from cache');
$t1 = (stat($CacheFile))[9];
is($t0, $t1, 'and cache timestamp has not changed');
PassTime(time());
$t0 = time();
open(FILE, ">>$XMLFile"); # Touch the XML file
print FILE "\n";
close(FILE);
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'parsed in expected value again');
$t2 = (stat($CacheFile))[9];
isnt($t1, $t2, 'and this time the cache timestamp has changed');
DeleteFile($XMLFile);
ok(! -e $XMLFile, 'deleted the source file');
open(FILE, ">$XMLFile"); # Re-create it (empty)
close(FILE);
ok(-e $XMLFile, 'recreated the source file');
is(-s $XMLFile, 0, 'but with nothing in it');
MakeNewerFile($XMLFile, $CacheFile, sub { # Make sure cache file is newer
Storable::nstore($Expected, $CacheFile);
});
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'got the expected data from the cache');
$t2 = (stat($CacheFile))[9];
PassTime($t2);
open(FILE, ">$XMLFile") || # Write some new data to the XML file
die "open(>$XMLFile): $!\n";
print FILE qq(<opt one="1" two="2"></opt>\n);
close(FILE);
$opt = XMLin($XMLFile); # Parse with no caching
is_deeply($opt, { one => 1, two => 2}, 'parsed in expected data from file');
$t0 = (stat($CacheFile))[9]; # And timestamp on cache file
my $s0 = (-s $CacheFile);
is($t0, $t2, 'and the cache file was not touched');
# Parse again with caching enabled
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, { one => 1, two => 2}, 'parsed expected data through cache');
$t1 = (stat($CacheFile))[9];
my $s1 = (-s $CacheFile);
ok(($t0 != $t1) || ($s0 != $s1),
'and the cache was updated'); # Content changes but date may not on Win32
ok(CopyFile($SrcFile, $XMLFile), 'copied back the original file');
PassTime($t1);
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'parsed expected data in through cache');
# Make sure scheme name is case-insensitive
$opt = XMLin($XMLFile, cache => 'Storable');
is_deeply($opt, $Expected, 'scheme name is case-insensitive');
# Make sure bad scheme names are trapped
$@='';
$_ = eval { XMLin($XMLFile, cache => 'Storubble'); };
is($_, undef, 'bad cache scheme names are trapped');
like($@, qr/Unsupported caching scheme: storubble/,
'with correct error message');
# Clean up and go
unlink($CacheFile);
unlink($XMLFile);
exit(0);
|