document timeout option
[yaz-moved-to-github.git] / test / tst_retrieval.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5 #include <yaz/retrieval.h>
6 #include <yaz/test.h>
7 #include <yaz/wrbuf.h>
8 #include <string.h>
9 #include <yaz/log.h>
10 #include <yaz/libxml2_error.h>
11
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #if YAZ_HAVE_XSLT
17
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20
21 yaz_retrieval_t conv_configure(const char *xmlstring, WRBUF w)
22 {
23     xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
24     if (!doc)
25     {
26         wrbuf_printf(w, "xmlParseMemory");
27         return 0;
28     }
29     else
30     {
31         xmlNodePtr ptr = xmlDocGetRootElement(doc);
32         yaz_retrieval_t p = yaz_retrieval_create();
33
34         if (p)
35         {
36             const char *srcdir = getenv("srcdir");
37             if (srcdir)
38                 yaz_retrieval_set_path(p, srcdir);
39         }
40         if (!ptr)
41         {
42             wrbuf_printf(w, "xmlDocGetRootElement");
43             yaz_retrieval_destroy(p);
44             p = 0;
45         }
46         else if (!p)
47         {
48             wrbuf_printf(w, "yaz_retrieval_create");
49         }
50         else
51         {
52             int r = yaz_retrieval_configure(p, ptr);
53             
54             if (r)
55             {
56                 wrbuf_puts(w, yaz_retrieval_get_error(p));
57                 yaz_retrieval_destroy(p);
58                 p = 0;
59             }
60         }
61         xmlFreeDoc(doc);
62         return p;
63     }    
64 }
65
66 int conv_configure_test(const char *xmlstring, const char *expect_error,
67                         yaz_retrieval_t *pt)
68 {
69     WRBUF w = wrbuf_alloc();
70     int ret;
71
72     yaz_retrieval_t p = conv_configure(xmlstring, w);
73
74     if (!p)
75     {
76         if (expect_error && !strcmp(wrbuf_cstr(w), expect_error))
77             ret = 1;
78         else
79         {
80             ret = 0;
81             printf("%s\n", wrbuf_cstr(w));
82         }
83     }
84     else
85     {
86         if (expect_error)
87         {
88             ret = 0;
89             yaz_retrieval_destroy(p);
90         }
91         else
92         {
93             if (pt)
94                 *pt = p;
95             else
96                 yaz_retrieval_destroy(p);
97             ret = 1;
98         }
99     }
100     wrbuf_destroy(w);
101     return ret;
102 }
103
104 static void tst_configure(void)
105 {
106     YAZ_CHECK(conv_configure_test("<bad", 
107                                   "xmlParseMemory", 0));
108
109     YAZ_CHECK(conv_configure_test("<bad/>", 
110                                   "Expected element <retrievalinfo>", 0));
111
112     YAZ_CHECK(conv_configure_test("<retrievalinfo/>", 0, 0));
113
114     YAZ_CHECK(conv_configure_test("<retrievalinfo><bad/></retrievalinfo>",
115                                   "Element <retrievalinfo>:"
116                                   " expected element <retrieval>, got <bad>",
117                                   0));
118
119     YAZ_CHECK(conv_configure_test("<retrievalinfo><retrieval/>"
120                                   "</retrievalinfo>",
121                                   "Missing 'syntax' attribute", 0));
122
123
124     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
125                                   "<retrieval" 
126                                   " unknown=\"unknown\""
127                                   ">"
128                                   "</retrieval>"
129                                   "</retrievalinfo>",
130                                   "Element <retrieval>:  expected attributes "
131                                   "'syntax', identifier' or 'name', got "
132                                   "'unknown'", 0));
133
134     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
135                                   "<retrieval" 
136                                   " syntax=\"unknown_synt\""
137                                   ">"
138                                   "</retrieval>"
139                                   "</retrievalinfo>",
140                                   "Element <retrieval>:  unknown attribute "
141                                   "value syntax='unknown_synt'", 0));
142
143     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
144                                   "<retrieval" 
145                                   " syntax=\"usmarc\""
146                                   "/>"
147                                   "</retrievalinfo>",
148                                   0, 0));
149
150     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
151                                   "<retrieval" 
152                                   " syntax=\"usmarc\""
153                                   " name=\"marcxml\"/>"
154                                   "</retrievalinfo>",
155                                   0, 0));
156
157
158     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
159                                   "<retrieval" 
160                                   " syntax=\"usmarc\""
161                                   " name=\"marcxml\"" 
162                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
163                                   "/>"
164                                   "</retrievalinfo>",
165                                   0, 0));
166
167
168
169     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
170                                   "<retrieval" 
171                                   " syntax=\"usmarc\""
172                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
173                                   " name=\"marcxml\">"
174                                   "<convert/>"
175                                   "</retrieval>" 
176                                   "</retrievalinfo>",
177                                   "Element <retrieval>: expected zero or one element "
178                                   "<backend>, got <convert>", 0));
179
180     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
181                                   "<retrieval" 
182                                   " syntax=\"usmarc\""
183                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
184                                   " name=\"marcxml\">"
185                                   " <backend syntax=\"usmarc\""
186                                   " schema=\"marcxml\""
187                                   "/>"
188                                   "</retrieval>"
189                                   "</retrievalinfo>",
190                                   "Element <backend>: expected attributes 'syntax' or 'name,"
191                                   " got 'schema'", 0));
192
193     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
194                                   "<retrieval" 
195                                   " syntax=\"usmarc\""
196                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
197                                   " name=\"marcxml\">"
198                                   " <backend syntax=\"usmarc\""
199                                   " name=\"marcxml\""
200                                   "/>"
201                                   "</retrieval>"
202                                   "</retrievalinfo>",
203                                   0, 0));
204
205     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
206                                   "<retrieval" 
207                                   " syntax=\"usmarc\""
208                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
209                                   " name=\"marcxml\">"
210                                   " <backend syntax=\"unknown\""
211                                   "/>"
212                                   "</retrieval>"
213                                   "</retrievalinfo>",
214                                   "Element <backend syntax='unknown'>: "
215                                   "attribute 'syntax' has invalid value "
216                                   "'unknown'", 0));
217
218
219     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
220                                   "<retrieval" 
221                                   " syntax=\"usmarc\""
222                                   " identifier=\"info:srw/schema/1/marcxml-v1.1\""
223                                     " name=\"marcxml\">"
224                                   " <backend syntax=\"usmarc\""
225                                   " unknown=\"silly\""
226                                   "/>"
227                                   "</retrieval>"
228                                   "</retrievalinfo>",
229                                   "Element <backend>: expected attributes "
230                                   "'syntax' or 'name, got 'unknown'", 0));
231
232
233     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
234                                   "<retrieval syntax=\"usmarc\">"
235                                   "<backend syntax=\"xml\" name=\"dc\">"
236                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
237                                   "<marc"
238                                   " inputcharset=\"utf-8\""
239                                   " outputcharset=\"non-existent\""
240                                   " inputformat=\"xml\""
241                                   " outputformat=\"marc\""
242                                   "/>"
243                                   "</backend>"
244                                   "</retrieval>"
245                                   "</retrievalinfo>",
246                                   "Element <marc inputcharset='utf-8'"
247                                   " outputcharset='non-existent'>: Unsupported character"
248                                   " set mapping defined by attribute values", 0));
249
250     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
251                                   "<retrieval syntax=\"usmarc\">"
252                                   "<backend syntax=\"xml\" name=\"dc\">"
253                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
254                                   "<marc"
255                                   " inputcharset=\"utf-8\""
256                                   " outputcharset=\"marc-8\""
257                                   " inputformat=\"not-existent\""
258                                   " outputformat=\"marc\""
259                                   "/>"
260                                   "</backend>"
261                                   "</retrieval>"
262                                   "</retrievalinfo>",
263                                   "Element <marc inputformat='not-existent'>:  Unsupported"
264                                   " input format defined by attribute value", 0));
265
266     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
267                                   "<retrieval syntax=\"usmarc\">"
268                                   "<backend syntax=\"xml\" name=\"dc\">"
269                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
270                                   "<marc"
271                                   " inputcharset=\"utf-8\""
272                                   " outputcharset=\"marc-8\""
273                                   " inputformat=\"xml\""
274                                   " outputformat=\"marc\""
275                                   "/>"
276                                   "</backend>"
277                                   "</retrieval>"
278                                   "</retrievalinfo>",
279                                   0, 0));
280
281     YAZ_CHECK(conv_configure_test(
282                                   "<retrievalinfo "
283                                   " xmlns=\"http://indexdata.com/yaz\" version=\"1.0\">"
284                                   "<retrieval syntax=\"grs-1\"/>"
285                                   "<retrieval syntax=\"usmarc\" name=\"F\"/>"
286                                   "<retrieval syntax=\"usmarc\" name=\"B\"/>"
287                                   "<retrieval syntax=\"xml\" name=\"marcxml\" "
288                                   "           identifier=\"info:srw/schema/1/marcxml-v1.1\">"
289                                   "  <backend syntax=\"usmarc\" name=\"F\">"
290                                   "    <marc inputformat=\"marc\" outputformat=\"marcxml\" "
291                                   "            inputcharset=\"marc-8\"/>"
292                                   "  </backend>"
293                                   "</retrieval>"
294                                   "<retrieval syntax=\"xml\" name=\"danmarc\">"
295                                   "  <backend syntax=\"usmarc\" name=\"F\">"
296                                   "    <marc inputformat=\"marc\" outputformat=\"marcxchange\" "
297                                   "          inputcharset=\"marc-8\"/>"
298                                   "  </backend>"
299                                   "</retrieval>"
300                                   "<retrieval syntax=\"xml\" name=\"dc\" "
301                                   "           identifier=\"info:srw/schema/1/dc-v1.1\">"
302                                   "  <backend syntax=\"usmarc\" name=\"F\">"
303                                   "    <marc inputformat=\"marc\" outputformat=\"marcxml\" "
304                                   "          inputcharset=\"marc-8\"/>"
305                                   "    <xslt stylesheet=\"tst_record_conv.xsl\"/> "
306                                   "  </backend>"
307                                   "</retrieval>"
308                                   "</retrievalinfo>",
309                                   0, 0));
310
311 }
312
313 #endif
314
315 int main(int argc, char **argv)
316 {
317     YAZ_CHECK_INIT(argc, argv);
318
319     libxml2_error_to_yazlog(0 /* disable it */, "");
320
321 #if YAZ_HAVE_XSLT
322     tst_configure();
323 #endif
324     YAZ_CHECK_TERM;
325 }
326
327 /*
328  * Local variables:
329  * c-basic-offset: 4
330  * c-file-style: "Stroustrup"
331  * indent-tabs-mode: nil
332  * End:
333  * vim: shiftwidth=4 tabstop=8 expandtab
334  */
335