Happy new year
[metaproxy-moved-to-github.git] / src / test_filter2.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
23
24 #include <metaproxy/filter.hpp>
25 #include "router_chain.hpp"
26 #include <metaproxy/package.hpp>
27
28 #include <iostream>
29
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
32 #include <boost/test/auto_unit_test.hpp>
33
34 using namespace boost::unit_test;
35
36 namespace mp = metaproxy_1;
37
38 class FilterConstant: public mp::filter::Base {
39 public:
40     FilterConstant() : m_constant(1234) { };
41     void process(mp::Package & package) const {
42         package.move();
43     };
44     void configure(const xmlNode* ptr, bool test_only);
45     int get_constant() const { return m_constant; };
46 private:
47     const xmlNode *m_ptr;
48     int m_constant;
49 };
50
51
52 void FilterConstant::configure(const xmlNode* ptr, bool test_only)
53 {
54     m_ptr = ptr;
55
56     BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
57     BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
58     
59     const struct _xmlAttr *attr;
60     
61     for (attr = ptr->properties; attr; attr = attr->next)
62     {
63         BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
64         const xmlNode *val = attr->children;
65         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
66         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
67     }
68     const xmlNode *p = ptr->children;
69     for (; p; p = p->next)
70     {
71         if (p->type != XML_ELEMENT_NODE)
72             continue;
73         
74         BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
75         BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
76         
77         const xmlNode *val = p->children;
78         BOOST_CHECK(val);
79         if (!val)
80             continue;
81         
82         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
83         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
84
85         m_constant = atoi((const char *) val->content);
86     }
87 }
88
89 // This filter dose not have a configure function
90     
91 class FilterDouble: public mp::filter::Base {
92 public:
93     void process(mp::Package & package) const {
94         package.move();
95     };
96 };
97
98
99 BOOST_AUTO_TEST_CASE( testfilter2_1 ) 
100 {
101     try {
102         FilterConstant fc;
103         FilterDouble fd;
104
105         {
106             mp::RouterChain router1;
107             
108             // test filter set/get/exception
109             router1.append(fc);
110             
111             router1.append(fd);
112
113             mp::Session session;
114             mp::Origin origin;
115             mp::Package pack(session, origin);
116             
117             pack.router(router1).move(); 
118             
119             //BOOST_CHECK_EQUAL(pack.data(), 2468);
120             
121         }
122         
123         {
124             mp::RouterChain router2;
125             
126             router2.append(fd);
127             router2.append(fc);
128             
129             mp::Session session;
130             mp::Origin origin;
131             mp::Package pack(session, origin);
132          
133             pack.router(router2).move();
134      
135             //BOOST_CHECK_EQUAL(pack.data(), 1234);
136             
137         }
138
139     }
140     catch (std::exception &e) {
141         std::cout << e.what() << "\n";
142         BOOST_CHECK (false);
143     }
144     catch ( ...) {
145         BOOST_CHECK (false);
146     }
147
148 }
149
150 BOOST_AUTO_TEST_CASE( testfilter2_2 ) 
151 {
152     try {
153         FilterConstant fc;
154         BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
155
156         mp::filter::Base *base = &fc;
157
158         std::string some_xml = "<?xml version=\"1.0\"?>\n"
159             "<filter type=\"constant\">\n"
160             " <value>2</value>\n"
161             "</filter>";
162         
163         // std::cout << some_xml  << std::endl;
164
165         xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
166
167         BOOST_CHECK(doc);
168
169         if (doc)
170         {
171             xmlNodePtr root_element = xmlDocGetRootElement(doc);
172             
173             base->configure(root_element, true);
174             
175             xmlFreeDoc(doc);
176         }
177
178         BOOST_CHECK_EQUAL(fc.get_constant(), 2);
179     }
180     catch (std::exception &e) {
181         std::cout << e.what() << "\n";
182         BOOST_CHECK (false);
183     }
184     catch ( ...) {
185         BOOST_CHECK (false);
186     }
187
188 }
189
190 /*
191  * Local variables:
192  * c-basic-offset: 4
193  * c-file-style: "Stroustrup"
194  * indent-tabs-mode: nil
195  * End:
196  * vim: shiftwidth=4 tabstop=8 expandtab
197  */
198