52f8935b625076e7f1b22901420f82b5bddc6feb
[idzebra-moved-to-github.git] / test / api / test_resources.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** test resources */
21 #include <stdlib.h>
22 #include <string.h>
23 #include "testlib.h"
24 #include <idzebra/api.h>
25
26 static void tst_res(void)
27 {
28     Res default_res;  /* default resources */
29     Res temp_res;     /* temporary resources */
30     ZebraService zs;
31     ZebraHandle zh;
32     const char *val;
33     int i;
34
35     default_res = res_open(0, 0); /* completely empty */
36     YAZ_CHECK(default_res);
37
38     res_set(default_res, "name1", "value1");
39
40     temp_res = res_open(0, 0); /* completely empty */
41     YAZ_CHECK(temp_res);
42     
43     zs = zebra_start_res(0, default_res, temp_res);
44     YAZ_CHECK(zs);
45     
46     zh = zebra_open(zs, 0);
47     YAZ_CHECK(zh);
48
49     YAZ_CHECK(zebra_select_database(zh, "Default") == ZEBRA_OK);
50
51     /* run this a few times so we can see leaks easier */
52     for (i = 0; i<10; i++)
53     {
54         /* we should find the name1 from default_res */
55         val = zebra_get_resource(zh, "name1", 0);
56         YAZ_CHECK(val && !strcmp(val, "value1"));
57
58         /* make local override */
59         res_set(temp_res, "name1", "value2");
60         res_set(temp_res, "name4", "value4");
61
62         /* we should find the name1 from temp_res */
63         val = zebra_get_resource(zh, "name1", 0);
64         YAZ_CHECK(val && !strcmp(val, "value2"));
65         
66         val = zebra_get_resource(zh, "name4", 0);
67         YAZ_CHECK(val && !strcmp(val, "value4"));
68         
69         res_clear(temp_res);
70     }
71     zebra_close(zh);
72     zebra_stop(zs);
73
74     res_close(temp_res);
75     res_close(default_res);
76 }
77
78 static void tst_no_config(void)
79 {
80     static char *xml_buf = "<gils><title>myx</title></gils>";
81     ZebraService zs;
82     ZebraHandle zh;
83     zint sysno = 0;
84
85     zs = zebra_start_res(0, 0, 0);
86     YAZ_CHECK(zs);
87
88     zh = zebra_open(zs, 0);
89     YAZ_CHECK(zh);
90
91     YAZ_CHECK_EQ(zebra_select_database(zh, "Default"), ZEBRA_OK);
92
93     YAZ_CHECK_EQ(zebra_init(zh), ZEBRA_OK);
94     
95     zebra_set_resource(zh, "profilePath", "${srcdir:-.}/../../tab");
96     
97     YAZ_CHECK_EQ(zebra_update_record(zh /* handle */,
98                                      action_insert,
99                                      "grs.sgml" /* record type */,
100                                      &sysno, 0 /* match */,
101                                      0 /* fname */,
102                                      xml_buf, strlen(xml_buf)),
103               ZEBRA_OK);
104
105     zebra_close(zh);
106     zebra_stop(zs);
107 }
108
109 static void tst(int argc, char **argv)
110 {
111     tst_res();
112     tst_no_config();
113 }
114
115 TL_MAIN
116 /*
117  * Local variables:
118  * c-basic-offset: 4
119  * indent-tabs-mode: nil
120  * End:
121  * vim: shiftwidth=4 tabstop=8 expandtab
122  */
123