aecb773808a8e8931678a97e1c3ae73e538d88ec
[yaz-moved-to-github.git] / test / tst_retrieval.c
1 /*
2  * Copyright (C) 2005-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tst_retrieval.c,v 1.2 2006-05-05 18:37:08 adam Exp $
6  *
7  */
8 #include <yaz/retrieval.h>
9 #include <yaz/test.h>
10 #include <yaz/wrbuf.h>
11 #include <string.h>
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #if HAVE_XSLT
18
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
21
22 yaz_retrieval_t conv_configure(const char *xmlstring, WRBUF w)
23 {
24     xmlDocPtr doc = xmlParseMemory(xmlstring, strlen(xmlstring));
25     if (!doc)
26     {
27         wrbuf_printf(w, "xmlParseMemory");
28         return 0;
29     }
30     else
31     {
32         xmlNodePtr ptr = xmlDocGetRootElement(doc);
33         yaz_retrieval_t p = yaz_retrieval_create();
34
35         if (p)
36         {
37             const char *srcdir = getenv("srcdir");
38             if (srcdir)
39                 yaz_retrieval_set_path(p, srcdir);
40         }
41         if (!ptr)
42         {
43             wrbuf_printf(w, "xmlDocGetRootElement");
44             yaz_retrieval_destroy(p);
45             p = 0;
46         }
47         else if (!p)
48         {
49             wrbuf_printf(w, "yaz_retrieval_create");
50         }
51         else
52         {
53             int r = yaz_retrieval_configure(p, ptr);
54             
55             if (r)
56             {
57                 wrbuf_puts(w, yaz_retrieval_get_error(p));
58                 yaz_retrieval_destroy(p);
59                 p = 0;
60             }
61         }
62         xmlFreeDoc(doc);
63         return p;
64     }    
65 }
66
67 int conv_configure_test(const char *xmlstring, const char *expect_error,
68                         yaz_retrieval_t *pt)
69 {
70     WRBUF w = wrbuf_alloc();
71     int ret;
72
73     yaz_retrieval_t p = conv_configure(xmlstring, w);
74
75     if (!p)
76     {
77         if (expect_error && !strcmp(wrbuf_buf(w), expect_error))
78             ret = 1;
79         else
80         {
81             ret = 0;
82             printf("%.*s\n", wrbuf_len(w), wrbuf_buf(w));
83         }
84     }
85     else
86     {
87         if (expect_error)
88         {
89             ret = 0;
90             yaz_retrieval_destroy(p);
91         }
92         else
93         {
94             if (pt)
95                 *pt = p;
96             else
97                 yaz_retrieval_destroy(p);
98             ret = 1;
99         }
100     }
101     wrbuf_free(w, 1);
102     return ret;
103 }
104
105 static void tst_configure()
106 {
107     YAZ_CHECK(conv_configure_test("<bad", 
108                                   "xmlParseMemory", 0));
109
110     YAZ_CHECK(conv_configure_test("<bad/>", 
111                                   "Missing 'retrievalinfo' element", 0));
112
113     YAZ_CHECK(conv_configure_test("<retrievalinfo/>", 0, 0));
114
115     YAZ_CHECK(conv_configure_test("<retrievalinfo><bad/></retrievalinfo>",
116                                   "Bad element 'bad'."
117                                   " Expected 'retrieval'", 0));
118
119     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
120                                   "<retrieval>"
121                                   "<convert>"
122                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
123                                   "<marc"
124                                   " inputcharset=\"utf-8\""
125                                   " outputcharset=\"marc-8\""
126                                   " inputformat=\"xml\""
127                                   " outputformat=\"marc\""
128                                   "/>"
129                                   "</convert>"
130                                   "</retrieval>"
131                                   "</retrievalinfo>",
132                                   0, 0));
133
134     YAZ_CHECK(conv_configure_test("<retrievalinfo>"
135                                   "<retrieval>"
136                                   "<convert>"
137                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
138                                   "<marc"
139                                   " inputcharset=\"utf-8\""
140                                   " outputcharset=\"marc-8\""
141                                   " inputformat=\"xml\""
142                                   " outputformat=\"marc\""
143                                   "/>"
144                                   "</convert>"
145                                   "</retrieval>"
146                                   "<retrieval>"
147                                   "<convert>"
148                                   "<xslt stylesheet=\"tst_record_conv.xsl\"/>"
149                                   "<marc"
150                                   " inputcharset=\"utf-8\""
151                                   " outputcharset=\"marc-8\""
152                                   " inputformat=\"xml\""
153                                   " outputformat=\"marc\""
154                                   "/>"
155                                   "</convert>"
156                                   "</retrieval>"
157                                   "</retrievalinfo>",
158                                   0, 0));
159 }
160
161 #endif
162
163 int main(int argc, char **argv)
164 {
165     YAZ_CHECK_INIT(argc, argv);
166 #if HAVE_XSLT
167     tst_configure();
168 #endif
169     YAZ_CHECK_TERM;
170 }
171
172 /*
173  * Local variables:
174  * c-basic-offset: 4
175  * indent-tabs-mode: nil
176  * End:
177  * vim: shiftwidth=4 tabstop=8 expandtab
178  */
179