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