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