1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
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
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
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
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
24 #include <metaproxy/filter.hpp>
25 #include <metaproxy/router_chain.hpp>
26 #include <metaproxy/package.hpp>
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
32 #include <boost/test/auto_unit_test.hpp>
34 using namespace boost::unit_test;
36 namespace mp = metaproxy_1;
38 class FilterConstant: public mp::filter::Base {
40 FilterConstant() : m_constant(1234) { };
41 void process(mp::Package & package) const {
44 void configure(const xmlNode* ptr, bool test_only, const char *path);
45 int get_constant() const { return m_constant; };
52 void FilterConstant::configure(const xmlNode* ptr, bool test_only,
57 BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
58 BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
60 const struct _xmlAttr *attr;
62 for (attr = ptr->properties; attr; attr = attr->next)
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");
69 const xmlNode *p = ptr->children;
70 for (; p; p = p->next)
72 if (p->type != XML_ELEMENT_NODE)
75 BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
76 BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
78 const xmlNode *val = p->children;
83 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
84 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
86 m_constant = atoi((const char *) val->content);
90 // This filter dose not have a configure function
92 class FilterDouble: public mp::filter::Base {
94 void process(mp::Package & package) const {
97 void configure(const xmlNode * ptr, bool test_only,
98 const char *path) { };
102 BOOST_AUTO_TEST_CASE( testfilter2_1 )
109 mp::RouterChain router1;
111 // test filter set/get/exception
118 mp::Package pack(session, origin);
120 pack.router(router1).move();
122 //BOOST_CHECK_EQUAL(pack.data(), 2468);
127 mp::RouterChain router2;
134 mp::Package pack(session, origin);
136 pack.router(router2).move();
138 //BOOST_CHECK_EQUAL(pack.data(), 1234);
143 catch (std::exception &e) {
144 std::cout << e.what() << "\n";
153 BOOST_AUTO_TEST_CASE( testfilter2_2 )
157 BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
159 mp::filter::Base *base = &fc;
161 std::string some_xml = "<?xml version=\"1.0\"?>\n"
162 "<filter type=\"constant\">\n"
163 " <value>2</value>\n"
166 // std::cout << some_xml << std::endl;
168 xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
174 xmlNodePtr root_element = xmlDocGetRootElement(doc);
176 base->configure(root_element, true, 0);
181 BOOST_CHECK_EQUAL(fc.get_constant(), 2);
183 catch (std::exception &e) {
184 std::cout << e.what() << "\n";
196 * c-file-style: "Stroustrup"
197 * indent-tabs-mode: nil
199 * vim: shiftwidth=4 tabstop=8 expandtab