| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; # use strict is for PBP |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Filter::Include; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | include blx::xsdsql::include; | ||
| 6 | #line 7 | ||
| 7 | 1 | ▶ | use Getopt::Std; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use File::Spec::Functions; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use File::Basename; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use XML::Writer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use blx::xsdsql::connection; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use blx::xsdsql::schema_repository; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||
| 16 | 1 | ▶ | use blx::xsdsql::xml; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use blx::xsdsql::ut::fake_ansicolor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 1 | ▶ | use constant { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | DIR_PREFIX => 'xml_' | ||
| 22 | ,STEP_FILE_PREFIX => '.step_' | ||
| 23 | ,CUST_PARAMS_FILE => 'custom_params' | ||
| 24 | ,PERL => $ENV{PERL} // 'perl' | ||
| 25 | }; | ||
| 26 | |||
| 27 | my %Opt=(); | ||
| 28 | |||
| 29 | sub abort { | ||
| 30 | my $conn=shift; | ||
| 31 | print STDERR join('',@_),"\n" if scalar(@_); | ||
| 32 | if (defined $conn) { | ||
| 33 | unless ($conn->{AutoCommit}) { | ||
| 34 | print STDERR "(W) ROLLBACK issue for abort\n"; | ||
| 35 | $conn->rollback; | ||
| 36 | } | ||
| 37 | $conn->disconnect; | ||
| 38 | } | ||
| 39 | exit 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub debug { | ||
| 43 | my ($n,@l)=@_; | ||
| 44 | $n='<undef>' unless defined $n; | ||
| 45 | print STDERR 'test (D ',$n,'): ',join(' ',grep(defined $_,@l)),"\n"; | ||
| 46 | undef; | ||
| 47 | } | ||
| 48 | sub count_rows { | ||
| 49 | my ($t,$pcount,%params)=@_; | ||
| 50 | my $table_name=$t->get_sql_name; | ||
| 51 | my $sql="select count(*) from $table_name"; | ||
| 52 | my $sth=$params{CONN}->prepare($sql) || die "prepare failed\n"; | ||
| 53 | $sth->execute || die "execute failed\n"; | ||
| 54 | my $row=$sth->fetchrow_arrayref || die "fetchrow_arrayref failed\n"; | ||
| 55 | my $c=$row->[0]; | ||
| 56 | $sth->finish; | ||
| 57 | if ($c > 0) { | ||
| 58 | print STDERR "table '$table_name' contains $c rows\n"; | ||
| 59 | $$pcount++; | ||
| 60 | } | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub loop_table_childs { | ||
| 65 | my ($t,$pcount,%params)=@_; | ||
| 66 | count_rows($t,$pcount,%params); | ||
| 67 | for my $child($t->get_child_tables) { | ||
| 68 | loop_table_childs($child,$pcount,%params); | ||
| 69 | } | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub test_delete { | ||
| 74 | my ($schema,$pcount,%params)=@_; | ||
| 75 | |||
| 76 | for my $t($params{REPO}->get_dtd_tables) { | ||
| 77 | loop_table_childs($t,$pcount,%params); | ||
| 78 | } | ||
| 79 | |||
| 80 | for my $k(qw(XML_CATALOG XML_ENCODING)) { | ||
| 81 | my $t=$params{REPO}->get_table_from_type(TYPE => $k); | ||
| 82 | loop_table_childs($t,$pcount,%params); | ||
| 83 | } | ||
| 84 | |||
| 85 | my %types=$schema->get_types_name; | ||
| 86 | for my $t(values %types) { | ||
| 87 | loop_table_childs($t,$pcount,%params); | ||
| 88 | } | ||
| 89 | |||
| 90 | my $root_table=$schema->get_root_table; | ||
| 91 | loop_table_childs($root_table,$pcount,%params); | ||
| 92 | |||
| 93 | for my $child($schema->get_childs_schema) { | ||
| 94 | test_delete($child->{SCHEMA},$pcount,%params); | ||
| 95 | } | ||
| 96 | |||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub get_xml_files { | ||
| 101 | my %p=@_; | ||
| 102 | my @xml=(); | ||
| 103 | if (opendir(my $fd,$p{DIR})) { | ||
| 104 | while(my $f=readdir($fd)) { | ||
| 105 | next if $f=~/^\./; | ||
| 106 | next if -d $f; | ||
| 107 | next unless $f=~/\.xml$/i; | ||
| 108 | if (defined (my $a=$p{ONLY_FILES})) { | ||
| 109 | next unless grep($_ eq $f,@$a); | ||
| 110 | } | ||
| 111 | my $xml=catfile($p{DIR},$f); | ||
| 112 | next unless -r $xml; | ||
| 113 | my $cmd=$p{XML_VALIDATOR}; | ||
| 114 | my $schema=$p{SCHEMA_FILE}; | ||
| 115 | $cmd=~s/\%s/$schema/g; | ||
| 116 | $cmd=~s/\%f/$xml/g; | ||
| 117 | debug(__LINE__,$cmd) if $p{DEBUG}; | ||
| 118 | system($cmd); | ||
| 119 | if ($?) { | ||
| 120 | print STDERR "$f: not valid xml\n"; | ||
| 121 | closedir($fd); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | push @xml,$f; | ||
| 125 | } | ||
| 126 | closedir($fd); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | print STDERR " error open current directory\n"; | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | [sort @xml]; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub check_xml_file { | ||
| 136 | my ($f,%p)=@_; | ||
| 137 | my $cmd=$p{XML_VALIDATOR}; | ||
| 138 | my $schema_file=$p{SCHEMA_FILE}; | ||
| 139 | $cmd=~s/\%s/$schema_file/g; | ||
| 140 | $cmd=~s/\%f/$f/g; | ||
| 141 | debug(__LINE__,$cmd) if $p{DEBUG}; | ||
| 142 | system($cmd); | ||
| 143 | return $? ? 0 : 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub compare_xml_files { | ||
| 147 | my ($f1,$f2,%p)=@_; | ||
| 148 | my ($name)=$f1=~/^(.*)\.xml$/i; | ||
| 149 | my $diff=$name.'.diff'; | ||
| 150 | system("cd '".$p{DIR}."' && xmldiff -c '$f1' '$f2' > '$diff'"); | ||
| 151 | my $rc=$?; | ||
| 152 | if ($rc && !$p{OK_FOR_DIFF}) { | ||
| 153 | print STDERR "'$f1' <--> '$f2' - the files are diff\n"; | ||
| 154 | return 1; | ||
| 155 | } | ||
| 156 | elsif(!$rc && $p{OK_FOR_DIFF}) { | ||
| 157 | print STDERR "'$f1' <--> '$f2' - the files are equal\n"; | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | return 0; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | sub get_root_tag_params { | ||
| 165 | my %p=@_; | ||
| 166 | my $tp=$p{ROOT_TAG_PARAMS}; | ||
| 167 | $tp=[ map { | ||
| 168 | my $out=$_; | ||
| 169 | SW: for ($out) { | ||
| 170 | /^"(.*)"$/ && do { $out=$1; last SW; }; | ||
| 171 | /^'(.*)'$/ && do { $out=$1; last SW; }; | ||
| 172 | last; | ||
| 173 | } | ||
| 174 | $out; | ||
| 175 | } split(/[,=]/,$tp) | ||
| 176 | ] if defined $tp; | ||
| 177 | |||
| 178 | return $tp; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub do_test { | ||
| 182 | my (%p)=@_; | ||
| 183 | my $step_file_prefix=STEP_FILE_PREFIX; | ||
| 184 | if ($p{CLEAN}) { | ||
| 185 | if (opendir(my $d,$p{DIR})) { | ||
| 186 | while(my $f=readdir($d)) { | ||
| 187 | next if -d $f; | ||
| 188 | if ($f=~/\.(diff|tmp)$/i || $f=~/^$step_file_prefix/) { | ||
| 189 | debug(__LINE__,"remove file $f") if $p{DEBUG}; | ||
| 190 | unless (unlink catfile($p{DIR},$f)) { | ||
| 191 | print STDERR "(W) $f: cannot remove: $!\n"; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | closedir($d); | ||
| 196 | } | ||
| 197 | else { | ||
| 198 | abort(undef,"cannot open current directory: $! \n"); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | return 0 if $p{NOT_EXECUTE}; | ||
| 202 | $p{SCHEMA_FILE}=catfile($p{DIR},$p{SCHEMA_FILE}); | ||
| 203 | unless (-r $p{SCHEMA_FILE}) { | ||
| 204 | print STDERR "(W) schema file not exist or not readable - directory skipped\n"; | ||
| 205 | return 0; | ||
| 206 | } | ||
| 207 | |||
| 208 | if ($p{USE_XML_REPO}) { | ||
| 209 | my $xml_files=get_xml_files(%p); | ||
| 210 | return 1 unless defined $xml_files; | ||
| 211 | |||
| 212 | if (open(my $fd,"|-",$p{REPO})) { | ||
| 213 | my $s= | ||
| 214 | 'create_catalog -o ' | ||
| 215 | .'"' | ||
| 216 | .'TABLE_PREFIX='.$p{TABLE_PREFIX} | ||
| 217 | .',VIEW_PREFIX='.$p{VIEW_PREFIX} | ||
| 218 | .',NO_FLAT_GROUPS=0' | ||
| 219 | .'" ' | ||
| 220 | .$p{CATALOG_NAME} | ||
| 221 | .' ' | ||
| 222 | .$p{SCHEMA_FILE} | ||
| 223 | ."\n" | ||
| 224 | ; | ||
| 225 | print $fd $s; | ||
| 226 | for my $xml(@$xml_files) { | ||
| 227 | my ($name)=$xml=~/^(.*)\.xml$/i; | ||
| 228 | my $s= | ||
| 229 | 'store_xml ' | ||
| 230 | .$p{CATALOG_NAME} | ||
| 231 | .' ' | ||
| 232 | .catfile($p{DIR},$xml) | ||
| 233 | .' ' | ||
| 234 | .$name | ||
| 235 | ."\n"; | ||
| 236 | print $fd $s; | ||
| 237 | my $tmpname=catfile($p{DIR},$name.'.tmp'); | ||
| 238 | $s="put_xml "; | ||
| 239 | if (defined (my $r=$p{ROOT_TAG_PARAMS})) { | ||
| 240 | $s.=" -x '$r' "; | ||
| 241 | } | ||
| 242 | $s.="-o '$tmpname' "; | ||
| 243 | $s.= " -1 -r " if $p{DELETE}; | ||
| 244 | $s.=$p{CATALOG_NAME}." $name\n"; | ||
| 245 | print $fd $s; | ||
| 246 | } | ||
| 247 | if ($p{TEST_VIEWS}) { | ||
| 248 | print $fd "create_catalog_views -i ",$p{CATALOG_NAME},"\n"; | ||
| 249 | } | ||
| 250 | close $fd; | ||
| 251 | my $rc=$?; | ||
| 252 | if ($rc) { | ||
| 253 | print STDERR $p{REPO}.": command failed - rc $rc\n" ; | ||
| 254 | return 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | for my $xml(@$xml_files) { | ||
| 258 | my ($name)=$xml=~/^(.*)\.xml$/i; | ||
| 259 | my $tmpname=$name.'.tmp'; | ||
| 260 | unless(check_xml_file(catfile($p{DIR},$tmpname),%p)) { | ||
| 261 | print STDERR "$tmpname: not valid xml\n"; | ||
| 262 | return 1; | ||
| 263 | } | ||
| 264 | my $rc=compare_xml_files($xml,$tmpname,%p); | ||
| 265 | return 1 if $rc && !$p{IGNORE_DIFF}; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | else { | ||
| 269 | print STDERR "can't run ".$p{REPO}."': $!\n"; | ||
| 270 | exit 1; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | else { | ||
| 274 | my $conn=$p{REPO}->get_attrs_value(qw(DB_CONN)); | ||
| 275 | if (defined (my $catalog=$p{REPO}->get_catalog($p{CATALOG_NAME}))) { | ||
| 276 | $catalog->drop; | ||
| 277 | } | ||
| 278 | |||
| 279 | my $schema=$p{PARSER}->parsefile( | ||
| 280 | $p{SCHEMA_FILE} | ||
| 281 | ,NO_FLAT_GROUPS => 0 | ||
| 282 | ,TABLE_PREFIX => $p{TABLE_PREFIX} | ||
| 283 | ,VIEW_PREFIX => $p{VIEW_PREFIX} | ||
| 284 | ); | ||
| 285 | |||
| 286 | my $catalog=$p{REPO}->create_catalog($p{CATALOG_NAME},$schema); | ||
| 287 | |||
| 288 | abort($conn,$p{CATALOG_NAME},": catalog already exist in the repository") | ||
| 289 | unless defined $catalog; | ||
| 290 | |||
| 291 | |||
| 292 | unless ($p{NOT_LOAD_SCHEMA}) { | ||
| 293 | unless($catalog=$p{REPO}->get_catalog($p{CATALOG_NAME})) { | ||
| 294 | abort($conn,$p{CATALOG_NAME},": catalog not exist in the repository"); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | my $catalog_xml=$catalog->get_catalog_xml( | ||
| 299 | WRITER => XML::Writer->new( | ||
| 300 | DATA_INDENT => 4 | ||
| 301 | ,DATA_MODE => 1 | ||
| 302 | ,NAMESPACES => 0 | ||
| 303 | ,UNSAFE => 1 | ||
| 304 | ) | ||
| 305 | ,EXECUTE_OBJECTS_PREFIX => undef | ||
| 306 | ,EXECUTE_OBJECTS_SUFFIX => undef | ||
| 307 | ); | ||
| 308 | |||
| 309 | |||
| 310 | my $xml_files=get_xml_files(%p); | ||
| 311 | return 1 unless defined $xml_files; | ||
| 312 | |||
| 313 | my $root_tag_params=get_root_tag_params(ROOT_TAG_PARAMS => $p{ROOT_TAG_PARAMS}); | ||
| 314 | for my $xml(@$xml_files) { | ||
| 315 | if (open(my $fd,'<',catfile($p{DIR},$xml))) { | ||
| 316 | my ($name)=$xml=~/^(.*)\.xml$/i; | ||
| 317 | my $id=$catalog_xml->store_xml( | ||
| 318 | XML_NAME => $name | ||
| 319 | ,FD => $fd | ||
| 320 | ); | ||
| 321 | close($fd); | ||
| 322 | abort($conn,$name,':already exist in repository') unless defined $id; | ||
| 323 | my $tmpname=$name.'.tmp'; | ||
| 324 | if (open(my $fd,'>',catfile($p{DIR},$tmpname))) { | ||
| 325 | $id=$catalog_xml->put_xml( | ||
| 326 | XML_NAME => $name | ||
| 327 | ,NO_WRITE_HEADER => 0 | ||
| 328 | ,NO_WRITE_FOOTER => 0 | ||
| 329 | ,FD => $fd | ||
| 330 | ,ROOT_TAG_PARAMS => $root_tag_params | ||
| 331 | ,DELETE => $p{DELETE} | ||
| 332 | ); | ||
| 333 | close $fd; | ||
| 334 | abort($conn,$name,': not found into repository') unless defined $id; | ||
| 335 | |||
| 336 | unless(check_xml_file(catfile($p{DIR},$tmpname),%p)) { | ||
| 337 | print STDERR "$tmpname: not valid xml\n"; | ||
| 338 | return 1; | ||
| 339 | } | ||
| 340 | |||
| 341 | my $rc=compare_xml_files($xml,$tmpname,%p); | ||
| 342 | return 1 if $rc && !$p{IGNORE_DIFF}; | ||
| 343 | |||
| 344 | if ($p{DELETE} && !$rc && !$p{OK_FOR_DIFF}) { | ||
| 345 | print STDERR "test delete... \n"; | ||
| 346 | my $count=0; | ||
| 347 | test_delete($schema,\$count,CONN => $conn,REPO => $p{REPO}); | ||
| 348 | if ($count > 0) { | ||
| 349 | print STDERR "delete test failed - $count tables has rows\n"; | ||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | } | ||
| 354 | else { | ||
| 355 | abort($conn,"$tmpname: open error"); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | else { | ||
| 359 | abort($conn,"$xml: open error"); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | if ($p{TEST_VIEWS}) { | ||
| 363 | unless ($p{REPO}->is_support_views) { | ||
| 364 | print STDERR "(W) database not support complex views - test views ignored\n"; | ||
| 365 | } | ||
| 366 | else { | ||
| 367 | print STDERR "test views\n"; | ||
| 368 | unless ($catalog->create_views) { | ||
| 369 | print STDERR "create views failed\n"; | ||
| 370 | return 1; | ||
| 371 | } | ||
| 372 | else { | ||
| 373 | print STDERR "test query views\n"; | ||
| 374 | my $binding=$p{REPO}->get_attrs_value(qw(BINDING)); | ||
| 375 | for my $r($catalog->get_object_names(TYPE => 'view')) { | ||
| 376 | my $view_name=$r->[2]; | ||
| 377 | local $@; | ||
| 378 | my $r=eval { $binding->query_from_view($view_name) }; | ||
| 379 | if (!$@ && defined $r) { | ||
| 380 | #empty | ||
| 381 | } | ||
| 382 | else { | ||
| 383 | print STDERR "query_from_view failed for view '$view_name'\n"; | ||
| 384 | print STDERR $@ if $p{DEBUG}; | ||
| 385 | return 1; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | } | ||
| 389 | } | ||
| 390 | } | ||
| 391 | } | ||
| 392 | return 0; | ||
| 393 | } | ||
| 394 | |||
| 395 | sub give_tests { | ||
| 396 | my @ot=(); | ||
| 397 | for my $a(@_) { | ||
| 398 | if ($a=~/^\d+$/) { | ||
| 399 | push @ot,$a; | ||
| 400 | } | ||
| 401 | elsif ($a=~/^(\d+)-(\d+)$/) { | ||
| 402 | push @ot,($1..$2); | ||
| 403 | } | ||
| 404 | else { | ||
| 405 | print STDERR "(W) $a: invalid test number - ignored\n"; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | return @ot; | ||
| 409 | }; | ||
| 410 | |||
| 411 | sub test_dirs { | ||
| 412 | my ($dp,$dir)=@_; | ||
| 413 | my @testdirs=(); | ||
| 414 | if (opendir(my $fd,$dir)) { | ||
| 415 | while(my $d=readdir($fd)) { | ||
| 416 | next unless $d=~/^$dp(\d+)$/; | ||
| 417 | next unless -d $d; | ||
| 418 | push @testdirs,$1; | ||
| 419 | } | ||
| 420 | closedir($fd); | ||
| 421 | } | ||
| 422 | else { | ||
| 423 | my $absdir=File::Spec->rel2abs($dir); | ||
| 424 | print STDERR "(W) $absdir: $!\n"; | ||
| 425 | } | ||
| 426 | my @a=sort @testdirs;; | ||
| 427 | return @a; | ||
| 428 | }; | ||
| 429 | |||
| 430 | sub get_dbconn_cs { | ||
| 431 | my ($connstr,%params)=@_; | ||
| 432 | unless (defined $connstr && length($connstr)) { | ||
| 433 | print STDERR "no connection string specify - set the option 'c' or define the env var DB_CONNECT_STRING\n"; | ||
| 434 | return (); | ||
| 435 | } | ||
| 436 | my $conn=blx::xsdsql::connection->new; | ||
| 437 | unless ($conn->do_connection_list($connstr)) { | ||
| 438 | print STDERR $conn->get_last_error,"\n"; | ||
| 439 | return (); | ||
| 440 | } | ||
| 441 | my ($output_namespace,$db_namespace)=map { $conn->get_attrs_value($_) } (qw(OUTPUT_NAMESPACE DB_NAMESPACE)); | ||
| 442 | my @namespaces=blx::xsdsql::schema_repository::get_namespaces; | ||
| 443 | my $namespace=$output_namespace.'::'.$db_namespace; | ||
| 444 | unless (grep($namespace eq $_,@namespaces)) { | ||
| 445 | print STDERR "$connstr: the namespace '$namespace' is not supported\n"; | ||
| 446 | print STDERR "the namespaces actually supported are: ",join(" ",map { "'".$_."'" } @namespaces),"\n"; | ||
| 447 | return (); | ||
| 448 | } | ||
| 449 | my @a=$conn->get_connection_list; | ||
| 450 | return ( { | ||
| 451 | OUTPUT_NAMESPACE => $output_namespace | ||
| 452 | ,DB_NAMESPACE => $db_namespace | ||
| 453 | },@a | ||
| 454 | ); | ||
| 455 | } | ||
| 456 | |||
| 457 | sub get_message { | ||
| 458 | my (%p)=@_; | ||
| 459 | my @f=(); | ||
| 460 | if (open(my $fd,'<',catfile($p{DIR},'message.txt'))) { | ||
| 461 | @f=<$fd>; | ||
| 462 | close $fd; | ||
| 463 | } | ||
| 464 | else { | ||
| 465 | my $name=readlink catfile($p{DIR},'schema.xsd'); | ||
| 466 | if ($name) { | ||
| 467 | $name=~s/_/ /g; | ||
| 468 | $name=~s/\.xsd$//i; | ||
| 469 | push @f,$name."\n"; | ||
| 470 | } | ||
| 471 | } | ||
| 472 | push @f,"\n" unless scalar(@f); | ||
| 473 | return wantarray ? @f : \@f; | ||
| 474 | } | ||
| 475 | |||
| 476 | sub get_schema_file{ | ||
| 477 | my $testdir=$_[0]; | ||
| 478 | my %schemas=(); | ||
| 479 | if (opendir(my $dd,$testdir)) { | ||
| 480 | my @files=(); | ||
| 481 | while(my $f=readdir($dd)) { | ||
| 482 | next unless $f=~/\.xsd$/; | ||
| 483 | my $fullname=catfile($testdir,$f); | ||
| 484 | next if -l $fullname; | ||
| 485 | next if -d $fullname; | ||
| 486 | next unless -r $fullname; | ||
| 487 | push @files,$f; | ||
| 488 | } | ||
| 489 | closedir $dd; | ||
| 490 | return @files if scalar(@files) < 2; | ||
| 491 | for my $f(@files) { | ||
| 492 | my $fullname=catfile($testdir,$f); | ||
| 493 | if (open(my $fd,'<',$fullname)) { | ||
| 494 | $schemas{$f}=1 unless exists $schemas{$f}; | ||
| 495 | while(<$fd>) { | ||
| 496 | if (/<!--\s+ROOT_SCHEMA\s+/) { | ||
| 497 | close $fd; | ||
| 498 | return ($f); | ||
| 499 | } | ||
| 500 | $schemas{$1}=0 if /include\s+schemaLocation="([^"]+)"/; | ||
| 501 | $schemas{$1}=0 if /import\s+.*\s+schemaLocation="([^"]+)"/; | ||
| 502 | } | ||
| 503 | close $fd; | ||
| 504 | } | ||
| 505 | else { | ||
| 506 | print STDERR "$fullname: $!\n"; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | } | ||
| 510 | else { | ||
| 511 | print STDERR "$testdir: $!\n"; | ||
| 512 | } | ||
| 513 | grep($schemas{$_},keys %schemas); | ||
| 514 | } | ||
| 515 | |||
| 516 | ###### main #### | ||
| 517 | |||
| 518 | unless (getopts ('hac:df:int:sv:xDFLTV',\%Opt)) { | ||
| 519 | print STDERR "invalid option or option not set\n"; | ||
| 520 | exit 1; | ||
| 521 | } | ||
| 522 | |||
| 523 | if ($Opt{h}) { | ||
| 524 | print STDOUT basename($0). | ||
| 525 | q{ [<options>] [<args>].. | ||
| 526 | exec battery test | ||
| 527 | <options>: | ||
| 528 | -h - this help | ||
| 529 | -a - display the know namespaces and exit | ||
| 530 | -c <connstr> - connect string to database - the default is the value of the env var DB_CONNECT_STRING | ||
| 531 | otherwise is an error | ||
| 532 | the form is [<output_namespace>::]<dbtype>:<user>/<password>@<dbname>[:hostname[:port]][;<attribute>[,<attribute>...]] | ||
| 533 | <output_namespace>::<dbtype> - see the output with 'a' option set | ||
| 534 | the default for <output_namespace> is 'sql' | ||
| 535 | <dbname> - database name | ||
| 536 | <hostname> - remote host name or ip address | ||
| 537 | <port> - remote host port | ||
| 538 | <attribute> - extra attribute | ||
| 539 | Examples: | ||
| 540 | sql::pg:user/pwd@mydb:127.0.0.1;RaiseError => 1,AutoCommit => 0,pg_enable_utf8 => 1 | ||
| 541 | sql::mysql:user/pwd@mydb:127.0.0.1;RaiseError => 1,AutoCommit => 0,mysql_enable_utf8 => 1 | ||
| 542 | sql::oracle:user/pwd@orcl:neutrino:1522;RaiseError => 1,AutoCommit => 0 | ||
| 543 | sql::DBM:dbm_mldbm=Storable;RaiseError => 1,f_dir=> q(/tmp) | ||
| 544 | -d - debug mode | ||
| 545 | -f <filename>[,<filename>...] - include in test only file match <filename> | ||
| 546 | -i - incremental test | ||
| 547 | -n - continue after xml difference | ||
| 548 | -t <c|r|C|R> - transaction database mode ((c)ommit for single test, (r)ollback for single test,(C)ommit global,(R)ollback global) - default c | ||
| 549 | -s - stop on first error | ||
| 550 | -v <command> - use <command> for xml validation | ||
| 551 | use %f for xml file tag and %s for schema (xsd) file tag | ||
| 552 | the default is 'xmllint -schema %s %f' | ||
| 553 | -x - use xml_repo.pl for test | ||
| 554 | -F - do not drop the repository on the end of tests | ||
| 555 | -L - do not load from repository the schema | ||
| 556 | -T - clean temporary files in test + step files and not execute the test | ||
| 557 | -V - not test the views | ||
| 558 | -D - do not delete rows after write xml | ||
| 559 | arguments>: | ||
| 560 | <testnumber>|<testnumber>-<testnumber>... | ||
| 561 | if <testnumber> is not spec all tests can be executed | ||
| 562 | if the test is OK the database is cleaned (see option -F) | ||
| 563 | }; | ||
| 564 | exit 0; | ||
| 565 | } | ||
| 566 | |||
| 567 | if (-t *STDERR) { ## no critic | ||
| 568 | eval { require Term::ANSIColor;Term::ANSIColor->import }; | ||
| 569 | |||
| 570 | } | ||
| 571 | |||
| 572 | sub my_color { | ||
| 573 | color @_; | ||
| 574 | } | ||
| 575 | |||
| 576 | |||
| 577 | if ($Opt{a}) { | ||
| 578 | for my $k(qw(c f i n t s v x L F T V D)) { | ||
| 579 | print STDERR "(W) Option '$k' is ignored is active option 'a'\n" if delete $Opt{$k}; | ||
| 580 | } | ||
| 581 | for my $s(blx::xsdsql::schema_repository::get_namespaces) { | ||
| 582 | print $s,"\n"; | ||
| 583 | } | ||
| 584 | exit 0; | ||
| 585 | } | ||
| 586 | |||
| 587 | if ($Opt{T}) { | ||
| 588 | for my $k(qw(c f i n t s v x L V D)) { | ||
| 589 | print STDERR "(W) Option '$k' is ignored is active option 'T'\n" if delete $Opt{$k}; | ||
| 590 | } | ||
| 591 | $Opt{F}=1; | ||
| 592 | } | ||
| 593 | |||
| 594 | if ($Opt{x}) { | ||
| 595 | for my $k(qw(L t)) { | ||
| 596 | print STDERR "(W) Option '$k' is ignored is active option 'x'\n" if delete $Opt{$k}; | ||
| 597 | } | ||
| 598 | } | ||
| 599 | |||
| 600 | |||
| 601 | $Opt{t}='c' unless defined $Opt{t}; | ||
| 602 | abort(undef, $Opt{t},": bad value for option t") unless $Opt{t}=~/^(c|r)$/i; | ||
| 603 | |||
| 604 | my @onlytests=give_tests(@ARGV); | ||
| 605 | |||
| 606 | my @testdirs=grep (defined $_ && length($_),map { | ||
| 607 | my $testnumber=$_; | ||
| 608 | if (scalar(@ARGV)) { | ||
| 609 | $testnumber=undef unless grep($_ == $testnumber,@onlytests); | ||
| 610 | } | ||
| 611 | $testnumber; | ||
| 612 | } test_dirs(DIR_PREFIX,File::Spec->curdir)); | ||
| 613 | |||
| 614 | unless (scalar(@testdirs)) { | ||
| 615 | print STDERR "(W) no test required\n"; | ||
| 616 | exit 0; | ||
| 617 | } | ||
| 618 | |||
| 619 | $Opt{NOT_EXECUTE}=1 if $Opt{T}; | ||
| 620 | $Opt{F}=1 if $Opt{i}; | ||
| 621 | |||
| 622 | |||
| 623 | |||
| 624 | unless ($Opt{NOT_EXECUTE}) { | ||
| 625 | $Opt{c}=$ENV{DB_CONNECT_STRING} unless defined $Opt{c}; | ||
| 626 | my ($ns,@cs)=get_dbconn_cs($Opt{c}); | ||
| 627 | exit 1 unless defined $ns; | ||
| 628 | $Opt{output_namespace}=$ns->{OUTPUT_NAMESPACE}; | ||
| 629 | $Opt{db_namespace}=$ns->{DB_NAMESPACE}; | ||
| 630 | $ENV{DB_CONNECT_STRING}=$Opt{c} if $Opt{x}; | ||
| 631 | if ($Opt{x}) { | ||
| 632 | $Opt{xml_repo}=PERL.' '.File::Spec->rel2abs(catfile('..','bin','xml_repo.pl'))." exec"; | ||
| 633 | $Opt{xml_repo}.=' -e "> "' if $Opt{d}; | ||
| 634 | unless ($Opt{i}) { | ||
| 635 | if (open(my $fd,"|-",$Opt{xml_repo})) { | ||
| 636 | print $fd 'instruction print STDERR qq(drop eventually existent repository...\n)'."\n"; | ||
| 637 | print $fd "drop_repository -i\n"; | ||
| 638 | print $fd 'instruction print STDERR qq(create repository...\n)'."\n"; | ||
| 639 | print $fd "create_repository\n"; | ||
| 640 | close $fd; | ||
| 641 | my $rc=$?; | ||
| 642 | abort(undef,$Opt{xml_repo},": command failed - rc $rc") if $rc; | ||
| 643 | } | ||
| 644 | else { | ||
| 645 | abort(undef,"can't run ",$Opt{xml_repo},"': $!"); | ||
| 646 | } | ||
| 647 | } | ||
| 648 | else { | ||
| 649 | if (open(my $fd,"|-",$Opt{xml_repo})) { | ||
| 650 | print $fd "catalog_names\n"; | ||
| 651 | close $fd; | ||
| 652 | my $rc=$?; | ||
| 653 | exit 1 if $rc; | ||
| 654 | } | ||
| 655 | } | ||
| 656 | } | ||
| 657 | else { | ||
| 658 | $Opt{conn}=eval { DBI->connect(@cs) }; | ||
| 659 | if ($@ || !defined $Opt{conn}) { | ||
| 660 | print STDERR $@ if $@; | ||
| 661 | abort($Opt{conn},$Opt{c}." connection failed"); | ||
| 662 | } | ||
| 663 | |||
| 664 | $Opt{repo}=blx::xsdsql::schema_repository->new( | ||
| 665 | DB_CONN => $Opt{conn} | ||
| 666 | ,OUTPUT_NAMESPACE => $Opt{output_namespace} | ||
| 667 | ,DB_NAMESPACE => $Opt{db_namespace} | ||
| 668 | ,DEBUG => $Opt{d} | ||
| 669 | ); | ||
| 670 | |||
| 671 | unless ($Opt{i}) { | ||
| 672 | print STDERR "drop eventually existent repository...\n"; | ||
| 673 | $Opt{repo}->drop_repository; | ||
| 674 | print STDERR "create repository...\n"; | ||
| 675 | $Opt{repo}->create_repository; | ||
| 676 | print STDERR "OK\n"; | ||
| 677 | } | ||
| 678 | else { | ||
| 679 | unless($Opt{repo}->is_repository_installed) { | ||
| 680 | abort($Opt{conn},"the repository is not installed"); | ||
| 681 | } | ||
| 682 | } | ||
| 683 | } | ||
| 684 | |||
| 685 | } | ||
| 686 | |||
| 687 | |||
| 688 | my $startdir=File::Spec->rel2abs(File::Spec->curdir); | ||
| 689 | |||
| 690 | my $only_files=defined $Opt{f} | ||
| 691 | ? [map { my $f=$_; $f.='.xml' unless $f=~/\.xml$/i; $f; } split(',',$Opt{f})] | ||
| 692 | : undef | ||
| 693 | ; | ||
| 694 | |||
| 695 | $Opt{v}='xmllint --schema \'%s\' --noout \'%f\'' unless defined $Opt{v}; | ||
| 696 | |||
| 697 | |||
| 698 | my $step_file=$Opt{NOT_EXECUTE} | ||
| 699 | ? undef | ||
| 700 | : STEP_FILE_PREFIX.'_'.$Opt{output_namespace}.'_'.$Opt{db_namespace}; | ||
| 701 | |||
| 702 | my $parser= $Opt{NOT_EXECUTE} | ||
| 703 | ? undef | ||
| 704 | : blx::xsdsql::xsd_parser->new( | ||
| 705 | OUTPUT_NAMESPACE => $Opt{output_namespace} | ||
| 706 | ,DB_NAMESPACE => $Opt{db_namespace} | ||
| 707 | ,DEBUG => $Opt{d} | ||
| 708 | ); | ||
| 709 | |||
| 710 | my %C=( #counters | ||
| 711 | N => 0 | ||
| 712 | ,F => 0 | ||
| 713 | ); | ||
| 714 | |||
| 715 | for my $n(@testdirs) { | ||
| 716 | my $testdir=DIR_PREFIX.$n; | ||
| 717 | my $sf=catfile($testdir,$step_file); | ||
| 718 | if ($Opt{T}) { | ||
| 719 | my %test_params=( | ||
| 720 | TEST_NUMBER => $n | ||
| 721 | ,CLEAN => 1 | ||
| 722 | ,NOT_EXECUTE => $Opt{NOT_EXECUTE} | ||
| 723 | ,DIR => $testdir | ||
| 724 | ); | ||
| 725 | do_test(%test_params); | ||
| 726 | print STDERR "test number $n - cleaned\n"; | ||
| 727 | } | ||
| 728 | else { | ||
| 729 | my $rc=0; | ||
| 730 | unlink($sf) unless $Opt{i}; | ||
| 731 | print STDERR my_color 'blue'; | ||
| 732 | print STDERR "test number '$n' - database '".$Opt{db_namespace}."' "; | ||
| 733 | unless( -e $sf) { | ||
| 734 | print STDERR get_message(DIR => $testdir); | ||
| 735 | print STDERR my_color 'reset'; | ||
| 736 | my %custom_params=(); | ||
| 737 | if (open(my $fd,'<',catfile($testdir,CUST_PARAMS_FILE))) { | ||
| 738 | while(<$fd>) { | ||
| 739 | chop; | ||
| 740 | next if /^\s*#/; | ||
| 741 | next if /^\s*$/; | ||
| 742 | if (/^\s*(\w+)\s+(.*)$/) { | ||
| 743 | $custom_params{$1}=$2; | ||
| 744 | } | ||
| 745 | else { | ||
| 746 | print STDERR "(W) ".CUST_PARAMS_FILE.": $_: line ignored\n"; | ||
| 747 | } | ||
| 748 | } | ||
| 749 | close $fd; | ||
| 750 | } | ||
| 751 | my $sn=sprintf("%03d",$n); | ||
| 752 | my @schema_files=get_schema_file($testdir); | ||
| 753 | if (scalar(@schema_files) == 0) { | ||
| 754 | print STDERR "no schema file in directory\n"; | ||
| 755 | $rc=1; | ||
| 756 | } | ||
| 757 | elsif (scalar(@schema_files) > 1) { | ||
| 758 | print STDERR join(' ',@schema_files),": many schema file in directory\n"; | ||
| 759 | $rc=1; | ||
| 760 | } | ||
| 761 | else { | ||
| 762 | $rc=eval { | ||
| 763 | do_test( | ||
| 764 | SCHEMA_FILE => $schema_files[0] | ||
| 765 | ,CATALOG_NAME => 'c'.$sn | ||
| 766 | ,VIEW_PREFIX => 'V'.$sn.'_' | ||
| 767 | ,TABLE_PREFIX => 'T'.$sn.'_' | ||
| 768 | ,DEBUG => $Opt{d} | ||
| 769 | ,XML_VALIDATOR => $Opt{v} | ||
| 770 | ,IGNORE_DIFF => $Opt{n} | ||
| 771 | ,DELETE => !$Opt{D} | ||
| 772 | ,NOT_LOAD_SCHEMA => $Opt{L} | ||
| 773 | ,TEST_VIEWS => !$Opt{V} | ||
| 774 | ,%custom_params | ||
| 775 | ,PARSER => $parser | ||
| 776 | ,REPO => ($Opt{x} ? $Opt{xml_repo} : $Opt{repo}) | ||
| 777 | ,USE_XML_REPO => $Opt{x} | ||
| 778 | ,TEST_NUMBER => $n | ||
| 779 | ,OUTPUT_NAMESPACE => $Opt{output_namespace} | ||
| 780 | ,DB_NAMESPACE => $Opt{db_namespace} | ||
| 781 | ,NOT_EXECUTE => $Opt{NOT_EXECUTE} | ||
| 782 | ,DIR => $testdir | ||
| 783 | ,ONLY_FILES => $only_files | ||
| 784 | ); | ||
| 785 | }; | ||
| 786 | if ($@) { | ||
| 787 | print STDERR $@; | ||
| 788 | $rc=1; | ||
| 789 | } | ||
| 790 | } | ||
| 791 | $C{N}++; | ||
| 792 | $C{F}++ if $rc; | ||
| 793 | if($rc) { | ||
| 794 | print STDERR my_color 'red'; | ||
| 795 | print STDERR '['.$sn.'] Failed'."\n"; | ||
| 796 | print STDERR my_color 'reset'; | ||
| 797 | } | ||
| 798 | else { | ||
| 799 | open(my $fd,">",$sf); | ||
| 800 | close $fd; | ||
| 801 | print STDERR my_color 'blue'; | ||
| 802 | print STDERR '['.$sn.'] OK'."\n"; | ||
| 803 | print STDERR my_color 'reset'; | ||
| 804 | } | ||
| 805 | if (defined (my $conn=$Opt{conn}) && ! $Opt{conn}->{AutoCommit}) { | ||
| 806 | if ($Opt{t} eq 'c') { | ||
| 807 | $conn->commit; | ||
| 808 | } | ||
| 809 | elsif ($Opt{t} eq 'r') { | ||
| 810 | print STDERR "(W) ROOLBACK issued for user request\n"; | ||
| 811 | $conn->rollback; | ||
| 812 | } | ||
| 813 | } | ||
| 814 | last if $rc && $Opt{s}; | ||
| 815 | } | ||
| 816 | else { | ||
| 817 | print STDERR " - skipped for already tested\n"; | ||
| 818 | print STDERR my_color 'reset'; | ||
| 819 | } | ||
| 820 | } | ||
| 821 | } | ||
| 822 | |||
| 823 | $Opt{F}=1 if $C{F}; | ||
| 824 | |||
| 825 | unless($Opt{F}) { | ||
| 826 | print STDERR "drop repository\n"; | ||
| 827 | if ($Opt{x}) { | ||
| 828 | if (open(my $fd,"|-",$Opt{xml_repo})) { | ||
| 829 | print $fd "drop_repository -i\n"; | ||
| 830 | close $fd; | ||
| 831 | my $rc=$?; | ||
| 832 | abort($Opt{conn},"xml_repo.pl failed - rc $rc") if $rc; | ||
| 833 | } | ||
| 834 | else { | ||
| 835 | abort($Opt{conn},"can't run ".$Opt{xml_repo}."': $!"); | ||
| 836 | } | ||
| 837 | } | ||
| 838 | else { | ||
| 839 | $Opt{repo}->drop_repository; | ||
| 840 | } | ||
| 841 | print STDERR "(W) REPOSITORY Dropped\n"; | ||
| 842 | } | ||
| 843 | |||
| 844 | if (defined (my $conn=$Opt{conn})) { | ||
| 845 | unless ($conn->{AutoCommit}) { | ||
| 846 | if ($Opt{t} =~/^c$/i) { | ||
| 847 | $conn->commit; | ||
| 848 | } | ||
| 849 | else { | ||
| 850 | print STDERR "(W) ROOLBACK issued\n"; | ||
| 851 | $conn->rollback; | ||
| 852 | } | ||
| 853 | } | ||
| 854 | $conn->disconnect; | ||
| 855 | } | ||
| 856 | |||
| 857 | print STDERR my_color($C{F} ? 'red' : 'blue'),"test numbers ",$C{N}," - failed ",$C{F},"\n"; | ||
| 858 | print STDERR my_color 'reset'; | ||
| 859 | |||
| 860 | exit ($C{F} ? 1 : 0); | ||
| 861 | |||
| 862 | __END__ | ||