Ignore tst_rpn2cql generated test program
[yaz-moved-to-github.git] / test / tst_record_conv.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5 #include <yaz/record_conv.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_XML2
17
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20
21 yaz_record_conv_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_record_conv_t p = yaz_record_conv_create();
33
34         if (p)
35         {
36             const char *srcdir = getenv("srcdir");
37             if (srcdir)
38                 yaz_record_conv_set_path(p, srcdir);
39         }
40         if (!ptr)
41         {
42             wrbuf_printf(w, "xmlDocGetRootElement");
43             yaz_record_conv_destroy(p);
44             p = 0;
45         }
46         else if (!p)
47         {
48             wrbuf_printf(w, "yaz_record_conv_create");
49         }
50         else
51         {
52
53
54             int r = yaz_record_conv_configure(p, ptr);
55             
56             if (r)
57             {
58                 wrbuf_puts(w, yaz_record_conv_get_error(p));
59                 yaz_record_conv_destroy(p);
60                 p = 0;
61             }
62         }
63         xmlFreeDoc(doc);
64         return p;
65     }    
66 }
67
68 int conv_configure_test(const char *xmlstring, const char *expect_error,
69                         yaz_record_conv_t *pt)
70 {
71     WRBUF w = wrbuf_alloc();
72     int ret;
73
74     yaz_record_conv_t p = conv_configure(xmlstring, w);
75
76     if (!p)
77     {
78         if (expect_error && !strcmp(wrbuf_cstr(w), expect_error))
79             ret = 1;
80         else
81         {
82             ret = 0;
83             printf("%s\n", wrbuf_cstr(w));
84         }
85     }
86     else
87     {
88         if (expect_error)
89             ret = 0;
90         else
91             ret = 1;
92     }
93
94     if (pt)
95         *pt = p;
96     else
97         if (p)
98             yaz_record_conv_destroy(p);
99
100     wrbuf_destroy(w);
101     return ret;
102 }
103
104 static void tst_configure(void)
105 {
106
107
108
109     YAZ_CHECK(conv_configure_test("<bad", "xmlParseMemory", 0));
110
111
112     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
113                                   "<bad/></backend>",
114                                   "Element <backend>: expected <marc> or "
115                                   "<xslt> element, got <bad>", 0));
116
117 #if YAZ_HAVE_XSLT
118     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
119                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
120                                   "<marc"
121                                   " inputcharset=\"marc-8\""
122                                   " outputcharset=\"marc-8\""
123                                   "/>"
124                                   "</backend>",
125                                   "Element <marc>: attribute 'inputformat' "
126                                   "required", 0));
127     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
128                                   "<xslt/>"
129                                   "</backend>",
130                                   "Element <xslt>: attribute 'stylesheet' "
131                                   "expected", 0));
132     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
133                                   "<marc"
134                                   " inputcharset=\"utf-8\""
135                                   " outputcharset=\"marc-8\""
136                                   " inputformat=\"xml\""
137                                   " outputformat=\"marc\""
138                                   "/>"
139                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
140                                   "</backend>",
141                                   0, 0));
142 #else
143     YAZ_CHECK(conv_configure_test("<backend syntax='usmarc' name='F'>"
144                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
145                                   "</backend>",
146                                   "xslt unsupported."
147                                   " YAZ compiled without XSLT support", 0));
148 #endif 
149 }
150
151 static int conv_convert_test(yaz_record_conv_t p,
152                              const char *input_record,
153                              const char *output_expect_record)
154 {
155     int ret = 0;
156     if (!p)
157     {
158         YAZ_CHECK(ret);
159     }
160     else
161     {
162         WRBUF output_record = wrbuf_alloc();
163         int r = yaz_record_conv_record(p, input_record, strlen(input_record),
164                                        output_record);
165         if (r)
166         {
167             if (output_expect_record)
168             {
169                 printf("yaz_record_conv error=%s\n",
170                        yaz_record_conv_get_error(p));
171                 ret = 0;
172             }
173             else
174                 ret = 1;
175         }
176         else
177         {
178             if (!output_expect_record)
179             {
180                 ret = 0;
181             }
182             else if (strcmp(output_expect_record, wrbuf_cstr(output_record)))
183             {
184                 ret = 0;
185                 printf("got-output_record len=%d: %s\n", 
186                        wrbuf_len(output_record),wrbuf_cstr(output_record));
187                 printf("output_expect_record len=%d %s\n",
188                        strlen(output_expect_record), output_expect_record);
189             }
190             else
191             {
192                 ret = 1;
193             }
194         }
195         wrbuf_destroy(output_record);
196     }
197     return ret;
198 }
199
200 static int conv_convert_test_iter(yaz_record_conv_t p,
201                                   const char *input_record,
202                                   const char *output_expect_record,
203                                   int num_iter)
204 {
205     int i;
206     int ret;
207     for (i = 0; i < num_iter; i++)
208     {
209         ret = conv_convert_test(p, input_record, output_expect_record);
210         if (!ret)
211             break;
212     }
213     return ret;
214 }
215
216 static void tst_convert1(void)
217 {
218     yaz_record_conv_t p = 0;
219     const char *marcxml_rec =
220         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
221         "  <leader>00080nam a22000498a 4500</leader>\n"
222         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
223         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
224         "    <subfield code=\"a\">   11224466 </subfield>\n"
225         "  </datafield>\n"
226         "</record>\n";
227     const char *iso2709_rec =
228         "\x30\x30\x30\x38\x30\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
229         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
230         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x37\x30\x30\x30\x31\x33"
231         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
232         "\x1F\x61\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
233
234     YAZ_CHECK(conv_configure_test("<backend>"
235                                   "<marc"
236                                   " inputcharset=\"utf-8\""
237                                   " outputcharset=\"marc-8\""
238                                   " inputformat=\"xml\""
239                                   " outputformat=\"marc\""
240                                   "/>"
241                                   "</backend>",
242                                   0, &p));
243     YAZ_CHECK(conv_convert_test(p, marcxml_rec, iso2709_rec));
244     yaz_record_conv_destroy(p);
245
246     YAZ_CHECK(conv_configure_test("<backend>"
247                                   "<marc"
248                                   " outputcharset=\"utf-8\""
249                                   " inputcharset=\"marc-8\""
250                                   " outputformat=\"marcxml\""
251                                   " inputformat=\"marc\""
252                                   "/>"
253                                   "</backend>",
254                                   0, &p));
255     YAZ_CHECK(conv_convert_test(p, iso2709_rec, marcxml_rec));
256     yaz_record_conv_destroy(p);
257
258
259     YAZ_CHECK(conv_configure_test("<backend>"
260                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
261                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
262                                   "<marc"
263                                   " inputcharset=\"utf-8\""
264                                   " outputcharset=\"marc-8\""
265                                   " inputformat=\"xml\""
266                                   " outputformat=\"marc\""
267                                   "/>"
268                                   "<marc"
269                                   " outputcharset=\"utf-8\""
270                                   " inputcharset=\"marc-8\""
271                                   " outputformat=\"marcxml\""
272                                   " inputformat=\"marc\""
273                                   "/>"
274                                   "</backend>",
275                                   0, &p));
276     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
277     yaz_record_conv_destroy(p);
278
279
280     YAZ_CHECK(conv_configure_test("<backend>"
281                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
282                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
283                                   "<marc"
284                                   " outputcharset=\"marc-8\""
285                                   " inputformat=\"xml\""
286                                   " outputformat=\"marc\""
287                                   "/>"
288                                   "<marc"
289                                   " inputcharset=\"marc-8\""
290                                   " outputformat=\"marcxml\""
291                                   " inputformat=\"marc\""
292                                   "/>"
293                                   "</backend>",
294                                   0, &p));
295     YAZ_CHECK(conv_convert_test(p, marcxml_rec, marcxml_rec));
296     yaz_record_conv_destroy(p);
297 }
298
299 static void tst_convert2(void)
300 {
301     yaz_record_conv_t p = 0;
302     const char *marcxml_rec =
303         "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
304         "  <leader>00080nam a22000498a 4500</leader>\n"
305         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
306         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
307         "    <subfield code=\"a\">k&#xf8;benhavn</subfield>\n"
308         "  </datafield>\n"
309         "</record>\n";
310     const char *iso2709_rec =
311         "\x30\x30\x30\x37\x37\x6E\x61\x6D\x20\x61\x32\x32\x30\x30\x30\x34"
312         "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
313         "\x30\x30\x30\x30\x30\x31\x30\x30\x30\x31\x34\x30\x30\x30\x31\x33"
314         "\x1E\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x20\x20"
315         "\x1F\x61\x6b\xb2\x62\x65\x6e\x68\x61\x76\x6e\x1E\x1D";
316
317     YAZ_CHECK(conv_configure_test("<backend>"
318                                   "<marc"
319                                   " inputcharset=\"utf-8\""
320                                   " outputcharset=\"marc-8\""
321                                   " inputformat=\"xml\""
322                                   " outputformat=\"marc\""
323                                   "/>"
324                                   "</backend>",
325                                   0, &p));
326     YAZ_CHECK(conv_convert_test_iter(p, marcxml_rec, iso2709_rec, 100));
327     yaz_record_conv_destroy(p);
328 }
329
330 #endif
331
332 int main(int argc, char **argv)
333 {
334     YAZ_CHECK_INIT(argc, argv);
335     libxml2_error_to_yazlog(0 /* disable log */, 0);
336 #if YAZ_HAVE_XML2
337     tst_configure();
338 #endif
339 #if  YAZ_HAVE_XSLT 
340     tst_convert1();
341     tst_convert2();
342 #endif
343     YAZ_CHECK_TERM;
344 }
345
346 /*
347  * Local variables:
348  * c-basic-offset: 4
349  * indent-tabs-mode: nil
350  * End:
351  * vim: shiftwidth=4 tabstop=8 expandtab
352  */
353