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