Added test t13 which illustrates the resource API and tests it.
[idzebra-moved-to-github.git] / test / api / t13.c
1 /* $Id: t13.c,v 1.1 2005-08-17 21:30:31 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 /** test resources */
24 #include <stdlib.h>
25 #include <string.h>
26 #include <idzebra/api.h>
27
28 void tst()
29 {
30     Res default_res;  /* default resources */
31     Res temp_res;     /* temporary resources */
32     ZebraService zs;
33     ZebraHandle zh;
34     const char *val;
35     int i;
36
37     default_res = res_open(0, 0, 0); /* completely empty */
38     if (!default_res)
39         exit(1);
40     res_set(default_res, "name1", "value1");
41
42     temp_res = res_open(0, 0, 0); /* completely empty */
43     if (!temp_res)
44         exit(1);
45     
46     zs = zebra_start_res(0, default_res, temp_res);
47     if (!zs)
48         exit(2);
49     
50     zh = zebra_open(zs);
51
52     zebra_select_database(zh, "Default");
53
54     /* run this a few times so we can see leaks easier */
55     for (i = 0; i<10; i++)
56     {
57         /* we should find the name1 from default_res */
58         val = zebra_get_resource(zh, "name1", 0);
59         if (!val || strcmp(val, "value1"))
60             exit(3);
61
62         /* make local override */
63         res_set(temp_res, "name1", "value2");
64         res_set(temp_res, "name4", "value4");
65
66         /* we should find the name1 from temp_res */
67         val = zebra_get_resource(zh, "name1", 0);
68         if (!val || strcmp(val, "value2"))
69             exit(4);
70         
71         val = zebra_get_resource(zh, "name4", 0);
72         if (!val || strcmp(val, "value4"))
73             exit(4);
74         
75         res_clear(temp_res);
76     }
77
78     zebra_close(zh);
79     zebra_stop(zs);
80
81     res_close(temp_res);
82     res_close(default_res);
83 }
84
85 int main(int argc, char **argv)
86 {
87     tst();
88     exit(0);
89 }