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