Method configure takes const xmlNode pointer. Added testfilter2_2 which
[metaproxy-moved-to-github.git] / src / test_filter2.cpp
1 /* $Id: test_filter2.cpp,v 1.10 2005-10-24 09:53:06 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
9
10 #include "config.hpp"
11 #include "filter.hpp"
12 #include "router.hpp"
13 #include "package.hpp"
14
15 #include <iostream>
16
17 #define BOOST_AUTO_TEST_MAIN
18 #include <boost/test/auto_unit_test.hpp>
19
20 using namespace boost::unit_test;
21
22
23 class FilterConstant: public yp2::filter::Base {
24 public:
25     void process(yp2::Package & package) const {
26         package.data() = 1234;
27         package.move();
28     };
29     void configure(const xmlNode* ptr = 0) {
30         m_ptr = ptr;
31
32         BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
33         BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
34
35         const struct _xmlAttr *attr;
36
37         for (attr = ptr->properties; attr; attr = attr->next)
38         {
39             BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
40             const xmlNode *val = attr->children;
41             BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
42             BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
43         }
44         const xmlNode *p = ptr->children;
45         for (; p; p = p->next)
46         {
47             if (p->type != XML_ELEMENT_NODE)
48                 continue;
49
50             BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
51             BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
52
53             const xmlNode *val = p->children;
54             BOOST_CHECK(val);
55             if (!val)
56                 continue;
57
58             BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
59             BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
60         }
61     }
62 private:
63     bool parse_xml_text(const xmlNode *xml_ptr, bool &val);
64     bool parse_xml_text(const xmlNode *xml_ptr, std::string &val);
65 private:
66     const xmlNode *m_ptr;
67 };
68
69
70 bool FilterConstant::parse_xml_text(const xmlNode  *xml_ptr, bool &val)
71 {
72     std::string v;
73     if (!parse_xml_text(xml_ptr, v))
74         return false;
75     if (v.length() == 1 && v[0] == '1')
76         val = true;
77     else
78         val = false;
79     return true;
80 }
81
82 bool FilterConstant::parse_xml_text(const xmlNode *xml_ptr, std::string &val)
83 {
84     xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
85     bool found = false;
86     std::string v;
87     for(ptr = ptr->children; ptr; ptr = ptr->next)
88         if (ptr->type == XML_TEXT_NODE)
89         {
90             xmlChar *t = ptr->content;
91             if (t)
92             {
93                 v += (const char *) t;
94                 found = true;
95             }
96         }
97     if (found)
98         val = v;
99     return found;
100 }
101
102 // This filter dose not have a configure function
103     
104 class FilterDouble: public yp2::filter::Base {
105 public:
106     void process(yp2::Package & package) const {
107         package.data() = package.data() * 2;
108         package.move();
109     };
110 };
111
112     
113 BOOST_AUTO_TEST_CASE( testfilter2_1 ) 
114 {
115     try {
116         FilterConstant fc;
117         fc.name() = "FilterConstant";
118         FilterDouble fd;
119         fd.name() = "FilterDouble";
120
121         {
122             yp2::RouterChain router1;
123             
124             // test filter set/get/exception
125             router1.rule(fc);
126             
127             router1.rule(fd);
128
129             yp2::Session session;
130             yp2::Origin origin;
131             yp2::Package pack(session, origin);
132             
133             pack.router(router1).move(); 
134             
135             BOOST_CHECK (pack.data() == 2468);
136             
137         }
138         
139         {
140             yp2::RouterChain router2;
141             
142             router2.rule(fd);
143             router2.rule(fc);
144             
145             yp2::Session session;
146             yp2::Origin origin;
147             yp2::Package pack(session, origin);
148          
149             pack.router(router2).move();
150      
151             BOOST_CHECK (pack.data() == 1234);
152             
153         }
154
155     }
156     catch (std::exception &e) {
157         std::cout << e.what() << "\n";
158         BOOST_CHECK (false);
159     }
160     catch ( ...) {
161         BOOST_CHECK (false);
162     }
163
164 }
165
166
167 BOOST_AUTO_TEST_CASE( testfilter2_2 ) 
168 {
169     try {
170         FilterConstant fc;
171
172         std::string some_xml = "<?xml version=\"1.0\"?>\n"
173             "<filter type=\"constant\">\n"
174             " <value>2</value>\n"
175             "</filter>";
176         
177         std::cout << some_xml  << std::endl;
178
179         xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
180
181         BOOST_CHECK(doc);
182
183         if (doc)
184         {
185             xmlNodePtr root_element = xmlDocGetRootElement(doc);
186             
187             fc.configure(root_element);
188             
189             FilterDouble fd;
190         }
191     }
192     catch (std::exception &e) {
193         std::cout << e.what() << "\n";
194         BOOST_CHECK (false);
195     }
196     catch ( ...) {
197         BOOST_CHECK (false);
198     }
199
200 }
201
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * indent-tabs-mode: nil
206  * c-file-style: "stroustrup"
207  * End:
208  * vim: shiftwidth=4 tabstop=8 expandtab
209  */