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