1 /* $Id: test_filter2.cpp,v 1.19 2006-05-15 10:12:33 adam Exp $
2 Copyright (c) 2005-2006, 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;
22 namespace mp = metaproxy_1;
24 class FilterConstant: public mp::filter::Base {
26 FilterConstant() : m_constant(1234) { };
27 void process(mp::Package & package) const {
28 package.data() = m_constant;
31 void configure(const xmlNode* ptr = 0);
32 int get_constant() const { return m_constant; };
39 void FilterConstant::configure(const xmlNode* ptr)
43 BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
44 BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
46 const struct _xmlAttr *attr;
48 for (attr = ptr->properties; attr; attr = attr->next)
50 BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
51 const xmlNode *val = attr->children;
52 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
53 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
55 const xmlNode *p = ptr->children;
56 for (; p; p = p->next)
58 if (p->type != XML_ELEMENT_NODE)
61 BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
62 BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
64 const xmlNode *val = p->children;
69 BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
70 BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
72 m_constant = atoi((const char *) val->content);
76 // This filter dose not have a configure function
78 class FilterDouble: public mp::filter::Base {
80 void process(mp::Package & package) const {
81 package.data() = package.data() * 2;
87 BOOST_AUTO_UNIT_TEST( testfilter2_1 )
94 mp::RouterChain router1;
96 // test filter set/get/exception
103 mp::Package pack(session, origin);
105 pack.router(router1).move();
107 BOOST_CHECK_EQUAL(pack.data(), 2468);
112 mp::RouterChain router2;
119 mp::Package pack(session, origin);
121 pack.router(router2).move();
123 BOOST_CHECK_EQUAL(pack.data(), 1234);
128 catch (std::exception &e) {
129 std::cout << e.what() << "\n";
138 BOOST_AUTO_UNIT_TEST( testfilter2_2 )
142 BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
144 mp::filter::Base *base = &fc;
146 std::string some_xml = "<?xml version=\"1.0\"?>\n"
147 "<filter type=\"constant\">\n"
148 " <value>2</value>\n"
151 // std::cout << some_xml << std::endl;
153 xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
159 xmlNodePtr root_element = xmlDocGetRootElement(doc);
161 base->configure(root_element);
166 BOOST_CHECK_EQUAL(fc.get_constant(), 2);
168 catch (std::exception &e) {
169 std::cout << e.what() << "\n";
181 * indent-tabs-mode: nil
182 * c-file-style: "stroustrup"
184 * vim: shiftwidth=4 tabstop=8 expandtab