Resplit success and failure functions: the latter now reports and
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Ping.pm
1 # $Id: Ping.pm,v 1.24 2007-05-03 14:43:31 mike Exp $
2
3 # See the "Main" test package for documentation
4
5 package ZOOM::IRSpy::Test::Ping;
6
7 use 5.008;
8 use strict;
9 use warnings;
10
11 use ZOOM::IRSpy::Test;
12 our @ISA = qw(ZOOM::IRSpy::Test);
13
14 use ZOOM::IRSpy::Utils qw(isodate);
15
16 use Text::Iconv;
17 my $conv = new Text::Iconv("LATIN1", "UTF8");
18
19
20 sub start {
21     my $class = shift();
22     my($conn) = @_;
23
24     $conn->irspy_connect(undef, {},
25                          ZOOM::Event::ZEND, \&connected,
26                          exception => \&not_connected);
27 }
28
29
30 sub connected {
31     my($conn, $__UNUSED_task, $__UNUSED_udata, $__UNUSED_event) = @_;
32
33     $conn->log("irspy_test", "connected");
34     $conn->record()->store_result("probe", ok => 1);
35
36     foreach my $opt (qw(search present delSet resourceReport
37                         triggerResourceCtrl resourceCtrl
38                         accessCtrl scan sort extendedServices
39                         level_1Segmentation level_2Segmentation
40                         concurrentOperations namedResultSets
41                         encapsulation resultCount negotiationModel
42                         duplicationDetection queryType104
43                         pQESCorrection stringSchema)) {
44         #print STDERR "\$conn->option('init_opt_$opt') = '", $conn->option("init_opt_$opt"), "'\n";
45         $conn->record()->store_result('init_opt', option => $opt)
46             if $conn->option("init_opt_$opt");
47     }
48
49     foreach my $opt (qw(serverImplementationId
50                         serverImplementationName
51                         serverImplementationVersion)) {
52         my $val = $conn->option($opt);
53         next if !defined $val; # not defined for SRU, for example
54
55         # There doesn't seem to be a reliable way to tell what
56         # character set the server uses for these.  At least one
57         # server (z3950.bcl.jcyl.es:210/AbsysCCFL) returns an ISO
58         # 8859-1 string containing an o-acute, which breaks the XML
59         # parser if we just insert it naively.  It seems reasonable,
60         # though, to guess that the great majority of servers will use
61         # ASCII, Latin-1 or Unicode.  The first of these is a subset
62         # of the second, so that brings it to down to two.  The
63         # strategy is simply this: assume it's ASCII-Latin-1, and try
64         # to convert to UTF-8.  If that conversion works, fine; if
65         # not, assume it's because the string was already UTF-8, so
66         # use it as is.
67         Text::Iconv->raise_error(1);
68         my $maybe;
69         eval {
70             $maybe = $conv->convert($val);
71         }; if (!$@ && $maybe ne $val) {
72             $conn->log("irspy", "converted '$val' from Latin-1 to UTF-8");
73             $val = $maybe;
74         }
75         $conn->record()->store_result($opt, value => $val);
76     }
77
78     return ZOOM::IRSpy::Status::TEST_GOOD;
79 }
80
81
82 sub not_connected {
83     my($conn, $__UNUSED_task, $__UNUSED_udata, $exception) = @_;
84
85     $conn->log("irspy_test", "not connected: $exception");
86     $conn->record()->store_result("probe",
87                                   ok => 0,
88                                   errcode => $exception->code(),
89                                   errmsg => $exception->message(),
90                                   addinfo => $exception->addinfo(),
91                                   diagset => $exception->diagset());
92
93     return ZOOM::IRSpy::Status::TEST_BAD;
94 }
95
96
97 1;