1 /* $Id: test_filter2.cpp,v 1.17 2006-01-09 13:43:59 adam 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 void configure(const xmlNode* ptr = 0);
31 int get_constant() const { return m_constant; };
33 bool parse_xml_text(const xmlNode *xml_ptr, bool &val);
34 bool parse_xml_text(const xmlNode *xml_ptr, std::string &val);
41 void FilterConstant::configure(const xmlNode* ptr)
45 BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
46 BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
48 const struct _xmlAttr *attr;
50 for (attr = ptr->properties; attr; attr = attr->next)
52 BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
53 const xmlNode *val = attr->children;
54 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
55 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
57 const xmlNode *p = ptr->children;
58 for (; p; p = p->next)
60 if (p->type != XML_ELEMENT_NODE)
63 BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
64 BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
66 const xmlNode *val = p->children;
71 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
72 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
74 m_constant = atoi((const char *) val->content);
78 bool FilterConstant::parse_xml_text(const xmlNode *xml_ptr, bool &val)
81 if (!parse_xml_text(xml_ptr, v))
83 if (v.length() == 1 && v[0] == '1')
90 bool FilterConstant::parse_xml_text(const xmlNode *xml_ptr, std::string &val)
92 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
95 for(ptr = ptr->children; ptr; ptr = ptr->next)
96 if (ptr->type == XML_TEXT_NODE)
98 xmlChar *t = ptr->content;
101 v += (const char *) t;
110 // This filter dose not have a configure function
112 class FilterDouble: public yp2::filter::Base {
114 void process(yp2::Package & package) const {
115 package.data() = package.data() * 2;
121 BOOST_AUTO_UNIT_TEST( testfilter2_1 )
128 yp2::RouterChain router1;
130 // test filter set/get/exception
135 yp2::Session session;
137 yp2::Package pack(session, origin);
139 pack.router(router1).move();
141 BOOST_CHECK_EQUAL(pack.data(), 2468);
146 yp2::RouterChain router2;
151 yp2::Session session;
153 yp2::Package pack(session, origin);
155 pack.router(router2).move();
157 BOOST_CHECK_EQUAL(pack.data(), 1234);
162 catch (std::exception &e) {
163 std::cout << e.what() << "\n";
172 BOOST_AUTO_UNIT_TEST( testfilter2_2 )
176 BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
178 yp2::filter::Base *base = &fc;
180 std::string some_xml = "<?xml version=\"1.0\"?>\n"
181 "<filter type=\"constant\">\n"
182 " <value>2</value>\n"
185 // std::cout << some_xml << std::endl;
187 xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
193 xmlNodePtr root_element = xmlDocGetRootElement(doc);
195 base->configure(root_element);
200 BOOST_CHECK_EQUAL(fc.get_constant(), 2);
202 catch (std::exception &e) {
203 std::cout << e.what() << "\n";
215 * indent-tabs-mode: nil
216 * c-file-style: "stroustrup"
218 * vim: shiftwidth=4 tabstop=8 expandtab