1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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 "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);
45 int get_constant() const { return m_constant; };
52 void FilterConstant::configure(const xmlNode* ptr, bool test_only)
56 BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
57 BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
59 const struct _xmlAttr *attr;
61 for (attr = ptr->properties; attr; attr = attr->next)
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");
68 const xmlNode *p = ptr->children;
69 for (; p; p = p->next)
71 if (p->type != XML_ELEMENT_NODE)
74 BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
75 BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
77 const xmlNode *val = p->children;
82 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
83 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
85 m_constant = atoi((const char *) val->content);
89 // This filter dose not have a configure function
91 class FilterDouble: public mp::filter::Base {
93 void process(mp::Package & package) const {
99 BOOST_AUTO_TEST_CASE( testfilter2_1 )
106 mp::RouterChain router1;
108 // test filter set/get/exception
115 mp::Package pack(session, origin);
117 pack.router(router1).move();
119 //BOOST_CHECK_EQUAL(pack.data(), 2468);
124 mp::RouterChain router2;
131 mp::Package pack(session, origin);
133 pack.router(router2).move();
135 //BOOST_CHECK_EQUAL(pack.data(), 1234);
140 catch (std::exception &e) {
141 std::cout << e.what() << "\n";
150 BOOST_AUTO_TEST_CASE( testfilter2_2 )
154 BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
156 mp::filter::Base *base = &fc;
158 std::string some_xml = "<?xml version=\"1.0\"?>\n"
159 "<filter type=\"constant\">\n"
160 " <value>2</value>\n"
163 // std::cout << some_xml << std::endl;
165 xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
171 xmlNodePtr root_element = xmlDocGetRootElement(doc);
173 base->configure(root_element, true);
178 BOOST_CHECK_EQUAL(fc.get_constant(), 2);
180 catch (std::exception &e) {
181 std::cout << e.what() << "\n";
193 * c-file-style: "Stroustrup"
194 * indent-tabs-mode: nil
196 * vim: shiftwidth=4 tabstop=8 expandtab