Year 2007.
[metaproxy-moved-to-github.git] / src / test_filter2.cpp
1 /* $Id: test_filter2.cpp,v 1.22 2007-01-25 14:05:54 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4    See the LICENSE file for details
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 namespace mp = metaproxy_1;
23
24 class FilterConstant: public mp::filter::Base {
25 public:
26     FilterConstant() : m_constant(1234) { };
27     void process(mp::Package & package) const {
28         package.move();
29     };
30     void configure(const xmlNode* ptr = 0);
31     int get_constant() const { return m_constant; };
32 private:
33     const xmlNode *m_ptr;
34     int m_constant;
35 };
36
37
38 void FilterConstant::configure(const xmlNode* ptr)
39 {
40     m_ptr = ptr;
41     
42     BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
43     BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
44     
45     const struct _xmlAttr *attr;
46     
47     for (attr = ptr->properties; attr; attr = attr->next)
48     {
49         BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
50         const xmlNode *val = attr->children;
51         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
52         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
53     }
54     const xmlNode *p = ptr->children;
55     for (; p; p = p->next)
56     {
57         if (p->type != XML_ELEMENT_NODE)
58             continue;
59         
60         BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
61         BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
62         
63         const xmlNode *val = p->children;
64         BOOST_CHECK(val);
65         if (!val)
66             continue;
67         
68         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
69         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
70
71         m_constant = atoi((const char *) val->content);
72     }
73 }
74
75 // This filter dose not have a configure function
76     
77 class FilterDouble: public mp::filter::Base {
78 public:
79     void process(mp::Package & package) const {
80         package.move();
81     };
82 };
83
84     
85 BOOST_AUTO_UNIT_TEST( testfilter2_1 ) 
86 {
87     try {
88         FilterConstant fc;
89         FilterDouble fd;
90
91         {
92             mp::RouterChain router1;
93             
94             // test filter set/get/exception
95             router1.append(fc);
96             
97             router1.append(fd);
98
99             mp::Session session;
100             mp::Origin origin;
101             mp::Package pack(session, origin);
102             
103             pack.router(router1).move(); 
104             
105             //BOOST_CHECK_EQUAL(pack.data(), 2468);
106             
107         }
108         
109         {
110             mp::RouterChain router2;
111             
112             router2.append(fd);
113             router2.append(fc);
114             
115             mp::Session session;
116             mp::Origin origin;
117             mp::Package pack(session, origin);
118          
119             pack.router(router2).move();
120      
121             //BOOST_CHECK_EQUAL(pack.data(), 1234);
122             
123         }
124
125     }
126     catch (std::exception &e) {
127         std::cout << e.what() << "\n";
128         BOOST_CHECK (false);
129     }
130     catch ( ...) {
131         BOOST_CHECK (false);
132     }
133
134 }
135
136 BOOST_AUTO_UNIT_TEST( testfilter2_2 ) 
137 {
138     try {
139         FilterConstant fc;
140         BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
141
142         mp::filter::Base *base = &fc;
143
144         std::string some_xml = "<?xml version=\"1.0\"?>\n"
145             "<filter type=\"constant\">\n"
146             " <value>2</value>\n"
147             "</filter>";
148         
149         // std::cout << some_xml  << std::endl;
150
151         xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
152
153         BOOST_CHECK(doc);
154
155         if (doc)
156         {
157             xmlNodePtr root_element = xmlDocGetRootElement(doc);
158             
159             base->configure(root_element);
160             
161             xmlFreeDoc(doc);
162         }
163
164         BOOST_CHECK_EQUAL(fc.get_constant(), 2);
165     }
166     catch (std::exception &e) {
167         std::cout << e.what() << "\n";
168         BOOST_CHECK (false);
169     }
170     catch ( ...) {
171         BOOST_CHECK (false);
172     }
173
174 }
175
176 /*
177  * Local variables:
178  * c-basic-offset: 4
179  * indent-tabs-mode: nil
180  * c-file-style: "stroustrup"
181  * End:
182  * vim: shiftwidth=4 tabstop=8 expandtab
183  */