1 /* $Id: test_filter2.cpp,v 1.14 2005-10-29 22:23:36 marc Exp $
2 Copyright (c) 2005, Index Data.
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
12 #include "router_chain.hpp"
13 #include "package.hpp"
17 #define BOOST_AUTO_TEST_MAIN
18 #include <boost/test/auto_unit_test.hpp>
20 using namespace boost::unit_test;
23 class FilterConstant: public yp2::filter::Base {
25 FilterConstant() : m_constant(1234) { };
26 void process(yp2::Package & package) const {
27 package.data() = m_constant;
30 const std::string type() const {
31 return "FilterConstant";
33 void configure(const xmlNode* ptr = 0);
34 int get_constant() const { return m_constant; };
36 bool parse_xml_text(const xmlNode *xml_ptr, bool &val);
37 bool parse_xml_text(const xmlNode *xml_ptr, std::string &val);
44 void FilterConstant::configure(const xmlNode* ptr)
48 BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
49 BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
51 const struct _xmlAttr *attr;
53 for (attr = ptr->properties; attr; attr = attr->next)
55 BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
56 const xmlNode *val = attr->children;
57 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
58 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
60 const xmlNode *p = ptr->children;
61 for (; p; p = p->next)
63 if (p->type != XML_ELEMENT_NODE)
66 BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
67 BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
69 const xmlNode *val = p->children;
74 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
75 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
77 m_constant = atoi((const char *) val->content);
81 bool FilterConstant::parse_xml_text(const xmlNode *xml_ptr, bool &val)
84 if (!parse_xml_text(xml_ptr, v))
86 if (v.length() == 1 && v[0] == '1')
93 bool FilterConstant::parse_xml_text(const xmlNode *xml_ptr, std::string &val)
95 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
98 for(ptr = ptr->children; ptr; ptr = ptr->next)
99 if (ptr->type == XML_TEXT_NODE)
101 xmlChar *t = ptr->content;
104 v += (const char *) t;
113 // This filter dose not have a configure function
115 class FilterDouble: public yp2::filter::Base {
117 void process(yp2::Package & package) const {
118 package.data() = package.data() * 2;
121 const std::string type() const {
122 return "FilterConstant";
127 BOOST_AUTO_TEST_CASE( testfilter2_1 )
134 yp2::RouterChain router1;
136 // test filter set/get/exception
141 yp2::Session session;
143 yp2::Package pack(session, origin);
145 pack.router(router1).move();
147 BOOST_CHECK (pack.data() == 2468);
152 yp2::RouterChain router2;
157 yp2::Session session;
159 yp2::Package pack(session, origin);
161 pack.router(router2).move();
163 BOOST_CHECK (pack.data() == 1234);
168 catch (std::exception &e) {
169 std::cout << e.what() << "\n";
178 BOOST_AUTO_TEST_CASE( testfilter2_2 )
182 BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
184 yp2::filter::Base *base = &fc;
186 std::string some_xml = "<?xml version=\"1.0\"?>\n"
187 "<filter type=\"constant\">\n"
188 " <value>2</value>\n"
191 // std::cout << some_xml << std::endl;
193 xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
199 xmlNodePtr root_element = xmlDocGetRootElement(doc);
201 base->configure(root_element);
206 BOOST_CHECK_EQUAL(fc.get_constant(), 2);
208 catch (std::exception &e) {
209 std::cout << e.what() << "\n";
221 * indent-tabs-mode: nil
222 * c-file-style: "stroustrup"
224 * vim: shiftwidth=4 tabstop=8 expandtab