filter factory in working shape, see example in test_filer_factory.cpp
[metaproxy-moved-to-github.git] / src / test_filter2.cpp
1 /* $Id: test_filter2.cpp,v 1.14 2005-10-29 22:23:36 marc 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_chain.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     FilterConstant() : m_constant(1234) { };
26     void process(yp2::Package & package) const {
27         package.data() = m_constant;
28         package.move();
29     };
30     const std::string type() const {
31         return "FilterConstant";
32     };
33     void configure(const xmlNode* ptr = 0);
34     int get_constant() const { return m_constant; };
35 private:
36     bool parse_xml_text(const xmlNode *xml_ptr, bool &val);
37     bool parse_xml_text(const xmlNode *xml_ptr, std::string &val);
38 private:
39     const xmlNode *m_ptr;
40     int m_constant;
41 };
42
43
44 void FilterConstant::configure(const xmlNode* ptr)
45 {
46     m_ptr = ptr;
47     
48     BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
49     BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
50     
51     const struct _xmlAttr *attr;
52     
53     for (attr = ptr->properties; attr; attr = attr->next)
54     {
55         BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
56         const xmlNode *val = attr->children;
57         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
58         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
59     }
60     const xmlNode *p = ptr->children;
61     for (; p; p = p->next)
62     {
63         if (p->type != XML_ELEMENT_NODE)
64             continue;
65         
66         BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
67         BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
68         
69         const xmlNode *val = p->children;
70         BOOST_CHECK(val);
71         if (!val)
72             continue;
73         
74         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
75         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
76
77         m_constant = atoi((const char *) val->content);
78     }
79 }
80
81 bool FilterConstant::parse_xml_text(const xmlNode  *xml_ptr, bool &val)
82 {
83     std::string v;
84     if (!parse_xml_text(xml_ptr, v))
85         return false;
86     if (v.length() == 1 && v[0] == '1')
87         val = true;
88     else
89         val = false;
90     return true;
91 }
92
93 bool FilterConstant::parse_xml_text(const xmlNode *xml_ptr, std::string &val)
94 {
95     xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
96     bool found = false;
97     std::string v;
98     for(ptr = ptr->children; ptr; ptr = ptr->next)
99         if (ptr->type == XML_TEXT_NODE)
100         {
101             xmlChar *t = ptr->content;
102             if (t)
103             {
104                 v += (const char *) t;
105                 found = true;
106             }
107         }
108     if (found)
109         val = v;
110     return found;
111 }
112
113 // This filter dose not have a configure function
114     
115 class FilterDouble: public yp2::filter::Base {
116 public:
117     void process(yp2::Package & package) const {
118         package.data() = package.data() * 2;
119         package.move();
120     };
121     const std::string type() const {
122         return "FilterConstant";
123     };
124 };
125
126     
127 BOOST_AUTO_TEST_CASE( testfilter2_1 ) 
128 {
129     try {
130         FilterConstant fc;
131         FilterDouble fd;
132
133         {
134             yp2::RouterChain router1;
135             
136             // test filter set/get/exception
137             router1.append(fc);
138             
139             router1.append(fd);
140
141             yp2::Session session;
142             yp2::Origin origin;
143             yp2::Package pack(session, origin);
144             
145             pack.router(router1).move(); 
146             
147             BOOST_CHECK (pack.data() == 2468);
148             
149         }
150         
151         {
152             yp2::RouterChain router2;
153             
154             router2.append(fd);
155             router2.append(fc);
156             
157             yp2::Session session;
158             yp2::Origin origin;
159             yp2::Package pack(session, origin);
160          
161             pack.router(router2).move();
162      
163             BOOST_CHECK (pack.data() == 1234);
164             
165         }
166
167     }
168     catch (std::exception &e) {
169         std::cout << e.what() << "\n";
170         BOOST_CHECK (false);
171     }
172     catch ( ...) {
173         BOOST_CHECK (false);
174     }
175
176 }
177
178 BOOST_AUTO_TEST_CASE( testfilter2_2 ) 
179 {
180     try {
181         FilterConstant fc;
182         BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
183
184         yp2::filter::Base *base = &fc;
185
186         std::string some_xml = "<?xml version=\"1.0\"?>\n"
187             "<filter type=\"constant\">\n"
188             " <value>2</value>\n"
189             "</filter>";
190         
191         // std::cout << some_xml  << std::endl;
192
193         xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
194
195         BOOST_CHECK(doc);
196
197         if (doc)
198         {
199             xmlNodePtr root_element = xmlDocGetRootElement(doc);
200             
201             base->configure(root_element);
202             
203             xmlFreeDoc(doc);
204         }
205
206         BOOST_CHECK_EQUAL(fc.get_constant(), 2);
207     }
208     catch (std::exception &e) {
209         std::cout << e.what() << "\n";
210         BOOST_CHECK (false);
211     }
212     catch ( ...) {
213         BOOST_CHECK (false);
214     }
215
216 }
217
218 /*
219  * Local variables:
220  * c-basic-offset: 4
221  * indent-tabs-mode: nil
222  * c-file-style: "stroustrup"
223  * End:
224  * vim: shiftwidth=4 tabstop=8 expandtab
225  */